How to deploy Flask Applications on CWP using Apache & Nginx
Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications.Being a Python Web framework, Flask requires Python.In this guide, we will demonstrate how to install and configure flask. We will then set up Nginx and Apache to proxy our flask application.
We will also be using the mod_wsgi Apache module that can communicate with flask over the WSGI interface specification.
1. Install latest version of python
Please refer to https://opentechy.com/install-python-3-7-3-from-source/ for a guide on how to install python
2. Install mod_wsgi
a) Download latest version of mod_wsgi. As of writing, the latest version was 4.6.5
# cd /usr/local/src # wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.5.zip
b) Unzip and build mod-wsgi with the version of python installed in step 1 above
# cd /usr/local/src # unzip 4.6.5.zip # cd mod_wsgi-4.6.5 # ./configure --with-python=/usr/local/bin/python3.7 # make # make install
c) Add mod_wsgi module to httpd.conf file
echo "LoadModule wsgi_module modules/mod_wsgi.so" >> /usr/local/apache/conf/httpd.conf
d) Restart Apache
systemctl restart httpd
e) Check if mod_wsgi is loaded
/usr/local/apache/bin/httpd -M | grep wsgi
Output should be similar to the below:
wsgi_module (shared)
f) Install flask with pip
# pip3.7 install flask
3. Setup and Configure Flask
a) Create account to host Flask App
With Flask installed in step 1, we will create an account on CWP to host our flask application.
CWP.Admin Left Menu -> User accounts -> New Account -> fill the details and click create.
In this article, i created the flaskapp.com domain with flask as the username.
b) Create a flask application called myproj
Creating a .wsgi file
To run your application you need a yourapplication.wsgi file. This file contains the code mod_wsgi is executing on startup to get the application object. The object called application in that file is then used as application
Create a myproj.wsgi file in your DocRoot with the following content:
#!/usr/local/bin/python3.7
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/home/flask/public_html/")
from myproj import app as application
application.secret_key = 'welcome123dfhfhdfjdksls'
Note: application.secret_key should be changed to your generated secret key
We then also create a file myproj.py which will contain flask application logic.For example, if we want to create a simple flask application which will display Hello, I love Flask!,
Add following logic to the file:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, I love Flask!"
if __name__ == "__main__":
app.run()
Finally, create __init__.py file
Your application directory should look like this in your DocRoot.
|----myproj.wsgi
|----myproj.py
|----__init__.py
|----static
|----templates
From above, you need to create two directories, static – to host your css,js,images and templates – to host html files
Note: The __init__.py is actually a blank file, but is very important without which you can’t get this working. The __init__.py makes it possible to import our app as a python module.
4. Configure Apache and Nginx
CWP.Admin left Menu -> Webserver settings -> Webserver Domain Conf -> Select Username -> create configuration -> select the option – nginx->apache->php-cgi -> check the box – Rebuild Webservers conf for domain on save -> save changes
Note:These options depend on the webservers you have currently installed.If the you built nginx & Apache or Nginx & Varnish & Apache as webservers, you will have the option nginx->apache->php-cgi available. Also note that a rebuild of webservers will destroy all the changes and custom configurations. Always backup vhost files(both nginx & apache) before a rebuild
a) Configure apache
Edit the vhost file for your domain in /usr/local/apache/conf.d/vhosts/ and add the following inside the virtualhost container
WSGIDaemonProcess myproj user=flask group=flask threads=5
WSGIScriptAlias / /home/flask/public_html/myproj.wsgi
WSGIScriptReloading On
Note : Please replace username and project name(myproj) as per your setup as this is a guide.
b) Configure Nginx
Edit the vhost file for your domain in /etc/nginx/conf.d/vhosts/ and add the following inside the server container
location /static/ {
root /home/flask/public_html/;
expires 1d;
}
c) Restart Apache and Nginx services
systemctl restart nginx httpd
d) In your web browser, visit http://flaskapp.com and you should see Hello, I love Flask!
Thank you! This wonderful article was the essential tutorial I needed to get the basics down and be prepared for a more complex and complete deployment!