First you need to edit all these configuration files to work and you need to give permissions for read and write or execute to the files
$ touch gunicorn_start
$ sudo touch /etc/systemd/system/gunicorn.service
$ nano gunicorn_start
Here we will put our configuration file which will be used at our service later
#!/bin/bash
NAME="djproject"
DJANGODIR=/path/to
USER=root
GROUP=root or whatever the user used
WORKERS=3
BIND=unix:/path/to/gunicorn.sock
DJANGO_SETTINGS_MODULE=mailqueue.settings
DJANGO_WSGI_MODULE=mailqueue.wsgi
LOGLEVEL=error
cd $DJANGODIR
source /path/to/venv/bin/activate
cd djangoproject
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$PROJECTDIR:$PYTHONPATH
exec /path/to/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $WORKERS \
--user=$USER \
--group=$GROUP \
--bind=$BIND \
--log-level=$LOGLEVEL \
--log-file=-
Make the file executable
$ chmod u+x gunicorn_start
Now we need to edit the service file to restart gracefully
$ sudo echo "
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root # or your user
Group=root # or your grp
WorkingDirectory=/your-project-path/
ExecStart=/path/to/gunicorn_start
[Install]
WantedBy=multi-user.target
" > /etc/systemd/system/gunicorn.service
Finally
Check the nginx configuration at $ ls /etc/nginx/conf.d/
if found any *.conf
file edit it if not, touch proj.conf
$ sudo echo "
upstream app_server {
server unix:/path/to/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 10.100.200.10; # <- insert here the ip address/domain name
keepalive_timeout 5;
client_max_body_size 4G;
access_log /var/logs/nginx-access.log;
error_log /var/logs/nginx-error.log;
location /static/ {
alias /path/to/static/;
}
location /media/ {
alias /path/to/media/;
}
location / {
try_files \$uri @proxy_to_app;
}
location @proxy_to_app {
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header Host \$http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
" > /etc/nginx/conf.d/proj.conf;
/etc/init/gunicorn.conf
– A.Raouf