How to set up Django Hosting on the latest Plesk Onyx - Plesk

How to set up Django Hosting on the latest Plesk Onyx

Following your Django installation on Plesk, it’s now time to learn how to organize Django hosting on Plesk Onyx. We’ll be using CentOS 7 and Plesk 17.8 for this use case scenario, however you can always refer to this article for instructions regarding different OS and serving applications by NGINX.

Getting started with Python

First, let’s check if Python is present in the system. At this stage, you’ll need a root server access, which you can get by issuing the following command:

python36 –version

One of the outputs you can get is the following:

Python 3.6.6

In case you don’t have Python installed, you can do it using the following commands.

a) Add EPEL repository and install Python 3.6:

# yum install -y epel-release
# yum install -y python36

b) Download and install the Python package manager from the official website:

# wget https://bootstrap.pypa.io/get-pip.py
# python36 get-pip.py

Next, it’s common practice to have a separate virtual environment for each Python application. So, let’s install the “virtualenv” package (under the “root” user):

python36 -m pip install virtualenv

We’ll then use Phusion Passenger as an application server for hosting Django projects. So, let’s install it

yum update
plesk installer --select-release-current --install-component passenger

Stage Two: Preconfiguring the web server

In our case, the application will be a server by Apache. So, you can enable the passenger module at Tools & Settings > Apache Web Server Settings as below.

Plesk + django hosting - screenshot 1 - enable passenger module

Then, in a Service Plan, which you will be using for domains with Django Apps, enable Proxy mode – if NGINX is installed on the server. And in the “Web Server” tab, add these additional directives, as in the image below:

PassengerEnabled On
PassengerAppType wsgi
PassengerStartupFile passenger_wsgi.py

Plesk + django hosting - screenshot 2 - additional directives for http and https

For the next step, set shell options at “Hosting Parameters” tab and don’t forget to save your configuration!

Plesk + django hosting - screenshot 3 - ssh access to the server shell enabked

Stage Three: Deploying your App

This is where the Django CMS comes in. This part is done on behalf of subscription system user connected via SSH. You must create the Subscription under Service Plan, and don’t forget to navigate to the domain’s Document Root, by default:

cd ~/httpdocs/

You also need to move existing files to the backup directory in the subscription, so they won’t be processed instead of the application:

mkdir ~/backup
$mv ~/httpdocs/* ~/backup/

Then, you can make the virtual environment for your App with the below command:

python36 -m virtualenv -p python36 python-app-venv

Great, now let’s enter it and make a couple of final adjustments before creating the App itself. Enter the virtual environment, install Django framework and check if it can be imported

source ./python-app-venv/bin/activate

pip install Django
python -c "import django;print(django.get_version())" 
2.0.3

The final step here is to create a passenger startup file passenger_wsgi.py inside of the “django-app” environment in order to serve our application via the application server. So, we’ll use the following:

import sys, os
ApplicationDirectory = 'djangoProject' 
ApplicationName = 'djangoProject' 
VirtualEnvDirectory = 'python-app-venv' 
VirtualEnv = os.path.join(os.getcwd(), VirtualEnvDirectory, 'bin', 'python') 
if sys.executable != VirtualEnv: os.execl(VirtualEnv, VirtualEnv, *sys.argv) 
sys.path.insert(0, os.path.join(os.getcwd(), ApplicationDirectory)) 
sys.path.insert(0, os.path.join(os.getcwd(), ApplicationDirectory, ApplicationName)) 
sys.path.insert(0, os.path.join(os.getcwd(), VirtualEnvDirectory, 'bin')) 
os.chdir(os.path.join(os.getcwd(), ApplicationDirectory)) 
os.environ.setdefault('DJANGO_SETTINGS_MODULE', ApplicationName + '.settings') 
from django.core.wsgi import get_wsgi_application 
application = get_wsgi_application()

Note that you should replace the variable values in lines 2 and 3 with your own variable values. Done? Now save the text file and get ready to deploy the app itself. As a sample, we’re going to use the “scaffold” app.

Create Django project:

django-admin startproject djangoProject

Allow serving requests from any host:

sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ['*']/" djangoProject/djangoProject/settings.py

Create a tmp directory for application caches:

mkdir tmp

Restart Application:
touch tmp/restart.txt

Now all you need to do is to change Domain Document Root, and if everything is OK, you’ll see your application as below.

Plesk + django hosting - screenshot 4 - change domain document root
Plesk + django hosting - screenshot 5 - django cms admin login

We hope that was helpful, and before we sign off, let’s just say a big thank you to Alexander Bashurov for his valuable contributions while writing this post. For further support, you can refer to our technical article here.

How useful was this post?

Click on a heart to rate it!

Average rating / 5. Vote count:

Oh no, sorry about that!

Let us know how we can do better below

About

Ivan Postnikov
Ivan has been a part of the Plesk team since 2017 and is our Support Team Leader. He's keen on everything connected to customer service and IT.
    Showing 2 comments
    • Avatar
      Alejandro
      Reply

      Hello Ivan,

      Excellent post, I just got a problem when executing :

      bash-4.2$ python36 -m virtualenv -p python36 python-app-venv

      I get this error:

      Could not find platform independent libraries
      Could not find platform dependent libraries
      Consider setting $PYTHONHOME to [:]
      Fatal Python error: Py_Initialize: Unable to get the locale encoding
      ModuleNotFoundError: No module named ‘encodings’

      Current thread 0x00007fdd9b956740 (most recent call first):
      Aborted

      there is a solution for that?

    • Ivan Postnikov
      Ivan Postnikov
      Reply

      Hello @Alejandro,

      As I can see, your issue was investigated in a ticket. I will add the cause here, it may be useful for other Pleskians!

      The cause was that “Access to the server over SSH ” from
      Subscriptions > example.com > Web Hosting Access
      was set to /bin/bash (chrooted).

      It might not be possible to properly add Python to the chroot due to the dependencies on the native and shared modules it has. The lack of these modules in the chroot causes such error to appear.

    Leave a Comment

    Start typing and press Enter to search