Django on Bluehost

Dec 28, 2011·
Julio Batista Silva
Julio Batista Silva
· 7 min read
blog Linux

This year I bought the domain juliobs.com and hosting at bluehost.com. So far it only holds a copy of my old site, built from scratch in PHP, and this blog.
But my vacation has finally arrived and I will have some time to redo everything.

Now I am thinking of building a more professional and easier-to-maintain website — good excuses to use a CMS or a framework.
I have built sites with Joomla and even liked it, but I want something different, something more pythonic…

Exactly: I will use Django!
I have wanted to play with this framework for a while and, since I am studying a lot of Python, I will combine business with pleasure.

The only problem is that Bluehost does not officially support Django: it is not in simplescripts, and the version of Python available on the server is 2.4, when the minimum recommended is 2.5.

In this post I will show a step-by-step guide for getting around that problem and installing Django on Bluehost.

Installing Python 2.7

  1. Get in touch with technical support and ask for SSH access. Their support is very good and the login should be enabled the same day.

  2. Connect to your box over SSH and download the most recent Python2 (2.7.3):

    julio@julio-acer ~> ssh juliobs.com
    Enter passphrase for key '/home/julio/.ssh/juliobs':
    
    juliobor@box780 ~  $  wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tgz
    juliobor@box780 ~  $  tar xvfz Python-2.7.3.tgz
    
  3. Go into the extracted folder, configure and install Python with wide Unicode support:

    juliobor@box780 ~  $  cd Python-2.7.3
    juliobor@box780 ~/Python-2.7.3  $  ./configure --prefix="$HOME" --enable-unicode=ucs4
    juliobor@box780 ~/Python-2.7.3  $  make
    juliobor@box780 ~/Python-2.7.3  $  make install
    

    If everything went well, you can already run the new Python like this:

    juliobor@box780 ~  $  ~/bin/python
    Python 2.7.3 (default, May 23 2012, 23:29:34)
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
  4. Edit your ~/.bashrc to export the PATH variable every time Bash starts (order matters):

    export PATH=$HOME/bin:$PATH
    
    juliobor@box780 ~  $  echo $PATH
    /home7/juliobor/bin:/usr/lib64/qt-3.3/bin:/home7/juliobor/perl5/bin:/usr/kerberos/bin:/ramdisk/bin:/usr/bin:/bin
    
  5. Source .bashrc and check that the Python called with the python command is the version you have just installed:

    juliobor@box780 ~  $  source .bashrc
    juliobor@box780 ~  $  python
    Python 2.7.3 (default, May 23 2012, 23:29:34)
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
  6. You can delete the files used for the installation.

    juliobor@box780 ~  $  rm -r  Python-2.7.3.tgz Python-2.7.3/
    

Done! Python is up to date and we can start installing Django.

Installing Django

  1. There are several ways to get Django. For me, the simplest is through a version control system, since it makes updating easier later and lets you develop with the latest version available.

    But note that using trunk/master will install the latest version, which is still in testing and therefore not recommended for sites that need stability. That is not my case.

    I recommend using git, since Django’s development moved to GitHub, but there are also mirrors on svn and mercurial (bluehost does not have mercurial installed).

    To update later, just run git pull if you are using git, or svn update if you are using SVN.

  2. Update setuptools and install pip:

    juliobor@box780 ~  $  wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
    juliobor@box780 ~  $  sh setuptools-0.6c11-py2.7.egg
    juliobor@box780 ~  $  rm setuptools-0.6c11-py2.7.egg
    
    juliobor@box780 ~  $  easy_install pip
    

    Or run the script https://bootstrap.pypa.io/get-pip.py

  3. Install the Django module.

    The easiest way to install Django is with pip. The following command downloads and installs it straight from GitHub. It also saves you from fiddling much with $PATH.

    juliobor@box780 ~  $  pip install git+https://github.com/django/django.git
    

    Alternatives:

    • Use pip but keep the version control directory: sudo pip install -e ~/django/ (easier to keep up to date with git pull)
    • Create a .pth file in the site-packages folder pointing to the directory downloaded by git/svn
    • Install using setup.py install --user
  4. Edit “~/.bashrc” again to:

    export PATH=$HOME/bin:$HOME/.local/bin:$PATH
    
    juliobor@box780 ~  $  echo $PATH
    /home7/juliobor/bin:/home7/juliobor/.local/bin:/usr/lib64/qt-3.3/bin:/home7/juliobor/perl5/bin:/usr/kerberos/bin:/ramdisk/bin:/usr/bin:/bin
    
  5. Check that you can import Django:

    juliobor@box780 ~  $  python
    Python 2.7.3 (default, May 23 2012, 23:29:34)
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import django
    >>> django.VERSION
    (1, 5, 0, 'alpha', 0)
    >>> print django.get_version()
    1.5
    
  6. To use the FastCGI protocol, we have to install Flup:

    juliobor@box780 ~  $  pip install flup
    

    Alternative:

    juliobor@box780 ~  $  wget http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz
    juliobor@box780 ~  $  tar xvfz flup-1.0.2.tar.gz
    juliobor@box780 ~  $  cd flup-1.0.2
    juliobor@box780 ~/flup-1.0.2  $  python setup.py build
    juliobor@box780 ~/flup-1.0.2  $  python setup.py install
    

Django is now installed. We can create a test project to learn the basic operations and see whether everything is working properly.

