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.
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.
cd ~ virtualenv -p /usr/bin/python2.7 djangoenv
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
To activate the virtual environment, type the following command:
source ~/djangoenv/bin/activate
To install Django and its dependencies, type the following command:
pip install django==1.8.7 flup6 paste
Django installs the SQLite database engine by default. However, you can use MySQL or PostgreSQL databases if you want:
pip install MySQL-python
To install the PostgreSQL database engine, type the following command:
pip install psycopg2
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.
cd ~/public_html django-admin.py startproject mysite
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]
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")
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
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:
STATIC_URL = 'http://example.com/mysite/public/static/'
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/'
By default, Django uses a SQLite database. However, you can configure Django to use a MySQL or PostgreSQL database instead:
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': '', } }
At the command prompt, type the following commands:
cd ~/public_html/mysite python manage.py syncdb
At the command prompt, type the following commands:
cd ~/public_html/mysite python manage.py collectstatic
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.
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: