Setup of Nginx and uWSGI with Django
Nginx and uWSGI configuration
Edit mysite/settings.py and add the following configurations
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static_main"),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Create directories static, static_main and media to the /Project folder.
If you use figures, etc. on the website which are included in the html-template,
add these figures to the directory main/static_main.
Using the command on terminal window
python manage.py collectstatic
these figures are copied from the static_main directory to static, and are then available for the web site.
Create a mysite_nginx.conf file in the folder where manage.py locates (/Project) and add the following content
upstream mysite {
server unix:///tmp/mysite.sock;
}
server {
listen 8888;
server_name 127.0.0.1;
charset utf-8;
client_max_body_size 75M;
location /media {
alias /../Project/media;
}
location /static {
alias /../Project/static;
}
location / {
uwsgi_pass mysite;
include /../Project/uwsgi_params;
}
}
Create a uwsgi_params file with the content
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
Locate the Nginx installation folder, for example /usr/local/etc/nginx/servers, and create a
soft link
ln -sf /../Project/mysite_nginx.conf /usr/local/etc/nginx/servers
Be careful that you use the complete paths.
Then create a mysite_uwsgi.ini file with the content
[uwsgi]
chdir = /../Project
module = mysite.wsgi
master = true
processes = 10
socket = /tmp/mysite.sock
chmod-socket = 666
vacuum = true
pidfile = /tmp/mysite.pid
daemonize = uwsgi.log
Now we are able to test the configuration. Run (or restart) Nginx and uWSGI
$ nginx
$ uwsgi --ini /../Project/mysite_uwsgi.ini
The website should be found from the address
http://127.0.0.1:8888/
Return to the
Mac main page.