Plesk Onyx and Django
Post image

Author: Lucas Fraser

Date: July 11, 2017


I’ve recently finished redesigning my website in Django, and here at Ion Programming our web sever is running Plesk. So naturally, I had to find a decent way to deploy our website to it, the internet is lacking in up to date useful information so after a lot of messing around and figuring things out I thought I would write my findings down here in case anyone else needed the information.

Whilst Plesk is a brilliant system for everyday PHP websites, from what I can tell there is limited support natively for Python web apps. They have recently added support for Docker containers, so that may be an option to look at down the track, for now I’ll outline exactly my process to get a web app running using the latest version of Django (currently 1.11).

Prepare the server

First things first is to check your server has all the requirements, mine already had Python3 so I didn’t need to worry about that, but it will be useful to install the following packages for the process:

apt-get install python3-dev build-essential libmysqlclient-dev libapache2-mod-wsgi-py3

These packages will come in handy later if you are using MySQL as the backend for your app.

Create website in Plesk Panel

Next you should create a domain / subdomain in the Plesk panel for your app to run at. You can use the git feature to pull your source code if you have it under version control, otherwise you can upload it to the document root.

Once you have your app code in the document root, you’ll want to create a ‘public’ or ‘static’ folder to store your static assets. You will then want to go into the hosting settings section:

Hosting Settings

Then modify the document root to point to that directory.

 Document Root Settings

Create a Virtual Environment to keep your dependencies in

You’ll want a Virtual Environment to keep all your libraries and Django in so you don’t rely on the single python install on your main server (this way you can run multiple apps with different Django versions and dependencies). First install the python virtual env software to create one.

apt-get install python3-venv

Now create a virtual environment in your website directory, ie.

cd /var/www/vhosts/<website>/
python3 -m venv djangoenv

Now within your website directory, you will have your website files, including, your public or static directory, and a python virtual environment.

Next up you’ll need to activate that virtual env and install the required dependencies for your app.

source /var/www/vhosts/<website>/djangoenv/bin/activate
pip install Django mysqlclient

Now you have the required dependencies, you can continue with the next step, setting up the Django instance to work on the server.

Making Django work on your server

This step is going to differ for everyone’s configuration, but the main thing you’ll want to do is set your settings the way they need to be for production, such as setting debug to false and updating mysql connection details.

You’ll also want to set STATIC_ROOT and ALLOWED_HOSTS.

Make ALLOWED_HOSTS have an entry for the domain in which you are configuring the website for. Set STATIC_ROOT to the location where your static directory is: /var/www/vhosts/<website>/public

Now, you can run the collectstatic Django command to import the static assets from your project to your public directory.

python3 collectstatic

Configure apache to run the WSGI application

All the required configuration is completed, so now you just need to add some additional directives to apache to make it serve the WSGI application instead of a standard index file.

Head back into the Plesk panel and navigate to the Apache and Ngix setttings.

Apache Settings

In there you’ll want to disable the Smart static files processing (you can use it but I believe it will require a little more configuration, which I’m not going to go into in this blog post)

You also need to add the following configuration to the addition directives for http and https sections:

Alias /static/ /var/www/vhosts/<website>/public/
<Directory /var/www/vhosts/<website>/public>
    Require all granted
</Directory> WSGIDaemonProcess processname python-home=/var/www/vhosts/<website>/djangoenv python-path=/var/www/vhosts/<website> WSGIProcessGroup processname WSGIScriptAlias / /var/www/vhosts/<website>/<appname>/ process-group=processname

By the end, you should have a configuration that looks somewhat similar to this:

Django Apache Config

This basically sets the static directory up to be served as usual (at the /static url), and then creates a WSGI alias and handler using the python virtual environment to proxy all other request to. More information on the WSGI configuration can be found on the Deployment page of the Django docs.

With any luck, you’ve got your website up and running now. If you’re receiving errors, it may be useful to check the apache error logs and see what’s going on.