How to run django with apache webserver

This post is not a tutorial. It’s just my record of the steps I did to serve django with apache webserver on my installation of Linux:

Prerequisite: virtualenv was installed and activated into venv.
The command was:

virtualenv -p python3 venv

You also have to install any requirements needed.

First install apache dev package:

sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3 apache2-dev

Now for my project myapplication, I edited myapplication/myappointments/wsgi.py

"""
WSGI config for myappointments project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""
import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('/home/joel/myappointments')
sys.path.append('/home/joel/myappointments/venv/lib/python3.5/site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myappointments.settings")
try:
    application = get_wsgi_application()
except Exception:
        # Error loading applications
        if 'mod_wsgi' in sys.modules:
           traceback.print_exc()
           os.kill(os.getpid(), signal.SIGINT)
           time.sleep(2.5)

Useful pages:
pypi page

Install mod_wsgi:
pip install mod_wsgi

Configure settings.py and allow the url.

Now we can use the following in the directory containing wsgi.py:

mod_wsgi-express start-server wsgi.py --port 8080

If successful, you’ll get something like:

Server URL         : http://localhost:8080/
Server Root        : /tmp/mod_wsgi-localhost:8080:1000
Server Conf        : /tmp/mod_wsgi-localhost:8080:1000/httpd.conf
Error Log File     : /tmp/mod_wsgi-localhost:8080:1000/error_log (warn)
Request Capacity   : 5 (1 process * 5 threads)
Request Timeout    : 60 (seconds)
Startup Timeout    : 15 (seconds)
Queue Backlog      : 100 (connections)
Queue Timeout      : 45 (seconds)
Server Capacity    : 20 (event/worker), 20 (prefork)
Server Backlog     : 500 (connections)
Locale Setting     : en_US.UTF-8

In my case, I started it from /home/joel/myappointments/myappointments:

cd /home/joel/myappointments/myappointments
mod_wsgi-express start-server wsgi.py --port 8080

Now, you can reach the site at http://yourdomain.com:8080/

After testing this,

mod_wsgi-express setup-server wsgi.py --port=80 \
    --user www-data --group www-data \
    --server-root=/etc/mod_wsgi-express-80

this will install the scripts.

Now we can start the server with:

/etc/mod_wsgi-express-80/apachectl start

Problems with static files
As advised in the django tutorials, I’d stored my static files for the app appointments of project myappointments at the location myappointments/appointments/static/appointments

Tree:

Solution:
Copy files:
/home/joel/myappointments# cp -a appointments/static/appointments ./htdocs

Now start server with:
/home/joel/myappointments# mod_wsgi-express start-server –document-root ./htdocs wsgi.py –user www-data –group www-data –port 80 –host myopip.com

wsgi.py contains:
# cat wsgi.py

"""
WSGI config for myappointments project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""
import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('/home/joel/myappointments')
sys.path.append('/home/joel/myappointments/venv/lib/python3.6/site-packages')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myappointments.settings")
try:
    application = get_wsgi_application()
except Exception:
        # Error loading applications
        if 'mod_wsgi' in sys.modules:
           traceback.print_exc()
           os.kill(os.getpid(), signal.SIGINT)
           time.sleep(2.5)

mod_wsgi-express is started with:

mod_wsgi-express start-server --document-root ./htdocs wsgi.py --user www-data --group www-data --port 80 --host myopip.com

Or:
In (venv) [email protected]:/home/joel/myappointments#

mod_wsgi-express start-server wsgi.py --user www-data --group www-data --port 80 --host myopip.com