Wordpress & Flask sitewide implimentation

54 views
Skip to first unread message

Gordon Charles

unread,
Sep 10, 2020, 7:12:03 PM9/10/20
to modwsgi
I've been trying to create a .conf file which would simultaneously support Wordpress and Flask (I have some flask apps which run as part of an embedded solution and would like for others to be able to drive the application in a simulation mode and like the idea of having a site wide implementation via flask for consistency.

I've been following the guidance found here:
Graham Dumpleton Post
and here:

I'm running on Ubuntu 20.04.1 LTS
Apache/2.4.41
Python 3.8.2

When I include the suggested configuration described in the Graham Dumpleton Post inside of the VirtualHost definition I get the following:
WSGIRestrictEmbedded cannot occur within <VirtualHost> section
Action 'restart' failed.

When I include the suggested configuration outside of the VirtualHost definition it breaks wordpress.

Any guidance here would be appriciated.


Graham Dumpleton

unread,
Sep 10, 2020, 7:12:58 PM9/10/20
to mod...@googlegroups.com
Only WSGIRestrictEmbedded needs to be outside of the VirtualHost. Leave everything else you may have inside of the VirtualHost.

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/modwsgi/ded196fd-0b21-44ba-b6d5-b91b194e196dn%40googlegroups.com.

Gordon Charles

unread,
Sep 10, 2020, 8:00:40 PM9/10/20
to modwsgi
Graham,

Thanks for the quick reply.  I am striving to work independently here.  I've made the modification, wordpress, is still not loading and have the following in the error.log file:

[Thu Sep 10 23:53:15.963956 2020] [mpm_prefork:notice] [pid 40224] AH00163: Apache/2.4.41 (Ubuntu) mod_wsgi/4.6.8 Python/3.8 configured -- resuming normal operations
[Thu Sep 10 23:53:15.964004 2020] [core:notice] [pid 40224] AH00094: Command line: '/usr/sbin/apache2'
[Thu Sep 10 23:53:27.097927 2020] [rewrite:error] [pid 43290] [client 99.46.143.132:55785] AH00670: Options FollowSymLinks and SymLinksIfOwnerMatch are both off, so the RewriteRule directive is also forbidden due to its similar ability to circumvent directory restrictions : /var/www/html/wp-admin/, referer: http://thegacway.com/wp-login.php

My assumption is that the configuration you have outlined should work for wordpress in absence of any wsgi files and am trying to setup the .conf file so that it first works with wordpress and then add the python and get that working.

Here's the configuration file:

<Directory /var/www/html>
        Require all granted
</Directory>
WSGIRestrictEmbedded On
<VirtualHost *:80>
        ServerName thegacway.com
        ServerAlias www.thegacway.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Define a mod_wsgi daemon process group.
        WSGIDaemonProcess www.thegacway.com display-name=%{GROUP}
        # Force the Python web application to run in the mod_wsgi daemon process group.
        WSGIProcessGroup www.thegacway.com
        WSGIApplicationGroup %{GLOBAL}
        # Disable embedded mode of mod_wsgi.
        # Set document root and rules for access.
        #DocumentRoot /var/www/html
        <Directory /var/www/html>
            Options ExecCGI
            DirectoryIndex index.php
            AddHandler application/x-httpd-php .php
            AddHandler wsgi-script .py
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ /main.py/$1 [QSA,PT,L]
        </Directory>

        #LogLevel info ssl:warn

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

</VirtualHost>

Graham Dumpleton

unread,
Sep 10, 2020, 8:29:45 PM9/10/20
to mod...@googlegroups.com
What is the ServerName/ServerAlias for the wordpress VirtualHost? It is different right?

Gordon Charles

unread,
Sep 10, 2020, 9:17:55 PM9/10/20
to modwsgi
Graham,

No it is not different.  My understanding having read your post Graham Dumpleton Post, was that when configured Apache would dynamically route requests to either the .php or through wsgi based upon the file extensions of the files located in the particular directory.  My assumption is the ServerName/ServerAlias would be the same for both and Apache is "routing" the requests responses.  Based upon your response, I'm missing something; and as such, If it is not too much trouble I would appreciate any clarification you provide on the mechanics of how this would work when implemented correctly.

Graham Dumpleton

unread,
Sep 10, 2020, 10:00:25 PM9/10/20
to mod...@googlegroups.com
You can't have two VirtualHosts for same port with same ServerName. Apache will always use the first matching one it finds when doing named based virtual hosting.

If you want both sites to be under the same host name, the configuration for each must be combined under the one VirtualHost definition.

Graham

Gordon Charles

unread,
Sep 10, 2020, 11:29:11 PM9/10/20
to modwsgi
Graham,

I genuinely appreciate your help and patience; however, I believe we may be struggling to communicate.  To be clear the .conf file I provided is the one, only and the complete contents of the .conf file.  To be clear, yes I would like the site(s) to be under the same address/port and the configuration file I've sent you is my attempt to do so with the end goal of being able to support either php generated by wordpress or python supported by flask.

Regards,

Gordon

Graham Dumpleton

unread,
Sep 11, 2020, 12:24:07 AM9/11/20
to mod...@googlegroups.com
Is there is a reason why use of mod_rewrite is disabled by not setting FollowSymLinks and SymLinksIfOwnerMatch. Can only presume then that is due to that.

Gordon Charles

unread,
Sep 11, 2020, 11:51:31 AM9/11/20
to modwsgi
Setting FollowSymLinks and SymLinksIfOwnerMatch worked.  Thank you.  I'm including the working .conf files for reference:

