Knowledge Base

How to install and configure Django with FastCGI on shared hosting

Django is a Python framework that enables you to easily create a Python site together with a database backend. This article demonstrates how to install Django on a shared hosting account by using virtualenv and pip, how to configure Django to work with FastCGI, and how to configure Django's administration interface.

Turbo Web Hosting servers do not support Python-based web applications, including Django.

Installing Django

To install Django, you first create a virtual environment for Python by using the virtualenv tool. After you activate the virtual environment, you can use the pip installer to install Django, as well as a database engine.

  1. Log in to your account using SSH.
  2. To create the virtual environment, type the following commands:
    cd ~
    virtualenv -p /usr/bin/python2.7 djangoenv
    
    The virtualenv command creates a virtual environment named djangoenv, and subsequent commands in this article assume that the environment is named djangoenv. If you use a different environment name, make sure you replace all occurrences of djangoenv with your own environment name.

    A2 Hosting currently only supports Django with Python 2.6 or 2.7. You can install Django using Python 3, but this configuration is unsupported. If you want to do this, type the following command instead:

    virtualenv -p /usr/bin/python3 djangoenv
    
  3. To activate the virtual environment, type the following command:

    source ~/djangoenv/bin/activate
    The command prompt now starts with (djangoenv) to indicate that you are working in a Python virtual environment. All of the following commands in this article assume that you are working in a Python environment. If you log out of your SSH session (or deactivate the virtual environment by using the deactivate command), make sure you reactivate the virtual environment before following any of the steps below.
  4. To install Django and its dependencies, type the following command:

    pip install django==1.8.7 flup6 paste
    • The flup6 and paste packages include several helpful tools for running WSGI (Web Server Gateway Interface) applications.
    • Django version 1.9 removes FastCGI support. Therefore, you must use Django version 1.8.7 on shared hosting servers.
  5. Django installs the SQLite database engine by default. However, you can use MySQL or PostgreSQL databases if you want:

    • To install the MySQL database engine, type the following command:
      pip install MySQL-python
    • To install the PostgreSQL database engine, type the following command:

      pip install psycopg2
  6. Django is now installed and ready to be configured.

Configuring Django

You have installed Django, but it will not run on your web site until you complete some additional configuration steps. At a minimum, you must configure Django to work with FastCGI. You can also set up the Django administration interface.

  1. To set up a Django application, type the following commands:
    cd ~/public_html
    django-admin.py startproject mysite
    
    These commands create an application named mysite, and subsequent commands and settings in this article assume that the application is named mysite. If you use a different application name, make sure you replace all occurrences of mysite with your own application name.
  2. Create an .htaccess file in the public_html directory that contains the following lines:

    AddHandler fcgid-script .fcgi
    RewriteEngine on
    # Set up static content redirect:
    RewriteRule static/(.+)$ mysite/public/static/$1
    # The following two lines are for FastCGI:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ application.fcgi/$1 [QSA,L]
    
  3. Create a file named application.fcgi in the public_html directory that contains the following lines. Replace username with your own account username:

    #!/home/username/djangoenv/bin/python
    
    # Set up the virtual environment:
    import os, sys
    os.environ.setdefault('PATH', '/bin:/usr/bin')
    os.environ['PATH'] = '/home/username/djangoenv/bin:' + os.environ['PATH']
    os.environ['VIRTUAL_ENV'] = '/home/username/djangoenv/bin'
    os.environ['PYTHON_EGG_CACHE'] = '/home/username/djangoenv/bin'
    os.chdir('/home/username/public_html/mysite')
    
    # Add a custom Python path.
    sys.path.insert(0, "/home/username/public_html/mysite")
    
    # Set the DJANGO_SETTINGS_MODULE environment variable to the file in the
    # application directory with the db settings etc.
    os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings"
    
    from django.core.servers.fastcgi import runfastcgi
    runfastcgi(method="threaded", daemonize="false")
    
  4. At the command prompt, type the following command to set the correct file permissions for the application.fcgi file:

    chmod 755 ~/public_html/application.fcgi
  5. Use your browser to go to http://www.example.com, where example.com represents your domain name. You should see the Django It worked! page.
Configuring the administration interface

Django includes a very nice administration interface that you can use to modify web site applications. The following steps show how to configure the Django administration interface:

  1. Use a text editor to open the /home/username/public_html/mysite/mysite/settings.py file. Locate the STATIC_URL line at the end of the file, and then modify it as follows. Replace example.com with your own domain name:
    STATIC_URL = 'http://example.com/mysite/public/static/'
    
  2. After the STATIC_URL line, add the following line to the settings.py file. Replace username with your account username:

    STATIC_ROOT = '/home/username/public_html/mysite/public/static/'
  3. By default, Django uses a SQLite database. However, you can configure Django to use a MySQL or PostgreSQL database instead:

    • To use a MySQL database, edit the DATABASES section in the settings.py file as follows. Replace the username_databasename, username_dbusername, and user_password values with the correct values for your MySQL database:
      DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.mysql',
              'NAME': 'username_databasename',
              'USER': 'username_dbusername',
              'PASSWORD': 'user_password',
              'HOST': 'localhost',
              'PORT': '3306',
          }
      }
      
    • To use a PostgreSQL database, edit the DATABASES section in the settings.py file as follows. Replace the username_databasename, username_dbusername, and user_password values with the correct values for your PostgreSQL database:

      DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.postgresql_psycopg2',
              'NAME': 'username_databasename',
              'USER': 'username_dbusername',
              'PASSWORD': 'user_password',
              'HOST': 'localhost',
              'PORT': '',
          }
      }
      
    Remember that correct indentation in Python is very important! Make sure you preserve the correct indentation.
  4. Save the changes to the settings.py file and exit the editor.
  5. At the command prompt, type the following commands:

    cd ~/public_html/mysite
    python manage.py syncdb
    
  6. When you are prompted to create a superuser, type yes and then press ENTER.
  7. At the Username prompt, type the administrator username, and then press ENTER.
  8. At the Email address prompt, type the administrator e-mail address, and then press ENTER.
  9. At the Password prompt, type the administrator password, and then press ENTER.
  10. At the command prompt, type the following commands:

    cd ~/public_html/mysite
    python manage.py collectstatic
    
  11. When you are asked if you want to overwrite existing files, type yes and then press ENTER.
  12. Use your browser to go to http://www.example.com/admin, where example.com represents your domain name. You should see the Django administration login page. To log in, use the superuser credentials that you created earlier.

    If you want to change or reset the superuser password, type the following commands. Replace username with the username you specified in step 7:

    cd ~/public_html/mysite
    python manage.py changepassword username
    

    Follow the prompts to set the new password.

More Information

Now that you have a Django-enabled web site up and running, you can start the real work of developing your own applications. The following resources can help: