简体   繁体   中英

Publish Django project with gunicorn and NGINX

Im trying to publish my Django project with NGINX and gunicorn, but I get permission denied from gunicorn.

My last steps what I did.

mkdir /home/sama/websites
cd /home/sama/websites
mkdir test_project
cd test_project

create Virtualenvirement for this project virtualenv --python=/usr/bin/python3.8 venv

Virtualenvirement activate source venv/bin/activate

Gunicorn & Django install

pip3 install gunicorn
pip3 install django

create project

cd ..
django-admin startproject config test_project

allow firewall to port 8000 sudo ufw allow 8000

run first test python manage.py runserver 0.0.0.0:8000

open my server ip on webbrowser myIP:8000 I can see Django basic page

stop server CTRL + C

testGunicorn gunicorn --bind 0.0.0.0:8000 config.wsgi

testagain on webbrowser Again I can see Djangos page

Server stop CTRL + C

exit virtualenvirement deactivate

config Gunicorn create 2 files in /etc/systemd/system/

gunicorn.socket and gunicorn.service

edit this files sudo nano /etc/systemd/system/gunicorn_test_project.socket

/etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn daemon

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target
/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=sama
Group=www-data
WorkingDirectory=/home/sama/websites/test_project
ExecStart=/home/sama/websites/test_project/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock config.wsgi:application

[Install]
WantedBy=multi-user.target

test socket

sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket

systemlink created, check it file /run/gunicorn.sock Output: /run/gunicorn.sock: socket

curl --unix-socket /run/gunicorn.sock localhost

And now I get permission denied

I already set permission for the folder test_project to user sama and group www-data, but without any effects. What is wrong?

I see no good reason to use systemd socket activation here.

Just use a service file and have Gunicorn bind to HTTP.

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=sama
Group=www-data
WorkingDirectory=/home/sama/websites/test_project
ExecStart=/home/sama/websites/test_project/venv/bin/gunicorn --access-logfile - --workers 3 --bind 0.0.0.0:8000 config.wsgi:application

[Install]
WantedBy=multi-user.target

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM