Example of a Django site served by uWSGI and nginx for testing and education purposes.
Implements a simple "Hello, world!", but with a few extras (templates, CSS, images, etc.). Follows Django best practices as much as possible.
Note: this example should be considered insecure! Although basic security settings are in place, make sure you do your own research on site security! Start with the Django documentation, for example.
-
Install nginx and uWSGI as system-wide services.
apt-get update && apt-get upgrade apt-get install nginx apt-get install uwsgi -
git clonethis repo, for example in/var/www. Note: if you choose a different location, changenginx.confanduwsgi.iniaccordingly after cloning.cd /var/www git clone https://github.com/nicodv/django-uwsgi-nginx.git -
Generate a Django secret key using the supplied script (
python django-uwsgi-nginx/tools/django_secret_keygen.py), and put it in theuwsgi.inifile. -
Create a virtualenv with the latest
pip,setuptools, anddjangopackages, for example in/var/www/django-uwsgi-nginx/venv. Note: if you choose a different location, changeuwsgi.iniaccordingly. -
Install the Python plugin to uWSGI. Note: I assume Python 3 here. The Python 2 command is the same, minus the
3. Also, if you're using Python 2, edituwsgi.iniaccordingly by editing the plugin name.apt-get install uwsgi-plugin-python3 -
Remove nginx's default site config and make a symbolic link to the
nginx.conffile in the/etc/nginx/conf.ddirectory.rm /etc/nginx/sites-enabled/default ln -s /var/www/django-uwsgi-nginx/conf/nginx.conf /etc/nginx/conf.d/ service nginx restart -
Make symbolic links of the
uwsgi.inifile to the/etc/uwsgi/apps-availableand/etc/uwsgi/apps-enableddirectories.ln -s /var/www/django-uwsgi-nginx/conf/uwsgi.ini /etc/uwsgi/apps-available/ ln -s /var/www/django-uwsgi-nginx/conf/uwsgi.ini /etc/uwsgi/apps-enabled/ -
Hack the
uwsgiservice to use the so-called emperor, which will automatically serve any.inifile in/etc/uwsgi/apps-enabled. Do this by editing/etc/init.d/uwsgiand adding--emperor /etc/uwsgi/apps-enabledafter the service start command so that it reads"Starting $DESC" "$NAME" --emperor /etc/uwsgi/apps-enabled). Then reload and restart the service:systemctl daemon-reload service uwsgi restart -
Create the log directories.
mkdir -p /var/log/uwsgi -
Set permissions correctly for user
www-data. Note: if you run as a different user, change/etc/nginx/nginx.conf,uwsgi.ini, and these commands accordingly.chown -R www-data:www-data /var/www/django-uwsgi-nginx/ chown www-data:www-data /var/log/uwsgi/ -
Copy all static folders into the
STATIC_ROOTdirectory:python manage.py collectstatic --settings=djangosite.settings.prod -
Check it out!
http://<your.ip.add.ress>/helloworld/
nginx logs can be found in /var/log/nginx, and uWSGI logs in /var/log/uwsgi.
You can also try to run the uWSGI server manually with:
uwsgi --ini /var/www/django-uwsgi-nginx/conf/uwsgi.ini
- This example is set up in such a way that by changing the
DJANGO_SETTINGS_MODULEreference inuwsgi.ini, you can easily switch between site settings. You can, for example, choose development server settings by referring to%(site).settings.dev.