UPDATE: We now have an application installer which provides build scripts for apache, mod_Wsgi, and django on python2.7. It is named 'Django 1.8 LTS' in the new application type list, it coverts the process outlined here into an automated process.
Opalstack's built-in Django installer sets up Django running behind a uwsgi server.
If you prefer to use Apache and mod_wsgi instead of uwsgi, here's how:
First create a proxy port app, make a note of the app name and port, and attach the app to a site.
In the rest of this example, the app is myapp
and the port is 55555
.
Next, install Apache HTTPD and mod_wsgi:
export APP=myapp
cd ~/apps/$APP
mkdir -p src tmp
export TMPDIR=$PWD/tmp
cd src
wget http://archive.apache.org/dist/httpd/httpd-2.4.41.tar.gz
tar zxf httpd-2.4.41.tar.gz
cd httpd-2.4.41
./configure --prefix=$HOME/apps/$APP/apache2 --enable-mods-shared=all --enable-mpms-shared=all --with-mpm=prefork
make
make install
cd ..
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.7.0.tar.gz
tar zxf 4.7.0.tar.gz
cd mod_wsgi-4.7.0
./configure --with-python=/usr/bin/python3.6 --with-apxs=$HOME/apps/$APP/apache2/bin/apxs
make
make install
cd ../..
cp apache2/conf/httpd.conf apache2/conf/httpd.conf.original
Next, make a virtual environment for your project. In this example I'll install Django and make a new project:
cd $HOME/apps/$APP
python3.6 -m venv env
source env/bin/activate
pip install django
django-admin startproject myproject
sed -r -i "s/^ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = \['\*'\]/" myproject/myproject/settings.py
sed -r -i "/^DATABASES =/, /^}$/ s/^/#/" myproject/myproject/settings.py
Next, configure Apache to listen on your assigned port and serve your project. To do so, edit the app's apache2/conf/httpd.conf
to look like this, changing the first four variables to match your user and app:
# change the next four values for your shell user and app
Define OPAL_USER myuser
Define APP_NAME myapp
Define APP_PORT 55555
Define PROJ_NAME myproject
# you usually don't need to edit anything below this line.
Define APP_ROOT /home/${OPAL_USER}/apps/${APP_NAME}
Define VIRT_ENV ${APP_ROOT}/env
Define PROJ_ROOT ${APP_ROOT}/${PROJ_NAME}
Define LOG_ROOT /home/${OPAL_USER}/logs/apps/${APP_NAME}
ServerRoot ${APP_ROOT}/apache2
LoadModule mpm_worker_module modules/mod_mpm_worker.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module modules/mod_mime.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule wsgi_module modules/mod_wsgi.so
LoadModule unixd_module modules/mod_unixd.so
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog ${LOG_ROOT}/access_${APP_NAME}.log combined
ErrorLog ${LOG_ROOT}/error_${APP_NAME}.log
DirectoryIndex index.py
DocumentRoot ${APP_ROOT}/apache2/htdocs
Listen ${APP_PORT}
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5
# python-home = path to your virtualenv
# python-path = path to your project directory
# this is usually all of the python path config that you need.
WSGIDaemonProcess ${APP_NAME} processes=2 threads=12 python-home=${VIRT_ENV} python-path=${PROJ_ROOT}
WSGIProcessGroup ${APP_NAME}
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / ${PROJ_ROOT}/${PROJ_NAME}/wsgi.py
Finally, start the app:
~/apps/myapp/apache2/bin/apachectl start
And that's it - you now have Django up and running on Apache + mod_wsgi with Python 3.6!