Mod-wsgi one daemon for multiple VirtualHosts with different ServerNames

apache-2.4mod-wsgi

I have an apache2 / mod-wsgi deploy serving multiple domains with what is essentially one single application.

All domains are served both on port 80 and 443.

So, my configuration would look something like:

<VirtualHost *:80>
    #ServerName www.example.com

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    WSGIDaemonProcess example processes=2 threads=12 python-path=/app_path
    WSGIProcessGroup example

    WSGIScriptAlias / /scripthome
    ......
</VirtualHost>
<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</virtualHost>
<VirtualHost _default_:443>
    ServerName www.example.com

    ErrorLog ${APACHE_LOG_DIR}/error_easyset_ssl.log
    CustomLog ${APACHE_LOG_DIR}/access_easyset_ssl.log combined

    WSGIDaemonProcess example_ssl processes=2 threads=12 python-path=/app_path
    WSGIProcessGroup example_ssl

    WSGIScriptAlias / /scripthome

    SSLEngine on
    ........
</VirtualHost>

Copy-paste this for all other domains and we end up with a server running many daemon processes for what is really just the one app. Please note that domain-specific logic is handled within the app, so all other domain configuration files are the same, except for paths to SSL certificate files. Before SSL was needed, there was just one single VirtualHost for the entire thing. This is visible from the above configuration excerpts as I'm still using one single VirtualHost for all port 80 requests to all domains (ServerName commented out).

Now, that's wasting server resources, so I started looking into documentation on how to remedy this. Here's the relevant section (near the end of WSGIDaemonProcess page):

If the WSGIDaemonProcess directive is specified outside of all virtual host containers, any WSGI application can be delegated to be run within that daemon process group. If the WSGIDaemonProcess directive is specified within a virtual host container, only WSGI applications associated with virtual hosts with the same server name as that virtual host can be delegated to that set of daemon processes.

Now, SSL obviously prevents me from using the second bolded part, but it would allow me to use the first one.

My problem is that I don't know how to use the first one. How does one specify mod-wsgi configuration outside of VirtualHost block?

Best Answer

You can configure the WSGIDaemonProcess directive as well as others in the wsgi module's wsgi.conf file located in the mods-enabled subdirectory of your apache config.

So it should be something like /etc/apache2/mods-enabled/wsgi.conf