Skip to content

PostgreSQL Django Nginx Gunicorn with VirtualenvWrapper on 16.04 LTS Ubuntu Server

Posted in Django, News, Server, and Ubuntu

Hello, and welcome!

If you’ve recently upgraded to Ubuntu 16.04 LTS, you probably are wondering if there are any revamps you might need to do. In my case, I had to.

Short story

I deploy my stuff entirely on DigitalOcean. Since 16.04 was recently released, I checked my DO control panel and realized I could move from the 14.04 to the 16.04. From the control panel, I follow the instructions and got the upgrade done.

However, the upgrade actually wasn’t done! The CP showed Linux kernel 4.4 on 16.04 Ubuntu, whereas doing a simple uname -rvs in my terminal on the server box printed that I am still on 14.04.

This came to light after I realized I couldn’t install LetsEncrypt. Issuing the command to install on 16.04 failed, as my box was still on 14.04.

Fast-forward, I manually changed my sources.list of apt-get ; I simply replaced all trusty to xenial in the file, after I made sure I had a backup. sudo apt-get update && sudo apt-get dist-upgrade was all left.

After about 10 minutes, everything came back to normal, except my Nginx had gone up to 1.1 and my PHP has shifted to version 7. All were exciting news, however, I needed to put a couple of changes in to get things going!

So Django’s Turn Came

In this piece, I wish to share with you how to get a quick turn around with setting up your PostgreSQL, Django 1.9, Nginx, Gunicorn with VirtualEnvWrapper under minutes, less than 10, actually!

Prepping Surgery Table

The above ensures you install the necessary packages, such as Python and Python3 and pip and pip3, along with Nginx and PostgreSQL. Oneliner, done! You might not need to install the python3 and pip3 if you’re not going to use any of them. It is up to you.

Our surgery table happens to be clean now, free of germs. Let’s bring the actual objects to operate on! In this case, the first in line:

PostgreSQL Setup

We’re using PostgreSQL as the database backend in our setup. PostgreSQL is a powerful relational database system, and perfectly marries Django in all of its glory. I personally have been using PostgreSQL, and the experience is solid. No wonder it is well and highly spoken of!

You probably have your PostgreSQL database setup already, since I assume you had everything running and only had to upgrade from 14.04 to 16.04. If that is what you did (like I did), you don’t have to set up anything anymore. Just jump to the Django part, and keep going.

Your databases, passwords etc are all available, and tying into your Django project should require details last used on 14.04.

If you are starting afresh with PostgreSQL setup, here we go:

The above are standard commands you probably have done already. Replace newproject and projectuser with actual names of your choice.

We entered the psql (PostgreSQL) shell and issued the commands to create the database, a new user and assign a password, set some settings on the created user which will be used for transactions. Anytime a transaction from this user goes to the DB, these settings will be attached.

Next, we assign the database to the user and exist.

Database setup was done!

Python Development Environment

If you’ve not heard of VirtualEnv, then please go ask Google for more details. In short, it allows you to run your Python projects within encapsulated environments, such that, what packages or dependencies you install in project A, doesn’t interfere with project B.

We’ll be going an extra step in using VirtualenvWrapper to make development slightly easier.

So from the official docs, we do this:

After install, we now have our virtualenv and its wrapper ready. To start making use of the commands given us by virtualenvnwrapper, you could add this to end of the file, ~/.bashrc

Then, on the terminal, enter:

source ~/.bashrc

To reload your bash, giving you access to all the new commands from virtualenvwrapper

Setup the environment for the project with virtualenvwrapper

to create a new virtual environment called myProject. Notice where the project is initialized. Mine is at /home/khophi/.virtualenvs/myProject/bin/python.

After entering the workon myProject, your terminal should change to something like this:

Then do the next:

Into Django’s Realm

Now that we have our Django project started, in the name of myApp and we’re in the project’s root directory, let’s go ahead to make some changes to the settings file to point to our PostgreSQL database.

