I am trying to get a Flask application running on AWS EC2 (standard Ubuntu AMI) through an Apache2 web server. My application internally uses Numpy. I tested the following:
Without Numpy, the application runs through Apache2. Using Numpy, it shuts down on import numpyand throws a 500 Server error (see Logs below).
When using Numpy, the application works fine when called directly from the command line ( i.e. python app.py). The numpy bits work as expected, and I can request the endpoints of the application from the outside.
I also printed out the system path ( print sys.path) immediately before import numpy as npand made sure that the corresponding directory of package sites is in the path. I also manually specified WSGIPythonPathto include the package sites directory. I tried to set the number of threads to 1 and edit the apache2.conf file. None of these efforts have been successful.
Below is my directory structure and the contents of the corresponding files.
The root folder /var/www/html/webserver_mockup
Root folder contents
/var/www/html/webserver_mockup/app.wsgi
/var/www/html/webserver_mockup/mockup/__init__.py
/var/www/html/webserver_mockup/mockup/app.py
app.wsgi
(Edited according to Graham's comment below)
import sys
import site
site.addsitedir('/home/ubuntu/.local/lib/python2.7/site-packages')
sys.path.insert(0, "/var/www/html/webserver_mockup")
from mockup.app import app as application
/etc/apache2/sites-available/000-default.conf
(Edited according to Graham's comment below)
WSGIPythonPath /usr/local/lib/python2.7/site-packages/
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
WSGIDaemonProcess webserver_mockup threads=5
WSGIScriptAlias / /var/www/html/webserver_mockup/app.wsgi
<Directory />
WSGIProcessGroup %{GLOBAL}
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
</Directory>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Apache Error Log
[Fri Apr 07 04:09:41.062282 2017] [mpm_event:notice] [pid 7221:tid
140325391972224] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0
Python/2.7.12 configured
[Fri Apr 07 04:09:41.062358 2017] [core:notice] [pid 7221:tid
140325391972224] AH00094: Command line: '/usr/sbin/apache2'
[Fri Apr 07 04:09:46.770159 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] mod_wsgi (pid=7225): Target
WSGI script '/var/www/html/webserver_mockup/app.wsgi' cannot
be loaded as Python module.
[Fri Apr 07 04:09:46.770300 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] mod_wsgi (pid=7225):
Exception occurred processing WSGI script
'/var/www/html/webserver_mockup/app.wsgi'.
[Fri Apr 07 04:09:46.770370 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] Traceback (most recent
call last):
[Fri Apr 07 04:09:46.770432 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] File
"/var/www/html/webserver_mockup/app.wsgi", line 7, in <module>
[Fri Apr 07 04:09:46.770534 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] from mockup.app import
app as application
[Fri Apr 07 04:09:46.770612 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] File
"/var/www/html/webserver_mockup/mockup/app.py", line 12, in
<module>
[Fri Apr 07 04:09:46.770693 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] import numpy as np
[Fri Apr 07 04:09:46.770755 2017] [wsgi:error] [pid 7225:tid
140325250004736] [client 24.6.50.183:58332] ImportError: No module
named numpy
My question is: why doesn't Apache2 find Numpy even though the corresponding site packages are in its way?