To set up Django on Plesk:
- First of all, let’s check Python presence in the system. we can do it by issuing the command:
python –version
- Install a Python package manager (under the “root” user):
- apt-get install python-pip
- It is common practice to have a separate virtual environment for each Python application. So, let’s install the “virtualenv” package (under the “root” user):
- pip install virtualenv
- We will use Phusion Passenger as an application server for hosting Django projects. So, let’s install it (under the “root” user):
- apt-get install libapache2-mod-passenger
- The next step is to create a domain in Plesk for the application. Let it be a subdomain in our case. We’ll have a separate directory for document root and app files.
- One should take into account that the document root is a subdirectory. This is needed for the proper functioning of the application server. To avoid problems, you need to clean up the newly created django-public directory (remove a default site stub). You can do it via file manager, FTP or SSH. It’s more convenient to work via SSH under a particular user (not root), so let’s enable SSH access for the subscription
- Go to the “django-app” directory and create a virtual environment for Python:cd django-app/
- virtualenv django-app-venv
- Activate the new virtual environment to make sure that subsequent commands will be performed in a proper context: source django-app-venv/bin/activate
- Next step is to install Django framework. The latest stable version of Django can be installed by “pip”:
pip install Django
- You can verify your Django installation using Python interpreter. Type python in the command shell and use the following code:
- import django
print(django.get_version())
- you can upload your application via FTP or SSH. If the application is located at the “app” directory, you need to upload files to “django-app/app” directory. So the “django-app” directory may look like the following:
app/
django-app-venv/
- One of the possible ways to serve static assets is to copy them to the corresponding directory:
cp -R ./django-app/django-app-venv/local/lib/python2.7/site-packages/django/contrib/admin/static
./django-app/django-public/
- To serve our application via the application server, we need to create 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()
- The final step is to check your app via a browser. If there were errors you can see the “Web application could not be started” error page from Phusion Passenger with details. If everything is OK, you’ll see your application
- In some cases, app restart is required. Instead of restarting of the whole web server, Passenger provides a simple way to restart app. You need to create directory named “tmp” inside “django-app”:
- mkdir tmp
- After that, to restart the app, you can use the following command:
- touch tmp/restart.txt