setup of Amazon EC2 + Apache2 + WSGI + flask

This article shows how to create simple Flask application on ec2 with mod_WSGI, Apache2 in virtualenv of Python 3.4. Apache is a web server. Flask is framework to build fancy website easily and quickly. mod_WSGI let them talk.

3. Create WSGI File

Create python file, /var/www/html/flaskapp/flaskapp.wsgi
We want to activate the virtual environment, but, I don't know why, libraries in the virtual environment cannot be imported. So, you may need site.toaddsitedir(your/venv/site-packages). To import flaskapp.py, we insert the path (which may not needed as they are in the same folder). WSGI need app to be named "application, as you see the last line.

If you use older version of Apache, You may edit /etc/apache2/sites-enabled/000-default.conf

5. Restart apache

Execute

$ sudo service httpd restart

6. Access your website

type your public DNS in the browser.

Where is Error log?

$ cat /var/log/httpd/error_log

Why do you get Fatal Python error: Py_Initialize: Unable to get the locale encoding ?

When you find following in /var/log/httpd/error_log

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ImportError: No module named 'encoding'

Your WSGIPythonPath (python-path) or WSGIPythonHome (python-home) is incorrect. WSGIPythonPath is where your modules are loaded. Activate virtualenv, import something, and see where it's from. For example,

>>> import numpy

>>> print(numpy.__file__)

For me, it was /usr/bin/myenv/lib64/python3.4/site-packages

WSGIPythonHome is where your python is. See by

>>> import sys

>>> sys.prefix

For me, it was /usr/bin/myenv. If your out of the virtual environment, it will be /usr