Django is a web framework based on Python. Python is available on all packages, and a Django application may be uploaded on any package. But, to create a new project on the server and complete this quickstart, a package with terminal access is necessary. This quickstart covers using Django with Passenger available on v6+ platforms.
pip-python install django mysql
Important (v6+ platforms): On v6+ platforms, first designate a Python interpreter (do not use the default, “system”), then after installation, change the interpreter location of
Now, Django has been installed on your account. Setup a Django application. We’ll modify Django slightly to create a web-accessible public/ folder and keep code outside a URL-accessible resource, one level down from the document root.
- Switch to /var/www to create a new project:
- Initialize an application named myapp
django-admin startproject myapp
- Note: per documentation, it’s recommended not to initialize projects under /var/www. This is only true if /var/www is accessible as a document root (which it is not)
- Django creates a management controller (
manage.py) within the folder named after the project, in addition to an app named after the project, one level down. Move to the app folder from
- Make a
tmp/directory for serving public files and storing logs. tmp/ will be used to control application restarts with Passenger.
mkdir tmp public
- Passenger will look for a file called
/var/www/myapp/myapp, create a link to satisfy this
ln -s wsgi.py passenger_wsgi.py
passenger_wsgi.pyto append your application path to Python. This requires 2 minor additions:
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
import os, sys sys.path.append("/var/www/myapp") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
- Explanation: only the first 2 lines are changed: (1) sys module is loaded after os, this is necessary for (2) appending a library path via
sys.path.append, the value being the project root (
/var/www/myappin this case).
- Lastly, connect this application to a web-accessible path
Restarting your application
An app may either be restarted upon each request or at a single point. To restart an app every invocation, create a file called always_restart.txt in tmp/:
touch tmp/always_restart.txt. To perform a single restart, create a file called restart.txt in tmp/:
touch tmp/restart.txt. Passenger, which handles process management, will compare the timestamp with its internal record and restart as necessary. To restart a second time, either reissue the command or delete, then recreate the file to update its modification time.
Viewing launcher errors
On newer v6 platforms, launcher errors may be viewed through the consolidated log file,
PassengerEnabled On PassengerAppRoot /var/www/app/root PassengerPython /usr/local/share/python/pyenv/shims/python