Question

Flask app on Fedora with Apache

I have a DO droplet running Fedora with Apache currently configured as a webserver. I need to deploy my simple python app with Flask and Apache. So far I have tried using mod_wsig, and deployment is (predictably) far from simple.

I have got Apache on my server, and it is using https with LetsEncrypt.

I have made a hello-world Flask app that runs on a development server.

I followed the instructions at https://pypi.org/project/mod-wsgi/ and installed mod-wsgi into a python virtual environment. I found the directives needed to configure Apache by running mod_wsgi-express module-config and put then into the .conf file that modifies the httpd.conf file. (I have created a ‘sites-enabled’ folder that links with a ‘sites-available’ folder)

After that, I was at a loss at what to do, so I have followed https://medium.com/@cdanielmedinas/deploying-your-first-flask-application-on-apache-wsgi-linux-server-fb54537aef41. I have run into problems on issues the tutorial did not cover.

Some I have fixed. Like understanding permissions. Which I think I have fixed by adding the user that is running Apache (as specified in the httpd.conf file) to have execute permissions on the files and folders on the flask app. Also, I am not sure why the tutorial seemed to not mount the project folder using Alias /project/ /srv/http/your_project . When I did this, it seemed to fix the “AH01630: client denied by server configuration” problem I had.

Of course there is some more troubleshooting to be done. There seems to be a problem with the .wsgi file as it was suggested to be written in that tutorial I had been following.

Activating the virtual environment by running some activate_this.py file like that tutorial suggests does not seem to work. Now here is the question: Should I remove that section because the virtual environment should be activated in the .conf file that modifies the httpd.conf file by writing WSGIDaemonProcess application_name python-home=/path/to/app/venv within my .conf file? (https://stackoverflow.com/questions/42661771/how-to-get-mod-wsgi-to-pick-up-my-virtualenv) (Note that I cannot install libapache2-mod-wsgi-py3 like the solution to the stackoverflow question suggests because I am deploying this app on Fedora.) What should the .wsgi file contain if not that?

I guess the problem is that I haven’t been able to come across a comprehensive guide to deploying a flask app with Fedora+Apache+mod_wsgi that I could make work.

What my question comes down to is this:

  1. What do we put in the .wsgi file in the Flask+Apache+mod_wsgi (when mod_wsgi is installed in the .venv using pip) scenario?
  2. Has anyone written a guide to this particular deployment setup where the Apache configuration process is explained and follows recommendations from the docs?

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
December 14, 2024

Heya,

The .wsgi file is responsible for telling mod_wsgi how to interact with your Flask application. Here’s a simple example for your setup:

import sys
import os

# Add the app directory to the Python path
sys.path.insert(0, "/path/to/your/flask/app")

# Activate the virtual environment
activate_this = '/path/to/your/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

# Import your Flask application
from app import app as application

Replace /path/to/your/flask/app and /path/to/your/venv with the actual paths to your Flask app directory and virtual environment.

As for Apache. Your Apache configuration is critical. Below is an example VirtualHost configuration for your Flask app using mod_wsgi:

<VirtualHost *:443>
    ServerName yourdomain.com
    DocumentRoot /path/to/your/flask/app

    # Enable SSL with Let's Encrypt
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem

    # WSGI configuration
    WSGIDaemonProcess flaskapp python-home=/path/to/your/venv python-path=/path/to/your/flask/app
    WSGIProcessGroup flaskapp
    WSGIScriptAlias / /path/to/your/flask/app/app.wsgi

    # Directory permissions
    <Directory /path/to/your/flask/app>
        Require all granted
    </Directory>

    # Error logs
    ErrorLog ${APACHE_LOG_DIR}/flaskapp_error.log
    CustomLog ${APACHE_LOG_DIR}/flaskapp_access.log combined
</VirtualHost>

Breaking Down the Configuration

  1. WSGIDaemonProcess

    • Specifies the Python virtual environment using python-home.
    • Points to your Flask app directory using python-path.
  2. WSGIScriptAlias

    • Maps the root URL (/) to your .wsgi file.
  3. Directory Permissions

    • Ensures Apache has access to the app directory.
  4. Logs

    • Set up error and access logs for troubleshooting.
  5. SSL Configuration

    • Ensures your site runs securely with Let’s Encrypt certificates.

Also,

The WSGIDaemonProcess directive activates the virtual environment for mod_wsgi. Therefore, you do not need to include the virtual environment activation code in your .wsgi file. The activate_this.py section in your .wsgi file can be omitted if you specify python-home in your Apache configuration.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.