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.
- Log into SSH with your cPanel credentials.
- Create and navigate to a temp directory for the installation:
mkdir temp; cd temp
- Download the latest version of Virtualnv:
curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz
- Extract the compressed install files into the temp directory:
gzip -cd virtualenv-12.0.7.tar.gz |tar xf -
- Navigate to the install files directory:
cd virtualenv-12.0.7
- Use Python to run the Virtualenv install:
python2.7 setup.py install --user
- Navigate to the home directory:
cd ~
- Remove the install files:
- Run find to check the contents of the directory before deleting it:
find temp -ls
- After ensuring that the directoy is correct, delete it:
find temp -delete
- Run find to check the contents of the directory before deleting it:
- Set up your virtual environment in the mydjango directory:
~/.local/bin/virtualenv mydjango --python=python2.7
- 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:
- Check which PIP is being used. The output should indicate the pip file in the~/mydjango/bin/ directory:
which pip
- Begin by upgrading PIP:
~/mydjango/bin/pip install pip --upgrade
- Install Django:
~/mydjango/bin/pip install django
- Install Flup:
~/mydjango/bin/pip install flup
- 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.
- Create a new project:
~/mydjango/bin/django-admin.py startproject testproject ~/mydjango/
- Override the default SCRIPT_NAME with the path to set it to index.fcgi:
echo FORCE_SCRIPT_NAME=\"/index.fcgi/\" >> ~/mydjango/testproject/settings.py
- Navigate to the new project directory:
cd mydjango/
- Grant executable permissions to the manage.py file:
chmod +x manage.py
- 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.
- Create index.fcgi and place it inside your public_html directory, or your desired document root.
- 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.
- Edit the file and enter this code:
#!/home/username/mydjango/bin/python
import os
import sysfrom flup.server.fcgi import WSGIServer
from django.core.wsgi import get_wsgi_applicationsys.path.insert(0, "/home/username/mydjango")
os.environ['DJANGO_SETTINGS_MODULE'] = "testproject.settings"WSGIServer(get_wsgi_application()).run()
- 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> - 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:
- Log into SSH.
- Navigate to your public_html directory:
cd ~/public_html
- 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.