Ashok
Ashok

Reputation: 65

Python 2.7 + Flask + Apache + mod_wsgi

I am trying to configure Apache to run my sample Flask application and I am running into an issue which I am unable to resolve. Here are the steps I followed (all as root user to avoid any permissions issue):

  1. Installed httpd on CentOS (yum install httpd)
  2. Installed mod_wsgi using pip (yum install mod_wsgi)
  3. Created a VirtualEnv under /var/www/FlaskApp/
  4. Installed Flask inside VirtualEnv (pip install Flask)
  5. Created a sample application under /var/www/FlaskApp/app.py
  6. Created a wsgi File under /var/www/FlaskApp/wsgi.py
  7. Configured VirtualHost on Apache (httpd.conf)
  8. Restarted Apache

wsgi file:

activate_this = '/var/www/FlaskApp/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import sys
import site

site.addsitedir('/var/www/FlaskApp/venv/lib/python2.7/site-packages')
site.addsitedir('/var/www/FlaskApp/venv/lib64/python2.7/site-packages')

sys.path.insert(0, '/var/www')

from FlaskApp import app as application

app.py:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

httpd.conf:

<VirtualHost *:80>

     ServerName localhost

     WSGIDaemonProcess FlaskApp user=ec2-user group=ec2-user threads=2

     WSGIScriptAlias / /var/www/FlaskApp/wsgi.py

     <Directory /var/www/FlaskApp>
         Allow from all
     </Directory>

</VirtualHost>

Directory Structure: (/var/www/FlaskApp/)

-rw-r--r-- 1 root root  154 Dec  8 23:04 app.py
drwxr-xr-x 7 root root 4096 Dec  8 23:11 venv
-rw-r--r-- 1 root root    0 Dec  8 23:36 __init__.py
-rw-r--r-- 1 root root  356 Dec  8 23:41 wsgi.py

Error in httpd error_log:

Apache/2.2.34 (Unix) DAV/2 mod_wsgi/3.2 Python/2.6.9 configured -- resuming normal operations
[client 127.0.0.1] mod_wsgi (pid=15119): Target WSGI script '/var/www/FlaskApp/wsgi.py' cannot be loaded as Python module.
[client 127.0.0.1] mod_wsgi (pid=15119): Exception occurred processing WSGI script '/var/www/FlaskApp/wsgi.py'.
[client 127.0.0.1] Traceback (most recent call last):
[client 127.0.0.1]   File "/var/www/FlaskApp/wsgi.py", line 12, in <module>
[client 127.0.0.1]     from FlaskApp import app as application
[client 127.0.0.1]   File "/var/www/FlaskApp/app.py", line 1, in <module>
[client 127.0.0.1]     from flask import Flask
[client 127.0.0.1]   File "/var/www/FlaskApp/venv/lib/python2.7/site-packages/flask/__init__.py", line 16, in <module>
[client 127.0.0.1]     from werkzeug.exceptions import abort
[client 127.0.0.1]   File "/var/www/FlaskApp/venv/lib/python2.7/site-packages/werkzeug/__init__.py", line 32
[client 127.0.0.1]      self._origin = {item: mod for mod, items in available.items() for item in items}
[client 127.0.0.1]                                  ^
[client 127.0.0.1]  SyntaxError: invalid syntax

Can someone please help me fix this issue? Thanks!

Upvotes: 0

Views: 933

Answers (1)

Aaron A
Aaron A

Reputation: 495

According to the first line of your error log, you're using Python 2.6.9 but your virtualenv has packages for Python 2.7+. The syntax error is a dict comprehension which was introduced in Python 2.7.

The mod_wsgi docs discuss this issue here: https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html#virtual-environment-and-python-version

As furas said, Python 2.7 is reaching its end of life so you should consider upgrading to Python 3 for the whole project.

Upvotes: 0

Related Questions