Applicable to:
- Plesk for Linux
Question
How to install Django applications in Plesk?
Answer
While it is not yet officially supported, it can be configured manually at your own risk & will.
Vote for adding Python support to the Plesk on our UserVoice.
Note: Here and forth Python 3.x will be used as a target Python version.
Prepare the server
Connect to the server via SSH and apply the steps according to the installed Linux distribution:
-
Install Python 3:
# apt install python3
Note: On Ubuntu 20, install the following packages:
# apt install python3 python-is-python3
-
Download and install the Python package manager from the official website:
# wget https://bootstrap.pypa.io/get-pip.py
# python3 get-pip.py -
Install the virtualenv package for python to separate the application's environment:
# python3 -m pip install virtualenv
-
Install Phusion Passenger, which will be used to run the application:
# plesk installer --select-release-current --install-component passenger
-
Enable passenger module in Tools & Settings > Apache Web Server (in case the application should be served by Apache)
-
Create a new service plan in Service Plans to preconfigure the web-server
-
Set the following configuration in Web Server tab:
-
To have application served by Apache:
-
Enable Proxy mode, if it is present
-
Set the following Additional Apache directives (for HTTP and HTTPS) to enable processing:
PLESK_INFO: PassengerEnabled On
-
-
To have application served by nginx:
-
Disable Proxy mode
-
Set the following Additional nginx directives, to enable processing:
PLESK_INFO: passenger_enabled on;
passenger_app_type wsgi;
passenger_startup_file passenger_wsgi.py;
-
-
-
Set the following configuration in Hosting Parameters tab:
-
Enable SSH access to the server shell under the subscription's system user. Set shell to /bin/bash
Warning: Providing users with shell access might be insecure and may allow them to exploit OS vulnerabilities. Make sure that the server has the latest updates installed.
-
-
Save the Service Plan and use it on subscriptions that should have Python applications installed
-
Add EPEL repository and install Python 3.6:
# yum install -y epel-release
# yum install -y python36 -
Download and install the Python package manager from the official website:
# wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
# python3.6 get-pip.py -
Install the virtualenv package for python to separate the application's environment:
# python3.6 -m pip install virtualenv
-
Install Phusion Passenger, which will be used to run the application:
# plesk installer --select-release-current --install-component passenger
-
Enable passenger module in Tools & Settings > Apache Web Server (in case application should be served by Apache)
-
Create a new service plan in Service Plans to preconfigure the web-server
-
Set the following configuration in Web Server tab:
-
To have application served by Apache:
-
Enable Proxy mode, if it is present
-
Set the following Additional Apache directives (for HTTP and HTTPS) to enable processing:
PLESK_INFO: PassengerEnabled On
-
-
To have application served by nginx:
-
Disable Proxy mode
-
Set the following Additional nginx directives, to enable processing:
PLESK_INFO: passenger_enabled on;
passenger_app_type wsgi;
passenger_startup_file passenger_wsgi.py;
-
-
-
Set the following configuration in the Hosting Parameters tab:
-
Enable SSH access to the server shell under the subscription's system user. Set shell to /bin/bash
Warning: Providing users with shell access might be insecure and may allow them to exploit OS vulnerabilities. Make sure that the server has the latest updates installed.
-
-
Save the Service Plan and use it on subscriptions that should have Python applications installed
-
Install Python 3.6:
# yum install -y alt-python36
-
Download and install the Python package manager from the official website:
# wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
# /opt/alt/python36/bin/python3.6 get-pip.py -
Install the virtualenv package for python to separate application's environment:
# /opt/alt/python36/bin/python3.6 -m pip install virtualenv
-
Install Phusion Passenger, which will be used to run the application:
# plesk installer --select-release-current --install-component passenger
-
Enable passenger module in Tools & Settings > Apache Web Server (in case application should be served by Apache)
-
Create a new service plan in Service Plans to preconfigure the web-server
-
Set the following configuration in Web Server tab:
-
To have application served by Apache:
-
Enable Proxy mode, if it is present
-
Set the following Additional Apache directives (for HTTP and HTTPS) to enable processing:
PLESK_INFO: PassengerEnabled On
-
-
To have application served by nginx:
-
Disable Proxy mode
-
Set the following Additional nginx directives, to enable processing:
PLESK_INFO: passenger_enabled on;
passenger_app_type wsgi;
passenger_startup_file passenger_wsgi.py;
-
-
-
Set the following configuration in Hosting Parameters tab:
-
Enable SSH access to the server shell under the subscription's system user. Set shell to /bin/bash
Warning: Providing users with shell access might be insecure and may allow them to exploit OS vulnerabilities. Make sure that the server has the latest updates installed.
-
-
Save the Service Plan and use it on subscriptions that should have Python applications installed
Deploy the application
-
Connect to the server via SSH as the subscription's system user (this can be found in Subscriptions > example.com > System User) and navigate to the Document Root
$ ssh example@example.com
$ cd ~/httpdocs/ -
Move existing files to the backup directory in the subscription, so they will not be processed instead of the application:
$ mkdir ~/backup
$ mv ~/httpdocs/* ~/backup/ -
Create a new virtual environment for the Django application:
-
On Ubuntu and Debian:
$ python3 -m virtualenv -p python3 python-app-venv
-
On RHEL and CentOS:
$ python3.6 -m virtualenv -p python3.6 python-app-venv
-
On CloudLinux:
$ /opt/alt/python36/bin/python3.6 -m virtualenv -p python3.6 python-app-venv
-
-
Enter the virtual environment:
$ source ./python-app-venv/bin/activate
-
Install the Django framework and check if it could be imported:
-
On RHEL/CentOS/CloudLinux 7:
(python-app-venv) $ pip install 'Django>=2.1,<2.2'
(python-app-venv) $ python -c "import django;print(django.get_version())"
2.1.8 -
On Debian 8, 9 and Ubuntu 14.04, 16.04 and 18.04:
(python-app-venv) $ pip install Django
(python-app-venv) $ python -c "import django;print(django.get_version())"
2.2
-
-
Create a
passenger_wsgi.py
file using vi:(python-app-venv) $ vi ~/httpdocs/passenger_wsgi.py
Add the following content to it:
CONFIG_TEXT: 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: This file describes to Passenger and WSGI how to correctly start and run the application.
Here djangoProject is used as the project name. It must be modified to the actual project name. -
Create the application itself (for example, Django CMS, or a scaffold Django project):
Create a "scaffold" Django application-
Create the default Django project:
(python-app-venv) $ django-admin startproject djangoProject
-
Edit configuration of the project to allow serving requests from any host:
(python-app-venv) $ sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ['*']/" djangoProject/djangoProject/settings.py
Note: By default, django does not permit application to be run on any host, therefore,
ALLOWED_HOSTS
value in thesettings.py
file of the application should be adjusted for it to run correctly. -
Create a
tmp
directory for Passenger and a filerestart.txt
in it:(python-app-venv) $ mkdir tmp
(python-app-venv) $ touch tmp/restart.txtNote: Passenger caches application to allow for faster responses. In order to apply changes made in code, this file must be created: Restarting Applications: restart.txt
Django CMS application-
Install
djangocms-installer
Python package:(python-app-venv) $ pip install djangocms-installer
-
Create the
djangoProject
to hold the project files:(python-app-venv) $ mkdir djangoProject
-
Deploy the Django CMS instance to the
djangoProject
directory with the correct name:(python-app-venv) $ djangocms -f -p ./djangoProject djangoProject
Note: Django CMS will also create a superuser
admin
with passwordadmin
by default. -
Edit configuration of the project to allow serving requests from any host:
(python-app-venv) $ sed -i "s/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ['*']/" djangoProject/djangoProject/settings.py
Note: By default, django does not permit application to be run on any host, therefore,
ALLOWED_HOSTS
value in thesettings.py
file of the application should be adjusted for it to run correctly. -
Create a
tmp
directory for Passenger and a filerestart.txt
in it:(python-app-venv) $ mkdir tmp
(python-app-venv) $ touch tmp/restart.txtNote: Passenger caches application to allow for faster responses. In order to apply changes made in code, this file must be created: Restarting Applications: restart.txt
-
-
Navigate to Domains > example.com > Hosting Settings and set Document Root to httpdocs/djangoProject
Note: If the application is showing error 403, please review that the domain's Additional Apache & Nginx directives are applied from the Service Plan. If these do not exist in the domain, copy them from the Service Plan and apply them directly in Domain > example.com > Additional Apache and Nginx Directives.
Comments
32 comments
Can you please clarify step 10? what is the directory structure before and after this command?
Thank you for bringing that to our attention. The article was modified.
Hi thanks for the tutorial, I tried to follow the steps however i seem to have issues with the Phusion Passenger. I tried through both nginx proxy and apache however in the first i get a forbidden 403 error and in the second to a generic centOS testing html page. it seems that when i got to my example.com site the django server is not reached.
@Ariel
I could not find any staff with similar symptoms in the internal knowledgebase.
So, I recommend you to contact our Technical Support in order to find the cause of the issue.
I have followed these instructions on a Centos 7 server with Plesk Onyx installed.
When I try to add these lines to additional apache directives (my setup is slightly different)
PassengerPython /home/.virtualenvs/myapp/bin/python
PassengerEnabled on
I get the error:
Invalid Apache configuration: [Wed Nov 22 11:18:45.584621 2017] [so:warn] [pid 19397] AH01574: module actions_module is already loaded, skipping [Wed Nov 22 11:18:45.587626 2017] [so:warn] [pid 19397] AH01574: module headers_module is already loaded, skipping [Wed Nov 22 11:18:45.588008 2017] [so:warn] [pid 19397] AH01574: module logio_module is already loaded, skipping [Wed Nov 22 11:18:45.589475 2017] [so:warn] [pid 19397] AH01574: module suexec_module is already loaded, skipping AH00526: Syntax error on line 3 of /var/www/vhosts/system/admin.myapp.com/conf/vhost.conf: Invalid command 'PassengerPython', perhaps misspelled or defined by a module not included in the server configuration
By default, Phusion Passenger was not installed, but I added it by going to
and now shows as installed.
On another server with Ubuntu rather than Centos and the same version of Plesk I was able to use this line in the apache directives successfully but am a bit wary of installing things outside of plesk:
WSGIScriptAlias / /home/django/myapp/config/wsgi.py
Are there any alternative approaches to getting this working?
@Phoebe Bright,
Hello! We have an article for the first "module is already loaded" issue: https://support.plesk.com/hc/en-us/articles/213944985
As for the second question, could you please clarify the purpose of this directive?
Hello, @Artyom Baranov, @Alexandr Bashurov
I think instead of `passenger` your developers could use `gunicorn` which is kind`a standard for Django hosting and it is much simpler, too.
Could you point me a link to a forum, where new features are discussed ?
Hi @Dmytro!
You may propose a new feature at Plesk user-voice. Each feature may be discussed, there is a functionality to comment there.
Suggested features are monitored by Plesk Development Team and popular suggestions are likely to be implemented.
Hello friends,
I'm using CentOS 7 and Plesk Onyx last patch.
when I log in as the hosting(customer) user and execute:
python3.6 -m virtualenv -p python36 python-app-venv
I get the following error message:
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007f5438eef740 (most recent call first):
Aborted
Which may be the cause?
thanks and all the best
@Alejandro Betancor,
As I understand, you were trying to deploy the virtualenv environment using the Python installed into the chrooted shell template.
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.
Please, change the shell for the system user to /bin/bash (not chrooted) in Domains > example.com > Web Hosting Access, as suggested in the article and prepare the environment using it.
@Alexandr Bashurov
I'm using /bin/bash and have the same problem.
I can not find the solution for this...
I was able to get my Django site up and running except for one thing.
I can not serve my media files. (My static files work fine)
How would I go about serving my media files with this configuration?
I tried following other guides online for serving media files on nginx.
This for example:
But they did not work.
Thanks in advance.
@Alejandro Betancor
Make sure that you have only one installed version of python on the server, otherwise, they may conflict with each other.
@Cbordon
Is "Serve static files directly by nginx" enabled in Domains > example.com > Apache & Nginx Settings?
Hi @Alisa Kasyanova
which are the best practices to remove python 2.7 from CentOS 7 without breaking the system(Plesk) ?
thanks
Hi @Alejandro Betancor,
It is not recommended to remove Python
Removing python without dependencies will break yum
Usual python removing with dependencies will fail with the following error:
Hello @Nikita Nikushkin
Then how can I afford to have only one version of python installed? CentOS comes by default with Python 2.7 and in this guide they are explaining how to install Python 3.6.
How can I acomplish to follow the advice of @Alisa Kasyanova, if I can not uninstall version 2.7?
There is a solution ?
All the best
@Alejandro Betancor,
She did not mean removing the Python 2.7 as is
There is a possibility that you have several Python 3.X versions installed from different repositories. In this case, yes, there is a probability that they can conflict with each other
I double checked the solution from the article but did not receive errors as yours
Please repeat all steps from the beginning and if the issue still persists create a request to Plesk Support Department to give us an opportunity to take a look at the issue
Hi @ Nikita Nikushin,
Thanks for your answer,
Regarding this "She did not mean removing the Python 2.7 as it" it is quite confusing when the answer was "Make sure that you have only one installed version of python on the server, otherwise, they may conflict with each other." the only way to "have only one version of python on the server" would be removing the other ones. Or maybe it needs a little bit of explanation how to have only one version when having 2.7 and 3.6 because is well known CentOS 7 comes with 2.7 by default and in this guide we are installing 3.6.
I went all the steps of the guide again with my CentOS 7 and so far I have the same problem.
I will proceed to create the ticket.
all the best
@Alejandro Betancor
I see that you have created a ticket with us. Let's proceed with the discussion there in order to avoid any further misunderstanding.
Hi @Alisa Kasyanova
Thanks for the quick reply, really looking forward to get Django working :)
All the best
Hey,
Thanks for the tutorial, but this seems to not work for me. I'm stuck at step 7 in installation (ubuntu) "Create a new service plan in Service Plans to preconfigure the web-server".
The Service Plans section does not exist in my admin panel (or I can't find it). Probably this is because I'm using Plesk 12 VPS Web Admin? I tryed to continue with all the other steps without errors, but loading the site in the browser leads to the 403 page, while the error log says no index file is found. The "Proxy" checkbox in my webserver configuration is checked but grey and cannot be changed. If It's not possible to run django with my Plesk lizenz, is there an easy way to bypass plesk and configure apache manually for mod_wsgi for specific domains?
Best, Lukas
@sarbot Service Plans are indeed not configurable in Web Admin edition. The step 8 is to be performed on the domain level: Websites & Domains > example.com >
Apache & nginx Settings. Same with the step 9 (Websites & Domains > example.com > Hosting Parameters). Please note that Plesk 12 has reached EOL; it is recommended to upgrade to Plesk Onyx 17.8 to make sure all prerequisites are met.
Hello,
We have done all that is listed above for our CentOS 7 server with Plesk but all we get is a 403 page. What could we be doing wrong?
The log file shows this error -
No matching DirectoryIndex (index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm,index.shtml) found, and server-generated directory index forbidden by Options directive
Deepak
Hello Deepak,
Make sure that your document root points to the folder with to your application files:
https://www.plesk.com/blog/guides/django-hosting-latest-plesk-onyx/
Hi Anton,
Yes, document root points to that folder. What I did was simply follow the instructions in the tutorial above and did not change any names. So, httpdocs/djangoProject is what the root folder. I still get a 403.
It was so frustrating that I gave up but I thought I'll try again but the same thing happened. Do let me know what to do.
Thank you.
Deepak
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.pl,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
PassengerStartupFile passenger_wsgi.py
2074#0: *19900 directory index of "/var/www/vhosts/XXX/httpdocs/djangoProject/" is forbidden
Hi! For the admins who get 403 Error! I used to get the exact same error after doing everything written on this tutorial. I noticed that after entering the Apache or nginx directives while creating the Service Plan for Python Django, these directives are not being inherited by the subscription. I added the subscription then on subscription page, enter the Apache and nginx settings and enter the directives again. Then application started to work. At least in my case. Maybe there is a bug that prevents the subscriptions to inherit the directives from Service Plan.
I spent a few days implementing this process and finally found what needed to be modified. So, I'm sharing my findings and hope it will help you as well.
After completing the process step by step, I was getting the error:
No matching DirectoryIndex (index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm,index.shtml) found, and server-generated directory index forbidden by Options directive
So, I unchecked the proxy mode to switch to nginx instead of apache. This time the Phusion Passenger was loading the page but it was saying that something was wrong. It didn't tell me what was wrong. So I did the following:
# cd /etc/nginx/conf.d/
# touch directives.conf
# vi /etc/nginx/conf.d/directives.conf
a text editor opened and I typed:
passenger_app_env development;
This time when I tried to open my website, passenger showed me the error details from which I found what was wrong.
In step 6, instead of using the command $ vi ~/httpdocs/passenger_wsgi.py, just create the file on your computer, copy the content in it, save it on your local machine and then manually upload it on the server. Don't use the command line!!!
That's it. now my Django website is up and running.
Good day,
I'm trying to install Django CMS on Plesk Obsidian CentOS 7 and I'm ending with an error.
I'm at point 7 Django CMS application point 3.
(python-app-venv) $ djangocms -f -p ./djangoProject djangoProject
Thank you for your help
Please sign in to leave a comment.