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.
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?
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.
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.
2074#0: *19900 directory index of “/var/www/vhosts/XXX/httpdocs/djangoProject/” is forbidden
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:
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:
When served by Apache Additional Nginx directives shouldn’t be specified.
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/wsgi-loader.py”, line 369, in
app_module = load_app()
File “/usr/share/passenger/helper-scripts/wsgi-loader.py”, line 76, in load_app
return imp.load_source(‘passenger_wsgi’, startup_file)
File “/var/www/vhosts/imagebucket.io/httpdocs/passenger_wsgi.py”, line 1, in
from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
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?
Done all steps, but I got 403 error for Apache and if i’m using Nginx then error is 500
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.