Django quickstart

Overview

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.

Quickstart

From the terminal, first install Django + MySQL from PyPI using Python’s package manager, pip.

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 /usr/local/bin/django-admin.

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.

  1. Switch to /var/www to create a new project:
    • cd /var/www
  2. 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)
  3. 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 /var/www:
    • cd myapp/myapp
  4. Make a public/ and tmp/ directory for serving public files and storing logs. tmp/ will be used to control application restarts with Passenger.
    • mkdir tmp public
  5. Passenger will look for a file called passenger_wsgi.py inside /var/www/myapp/myapp, create a link to satisfy this
    • ln -s wsgi.py passenger_wsgi.py
  6. Edit passenger_wsgi.py to append your application path to Python. This requires 2 minor additions:
    • Before:
      import os
      os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
      from django.core.wsgi import get_wsgi_application
      application = get_wsgi_application()
    • After:
      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/myapp in this case).
  7. Lastly, connect this application to a web-accessible path
    • visit Web > Subdomains within the control panel. Create a new subdomain called myapp with the document root /var/www/myapp/myapp/public

      Linking a Django subdomain underneath a project called myapp and its first app called "myapp" using Passenger.

      Linking a Django subdomain underneath a project called myapp and its first app called “myapp” using Passenger.

  8. Enjoy!

    Confirmation page that Django is up and running a-OK!

    Confirmation page that Django is up and running a-OK!

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, /var/log/passenger.log.

See also

 

Leave a Reply