How to configure Django application in Plesk?

Created:

2017-04-19 23:33:41 UTC

Modified:

2017-08-16 16:56:36 UTC

4

Was this article helpful?


Have more questions?

Submit a request

How to configure Django application in Plesk?

Applicable to:

  • Plesk for Linux

Question

How to install Django application for a website?

Answer

1. Install a Python package manager:

For Debian-based OS:

# apt-get install python-pip

For yum-based OS:

# yum install python-pip

For CloudLinux:

# curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
# python get-pip.py

2. It is common practice to have a separate virtual environment for each Python application. To make this possible install the “virtualenv” package:

# pip install virtualenv

3. To host Django projects, the Phusion Passenger application server will be used. Install it with the help of Plesk Installer under Tools & Settings > Updates and Upgrades or via command line:

# plesk installer

4. In the directory of the domain for which it is needed to create the application, create a separate folder for Django. Alternatively, create a new domain/sub-domain, where the application will be run.

Note: If a new sub-domain is created, remove a default site content.

5. All the next steps should be performed on behalf of a subscription user. Log in as a domain user over ssh, or using su - %username% command.

Note: '/bin/bash' access type should be set under Plesk > Domains > example.com > Web Hosting Access > Access to the server via SSH

6. Enter domains root directory, and then activate the new virtual environment for Python:

# su djangouser
$ virtualenv django-app-venv

7. Enter into the virtual environment:

$ source ./django-app-venv/bin/activate

8. The next step is to install the Django framework. The latest stable version of Django can be installed using the pip command. It will be installed in the created virtual environment:

$ pip install Django

9. Check the Django installation using the Python interpreter of the application virtual environment. Type python into the shell and use the following code:

(django-app-venv) $ python
>>> import django
>>> print(django.get_version())
1.7

The output above means that Django 1.7 is installed.

10. Download the application via FTP or SSH to the django-app/ application directory:

$ ls /var/www/vhosts/example.com/httpdocs/app
app/ django-app-venv/​
Note: There is a possibility to create a template for application using the following command in virtual environment:

(django-app-venv) $ django-admin.py startproject 'app' && deactivate

11. Change domain's document root to /httpdocs/app/

12. Activate Phusion Passenger support for a domain.

  • If domain is using nginx, then it is required to perform the following actions:
    1. Disable Proxy mode in Domains > example.com > Apache & nginx Settings .
    2. Insert the following lines into Domains > example.com > Apache & nginx Settings > Additional nginx directives :
    passenger_enabled on;
    passenger_python /var/www/vhosts/djangotest.tt/django-app-venv/bin/python;
  • If domain is using Apache, insert the following lines into Domains > example.com > Apache & nginx Settings > Additional Apache directives :
    PassengerPython /var/www/vhosts/example.com/django-app-venv/bin/python
    PassengerEnabled on

13. To serve the application through the application server, it is needed to create the passenger_wsgi.py file inside the django-app directory with the following content:

import sys, os
app_name = 'app'
env_name = 'django-app-venv'
cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/' + app_name)
INTERP = cwd + '/' + env_name + '/bin/python'
if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
sys.path.insert(0, cwd + '/' + env_name + '/bin')
sys.path.insert(0, cwd + '/' + env_name + '/lib/python2.7/site-packages/django')
sys.path.insert(0, cwd + '/' + env_name + '/lib/python2.7/site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", app_name + ".settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Note: Variable value in line 2 should be replaced with the name of application.

For this test case, it is required to adjust  ALLOWED_HOSTS in /var/www/vhosts/djangotest.tt/httpdocs/app/app/settings.py file .

# cat /var/www/vhosts/djangotest.tt/httpdocs/app/app/settings.py
ALLOWED_HOSTS = ["*"]

14. Check the application by navigating to example.com via a browser.

Additional information

If you have multiple Python installations  on the server, use the following command to create a virtial environment with different Python versions:

$ virtualenv -p python3 virtual/environment3
$ virtualenv -p python2 virtual/environment2
Have more questions? Submit a request
Please sign in to leave a comment.