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
# python36

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

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())" 

The final step here is to create a passenger startup file 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/

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 4.3 / 5. Vote count: 16

No votes so far! Be the first to rate this post.

Oh no, sorry about that!

Let us know how we can do better below

Tell us how we can improve this post?


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 8 comments
    • Avatar

      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):

      there is a solution for that?

    • Ivan Postnikov
      Ivan Postnikov

      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 > > 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.

    • Avatar
      Güneş Erbayraktar

      I did all the steps but it doesn’t work. I do not get an error in uploads or transactions. but the page does not work.

      AH01276: Cannot serve directory /var/www/vhosts/XXX/httpdocs/djangoProject/: No matching DirectoryIndex (index.html,index.cgi,,index.php,index.xhtml,index.htm,index.shtml) found, and server-generated directory index forbidden by Options directive

      I’m just not sure about ngix.

      PassengerEnabled On
      PassengerAppType wsgi

      2074#0: *19900 directory index of “/var/www/vhosts/XXX/httpdocs/djangoProject/” is forbidden

    • Ivan Postnikov
      Ivan Postnikov

      Hello @Güneş Erbayraktar,

      As I understand, you’re configuring applications to be served by Nginx.

      In that case “Proxy mode” has to be disabled and the following “Additional Nginx directives” are to be specified:

      passenger_enabled on;
      passenger_app_type wsgi;

      When served by Nginx Additional Apache directives shouldn’t be used.

      For applications to be served by Apache “Proxy mode” has to be enabled and only one Apache directive is required:

      PassengerEnabled On

      When served by Apache Additional Nginx directives shouldn’t be specified.

    • Avatar
      Hussain Mustafa

      I successfully display the Phusion server webpage detailing something wrong with my Django application.

      Traceback (most recent call last):
      File “/usr/share/passenger/helper-scripts/”, line 369, in
      app_module = load_app()
      File “/usr/share/passenger/helper-scripts/”, line 76, in load_app
      return imp.load_source(‘passenger_wsgi’, startup_file)
      File “/var/www/vhosts/”, line 1, in
      from django.core.wsgi import get_wsgi_application
      ImportError: No module named django.core.wsgi

      • Avatar
        Jess from Plesk

        Hello Hussain,
        Is there a possibility that the virtual environment for the application was set incorrectly? A possible cause is that steps for a different OS were used. Please make sure that steps for the correct OS were performed. You can follow the instructions here: How to allow installing and install Django applications?

    • Avatar
      Till Koch

      Done all steps, but I got 403 error for Apache and if i’m using Nginx then error is 500

      • Laura from Plesk
        Laura from Plesk

        Hello! The 755 is the default permission setting for the vhosts folders. For /tmp, we recommend you review if there’s some ACL rule managing the access.

    Leave a Comment

    Start typing and press Enter to search