How to install Python3.9 with Apache on WSGI on Ubuntu 18.04

taiphuong
2 min readMar 12, 2023

1. Install python3.9

# Add repository
root@vps:~ sudo apt install software-properties-common
root@vps:~ sudo add-apt-repository ppa:deadsnakes/ppa
root@vps:~ sudo apt update
---
# Install python3.9
root@vps:~ sudo apt install python3.9 python3.9-dev -y
---
# Create symbolink for new python
root@vps:~ sudo update-alternatives --install /usr/bin/python python3 /usr/bin/python3.9 1
root@vps:~ sudo update-alternatives --config python3
---

2/ Install Apache

sudo apt install apache2
sudo apt install apache2-dev
# Open firewall for Apache2
sudo ufw app list
sudo ufw allow 'Apache'
sudo ufw status
----
Output
Status: active

To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
----
# Check status Apache2
sudo systemctl status apache2

3. Install mod-wsgi=4.9.4

# Download from source
root@vps:~ wget https://github.com/GrahamDumpleton/mod_wsgi/archive/refs/tags/4.9.4.tar.gz
root@vps:~ tar xvfz 4.9.4.tar.gz
root@vps:~ cd mod_wsgi-4.9.4
# Configuration with current python.
root@vps:~ configure --with-python=/usr/local/bin/python3
root@vps:~ make
root@vps:~ make install
# when it done
root@vps:~ sudo chmod 644 /usr/lib/apache2/modules/mod_wsgi.so

4. Test mod-wsgi on Apache2

# Try to add new file for testing WSGI

root@vps:~ vi /etc/apache2/sites-enabled/000-default.conf

# copy the content
<VirtualHost *:443>
ServerName $DOMAIN
DocumentRoot /var/$DOMAIN

WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess $BRANCH python-path=/var/$DOMAIN python-home=/var/$DOMAIN/venv
WSGIProcessGroup $BRANCH
WSGIScriptAlias / /var/$DOMAIN/sn/wsgi.py process-group=$BRANCH
WSGIPassAuthorization On

Alias /static/ /var/$DOMAIN/static/
Alias /media/ /var/$DOMAIN/media/
Alias /favicon.ico /var/$DOMAIN/static/favicon.ico

<Directory /var/$DOMAIN/static>
Require all granted
</Directory>

<Directory /var/$DOMAIN/media>
Require all granted
</Directory>

<Directory /var/$DOMAIN/sn>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>
</VirtualHost>
#########
root@vps: service restart apache2
#### you will recieve the error
# Job for apache2.service failed because the control process exited with error code.
# See "systemctl status apache2.service" and "journalctl -xe" for details.
# Check the error

root@vps: journalctl -xe

### Error here
apachectl[19449]: AH00112: Warning: DocumentRoot [/var/$DOMAIN] does not e
Mar 12 04:22:51 ip-172-31-0-65 apachectl[19449]: AH00526: Syntax error on line 6 of /etc/apache2/sites-en
Mar 12 04:22:51 ip-172-31-0-65 apachectl[19449]: Invalid command 'WSGIApplicationGroup', perhaps misspell
Mar 12 04:22:51 ip-172-31-0-65 apachectl[19449]: Action 'start' failed.
###

# Adding Loadmodule
root@vps:~ vi /etc/apache2/apache2.conf

### add this line to bottom the file

LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
###

root@vps: service apache2 restart
root@vps: service apache2 status

### Done when you got
apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Sun 2023-03-12 04:37:10 UTC; 1min 13s ago
Process: 19573 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 19595 (apache2)
Tasks: 56 (limit: 1134)
CGroup: /system.slice/apache2.service
├─19595 /usr/sbin/apache2 -k start
├─19598 /usr/sbin/apache2 -k start
└─19599 /usr/sbin/apache2 -k start

Mar 12 04:37:10 ip-172-31-0-65 systemd[1]: Starting The Apache HTTP Server...
Mar 12 04:37:10 ip-172-31-0-65 apachectl[19573]: AH00112: Warning: DocumentRoot [/var/$DOMAIN] does not exist
Mar 12 04:37:10 ip-172-31-0-65 systemd[1]: Started The Apache HTTP Server.
####

Good luck!

Reference: https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html

--

--