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.
- Additional Steps for VPS and Dedicated Servers
- Setup Django Without Virtualenv
- Setup Django Using Virtualenv
- Setting up Django Projects
- Managing your Project
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:
- Use SSH to login to the server as root.
- 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
- Run the following command:
- Install Python 2.7 with yum:
yum install Python27
-
. /etc/profile.d/python27.sh
- Add fcgid_module to your active Apache configuration:
echo "LoadModule fcgid_module modules/mod_fcgid.so" >> /usr/local/apache/conf/includes/pre_main_global.conf
- Restart the httpd service:
service httpd restart
- Install the libffi compiler dependencies:
yum install libffi libffi-devel
- 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.
- Log into SSH as the cPanel user.
- Add /.local/bin/ to the top of your PATH:
echo "export PATH=~/.local/bin:\${PATH}" >> ~/.bashrc
- Update your PATH to the newly configured PATH:
. ~/.bashrc - Create a symlink to use Python 2.7:
ln -s /opt/python27/bin/python ~/.local/bin/python
- 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
- Install PIP:
python <(GET https://bootstrap.pypa.io/get-pip.py) --user
- Check which PIP is being used. The output should indicate the PIP file in the ~/.local/bin/ directory:
which pip
- 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.
- 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
- Download Django to save it as a tarball:
wget -q -O django.tar.gz https://www.djangoproject.com/download/1.11.11/tarball
- 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)user@domain.com [~]#
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:
- Install Django using the contents of the tarball:
~/mydjango/bin/pip install -e Django-1.11.11/
- 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
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.
-
Create a new project:
If you used Virtualenv, you will need to specify the path of django-admin.py:~/mydjango/bin/django-admin.py startproject testproject ~/mydjango/
If you did not use Virtualenv, you may call django-admin.py without the path:
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 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()
- 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.
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.