Skip to content

Django

Django is an open-source Python framework for developing WWW applications. On Serv00.com servers it is possible to run websites based on this framework thanks to the use of the Phusion Passenger technology.

Initial configuration

To run a website written in Django, first make sure your environment and domain are properly configured. Before proceeding to the next steps, make sure that:

  • Added virtualenv environment for python 3.X (or 2.7 for older projects) and installed Django with command: pip install Django.
  • The domain is properly added and configured in our DNS system.
  • WWW page of the type python has been added along with the path to the binary python inside the previously configured environment.

The Django project

The main directory of the Django project (i.e. the directory where the manage.py file resides) must be /usr/home/LOGIN/domains/DOMAIN/public_python. The Django project name does not matter as long as the above directory name is retained. You should also delete the index.html file from /usr/home/LOGIN/domains/DOMAIN/public_python/public/ directory.

New Django project

A new Django project should be created in the /usr/home/LOGIN/domains/DOMAIN/public_python directory - the easiest way to create an appropriate project is to run the following commands:

cd /usr/home/LOGIN/domains/DOMAIN/
rm -r public_python
django-admin.py startproject public_python

In the last command, you can rename public_python to any name of the project - then remember that the project will be created in a directory with that name, which must be changed to public_python.

Existing Django project

Place your existing Django project as /usr/home/LOGIN/domains/DOMAIN/public_python. In case the project is not named public_python, its name should be changed.

Phusion Passenger configuration

The last step is to prepare the configuration for the Phusion Passenger server which is responsible for running the Django application. In the Django project directory (/usr/home/LOGIN/domains/DOMAIN/public_python) create the passenger_wsgi.py file and place the following content (depending on the Django version), modifying the line indicating the name of the application (marked with a comment):

Django 1.7 and above

import sys, os

sys.path.append(os.getcwd())
os.environ['DJANGO_SETTINGS_MODULE'] = "application_name.settings" # change 'application_name' to
                                                                   # the name of the Django project

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Name of the application

The name of the application (Django project) can be read from the manage.py file with the command: grep DJANGO_SETTINGS_MODULE manage.py.

Testing the configuration

The correctness of the configuration for the Phusion Passenger server can be checked by issuing the command python passenger_wsgi.py - if the application does not return any errors, it means that the application should be properly launched by the server.

Warning

The command python passenger_wsgi.py will only check the correct configuration for the Phusion Passenger server - in the event of errors in the Django application themselves they will be handled by the server or Django itself.

An error preventing the application from starting will cause the page to display a generic error from the Phusion Passenger server. Errors while the application is running will be caught by Django and their handling depends on the configuration of the application itself.

Restart the application

Restart the Django application can be done using our service management panel in the WWW websites tab or using the command Devil account management system: devil www DOMAIN restart. On restart .pyc files are not removed.

Process limit for Phusion Passenger

Possibility to set the number of processes for python pages. Command syntax: devil www options DOMAIN processes QUANTITY

Where: - DOMAIN - stands for the python type WWW domain - QUANTITY - number of processes in the range from 1 to 80% of system processes of the hosting account

The option is also available in the DevilWEB panel: WWW webpagesManage (with a selected domain) ➡ DetailsProcess limit ➡ enter a new value and save the changes with the Save changes button.

Static files

All files under /usr/home/LOGIN/domains/DOMAIN/public_python/public are handled as static files. It is best to put all pictures, scripts, styles, etc. in this directory. Requests to files in this directory will not be processed by Python processes and will not burden the interpreter. For example, the file /usr/home/LOGIN/domains/DOMAIN/public_python/public/robots.txt will be available at http://DOMAIN/robots.txt.

To put all of the Django static files in /usr/home/LOGIN/domains/DOMAIN/public_python/public, add:

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

# The BASE_DIR variable should be created by Django in the settings.py file
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_ROOT = os.path.join(BASE_DIR, 'public', 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'public', 'media')
# or
# STATIC_ROOT = '/home/LOGIN/domains/DOMAIN/public_python/public/static/'
# MEDIA_ROOT = '/home/LOGIN/domains/DOMAIN/public_python/public/media/'

Then execute the command python manage.py collectstatic in the console.

Logs

Warning

Do not delete the logs directory, it may cause problems with Node.js, Ruby, Python pages.

The error logs are saved to the error.log file in the /usr/home/LOGIN/domains/DOMAIN/logs/ directory.

App lifetime

If there are no requests to the site for 24 hours, the application is automatically shut down and started again automatically on the first next visit. In the event of a change in the website configuration, the application is also automatically shut down and launched the next time the website is accessed for the first time.

External references