Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use multiple mod_wsgi installed with pip install in different virtual environments #882

Closed
eduardo-boutiquenumerique opened this issue Apr 4, 2024 · 2 comments

Comments

@eduardo-boutiquenumerique

Hi,

I have multiple projects using django with different virtual environnements (one for each project) and am trying to figure out how to use multiple mod_wsgi installed with pip install (one for each project / venv).

I'm reading the docs from pypi and in this other issue where it's said to "Copy that into the system Apache to load mod_wsgi. Then configure Apache to load your WSGI application."

I'm using ubuntu and usually to activate a module there is a file (/etc/apache2/mods-available/wsgi.load) to load the mod_wsgi.so file, but if I have different venvs, I'll have different mod_wsgi.so files to load (one for each venv). Should I put multiple lines (one for each) in the file ?

I can't find this case in the docs.

Thanks in advance

@GrahamDumpleton
Copy link
Owner

The only thing that would usually be in the wsgi.load file is the loading of the mod_wsgi.so file. There can only be one mod_wsgi.so file loaded for the whole of Apache. You cannot load it multiples times and if you do only the first is used.

Also be aware that one Apache instance can only be used with one Python X.Y.Z version at a time. You cannot have virtual environments using different Python versions.

So the wsgi.load file should load a mod_wsgi.so file compiled against the specific X.Y.Z Python version. All that matters at this point is that it is the same X.Y.Z version that all virtual environments should use.

Next is that configurations for different Python WSGI applications would be in their respective VirtualHost definitions (not the wsgi.load file).

Because you are having multiple Python WSGI applications, you should use mod_wsgi daemon mode and each VirtualHost or WSGI application should be delegated to use their own mod_wsgi daemon process group. It is at the same time strongly recommended to disable mod_wsgi embedded mode, which is done by adding:

WSGIRestrictEmbedded On

to the end of the wsgi.load file.

By disabling embedded mode, it will highlight by way of errors when you screw up the configuration and don't properly delegate WSGI applications to different mod_wsgi daemon process groups.

Now when defining each mod_wsgi daemon process group, specify python-home to indicate where the virtual environment is that daemon process group should use, and thus under what virtual environment the WSGI application delegated to that mod_wsgi daemon process group uses.

Ensure you use WSGIProcessGroup or the process-group option of WSGIScriptAlias directive to delegate the WSGI application to its intended daemon process group.

Since you would only have one WSGI application per daemon process group, ensure you force the use of the main Python interpreter context by using WSGIApplicationGroup %{GLOBAL} directive or the application-group=%{GLOBAL} option to WSGIScriptAlias.

So in summary:

  • Load only one mod_wsgi.so module.
  • This must be the X.Y.Z version of Python you intend using for all WSGI applications and virtual environments.
  • Compile mod_wsgi.so against system Python, or create a special virtual environment just for building it.
  • Disable embedded mode.
  • Use a separate daemon process group for each WSGI application.
  • Use the main Python interpreter context in the respective daemon process groups.

@eduardo-boutiquenumerique
Copy link
Author

Thanks a lot for your detailed response !

It's very clear.

Adding "WSGIRestrictEmbedded On" was a game changer. it was really hiding issues.

I had gotten everything else correct and my issue was that the home directory needed to have +x permission (which is by default on ubuntu 20.04 but not 22.04.

I run the daemon with user "ubuntu" but apparently apache needs some other permission for his own user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants