So after reading the various tutorials online for setting up NGINX + Django + uWSGI and all of them not working correctly, I decided to write my own.
This tutorial was tested on a blank install of Ubuntu Server 12.04 LTS 64-bit, if you follow the steps carefully in the correct order all should be well :)
1. Add a new user, give them sudo privileges and switch to that user, below I’ve named mine “user”.
sudo adduser user sudo adduser user sudo su user
2. Ensure your system hostname is set to localhost
sudo echo "localhost" > /etc/hostname sudo hostname localhost
3. Since this is a new install, update the system.
sudo apt-get update sudo apt-get upgrade
4. Install python, virtual environment builder and python dev
sudo apt-get install python sudo apt-get install python-virtualenv sudo apt-get install python2.7-dev
5. Install and start the NGINX web server
sudo apt-get install nginx sudo service nginx start
6. Install uWSGI
sudo apt-get install uwsgi
7. Setup a Django project
sudo mkdir /var/www sudo mkdir /var/www/example.com cd /var/www/example.com sudo mkdir venv conf src logs
This will give the below pictured folder structure
8. Set-up the virtual environment and activate it
sudo virtualenv /var/www/example.com/venv source /var/www/example.com/venv/bin/activate
9. Install Django
sudo pip install django
10. Change to the “src” directory, then copy your Django project files into it
cd /var/www/example.com/src
10. Create your uwsgi.ini config file, with the below content
sudo nano /var/www/example.com/conf/uwsgi.ini
[uwsgi] # variables projectname = example_project projectdomain = example.com base = /var/www/example.com # config plugins = python master = true protocol = uwsgi env = DJANGO_SETTINGS_MODULE=%(projectname).settings pythonpath = %(base)/src/%(projectname) module = %(projectname).wsgi socket = 127.0.0.1:8889 logto = %(base)/logs/uwsgi.log #below line runs it as a daemon in background daemonize = /var/log/uwsgi/example_project.log
11. Create an NGINX config file for this domain, with the below content
sudo nano /var/www/example.com/conf/nginx.conf
server { listen 80; server_name example.com www.example.com; root /var/www/example.com/src/example_project; access_log /var/www/example.com/logs/access.log; error_log /var/www/example.com/logs/error.log; location /static/ { # STATIC_URL alias /var/www/example.com/src/static/; # STATIC_ROOT expires 30d; } location /media/ { # MEDIA_URL alias /var/www/example.com/src/media/; # MEDIA_ROOT expires 30d; } location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8889; } }
12. Edit the main nginx.conf to import our domain conf file, see below content as a guide
sudo nano /etc/nginx/nginx.conf
user www-data; # ... http { # ... include /var/www/*/conf/nginx.conf; # ... }
13. Restart NGINX (to load apply our config changes)
sudo service nginx restart
14. Install MySQL and secure it
sudo apt-get install mysql-server sudo mysql_secure_installation
15. Install Python MySQL and uWSGI plugins
sudo apt-get install python-mysqldb sudo apt-get install uwsgi-plugin-python
16. Install south (optional)
sudo pip install south
17. Test that uWSGI is working
sudo uwsgi --ini /var/www/example.com/conf/uwsgi.ini
If you visit your site it should now show django. If it doesn’t common causes are:
- ALLOWED_HOSTS in settings.py isn’t set
- DEBUG isn’t off in settings.py
- Database isn’t configured
18. Setup uWSGI to run on system boot
Create the following file, with the below content
sudo nano /etc/init/uwsgi.conf
# Emperor uWSGI script description "uWSGI Emperor" start on runlevel [2345] stop on runlevel [06] exec uwsgi --master --die-on-term --emperor /var/www/example.com/conf/uwsgi.ini
19. Now reboot the server and navigate to your website
sudo reboot
Hey great blog!
I was able to get everything working perfectly except for outside access to my webserver which sits behind a router. To the best of my knowledge port 80 is forwarded corrently and is open and I saw when I attempted to hit it via it’s local network IP everything worked perfectly but when I tried to get to it from an external IP the request would just hang and time out and the logs showed something like this:
192.168.0.4 – – [01/May/2013:22:12:16 -0500] “-” 400 0 “-” “-”
Is there further nginx config needed or is this likely an issue with my router config?
ah nevermind it seems to only do that when I access the outside IP from within my network. The outside IP seems to resolve just fine to the rest of the world.
Glad it’s working for you :)
Ho did u solved it Joel, im having same issue with my API in Django behind uwsgi and nginx, im calling froma mobile app
I am getting a uWSGI Error “Python application not found”. Is pythonpath supposed to point to the django project folder? Or the virtualenv?
I just fixed the pythonpath issue. But now I am just getting 502 Bad Gateway error. I used the same port as in the example. My error.log reads:
[error] 3787#0: *5 connect() failed (111: Connection refused) while connecting to upstream … request: “GET / HTTP/1.1”, upstream: “uwsgi://127.0.0.1:8889”
I am very new to server configuration, do I have to open or listen to port 8889 somehow or do the nginx/uwsgi settings take care of it?
I can’t be sure but have you tried rebooting? (incase another instance is running already) Also do you have debug=false in your settings.py and static file serving paths all set in your nginx.conf?
Michael – how did you get around this problem?
Hi i am also getting the same issue.. did u solve the issue?
Hi it works great with an application i tried. Now i wanted to know if the same method works with multiple applications? If so how do i change the settings?
Do I need to install python dev if python is already on the system by default?
To the best of my knowledge port 80 is forwarded corrently and is open and I saw when I attempted to hit it via it’s local network IP everything worked perfectly but when I tried to get to it from an external IP the request would just hang and time out .
nginx hosting
is the above configuration working with sockets or through a port? thanks for the great post!
This worked perfectly. Thank you. You have no idea (or maybe you do) how useful something like this is. Thanks again.
Hello.
I’ve django’s “Internal Server Error” page, so I think that everything fine with nginx, but I need to get some logs and my /var/www/example.com/logs/error.log is completely clean. I’m working on server with root user, so I’ve created this log folder by root. Is it possible, that django can’t write in it? What is the best way for me to solve this problem? (debian is my operation system, if it matter)
I have the same problem
Helllo Richard, many thanks indeed and after such much of googling around and trials I finally got things in good shape. You just rock !
I found this tutorial very helpful! Worked perfectly for me! Keep up the great work! Someone had to come up with a proper tutorial for deploying django apps, as the django documentation lacks heavily in that area.
Bad gateway. Tutorial #789543 doesn’t work. Seriously nginx is providing the most newbie-hostile experience I have ever seen. I am seriously tired and frustrated at this point.
I used a different tutorial and got close but couldn’t get it to work, so I’m thinking of reimaging my server and trying yours.
I do have one question. I will need Emperor mode as I want to run more than one site on nginx.
As a side note, I also need to run php apps, but I think I can deal with getting that to work with nginx.
But setting up Emperor mode after I’ve completed your tutorial is needed. Can anyone help?
I think you may have addressed Emperor mode, but I’m used to seeing something like a vassals directory that it keeps monitoring. Instead of do that, do you just add additional sites to the conf file that you’re running at startup?
why do i have a virtualenv when i sudo pip install django? sudo makes it install in /usr/local
Thanks! after 1 hour of frustration finally found your tutorial and it’s now all clear.
Thank you so much, this is what I looking for a long time. It’s useful. Once again, thank you so much for easy explanation.
Thanks for the resource… I think this tutorial is great. I’m running Python 3 and I had to install the uwsgi plugin:
sudo apt-get install uwsgi-plugin-python3
Best,
Nick
Hey — this saved my ass after hours of looking around. The problem was apparently ALLOWED_HOSTS ( I didn’t now this had been added to django since I had last used it) but in general your description and explanation of the variables involved was a huge help! Thank you!
nice information
thanks for your information