How to get apache2 to use a conda python install?

I need to serve a django app via apache2/wsgi. The django app is running in a conda venv. From what I understand, apache2 wsgi needs to be built against the same python libs as the ones used by the django app.

Is there a sanctioned way to do this? I find a frightening amount of google hits from 2012-2014 and not much since on how to do this, and none of them actually came up with a solution. How is everyone doing this?

I think I have a solution:

wsgi needs to be compiled with the same version as the conda framework’s python.

make sure you run the following from within the conda environment you’re deploying from (note my anaconda3 root directory is in ~/anaconda3, and my conda venv is named ‘django’)

sudo apt install apache2-dev
python -m pip install mod_wsgi

Be warned though, using pip inside a conda environment is not a good plan, but I couldn’t find a suitable conda install candidate.

This builds a mod_wsgi binary into the conda venv in use, which then needs deployed to the local system:

sudo ~/anaconda3/envs/django/bin/mod_wsgi-express install-module

This install command will output the two directives needed to load the correct libraries and python venv, add them to the following files:

sudo vi /etc/apache2/mods-available/wsgi.load
LoadModule wsgi_module "/usr/lib/apache2/modules/mod_wsgi-py310.cpython-310-x86_64-linux-gnu.so"

and

sudo vi /etc/apache2/conf-available/wsgi.conf
WSGIPythonHome "/home/ubuntu/anaconda3/envs/django"

lastly,

sudo a2enconf wsgi
sudo service apache2 restart

and you’re done.