Django with fastcgi

Note: Django uses a SQLite database by default. Using another database software would require custom configuration that is not supported.

While it is possible to install Django on HostGator servers, it is not a supported application. While HostGator will not assist with installation or support of Django, we may assist with the following:

  • Providing the environment to run Django in.
  • Providing developers with error logs.

It is recommended that you have a developer available to assist you with any issues that may arise while installing or using this software. Users on Shared and Reseller servers will need to use the steps to setup Django using virtualenv.

VPS and Dedicated Servers

VPS and Dedicated servers may require installation and configuration of multiple software dependencies in order to successfully install Django. To install the required dependencies and set up your server for use with Django:

  1. Use SSH to login to the server as root.
  2. Ensure fcgi is installed:
    • Run the following command:
      httpd -M | grep fcgid
    • If an fcgi module is returned, skip to step 3. Example:
      root@server [~]# httpd -M | grep fcgi
      fcgid_module (shared)
      Syntax OK
    • If an fcgi module is not located, please expand the instructions to install fcgid_module:

      Install fcgid_module

  3. Install Python 2.7 with yum:
    yum install Python27
  4. . /etc/profile.d/
  5. Add fcgid_module to your active Apache configuration:
    echo "LoadModule fcgid_module modules/" >> /usr/local/apache/conf/includes/pre_main_global.conf
  6. Restart the httpd service:
    service httpd restart
  7. Install the libffi compiler dependencies:
    yum install libffi libffi-devel
  8. Log out of the server as root.

Your server should now be prepared to successfully install Django either using Virtualenv, or without Virtualenv.

Setup Django Without Virtualenv

Setting up Django without using Virtualenv is not possible on HostGator shared hosting. Before following these steps the server must have the correct dependencies installed and active, and the cPanel user must have compiler access. Compiler access may be granted from WHM, however it is enabled by default.

Note: Do NOT perform these steps as the root user. The following instructions are intended to be run from SSH as the cPanel user that the installation is for.
  1. Log into SSH as the cPanel user.
  2. Add /.local/bin/ to the top of your PATH:
    echo "export PATH=~/.local/bin:\${PATH}" >> ~/.bashrc
  3. Update your PATH to the newly configured PATH:
    . ~/.bashrc
  4. Create a symlink to use Python 2.7:
    ln -s /opt/python27/bin/python ~/.local/bin/python
  5. If the environment knows where to find Python, you need it to forget so that it will find the new link you just made. This command tells it to forget:
    hash -r
  6. Install PIP:
    python <(GET --user
  7. Check which PIP is being used. The output should indicate the PIP file in the ~/.local/bin/ directory:
    which pip
  8. Install Django:
    pip install certifi pyopenssl ndg-httpsclient pyasn1 flup django --user --upgrade

Congratulations! Django is now installed and useable for this user account. To follow the tutorials included in this article for creating and managing Django projects, please run the following command to create a mydjango directory:

mkdir ~/mydjango

Setup Django Using Virtualenv

On our Shared and Reseller accounts you will need to use Virtualenv to install a localized version of Django without root access. These steps may also be used on accounts on a VPS or Dedicated Server that has the necessary software dependencies installed. 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
  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 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
  11. Download Django to save it as a tarball:
    wget -q -O django.tar.gz
  12. Unzip and unpack the downloaded tarball:
    tar -zxvf django.tar.gz

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

(mydjango) [~]#

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. Install Django using the contents of the tarball:
    ~/mydjango/bin/pip install -e Django-1.11.11/
  2. Install Flup:
    ~/mydjango/bin/pip install flup
  3. Grant Executable permissions to the file:
    chmod +x ~/mydjango/bin/

Setting up Django Projects

If you used Virtualenv to install Django, make sure your virtual environment is active when following these steps. Please replace testproject with the name of the directory you wish the project to be run from.

  1. Create a new project:

    If you used Virtualenv, you will need to specify the path of

    ~/mydjango/bin/ startproject testproject ~/mydjango/

    If you did not use Virtualenv, you may call without the path:
 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/
  3. Navigate to the new project directory:
    cd mydjango/
  4. Grant executable permissions to the file:
    chmod +x
  5. Start the new app:
    ./ 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:
    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"

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/mydjango/bin/python" to match the output when running "which python", and "/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]
  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:

Note: Replace with the domain you installed django to.
Be aware, because of the way that .htaccess rules inherit, the DirectoryIndex command will set your default index file for this directory and all subdirectories and domains to look for an index.fcgi. If you have a site that uses a different file type(For example: index.html or index.php) you will need to edit the .htaccess file in the document root of that site and add a DirectoryIndex command that specifies the desired file you wish to load.