DEV Community

Cover image for Flask Web-App Setup Using WSGI and Apache or httpd on Debian and fedora based servers
Shubham Singh
Shubham Singh

Posted on • Updated on

Flask Web-App Setup Using WSGI and Apache or httpd on Debian and fedora based servers

[1] Installing Dependence

Debian

sudo apt-get install libapache2-mod-wsgi python-dev
Enter fullscreen mode Exit fullscreen mode
pip3 install virtualenv
Enter fullscreen mode Exit fullscreen mode

Fedora

sudo dnf install python-dev python3-pip mod_wsgi
Enter fullscreen mode Exit fullscreen mode
pip3 install virtualenv
Enter fullscreen mode Exit fullscreen mode

[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
Enter fullscreen mode Exit fullscreen mode
virtualenv venv
source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode
pip install flask
Enter fullscreen mode Exit fullscreen mode

app.py content

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run()
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

[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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

For the WSGI script

cd /var/www
semanage fcontext -a -t httpd_sys_script_exec_t './webapp/webapp.wsgi'
restorecon -vvRF ./webapp/webapp.wsgi
Enter fullscreen mode Exit fullscreen mode

To check context

ls -ldZ <file_or_dir>
Enter fullscreen mode Exit fullscreen mode

[5] Refresh the server

sudo systemctl stop httpd
sudo systemctl start httpd
sudo systemctl enable httpd
Enter fullscreen mode Exit fullscreen mode

To check the Status

sudo systemctl status httpd
Enter fullscreen mode Exit fullscreen mode

Top comments (0)