I have in my server two Python versions: the Python default, already present in Ubuntu Server 16.04 and Anaconda Python. When I run the Flask application with Apache, because this is running in user www-data, the application runs in Python default, but I have to run in Anaconda installation. How to configure to use the Anaconda Python version and how to install new modules in this version? The path of Anaconda installed is /home/wandre/anaconda2
This is one way that worked for me to let Apache run the python module in Anaconda environment so that Flask app can run.
First is installation. Install Apache with the command sudo apt-get install apache2 apache2-bin apache2-dev
. Then, install Anaconda under the directory /opt as sudo. After having Anaconda installed on /opt/anaconda2, install conda packages as below:
sudo su # Login as superuser
export PATH=/opt/anaconda2/bin:$PATH # Add conda to your path
conda install -c anaconda flask
pip install mod_wsgi
mod_wsgi-express install-module # the outputs to this is used for Apache configuration in the next step
Now you need to update the Apache configurations. This involves updating three files:
wsgi.load
Open the file /etc/apache2/mods-available/wsgi.load and add the following (this is the output from
mod_wsgi-express install-module
so yours could be different)LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi-py27.so
If the
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
was originally included in this file, comment it out. Otherwise (at least from what I checked), the apache server will run the default python instead of the one in Anaconda. Next, enable the wsgi mod:sudo a2enmod wsgi
wsgi.conf
Open the file /etc/apache2/mods-available/wsgi.conf and add the following
<IfModule mod_wsgi.c> WSGIPythonHome /opt/anaconda2 </IfModule>
000-default.conf (your filename may be different)
Open the file /etc/apache2/sites-available/000-default.conf and add the following
WSGIDaemonProcess flaskapp python-home=/opt/anaconda2 python-path=/var/www/flask WSGIScriptAlias / /var/www/flask/flaskapp.wsgi <Directory /var/www/flask> WSGIProcessGroup flaskapp WSGIApplicationGroup %{GLOBAL} Require all granted </Directory>
You are all set.
Please note that I was not able to find tutorial for setting 'Anaconda + Flask + Apache' so I referred to a post on 'Anaconda + Django + Apache'.
-
1You should not need
WSGIPythonPath /opt/anaconda2/lib/python2.7/site-packages
. You also don't needWSGIScriptReloading On
as that is the default anyway. For Apache 2.4, useRequire all granted
. You don't needOrder
andAllow
. – Graham Dumpleton Jun 15 '18 at 21:56 -
@GrahamDumpleton Your suggestions worked perfect. Thanks! My post is now updated. – jhch Jun 15 '18 at 22:09
pip install
method as described in pypi.org/project/mod_wsgi It is necessary to recompile as mod_wsgi has to be compiled for Python version/distro you want to use. – Graham Dumpleton May 30 '18 at 14:53