Apache Python Flask
-
Install Apache
-
Install Python
-
Install wsgi (via pip)
pip3 install mod_wsgi
-
Install Flask (via pip)
-
Create Virtual Environment
python3 -m venv venv
-
Active Environment:
. venv/bin/activate
(to deactivate, typedeactivate
Your local apache config will differ based on os. For mac it's
httpd.conf
and for linux it'sapache2.conf
- Go into your apache config (
httpd.conf
) and update the values as described here
This step may not be necessary, but it's the first thing I did. Mainly you want to update your User and Group to be your computer's active User and Group (the user displayed when you open a terminal. Type
whoami
to see what is what.
- Create an apache config file for your application. See docs
Copy and paste this to /etc/apache2/other/MyApp.conf Make sure to change the paths (USING ONLY ABSOLUTE PATHS) and the user and group to match
Point the WSGI alias to your app.wsgi file in your project
<VirtualHost *>
# Add machine's IP address (use ifconfig command)
ServerName localhost
# Give an alias to to start your website url with
WSGIDaemonProcess /Users/rchacon/code/github/orpheus/python/APF user=rchacon group=staff threads=5
WSGIScriptAlias / /Users/rchacon/code/github/orpheus/python/APF/app.wsgi
<Directory /Users/rchacon/code/github/orpheus/python/APF>
# set permissions as per apache2.conf file
# WSGIProcessGroup APF
# WSGIApplicationGroup %{GLOBAL}
Order deny,allow
# AllowOverride All
Allow from all
</Directory>
ErrorLog /tmp/error.log
LogLevel warn
CustomLog /tmp/access.log combined
</VirtualHost>
-
Add wsgi module to your apache config
httpd.conf
. Run this command to and add it's output to yourhttpd.conf
:mod_wsgi-express module-config
If that command doesn't exist, you need to find out where the wsgi module lives in your computer and add:LoadModule wsgi_module /path/to/module.so
-
in your
httpd.conf
, changeRequire all none
toRequire all granted
at your root<Directory /> ... </Directory>
tag (do a search for<Directory />
and find the one without any arguments passed to it.
<Directory />
AllowOverride none
Require all granted
</Directory>
-
add
Include /etc/apache2/other/*.conf
to yourhttpd.conf
. Change the path to wherever your custom application configurations go -
Restart your apache server
-
Your app should be running on localhost
- Make sure python, flask, apache, and wsgi are installed
- Create an Apache config for your application
- Create a
.wsgi
file for your python app - Load the wsgi module in the Apache config
- Restart your apache server for changes to take effect
export FLASK_ENV=development
flask run
Start: sudo apachectl start
Stop: sudo apachectl stop
Config: vi /etc/apache2/httpd.conf
Restart: sudo apachectl -k restart