Django with fastcgi

While it is possible to install Django on DCTIT servers, it is not a supported application, and we are unable to install it for you. It is recommended that you have a developer available to assist you with any issues that may arise while installing or using this software.

This article will cover installation, setting up Django projects, and managing your project:

Installation

On our shared and reseller accounts you will need to use Virtualenv to install a localized version of Django without root access. To start, you’ll need to set up and activate Virtualenv.

  1. Log into SSH with your cPanel credentials.
  2. Create and navigate to a temp directory for the installation:
    mkdir temp; cd temp
  3. Download the latest version of Virtualnv:
    curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz
  4. Extract the compressed install files into the temp directory:
    gzip -cd virtualenv-12.0.7.tar.gz |tar xf -
  5. Navigate to the install files directory:
    cd virtualenv-12.0.7
  6. Use Python to run the Virtualenv install:
    python2.7 setup.py install --user
  7. Navigate to the home directory:
    cd ~
  8. Remove the install files:
    1. Run find to check the contents of the directory before deleting it:
      find temp -ls
    2. After ensuring that the directoy is correct, delete it:
      find temp -delete
  9. Set up your virtual environment in the mydjango directory:
    ~/.local/bin/virtualenv mydjango --python=python2.7
  10. Activate your mydjango Virtualenv:
    source ~/mydjango/bin/activate

Once this is complete you will now be working in a Virtualenv setting and your command prompt should look similar to this:

(mydjango)[email protected] [~]#

The best practice for exiting Virtualenv is to log out of SSH and then log back in to start a new session. You can do this and then re-activate and begin working from this environment at any time by once again entering this full command:

source ~/mydjango/bin/activate

Next, to install Django. Please ensure that virtual environment is activated before proceeding:

  1. Check which PIP is being used. The output should indicate the pip file in the~/mydjango/bin/ directory:
    which pip
  2. Begin by upgrading PIP:
    ~/mydjango/bin/pip install pip --upgrade
  3. Install Django:
    ~/mydjango/bin/pip install django
  4. Install Flup:
    ~/mydjango/bin/pip install flup
  5. Grant Executable permissions to the django-admin.py file:
    chmod +x ~/mydjango/bin/django-admin.py

Setting up Django Projects

In order to create a new Django project, make sure your virtual environment is active and run these commands replacing testproject with the name of the directory you wish the project to be run from.

  1. Create a new project:
    ~/mydjango/bin/django-admin.py startproject testproject ~/mydjango/
  2. Override the default SCRIPT_NAME with the path to set it to index.fcgi: 
    echo FORCE_SCRIPT_NAME=\"/index.fcgi/\" >> ~/mydjango/testproject/settings.py
  3. Navigate to the new project directory:
    cd mydjango/
  4. Grant executable permissions to the manage.py file:
    chmod +x manage.py
  5. Start the new app:
    ./manage.py startapp newapp

Managing your Project

In order to manage your project, you will need to set up an index file that you can access from your browser.

  1. Create index.fcgi and place it inside your public_html directory, or your desired document root.
  2. Change the file’s permissions to 0755.
    Note: Failing to change this file’s permissions will cause Django to produce a 500 internal service error.
  3. Edit the file and enter this code:
    #!/home/username/mydjango/bin/python
    import os
    import sys

    from flup.server.fcgi import WSGIServer
    from django.core.wsgi import get_wsgi_application

    sys.path.insert(0, "/home/username/mydjango")
    os.environ['DJANGO_SETTINGS_MODULE'] = "testproject.settings"

    WSGIServer(get_wsgi_application()).run()

Note: The above code is only an example, and you will need to modify the path names specified to match your configuration. (For example, change “/home/username” to the path to your home directory, etc.)
  1. Add the following .htaccess rewrite rules to the .htaccess file in the same directory as your new index.fcgi file:
    AddHandler fcgid-script .fcgi
    DirectoryIndex index.fcgi

    <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteBase /
      RewriteRule ^index\.fcgi$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /index.fcgi/$1 [L]
    </IfModule>

  2. Copy your /static directory, which contains your CSS, Javascript, and image files, to your public_html directory or the document root you intend to use. This can be done using the SSH command line:
    1. Log into SSH.
    2. Navigate to your public_html directory:
      cd ~/public_html
    3. Copy the /static directory:
      cp ~/mydjango/lib/python2.7/site-packages/django/contrib/admin/static . -R

At this time, Django should now be correctly installed and you can manage your project by logging into your admin url:

http://mydomain.com/index.fcgi/admin/

Note: Replace mydomain.com with the domain you installed django to.