My first post, and I'm new to both Python and Apache, so please go easy on me. I have a Python web application in a conda environment that serves up content just fine using Flask. Now I need to serve everything with Apache instead of Flask. I can get Apache to run Python with mod_wsgi, but only with virtualenv. I tried installing virtualenv with Anaconda, but I received a warning that such a setup is unsupported and not recommended. So my question is this:
How do I set up Apache to run my application in a conda environment?
Here, there's a section on working with virtual environments, and it says I need to enter a line into my wsgi file showing where to find activate_this.py. A virtualenv virtual environment has activate_this.py, but a conda environment does not. It seems that without the right instructions for activating the virtual environment, I'm just getting error 500 in my browser and "Import error: no module named flask" in my httpd error_log.
This is in my httpd.conf file:
<VirtualHost *:80>
DocumentRoot /var/www
WSGIScriptAlias / var/www/tsm.wsgi
<Directory /var/www/tsm>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And here is tsm.wsgi:
import sys
import site
site.addsitedir('~/.conda/envs/tsm/lib/python2.7/site-packages')
sys.path.insert(0, '/var/www/tsm/server/time_series_machine')
sys.stdout = sys.stderr
from app import app as application
Any ideas? Thanks for any help you can provide.
Whilst conda can not install files from GitHub directly, we can use conda to install pip, and (via pip) access GitHub. Whilst over 1,500 packages are available in the Anaconda repository, this is tiny compared to the over 150,000 packages available on PyPI.
You can install pip in the current conda environment with the command conda install pip , as discussed in Using pip in an environment. If there are instances of pip installed both inside and outside the current conda environment, the instance of pip installed inside the current conda environment is used.
I'm late to the party on this but I was having the same problem. For what its worth, I didn't have to recompile anything and was able to get this to work by including something like this in my VirtualHost configuration using the system-installed mod_wsgi:
WSGIDaemonProcess mysite python-path=/path/to/anaconda2/lib/python2.7/site-packages
Note that this points to the site-packages
directory.
To add to @dino's answer, you can also install mod_wsgi
into your root conda environment:
# Instal `mod_wsgi`
$ pip install mod_wsgi
# Find the full path to installed `mod_wsgi`
$ which mod_wsgi-express
# Install and register the `mod_wsgi` module with Apache
$ sudo /full/path/to/installed/mod_wsgi-express install-module
You can then create conda environments for multiple sites:
# Create 3 conda environments
conda create -n mysite1 python django
conda create -n mysite2 python django
conda create -n mysite3 python django
And set WSGIDaemonProcess
in the Apache site configuration file to use the appropriate environment for each site:
# /etc/apache2/sites-enabled/mysite1.conf
WSGIDaemonProcess mysite1 python-path=/path/to/anaconda3/envs/mysite1/lib/python3.5/site-packages
# /etc/apache2/sites-enabled/mysite2.conf
WSGIDaemonProcess mysite2 python-path=/path/to/anaconda3/envs/mysite2/lib/python3.5/site-packages
# /etc/apache2/sites-enabled/mysite3.conf
WSGIDaemonProcess mysite3 python-path=/path/to/anaconda3/envs/mysite3/lib/python3.5/site-packages
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With