I recently posted this question to clear a hurdle in launching a Flask webapp on my Amazon ec2 RedHat 7 instance.

In brief, I am attempting to launch a Flask webapp via an Anaconda environment using Python 3.6. Currently, I have a simple Hello World Flask application in /var/www/html/example, following the instructions found here. I was able to run this script previously, using the pre-installed Python 2.7.5 (found at /usr/bin/python), and since have been attempting to run the same application using Python 3.6.

I have been referring to multiple posts regarding similar issues, including this, this, and this, but I am still a bit lost.

I have configured mod_wsgi via the conda environment (for 3.6).

Some relevant system information:

$ which python
~/anaconda3/envs/myenv/bin/python (abspath is /home/ec2-user/anaconda3/envs/myenv/bin/python)
$ python -V
Python 3.6.2 :: Continuum Analytics, Inc.
$ which mod_wsgi-express
$ mod_wsgi-express module-config
LoadModule wsgi_module "/home/ec2-user/anaconda3/envs/myenv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/home/ec2-user/anaconda3/envs/myenv"

The contents of file /etc/httpd/conf/httpd.conf:

DocumentRoot "/var/www/html"
WSGIDaemonProcess myenv python-path=/home/ec2-user/anaconda3/envs/myenv/lib/python3.6/site-packages
WSGIScriptAlias / /var/www/html/examples/example.wsgi process-group=myenv application-group=%{GLOBAL}
LoadModule wsgi_module "/home/ec2-user/anaconda3/envs/myenv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome /home/ec2-user/anaconda3/envs/myenv

<Directory example>
    WSGIProcessGroup myenv
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all

Upon attempting to run sudo apachectl restart, I receive this output of systemctl status httpd.service:

httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sun 2018-04-08 21:41:38 UTC; 3min 7s ago
  Docs: man:httpd(8)
  Process: 20541 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
  Process: 20540 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
 Main PID: 20540 (code=exited, status=1/FAILURE)

Apr 08 21:41:38 [ip].compute.internal systemd[1]: Starting The Apache HTTP Server...
Apr 08 21:41:38 [ip].compute.internal httpd[20540]: httpd: Syntax error on line 125 of /etc/httpd/conf/httpd.conf: Cannot load /home/ec2-user/anaconda3/envs/myenv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so into server: : /home/ec2-user/anaconda3/envs/myenv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so: cannot open shared object file: Permission denied

The file in question (mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so) has executable permissions for all users.

I've also noticed that /var/log/httpd/error_log is outputting ImportError: No module named site, despite the fact that I am not attempting to import the site module in any Python file within /var/www/html/example.


SOLUTION: Realized that this error was being thrown because my installation of Python was in my home directory. Thus, Apache didn't have permission to access the environment.

To solve this, I did the following:

Removed my installation of anaconda3:

[ec2-user@ip] ~ $ rm -rf anaconda3/
# -- be sure to remove directories .conda/ and .anaconda_backup/ -- #
# -- these should be in the same directory as the anaconda3/ directory -- #

Re-installed anaconda3 as root in directory /usr/local (accessible by all users, works with Apache):

[ec2-user@ip] ~ $ sudo su -
[root@ip] ~ $ cd /usr/local
[root@ip] ~ $ wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh 
                  # ^ whichever version you want. Go to https://repo.continuum.io/archive/
[root@ip] ~ $ bash Anaconda3-5.1.0-Linux-x86_64.sh
    # specify your path when prompted --> /usr/local/anaconda3
    # note that directory /usr/local/anaconda3 cannot exist prior to running the command

Add the line export PATH='/usr/local/anaconda3/bin:$PATH' to the end of your /root/.bashrc file AS THE ROOT USER! (edit the old anaconda3 path if it already exists at the end of this file)

[root@ip] ~ $ vi /root/.bashrc # -- add/edit the line here -- #
[root@ip] ~ $ source /root/.bashrc

Verify that the root user is using the right version of Python:

[root@ip] ~ $ which python

Exit as root user:

[root@ip] ~ $ exit

Verify that the right version of Python works as default user:

[ec2-user@ip] ~ $ which python

If this prints /usr/bin/python or something other than /usr/local/anaconda3/bin/python, make sure you add the PATH I specified above to your user's ~/.bashrc file (just like you did as the root user)

NOTE: all new installations/environment configurations must now be done as ROOT. I.e.:

[ec2-user@ip] ~ $ sudo su -
[root@ip] ~ $ conda install flask
[root@ip] ~ $ exit
# -- open Python to verify that your installation worked -- #
[ec2-user@ip] ~ $ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
# -- shouldn't throw an ImportError -- #
>>> exit()

If anyone stumbles upon this in the future, COMMENT! I'd love to be able to offer some help.

Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.