Django is a very popular, efficient, adjustable and dynamically evolving framework for web applications. When Django initially gained popularity the recommended server setup for running applications was based around Apache with mod_wsgi. This days the art of running Django advanced and the configuration is more productive and resilient, but also more complex including such tools as Nginx, Gunicorn and Supservisor.
While I was trying to deploy my first Django application to a production server I realized that it was not such a piece of cake especially for someone who is not an IT specialist . So I had to do an intensive research to collect information in pieces and the make a puzzle of steps witch I need to follow to complete the process of deploying.
In this article I am going to share with you how to combine all of this components into a Django server running on Linux.
A sever available with root privileges
DNS configured to point at the server's IP
I am using a server running Ubuntu 14.04 so everything should also work on any Debian-based Distribution.
Let's get started by making sure our system is up to date
$ sudo apt-get update$ sudo apt-get upgrade
MySql is a popular database management system used for web and server applications
sudo apt-get install mysql-server
During the installation process you will prompted to choose and set a password for MySql root user. Choose a strong one and keep it in a safe place for future referencies.
Web applications can become compromised even though Django has a pretty good security track record. As a good practice for security is to run the application as system user with limited privileges. If the application has limited access to resources on your server, potential damage can also be limited.
$ sudo groupadd --system webapps
$ sudo useradd --system --gid webapps --shell /bin/bash –home /www/myapp_django myappuser
Virtualenv is a powerful tool which allows you to create separate Python environments in your system. We need virtual environment to run application with different sets of requirements concurrently.Virtualenv is easy to install on Ubuntu:
$ sudo apt-get install python-virtualenv
I usually keep all may web application in a
/www/ directory. Create a diractory to store you application like
/www/myapp_django and change the owner of this diractory to your application user
$ sudo mkdir -p /www/myapp_django
$ sudo chown myapp_user /www/myapp_django
Then as the application user myapp_user create a virtual Python environment in the application directory /www/myapp_django:
$ sudo su - myapp_user
myapp_user@ghost:~$ cd /www/myapp_djangoNew python executable in myapp_django/bin/python Installing distribute..............done. Installing pip.....................done. myapp_user@ghost:~$ source myappenv/bin/activate (myappenv)myapp_user@ghost:~$
Your environment is now activated and you can proceed to install your application requirements.
(myappenv)myapp_user@ghost:~$ pip install django
Downloading/unpacking django (...) Installing collected packages: django (...) Successfully installed django Cleaning up...
Your environment with Django should now be ready to use. Go ahead and create a Django project
(myappenv)myapp_user@ghost:~$ django-admin.py startproject myproject
Test your new project by running the development server
(myappenv)myapp_user@ghost:~$ ./manage.py runserver example.com:8000 Validating models... 0 errors found June 09, 2013 - 06:12:00 Django version 1.5.1, using settings 'hello.settings' Development server is running at http://example.com:8000/ Quit the server with CONTROL-C.
You should now be able to access your development server from http://example.com:8000
Your application is properly configured to run as user myapp_user, who owns the entire application directory. If you want reqular users to have write access to application files you can set properly the group owner and give the group write permitions.
In production we're going to use a dedicated application server called Gunicorn
First you'll have to install Gunicorn in your applications virtualenv:
pip install gunicorn
Downloading/unpacking gunicorn Downloading gunicorn-0.17.4.tar.gz (372Kb): 372Kb downloaded Running setup.py egg_info for package gunicornInstalling collected packages: gunicorn Running setup.py install for gunicorn Installing gunicorn_paster script to /webapps/hello_django/bin Installing gunicorn script to /webapps/hello_django/bin Installing gunicorn_django script to /webapps/hello_django/bin Successfully installed gunicorn Cleaning up...
Now that you have installed Gunicorn you can test wether it can server your Django application by running the following command
(myappenv)myapp_user@ghost:~$ gunicorn myproject.wsgi:application --bind example.com:8001
You should now be able to access the Gunicorn server from http://example.com:8001 . I intentionally changed port 8000 to 8001 to force your browser to establish a new connection.
Gunicorn is installed and ready to serve your app. Let’s set some configuration options to make it more useful. I like to set a number of parameters, so let’s put them all into a small BASH script, which I save as
Set the executable bit on the
$ sudo chmod u+x bin/gunicorn_start