# Configure Flask with WSGI
1.Install and Enable mod_wsgi
sudo apt-get install libapache2-mod-wsgi python-dev
#To enable mod_wsgi, run the following command:
sudo a2enmod wsgi
2. Creating a Flask App
a. cd /var/www
b. sudo mkdir FlaskApp
c. cd FlaskApp
d. sudo mkdir FlaskApp
e .cd FlaskApp
f .sudo mkdir static templates
3.Now, create the __init__.py file that will contain the flask application logic.
sudo nano __init__.py
#Add following logic to the file:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, I love Digital Ocean!"
if __name__ == "__main__":
app.run()
4.Configure and Enable a New Virtual Host
sudo nano /etc/apache2/sites-available/FlaskApp (for older ubuntu version)
sudo nano /etc/apache2/sites-available/FlaskApp.conf (for 13+ ubuntu version)
#Add the following lines of code to the file to configure the virtual host.
<VirtualHost *:8080>
ServerName localhost
ServerAdmin admin@mywebsite.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Note :- Define LISTEN port before VirtualHost
Save and close the file.
5.Enable the virtual host with the following command:
sudo a2ensite FlaskApp
6.Create the .wsgi File
cd /var/www/FlaskApp
sudo nano flaskapp.wsgi
Add the following lines of code to the flaskapp.wsgi file:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import app as application
application.secret_key = '123456789'
Save and close the file.
Now your directory structure should look like this:
|--------FlaskApp
|----------------FlaskApp
|-----------------------static
|-----------------------templates
|-----------------------__init__.py
|----------------flaskapp.wsgi
7.Than Restart the apache server following this command
sudo service apache2 restart