Configure EasyApache-Experimental for Python with mod_wsgi

CanadaGuy

Active Member
Sep 24, 2018
39
8
8
Ottawa
cPanel Access Level
Root Administrator
I would like to run Django (python) on my cPanel VPS. My current web host runs on CloudLinux, and as I understand it, that is a requirement for the Setup Python App. I also understand there is an aspect of this tool called Passenger, and that I'm entirely unfamiliar with. I'm looking for some more flexibility on software versions, hence why I'm looking into a VPS. I have come across the following link:

Install mod_wsgi?

I understand I can go through the EasyApache4 Experimental Repository to install mod_wsgi. Can anyone provide come guidance on how I would continue configuring cPanel, and most likely Apache, to serve a Django site? The Setup Python App places the following .htaccess in the web root:

Code:
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/user/path-to-application-directory"
PassengerBaseURI "/"
PassengerPython "/home/user/virtualenv/name-of-application-directory/3.7/bin/python3.7"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
The Quick Configuration Guide here:

Quick Configuration Guide — mod_wsgi 4.6.5 documentation

Is mildly similar, but doesn't reference the virtualenv that is to be used by Django:

Code:
WSGIScriptAlias / /usr/local/www/wsgi-scripts/myapp.wsgi
but it also indicates "any static files contained in the DocumentRoot will be hidden and requests against URLs pertaining to the static files will instead be processed by the WSGI application." This seems somewhat different from the CloudLinux way which allows Apache to still serve any content in the document root.

I know this is all at my own risk, but I don't mind learning something, and will just redo the VPS if the cPanel installation is fubar'd. Anyone have some guidance?
 

CanadaGuy

Active Member
Sep 24, 2018
39
8
8
Ottawa
cPanel Access Level
Root Administrator
I managed to get things going and thought I would document it here. I installed these packages:

Code:
yum install ea4-experimental
yum install ea-apache24-mod_wsgi
then followed this, created the include file as directed:

Modify Apache Virtual Hosts with Include Files - EasyApache 4 - cPanel Documentation

with this inside it:

Code:
WSGIScriptAlias / /home/accountname/app-directory/wsgi.py
Also per the above link, I regenerated the httpd.conf and restarted the Apache server.

I have not yet figured out how to install and point to Python 3.7 however...
 

CanadaGuy

Active Member
Sep 24, 2018
39
8
8
Ottawa
cPanel Access Level
Root Administrator
I have not yet figured out how to install and point to Python 3.7 however...
Does a CentOS cPanel install make use of Python in any way? I used a symbolic link to point to python36, and it seems to work after reloading Apache. I was thinking this could work until I find a better solution at the server configuration level and to work with virtualenv.
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,296
1,266
313
Houston
HI @CanadaGuy

We do use python 2.7.5:

Code:
# python --version
Python 2.7.5
As far as installing a newer version of Python, we don't provide support for that but I did find a few tutorials online that suggested ways this can be done:

Install Python 3.x in WHM-cPanel Server - Sysally
Building Python2.7 and Python3 on CentOS and cPanel - The Wonderful World Of Linux

I can't speak for the validity of these but I did look them over, there are a number of other tutorials out there as well.


Thanks!
 

CanadaGuy

Active Member
Sep 24, 2018
39
8
8
Ottawa
cPanel Access Level
Root Administrator
HI @CanadaGuy

We do use python 2.7.5:
Is there a list of scripts that use the default Python install? I'd be interested in verifying they could run under Python 3, unless you already know it doesn't.

**edit** I guess this is pretty irrelevant. Installing python3 in parallel isn't the trouble, it is the server configuration I'm having difficulty with.
 
Last edited:

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,296
1,266
313
Houston
Hi @CanadaGuy


I do not have a list, though amongst other items I can tell you mailman and SpamAssassin are both python based. The links I shared should have provided some information on how to run Python3 on the server though I'm sure there are other tutorials out there.
 

mostaquim

Registered
Feb 19, 2019
1
1
3
Dhaka, Bangladesh
cPanel Access Level
Root Administrator
Hi @CanadaGuy

I am not sure if you have solved this or not.

This is the steps I took to serve Django App, I am sure someone might find it useful.

1. Install Python3.6
2. Install mod-alt-passenger package which comes with CloudLinux
3. Uploaded the django app on a directory ( let's say it is /home/user/approot/)
4. Install virtual environment, I installed it in the app root ( /home/user/approot/env)
5. Activate the virtual environment and install required packages.
6. Run manage.py runserver to check if the server runs without any error. ( I did it as a final check if I missed anything)
7. Build the passenger_wsgi.py, this is my very simple passenger_wsgi.py, advanced instructions here

Code:
import sys, os
#INTERP is present twice so that the new python interpreter
#knows the actual virtual environment path
INTERP = "/home/user/approot/env/bin/python3"

if sys.executable != INTERP:os.execl(INTERP, INTERP, *sys.argv)

cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/appname')

sys.path.insert(0,cwd+'/env/bin')
sys.path.insert(0,cwd+'/env/lib/python3.6/site-packages')

os.environ['DJANGO_SETTINGS_MODULE'] = "appname.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
7. Now to overwrite Apache settings. Create a include.conf file and follow the instructions here.

Code:
# include.conf
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
UseCanonicalName Off

DocumentRoot /home/user/approot/appname
PassengerAppRoot /home/user/approot

# Tell Passenger that your app is a Python app
PassengerAppType wsgi
PassengerStartupFile passenger_wsgi.py
PassengerHighPerformance on

# Relax Apache security settings
<Directory /home/user/approot/appname>
 Allow from all
 Options -MultiViews
 # Uncomment this if you're on Apache >= 2.4:
 #Require all granted
</Directory>
8. Now rebuild httpconfig
Code:
/usr/local/cpanel/scripts/rebuildhttpdconf
9. Now restart apache server
Code:
/usr/local/cpanel/scripts/restartsrv_httpd
10. The site should be live by now.

Some debugging tips:
# enable virtualenv with ssh and run manage.py runserver to see if anything is missing
# you need to restart apache everytime you apply an update, I am not sure if there are other way
 
Last edited:
  • Like
Reactions: cPanelLauren