Flask Web-App Setup Using WSGI and Apache or httpd on Debian and fedora based servers
Shubham Singh
Posted on May 6, 2022
[1] Installing Dependence
Debian
sudo apt-get install libapache2-mod-wsgi python-dev
pip3 install virtualenv
Fedora
sudo dnf install python-dev python3-pip mod_wsgi
pip3 install virtualenv
[2] Setting up the File System
mkdir /var/www/webapp
mkdir /var/www/webapp/logs
mkdir /var/www/webapp/static
touch app.py && touch webapp.wsgi
cd /var/www/webapp
virtualenv venv
source venv/bin/activate
pip install flask
app.py
content
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
if __name__ == "__main__":
app.run()
webapp.wsgi
content
#!/usr/bin/python
import sys
import logging
activate_this = "/var/www/webapp/venv/bin/activate_this.py"
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/var/www/web-app/webapp')
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
from app import app as application
[3] Setting up httpd
Fedora
In /etc/httpd/conf.d/
create a new config file of any name flaskwebapp.conf
<VirtualHost *:80>
ServerName <you_server_ip>
ServerAdmin <email>
WSGIDaemonProcess <webapp> user=apache group=apache threads=5
WSGIScriptAlias / /var/www/webapp/webapp.wsgi
<Directory /var/www/webapp/>
Order allow,deny
Allow from all
</Directory>
#FOR FLASK STATIC FOLDER
Alias /static /var/www/web-app/webapp/static
<Directory /var/www/webapp/static/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Debian
for Debian base system running apache2, create .conf
in /etc/apache2/sites-available/FlaskApp
.conf
content
<VirtualHost *:80>
ServerName <you_server_ip>
ServerAdmin <email>
WSGIDaemonProcess <webapp> user=apache group=apache threads=5
WSGIScriptAlias / /var/www/webapp/webapp.wsgi
<Directory /var/www/webapp/>
Order allow,deny
Allow from all
</Directory>
#FOR FLASK STATIC FOLDER
Alias /static /var/www/web-app/webapp/static
<Directory /var/www/webapp/static/>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
after creating the .conf
run this sudo a2ensite <naem_of_the_conf_without_.conf>
[4] changing context's for Fedora
For project folder
cd /var/www
semanage fcontext -a -t httpd_sys_content_t './webapp(/.*)?'
restorecon -vvRF ./webapp
For the WSGI script
cd /var/www
semanage fcontext -a -t httpd_sys_script_exec_t './webapp/webapp.wsgi'
restorecon -vvRF ./webapp/webapp.wsgi
To check context
ls -ldZ <file_or_dir>
[5] Refresh the server
sudo systemctl stop httpd
sudo systemctl start httpd
sudo systemctl enable httpd
To check the Status
sudo systemctl status httpd
💖 💪 🙅 🚩
Shubham Singh
Posted on May 6, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
watercooler Why does a reboot make your PC run SO much faster than running all the cleaning tools you can possibly imagine?
November 30, 2024