Ok, recommending it not very helpful, so here is a basic configuration for your nginx and gunicorn.
In order ot make it simple, let's assume your app is located in this directory: /home/root/app/src/
and we're gonna use root
user (but you should create separate user for your app)
First your nginx. You have to insert a new file to your /etc/nginx/sites-enabled/yourapp.conf
, if there is a file named default.conf
- remove it.
Bellow I'm posting a nginx conf file, which will try to run your service with using gunicorn.sock:
upstream yourappname {
server unix:/home/root/app/src/gunicorn.sock fail_timeout=0;
}
server {
root /home/root/app/src/;
listen 80;
server_name yourdomain.com *.yourdomain.com
charset utf-8;
client_max_body_size 100m;
access_log /home/root/app/src/logs/nginx-access.log; #you have to have logs folder in src
error_log /home/root/app/src/logs/nginx-error.log;
location /static/ {
alias /home/root/app/src/static/;
}
location /media/ {
alias /home/root/app/src/media/;
}
}
so now on gunicorn start script .
#!/bin/bash
ME="root"
DJANGODIR=/home/root/app/src
SOCKFILE=/home/root/app/src/gunicorn.sock
USER=root
GROUP=webapps
NUM_WORKERS=3
DJANGO_SETTINGS_MODULE=yourapp.yoursettings
DJANGO_WSGI_MODULE=yourapp.wsgi
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source /home/root/app/env/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /home/root/app/env/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name root \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-
ok now in order to be able to run your gunicorn start script it has to have execution model enabled so
sudo chmod u+x gunicorn_start
now you will be able to start your gunicorn server with just using ./gunicorn_start
in top of that I'll post also supervisor conf, which will try to start your app whenever it fail, or just when system boots.
At first install supervisor.
Then create a .conf
file in your main directory /etc/supervisor/conf.d/your_conf_file.conf
inside insert:
[program:yourappname]
command = /home/root/app/src/gunicorn_start
user = root
stdout_logfile = /home/root/app/src/logs/gunicorn_supervisor.log
redirect_stderr = true
so having that done we have to tell our supervisor that we have just added new configuration file. Simply run those commands:
sudo supervisorctl reread
sudo supervisorctl update
and in order to check if your app is running correctly just run
sudo supervisorctl status yourappname
I hope this is helpful, if you have any questions just ask