How to install django on cPanel

Trainee Asked on October 12, 2019 in Cpanel.
Add Comment
1 Answer(s)

It’s probably not the best thing to run Django application on cPanel (shared hosting) because of the following:

  • Most shared hosting providers do not allow you to install custom libraries which need to be compiled. You can still however create virtualenv and pip install packages into as long as they do not require anything to be compiled (e.g. Django)
  • Performance. In my experience it is possible to deploy a simple Django application on shared hosting however it is not very reliable and does not perform very well.

However it is not to say that it is impossible. These rough steps should be accurate enough to guide you to the right path. I haven’t done this is a while so there might be mistakes.

  • First you have to have SSH access
  • Login into your account and create virtualenv for your django project
    $ cd ~
    $ wget https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.1.tar.gz
    $ tar -zxvf virtualenv-1.9.1.tar.gz
    $ python virtualenv-1.9.1/virtualenv.py djangovevn
  • Add virtualenv bin folder to the path (inside .bash_profile)
    export PATH="/home/<username>/djangovenv/bin:$PATH" # inside .bash_profile
    
    # activate .bash_profile
    $ source .bash_profile
  • Then pip install everything your project requires. Make sure to activate virtualenv first
    $ source ~/djangovevn/bin/activate
    $ pip install django
  • Configure Django as usual. Make sure DEBUG is False
  • Inside public_html create index.fcgi. Make sure to use the virtualenv Python path. Django docs about this here.
    !/home/<username>/djangovenv/bin/python
    import sys, os
    
    # add projects directory to the path so that
    # settings from the project can be imported
    sys.path.insert(0, "/home/<username>/path/to/project")
    
    # Switch to the directory of your project #
    os.chdir("/home/<username>/path/to/project")
    
    # Set the DJANGO_SETTINGS_MODULE environment variable #
    os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
    
    
    # Run the fastcgi instance #
    from django.core.servers.fastcgi import runfastcgi
    runfastcgi(method="threaded", daemonize="false")
  • Configure the index.fcgi in public_html/.htaccess
    AddHandler fcgid-script .fcgi
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]
  • Activate the fastcgi
    $ cd ~/public_html
    $ touch index.fcgi
    $ chmod 0755 .htaccess
    $ chmod 0755 index.fcgi
  • All Done!

However this method will drive you crazy very fast. Apache is not meant for this and this method is not popular for a reason. This should be good enough in the beginning however as you will get more advanced deploying Django app, you should consider using some other hosting provider which allows for more flexibility such as WebFaction or heroku.

Reviewer Answered on October 12, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.