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:
One of the outputs you can get is the following:
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
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.
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:
For the next step, set shell options at “Hosting Parameters” tab and don’t forget to save your configuration!
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:
You also need to move existing files to the backup directory in the subscription, so they won’t be processed instead of the application:
$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
pip install Django
python -c "import django;print(django.get_version())"
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.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:
Now all you need to do is to change Domain Document Root, and if everything is OK, you’ll see your application as below.
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.2 / 5. Vote count: 13
Oh no, sorry about that!
Let us know how we can do better below
Thanks for your feedback!