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 python-is-python3 python3-venv
-
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 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
1 comment
Hallo,
Ich erhalte eine Fehlermeldung, wenn ich die Adresse meiner Domain in einem Browser öffne.
We're sorry, but something went wrong.
The issue has been logged for investigation. Please try again later.
Ich habe mich als 'admin' angemeldet.
Stimmen die Pfade oben alle, besonders dass Setup der 'wsgi'-Files?
Die SSH-Konsole habe ich in PLESK geöffnet und bin dann zum Datei-System der Ziel-Domäne gewechselt. (var/www/vhosts/...)
Die Domäne habe ich dann als HOME betrachtet.
Ich war soweit, dass ich die Django-Dateien im Browser sehen konnte, will ja aber die Default-Page als Antwort.
Ubuntu 18.04.6 LTS
Vielen Dank,
Alexander Fleck
Please sign in to leave a comment.