Serve Python 3.7 with `mod_wsgi` on Ubuntu 16

Gareth Johnson
3 min readFeb 5, 2019

--

I was trying to deploy a Django website written in Python 3.7 last night, and it was not easy, so I thought that I would write up how I did it to help anyone else trying to do something similar (and myself in the future).

For a site with Python 3.5, I could easily run apt install libapache2-mod-wsgi-py3. However, there is currently no such easy package for 3.7, so we will actually make custom builds of Python and mod_wsgi ourselves.

(By the way, this was all tested on a Digital Ocean server with Ubuntu 16.04. I am a Mac user, so I am by no means an Ubuntu expert. Please let me know if you had to add anything to get this to work on your own machine.)

1. Install prerequisites

Note: I ran all of these commands as the root Ubuntu user, so you might have to add sudo in front if you are not using the root user.

Anyway, as with any Ubuntu installation process, you should probably start by running something like this:

$ apt update

Next, as I found on a couple of other sites, we need to install these:

$ apt install build-essential checkinstall$ apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

What those sites did not tell me, however, is that we also need to install libffi-devf for our purposes:

$ apt install libffi-dev

2. Build Python

Now, we have to make a custom build of Python 3.7 to get it to work with mod_wsgi. Start from a directory to put the build files in, such as /etc/src, and download and unzip Python:

$ sudo wget https://www.python.org/ftp/python/3.7.10/Python-3.7.10.tgz$ sudo tar xzf Python-3.7.10.tgz$ cd Python-3.7.10

Below are the build commands that worked for me. The first time I ran make, it took a really long time, so be patient.

$ ./configure --enable-shared$ make$ make install

After that, you need to do something that, frankly, I don’t understand. But it seems to work.

Open up the file /etc/ld.so.conf with your favourite editor for the job, and add this line to the bottom:

/usr/local/lib/python3.7

Save the file, and run this:

$ /sbin/ldconfig -v

What does this do? Your guess is at least as good as mine. Did it work? Try for yourself:

$ python3.7

If you see a Python 3.7 command prompt, then you can call this section a success, exit Python, and move on to the next section.

3. Build `mod_wsgi`

If you have not done so already, install Apache and its dev package:

$ apt install apache2 apache2-dev

Next, let us follow some steps that I found in the mod_wsgi documentation.

Download and unzip it into some directory (using a better tar.gz file from here if you want to ensure you get the latest version):

$ wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.7.1.tar.gz$ tar xvfz 4.7.1.tar.gz$ cd mod_wsgi-4.7.1

Now, we need to build it in a somewhat similar way to Python above. The main difference is that we need to specify the newly built version of Python.

$ ./configure --with-python=/usr/local/bin/python3.7$ make$ make install

And that’s it! You’re done! You might also want to check out how to load mod_wsgi into Apache and clean up your build files, though.

Anyway, I hope this article helped you. Let me know if you get stuck, and I will try to help.

--

--