Creating a project

  • Create a projects directory somewhere that is not reachable from the Web (i.e., outside ~/public_html). E.g.:

    juliobor@box780 ~  $  mkdir django_projects; cd django_projects
    
  • Create a project inside your projects folder

    juliobor@box780 ~/django_projects  $  django-admin.py startproject juliobs
    

    If you did not use pip, you will probably need to edit $PATH to use django-admin.py as in the command above:

    juliobor@box780 ~  $  export PATH=/home7/juliobor/django/django/bin:$PATH
    
  • Edit the settings.py file with your database settings.

    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': '/home7/juliobor/django_projects/mysite/testDB',
    

    sqlite3 is fine for testing, but I recommend using PostgreSQL. You will need to install psycopg2:

    juliobor@box780 ~  $  pip install psycopg2
    

    Alternative:

    juliobor@box780 ~  $  easy_install psycopg2
    
  • Run the following command to create the database tables. Answer no if it asks to create a user:

    juliobor@box780 ~/django_projects/juliobs $ python manage.py syncdb
    
  • Create a .fcgi file in ~/public_html (or in a subdirectory if you prefer)

    Edit the django.fcgi file and make it executable:

    #!/home7/juliobor/bin/python
    import sys
    import os
    
    # Add a custom Python path.
    sys.path.insert(0,"/home7/juliobor/django_projects/juliobs")
    sys.path.insert(0,"/home7/juliobor/bin")
    
    os.chdir("/home7/juliobor/django_projects/juliobs")
    
    # Set the DJANGO_SETTINGS_MODULE environment variable.
    os.environ['DJANGO_SETTINGS_MODULE'] = "juliobs.settings"
    
    from django.core.servers.fastcgi import runfastcgi
    runfastcgi(method="threaded", daemonize="false")
    
    juliobor@box780 ~/public_html  $  chmod a+x django.fcgi
    
  • Add a .htaccess

  • If Django is at the root of the site:

    AddHandler fcgid-script .fcgi
    Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(media/.*)$ - [L]
    RewriteCond %{REQUEST_URI} !(django.fcgi)
    RewriteRule ^(.*)$ django.fcgi/$1 [L]
    
  • If the site is at /django:

    ~/public_html/.htaccess

    AddHandler fcgid-script .fcgi
    Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(django\.fcgi/.*)$ - [L]
    RewriteRule ^(.*)$ /django/django.fcgi/$1 [L]
    

Now you can follow any Django tutorial. I am learning most things by reading The Django Book (The Definitive Guide to Django), which has a Web preview, is quite up to date and explains the concepts well.

Django + Apache + mod_wsgi

For obvious reasons I am not going to develop directly on the server hosting my site, but on my own laptop. Django has a nice way of testing the site without having to install a server, which is to run the command python2 manage.py runserver

The site should show up at http://localhost:8000

But some things get harder to test that way and, since I already have Apache installed here, I find it far more practical to develop and test on my Apache.

  1. Install Django from git with pip:

    julio@julio-acer ~> sudo pip2 install git+https://github.com/django/django.git
    
  2. Install mod_wsgi:

    julio@julio-acer ~> sudo pacman -S mod_wsgi
    
  3. Edit wsgi.py:

    import os
    import sys
    
    # Adds the project path and its parent directory to sys.path (needed for
    # import juliobs.settings). Can also be done in httpd.conf with PythonPath
    sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
    sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "juliobs.settings")
    
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    
  4. Edit http.conf

    LoadModule wsgi_module modules/mod_wsgi.so
    
    #WSGI - (for Django)
    WSGIScriptAlias /django /media/externo/Documentos/Programacao/Web/django_projects/juliobs/wsgi.py
    WSGIPythonPath /media/externo/Documentos/Programacao/Web/django_projects/juliobs/
    
    <Directory /media/externo/Documentos/Programacao/Web/django_projects/juliobs>
        <Files wsgi.py>
        Order deny,allow
        Allow from all
        </Files>
    </Directory>
    
    Alias /media/ /media/externo/Documentos/Programacao/Web/django_projects/juliobs/juliobs/media/
    <Directory /media/externo/Documentos/Programacao/Web/django_projects/juliobs/juliobs/media>
    SetHandler None
    Order allow,deny
    Allow from all
    </Directory>
    
  5. You should be able to reach the site at http://localhost/django

Put your site under version control

Now that we have everything working, we can start developing the site. It is worth remembering that the code of the site belongs under version control, for example in a Git repository.

If you would rather not keep your site’s code in a repository such as GitHub, a good alternative is rsync:

julio@julio-acer Web/django/django_projects> rsync -avze --delete ssh juliobs juliobor@juliobs.com:~/django_projects

Notes:

  • Error while creating the super user:

    python manage.py createsuperuser
        return getpass.getuser().decode(locale.getdefaultlocale()[1])
    TypeError: decode() argument 1 must be string, not None
    

    I solved it by setting the locale:

    juliobor@box780 ~  $  export LANG=en_US.UTF-8
    
  • On Arch Linux mod_wsgi is for Python3; you need to install mod_wsgi2.


Julio Batista Silva
Authors
Senior Cloud Developer

I’m a Brazilian-German computer engineer based in Germany, passionate about tech, science, photography, and languages.

I’ve been programming for about two decades already, exploring everything from mobile apps and web development to machine learning. These days I focus on cloud SRE and data engineering.

I volunteer in the open source and Python communities, helping organize PyCon DE and PyData Berlin, serving on the PSF D&I Work Group, mentoring, and contributing code and translations.

On my blog, I share Linux tips, setup guides, and personal notes I’ve written for future reference. I hope others find them helpful as well. The content is available in multiple languages.

Browse my gallery for some of my photography.

Away from the keyboard, you’ll find me at concerts, playing clarinet, cycling, scuba diving, or exploring new places, cultures, and cuisines.

Always happy to connect! 🙂

comments powered by Disqus