<Directory /var/www/html>
        Require all granted
</Directory>
WSGIRestrictEmbedded On
<VirtualHost *:80>
        ServerName thegacway.com
        ServerAlias www.thegacway.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Define a mod_wsgi daemon process group.
        WSGIDaemonProcess www.thegacway.com display-name=%{GROUP}
        # Force the Python web application to run in the mod_wsgi daemon process group.
        WSGIProcessGroup www.thegacway.com
        WSGIApplicationGroup %{GLOBAL}
        # Disable embedded mode of mod_wsgi.
        # Set document root and rules for access.
        #DocumentRoot /var/www/html
        <Directory /var/www/html>

            Options ExecCGI SymLinksIfOwnerMatch FollowSymLinks


            DirectoryIndex index.php
            AddHandler application/x-httpd-php .php
            AddHandler wsgi-script .py
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ /main.py/$1 [QSA,PT,L]
        </Directory>

        #LogLevel info ssl:warn

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

</VirtualHost>

Gordon Charles

unread,
Sep 11, 2020, 4:52:41 PM9/11/20
to modwsgi
Graham,

I have not been successful in launching a python application.  My understanding is the configuration should launch the php handler if it finds a .php file in a directory and if not a .php file, but a ,py file exists it should launch that.  The rules in the configuration file and the code you have supplied for the python file want that file to be named main.py.  So I created a subdirectory, zones, off of the root directory with only main.py in that directory.  File permissions are read for all on main.py and executable for all for the subdirectory.  group and owner are the same for the root directory, the subdirectory and main.py.

I get the following:

[Fri Sep 11 20:48:22.125318 2020] [autoindex:error] [pid 63687] [client 99.46.143.132:59742] AH01276: Cannot serve directory /var/www/html/zones/: No matching DirectoryIndex (index.php) found, and server-generated directory index forbidden by Options directive

If I add Indexes to the Options directive, I get the directory listing of http://thegacway.com/zones/.

Any assistance would be appreciated.

Regards,

Gordon

Graham Dumpleton

unread,
Sep 11, 2020, 4:55:13 PM9/11/20
to mod...@googlegroups.com
If you are still using:

    RewriteRule ^(.*)$ /main.py/$1 [QSA,PT,L]

the file 'main.py' must be in the document root directory for Apache, presumably next to 'index.php'.

Try that first.

Graham

Gordon Charles

unread,
Sep 11, 2020, 5:34:20 PM9/11/20
to modwsgi
Same result.  If the browser is directed to http://thegacway.com/zones/ the same error is generated.  Unfortunately, I don't believe I have a handle on the mechanisms / behaviors of this solution.  My understanding was that if a directory under the root directory did not have a .php file in that directory, apache would route the url request matching that directory to a python file in that directory, if it existed which is apparently not the case.  My understanding is now if there is a subdirectory which does not have a .php file then the request will be routed to main.py in the root directory, regardless of which directory was included in the URL.

Graham Dumpleton

unread,
Sep 13, 2020, 1:41:02 AM9/13/20
to mod...@googlegroups.com
What is the 'zones' sub directory under the document root for?

A request will only be redirected to use main.py in document root as handler if the URL path doesn't match either a directory or any file (not just .php files).

So if you have a 'zones' sub directory, and use a URL with path of /zones, then it matches the directory and tries to serve up a directory listing of it if index generation is enabled, or otherwise will fail. It will not redirect to 'main.py' in that case.

So for any URL path you want handled by the Python `main.py`, there cannot be a directory or file which would be matched by it.

Graham

Gordon Charles

unread,
Sep 19, 2020, 10:59:57 PM9/19/20
to modwsgi
Graham,

A gracious thank you for your help and all the effort you have put into mod_wsgi.  Removing the zones subdirectory resulted in the launching of main.py and the site is now working as expected.  While not in this space and predominantly in IC development, my organization and myself have had to support customer facing products and as a result support those who do not read the documentation throughly.  In the spirit of understanding how guilty I was of not reading throughly I went back and re-read your post and reading it a second time I still come away with the impression that an empty directory should result in the correct behavior, which clearly it does not.  The line which convinces me of this is "If however the URL could not be mapped to an actual physical file in the document directory, the request will be rewritten such that the request will be redirected to the resource 'main.py'."  While I may not fully appreciate this line in the context of the article, I'm bringing it to your attention so you are aware of at least where one reader became lost.

Cheers,

Gordon

Graham Dumpleton

unread,
Sep 20, 2020, 3:19:54 AM9/20/20
to mod...@googlegroups.com
The behaviour is dictated by:

            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d

This in combination says to rewrite it if the target is neither a file or a directory.

Technically you could remove the second one for the directory (I think), and if the URL had matched to a directory it would still redirect to the WSGI application.

If however you had wanted Apache to do directory indexes, then you probably wouldn't want to do it.

So the configuration explains the exact behaviour, but then I guess it depends on whether you understood what the configuration was doing. :-)

Graham 

Gordon Charles

unread,
Sep 21, 2020, 11:40:17 AM9/21/20
to modwsgi
Graham,

After you stated the expected behavior in this thread,  I did spend the time to read through the Apache documentation on RewriteCond and RewriteRule (in particular the flags); however, even if I had read the Apache documentation and interpreted it correctly prior to following the post I would have doubted my own interpretation based upon the language in your post - deferring to expert.  Thanks again for all the help.

Cheers,

Gordon

Reply all
Reply to author
Forward
0 new messages