Using nano, open the Django settings file of the myApp project, like so: (myProject) myApp/ $ nano myApp/settings.py

Then, in there:

With that out of the way, we need to test if our database connection is working.

To serve static files in production, you need to have your STATIC_URL and STATIC_ROOT set properly. In the above, we’re saying, ‘Look into the static/ folder within the root of the current project’.

Thus, whenever you do, {% static 'my/image/is.jpg'%} you’re saying, mywebsite.com/static/my/image/is.jpg which will be intercepted by our Nginx which is configured to serve files coming, having a starting URL of /static/ with the contents from the os.path.join(BASE_DIR, 'static/'), which in our case, is ~/myProject/static/

Testing database connection:

The above are standard Django commands, you already know. Since we wish to use Nginx to serve the static files. So, let us ‘taste’ the fruits of our work now:

Assuming you don’t have firewall blocking the outside world in accessing your site, (of which you can remove by doing sudo ufw allow 8000), then visit

yourIpAddress:8000 in your browser, and you should see a Django ‘it worked’ page like so:

Django "It Worked" page
Django “It Worked” page

If you see the above page, then you’re a happy man. However, not fully a happy man, as we need to test one more thing. We plan using Gunicorn (The Python WSGI HTTP Server for UNIX) to serve our Django (Python) project. Shall we test to see how that’ll be:

You might get errors relating to ‘No WSGI module found’ or something similar. Kindly check that you’re pointing to the WSGI file and referencing the application function in it properly.

If you hit up the site in your browser, like we did earlier, this time, your site or admin page will look broken. Why? Gunicorn doesn’t know anything about where you’re storing your static files. That job will be done by Nginx, and we’ll get there soon.

[wp_ad_camp_1]

A broken layout page of Django admin, when served with Gunicorn is no sign of error, it simply means, you’re on the right track, just left with a couple of ‘Engines’ (Nginx – Happy to pronounce it the wrong way!).

In this case, since we are in our project root already (myApp/) and the WSGI file is in the myApp/myApp/folder, that is why we’re accessing via the myApp.wsgi:application

If Gunicorn is able to boot properly and serve the site properly without any errors, then our Gunicorn is fine and our project is good to go.

We don’t want to come here enter this Gunicorn command all the time to start our Django server. What if our server when we’re asleep? That’ll mean no one accesses our site until we wake up in the morning, finish brushing our teeth, saying hi to friends and neighbors, before fixing our damn broken site!

We don’t want that! Let’s automate with a systemd service, which will kick in the server whenever the server boots and the network are up!

SystemD Service for Gunicorn

This step is straightforward. Create a service for the specific Django project, sudo nano /etc/systemd/system/myapp.service .

In this myapp.service, add:

The above is the basic structure for a systemd service file.

Kick the above file into action by doing

sudo systemctl start gunicorn
sudo systemctl enable gunicorn

Done!

Engines Revving, Nginx!

Our last but also useful piece in this PostgreSQL-Django-Nginx-Gunicorn Euro Championship is the Nginx.

Our Nginx will serve as the front-facing server. Any incoming request from a user that matches a particular domain but doesn’t match any location block we specify in the Nginx server configuration will be pushed to Gunicorn, which is standing by to accept forwarded requests and respond accordingly.

Something like this will do. CTRL + X will save the file when done, after opening a new Nginx conf for our myApp project with this: sudo nano /etc/nginx/sites-available/myapp

The above is a snippet taken from the Nginx configuration for the logECG website. This might match your needs if you’re using SSL on your website, otherwise, you simply can comment out the last server block, then replace listen 443 ssl http2;in the first server blog, with listen 80;and you’re good to go.

Visit server_name you specified, in the above, let us say, logecg.khophi.co, and you see your myApp project up and running with the “It Worked” page from above.

This has been a long guide, but I am sure you were able to follow, and rushing through this app should get you up and running less than 10 minutes.

Thanks for reading, and I’ll see you in the next one. Lemme know if you have any questions.