Skip to main content

Django on WHM

I spent pretty much all day today trying to peice together how to get Django running on a WHM server. Although I've done it before, I no longer have the configs form the old server and it was running EasyApache 3 which is no loner used. Since I've spent so much time trying to get to this point and wasn't able to find any clear documentation on how to set it up I figured I'd post it here.
First we need to install python 3 since that's what Django requires, however as it's not available in the default repositories for CentOS, you'll need to install the EPEL Release repository by running:

yum install epel-release

Once the epel-release repository is installed, it's time to install python3, it's virtualenv installer and pip as follows:

yum install python34 python34-pip python34-virtualenv

After you have python 3 installed we need to compile mod_wsgi which is documented here. However we'll be changing 2 things and we need WHM's apache24-build in order to get apxs(required for the build). So the steps will be as follows:
  1. Find the source code on github and download as follows:
    wget https://github.com/GrahamDumpleton/mod_wsgi/archive/X.Y.Z.tar.gz
  2. Extract the package and cd to the directory it created:
    tar xzf X.Y.Z.tar.gz
    cd mod_wsgi-X.Y.Z
            
  3. Install apache24-devel from WHM's EasyApache repo:
    yum install ea-apache24-devel
  4. Configure the build, ensuring that it's pointing to the correct version of Python. If this is the wrong version of Python, you'll get your apache error log filling up with "ImportError: No module named site" errors as soon as you point the python home to your virtual environment. The configure command should look like this:
    ./configure   --with-python=/usr/bin/python3.4
  5. Compile and install mod_wsgi:
    make&&make install
    You can make sure that it's installed as follows:
    apachectl -M|grep wsgi
We can now create the virtual environment to run django from. I created mine in the root of the cpanel user's home directory and named it for the version of python in case I decide to create an additional virtualenv for a new version of python in the future. This can be done as follows:
cd ~<cpaneluser>
virtualenv virtualenv34
. virtualenv/bin/activate
I'm going to assume that you have a django application started and ready to go online. If not you can refer to django's tutorials on how to to this. I'm creating mine in the djangosites directory, so to install the required packages and test I can simply run:
cd ~<cpaneluser>/djangosites/<djangoapp>
pip install -r pip install -r requirements.txt
python manage.py runserver
Once I see that it's able to run in the virtual environment I've set up, it's time to configure apache to run the app using wsgi. The first step is going to be to add a custom configuration file. By creating this file in a specific directory, WHM's rebuildhttpdconf will autodtect it and include it for the appropriate virtualhost:
mkdir -p /etc/apache2/conf.d/userdata/std/2_4/<cpaneluser>/mywebsite.com/
touch /etc/apache2/conf.d/userdata/std/2_4/<cpaneluser>/mywebsite.com/django_site.conf
/usr/local/cpanel/scripts/rebuildhttpdconf
/usr/local/cpanel/scripts/restartsrv_httpd
Once the config file is in place it's time to configure WSGI. The first part is to a .conf file in the "/etc/apache2/conf.d/" directory. I simply called mine "wsgi.conf". It should contain the following line:
WSGISocketPrefix /tmp/wsgi
Without the WSGISocketPrefix line the socket gets created in the /var/run/apache2 directory which the daemon wsgi process won't be able to access. Finally we need to configure the virtualhost to run a wsgi daemon and what paths to route to it. To do this we'll edit the file we created earlier "/etc/apache2/conf.d/userdata/std/2_4/<cpaneluser>/mywebsite.com/django_site.conf" and populate ir as follows:
WSGIDaemonProcess example.com python-home=/home/<cpaneluser>/virtualenv34 python-path=/home/<cpaneluser>/djangosites/<djangoapp> socket-user=<cpaneluser>

WSGIProcessGroup example.com
WSGIScriptAlias / /home/<cpaneluser>/djangosites/licd/<djangoapp>/wsgi.py process-group=example.com

<Directory /home/<cpaneluser>/djangosites/<djangoapp>>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

Alias /media/ /home/<cpaneluser>/djangosites/<djangoapp>/media/
Alias /static/ /home/<cpaneluser>/djangosites/<djangoapp>/static/

<Directory /home/<cpaneluser>/djangosites/<djangoapp>/media>
Require all granted
</Directory>

<Directory /home/<cpaneluser>/djangosites/<djangoapp>/static>
Require all granted
</Directory>
Once the virtualhost configuration is saved we can restart apache again and the django app should load.

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment