Replace virtual environment with conda environment; Using one (conda environment) per app/virtualhost

162 views
Skip to first unread message

Nana Okyere

unread,
Mar 10, 2016, 6:23:14 PM3/10/16
to modwsgi
Graham,
Great post. In my current set up, I have multiple flask python applications hosted on multiple virtual hosts. 
I have created only one virtual environment that all the applications use.

Outside of all the virtual hosts, I have:
##########################################
WSGISocketPrefix /var/run/wsgi
WSGIPythonHome /WebHA/catworld/featureanalytics-443S/webpython   #folder of my virtualenv
########################################

Then in each virtual host, I have among others:
#########################################
DocumentRoot "/WebHA/catworld/featureanalytics-443S/workforce_tool/"
ServerName wpat.cat.com
ServerAlias wpat.cat.com

WSGIDaemonProcess wpat.cat.com user=http group=http processes=4 threads=4
WSGIProcessGroup wpat.cat.com
WSGIPassAuthorization On

WSGIScriptAlias / /WebHA/catworld/featureanalytics-443S/workforce_tool/workforce_tool.wsgi
Alias /static /WebHA/catworld/featureanalytics-443S/workforce_tool/app/static

<Directory /WebHA/catworld/featureanalytics-443S/workforce_tool/app/static/>
    Order allow,deny
    Allow from all
</Directory>
#######################################


The corresponding wsgi file for the above virtual host is:
###############################################
#workforce_tool.wsgi

#!/WebHA/catworld/featureanalytics-443S/webpython/bin/python

activate_this = '/WebHA/catworld/featureanalytics-443S/webpython/bin/activate_this.py'
exec(open(activate_this).read())

import sys
import logging

logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/WebHA/catworld/featureanalytics-443S/workforce_tool/")

from app import theapp as application
application.secret_key = 'mykey'
#############################################

Python 3.4.3 
mod_wsgi 4.4.22 
Daemon mode


I followed a tutorial online to set things up this way.
For other reasons, I have to switch to use anaconda/conda distribution of python. So I'll be creating conda environments for each app.

1. How will the code in the wsgi file change especially about activating the python environment? 
2. How will my set up change to use a different conda environment for each app/virtual host? I want to move away from using one environment for every python app. 

Thanks

Graham Dumpleton

unread,
Mar 10, 2016, 6:35:34 PM3/10/16
to mod...@googlegroups.com
Before you can even do that, mod_wsgi would need to be uninstalled and recompiled from source code against the Anaconda Python distribution you want to use. I am not sure how well that will work as I have seen mixed reports about the Anaconda distribution and whether it will work. The issue I have heard of was with older versions, so later versions of Anaconda may work better.

1. How will the code in the wsgi file change especially about activating the python environment? 

The activation of the Python virtual should be removed. You shouldn’t do it that way. Also don’t modify sys.path either.

2. How will my set up change to use a different conda environment for each app/virtual host? I want to move away from using one environment for every python app. 

You must have each application delegated to its own mod_wsgi daemon process.

Outside of all VirtualHost’s add:

    WSGIRestrictedEmbedded On

so you don’t accidentally run stuff in embedded mode.

You should then use options to WSGIDaemonProcess to specify the location of the virtual environment using the python-home option. Use python-path option to specify other directories to search.

    WSGIDaemonProcess wpat.cat.com user=http group=http processes=4 threads=4 python-home=/WebHA/catworld/featureanalytics-443S/webpython python-path=/WebHA/catworld/featureanalytics-443S/workforce_tool

    WSGIProcessGroup wpat.cat.com
    WSGIApplicationGroup %{GLOBAL}

    WSGIScriptAlias / /WebHA/catworld/featureanalytics-443S/workforce_tool/workforce_tool.wsgi

This config assumes one application per VirtualHost.

Note that have added WSGIApplicationGroup to force use of main Python interpreter context. This helps with some third party C extension modules for Python that will not work in sub interpreters. It is okay to override so long as only running one application per daemon process group.

For some reading see:


Graham

Nana Okyere

unread,
Mar 12, 2016, 4:03:26 PM3/12/16
to modwsgi
Graham,

Thanks. I'm sticking to the python I built from source for now and get back to trying to use anaconda later. That said, after making the suggested changes, I get: 

Syntax error on line 1115 of /WebHA/apache/V2.2/http-home/featureanalytics-443S/httpd.conf:
Invalid command 'WSGIRestrictedEmbedded', perhaps misspelled or defined by a module not included in the server configuration
Server could not be started
 
when I try to start apache. This error is coming from httpd.conf. When I comment the WSGIRestrictedEmbedded line out, I'm able to start apache just fine.
Yes, I have the line :

LoadModule wsgi_module modules/mod_wsgi.so

up in the file.

Before and above all virtual hosts, I have:

WSGISocketPrefix /var/run/wsgi
WSGIRestrictedEmbedded On

I only have this one virtual host:

########################################################################
<VirtualHost *:443>

    DocumentRoot "/WebHA/catworld/featureanalytics-443S/dataupload_app/"
    ServerName fbbidataupload.cat.com
    ServerAlias fbbidataupload.cat.com
    

    WSGIDaemonProcess fbbidataupload.cat.com \
        user=http group=http processes=2 threads=15 \
        python-home=/WebHA/catworld/featureanalytics-443S/webpython \
        python-path=/WebHA/catworld/featureanalytics-443S/dataupload_app

    WSGIProcessGroup fbbidataupload.cat.com
    WSGIApplicationGroup %{GLOBAL}
    WSGIPassAuthorization On

    WSGIScriptAlias / /WebHA/catworld/featureanalytics-443S/dataupload_app/uploadapp.wsgi

    Alias /static /WebHA/catworld/featureanalytics-443S/dataupload_app/app/static
    <Directory /WebHA/catworld/featureanalytics-443S/dataupload_app/app/static/>
   Order allow,deny
   Allow from all
    </Directory>

    LogLevel info

    <Directory "/WebHA/catworld/featureanalytics-443S/dataupload_app">
   Options FollowSymLinks Indexes MultiViews
   AllowOverride All
   Order allow,deny
   Allow from all
    </Directory>


</VirtualHost>
########################################################################

Know why I'm getting the WSGIRestrictedEmbedded error?

Graham Dumpleton

unread,
Mar 12, 2016, 10:04:09 PM3/12/16
to mod...@googlegroups.com
Use:

WSGIRestrictEmbedded On

I can't easily see original message right now to see if I had it wrong. Is only Restrict not Restricted.

Sent from my iPhone
--
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 post to this group, send email to mod...@googlegroups.com.
Visit this group at https://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/d/optout.

Nana Okyere

unread,
Mar 13, 2016, 4:08:32 PM3/13/16
to modwsgi
That was it. Thanks.
Reply all
Reply to author
Forward
0 new messages