Okay, we've worked hard on our project and, after having refined every aspect of our work, it's time to show everyone what we've created. Be aware that this section could feel a bit hostile if you don't have a basic knowledge of UNIX systems. However, it could be regarded as an additional, and not mandatory, step that can make you understand how Django effectively works in production environments.
Here, I would like to show you one of the many ways to deploy a Django project on a server. For our examples, we will use an Ubuntu Server 12.04 LTS, a 64-bit operating system and we will install Nginx on it, as HTTP server, and uWSGI as application container server. The configuration we've chosen is a typical configuration that runs Django applications for production purposes, and can be used on many production servers.
If you don't have a running server to house a real deployment, you can always use virtualizationtools on your local machine, like VirtualBox from Oracle (https://www.virtualbox.org/), on which you can install Ubuntu Server, that you can download from the Ubuntu official website (http://www.ubuntu.com/download/server).
Following through the installation of an Ubuntu OS on a virtualized environment is obviously out of the scope of this book, so I will assume that from this moment on you have an installation of an Ubuntu Server 12.04 up and running and you are able to work on it.
Now that we have a fresh installation of Ubuntu Server, Python is already there, but not Django, so we need to install it and you should already know how to do it. To refresh your memory, we will install it with pip. So let's get pip first:
$ sudo apt-get install python-pip
Now we can install Django by typing the following command:
$ sudo pip install Django
To get Nginx up and running on your Ubuntu machine you must run the following command as root:
$ sudo apt-get install nginx
As soon as the installation is complete, we can try our server to see if it runs by launching the following command as root:
$ sudo service nginx start
You should see a successful message on the screen saying Starting nginx: nginx.
To verify that Nginx is running, go to the URL or IP of you server with your browser (for example http://jumpingintodjango/
) and you should see a page with the message Welcome to Nginx. Note that in all the examples I will use the hostname jumpingintodjango
to refer to the Ubuntu server.
We will come back to Nginx later on for the final configuration. Now it's time to install uWSGI. In the Ubuntu repository there is a package for uWSGI, so all we need to do is toexecute this command:
$ sudo apt-get install uwsgi uwsgi-plugin-python
Now everything we need is installed. We can proceed to configure our web application.
Great, now that uWSGI is also installed with the Python plugin, let's add our sample website to Nginx available sites. Create a file called jumpingintodjango
in the /etc/nginx/sites-available/
folder.
The file should look similar to the following code snippet:
server { listen 80; server_name jumpingintodjango; access_log /var/log/nginx/jumpingintodjango.log; location / { uwsgi_pass 127.0.0.1:3031; include uwsgi_params; } }
Once we have created our website configuration file, we need to enable it. To do so, it's enough to create a symbolic link from the sites-available
folder of Nginx:
$ sudo ln -s /etc/nginx/sites-available/jumpingintodjango /etc/nginx/sites-enabled/jumpingintodjango
Before restarting Nginx we will remove the default ServerBlock with the following command:
$ sudo rm /etc/nginx/sites-enabled/default
In this way you can access the Django project using both the IP and the hostname.
Restart Nginx using the following command:
$ sudo /etc/init.d/nginx restart
Now, if you try to reach the Ubuntu server with your browser, you should see an error page telling you 502 Bad Gateway. Don't worry, this is normal because we configured Nginx to dial with uWSGI, but we still have to add our Django app to the uWSGI available apps.
Before going ahead, we should first copy our sample Django project on our server. In our example, we are going to put our project in the /var/www/jumpingintodjango-webapp/
folder:
$ sudo mkdir -p /var/www/jumpingintodjango-webapp
Then, take the content of the jumpingintodjango
outer folder that we used as the root folder for our project and copy it inside this folder.
Of course, the paths of the template folder and the SQLite database on our server are different from our local machine so we need to edit some lines in the jumpingintodjango/settings.py
file:
DATABASES = { 'default': { . . 'NAME': '/var/www/jumpingintodjango-webapp/db.sqlite', . . } } . . TEMPLATE_DIRS = ( "/var/www/jumpingintodjango-webapp/templates", ) . .
Now let's give the right permissions to the www/
folder:
$ sudo chown -R www-data:www-data /var/www/
Nice! Now it's time to create the uWSGI .ini
file for our project.
Create a /etc/uwsgi/apps-available/jumpingintodjango.ini
file that has the following code in it:
[uwsgi] chdir=/var/www/jumpingintodjango-webapp/ socket = 127.0.0.1:3031 master = True vacuum = True DEBUG = True module = jumpingintodjango.wsgi:application daemonize=/var/log/uwsgi/jumpingintodjango.log
Now, as we did for Nginx, we need to enable this app by creating a symbolic link in the following way:
$ sudo ln -s /etc/uwsgi/apps-available/jumpingintodjango.ini /etc/uwsgi/apps-enabled/jumpingintodjango.ini
Restart/start the uWSGI with the following command:
$ sudo /etc/init.d/uwsgi restart
Okay, now with your browser go to the web address of your server, in my case it's http://jumpingintodjango/
, and you should see your project up and running.
This is a common way to deploy Django applications and of course there are several options, on both Nginx and uWSGI, that can be used to tune your applications but this is up to you to discover.