I installed WordPress on my server and it's working fine. Now I'm trying to set up another application made using Flask on the same server. This is what my .conf file looks now:

<Directory /var/www/html/mydomain.com/public_html>
    Require all granted

<VirtualHost *:80>
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/mydomain.com/public_html

    WSGIDaemonProcess test user=www-data group=group-wp threads=5
    WSGIScriptAlias /newsletter /var/www/newsletter.wsgi

    <Directory /var/www/html/mydomain.com/public_html/newsletter>
        WSGIProcessGroup test
        WSGIApplicationGroup %{GLOBAL}
        Require all granted

    ErrorLog /var/www/html/mydomain.com/logs/error.log
    CustomLog /var/www/html/mydomain.com/logs/access.log combined

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.mydomain.com [OR]
    RewriteCond %{SERVER_NAME} =mydomain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

My Flask app test.py looks like this

import sqlite3
from flask import Flask, g, jsonify, abort, request

app = Flask(__name__)

@app.route('/', methods=['GET'])
def index():
    return "testing"

@app.route('/api/list', methods=['GET'])
def list_all():
    return "also testing"

if __name__ == '__main__':

and my newsletter.wsgi file looks like this:

import sys

sys.path.insert(0, '/var/www/newsletter')

from test import app as application

I've installed libapache2-mod-wsgi-py3 to run the wsgi script but I'm getting a 404 page at mydomain.com/newsletter, mydomain.com/api_list, mydomain.com/newsletter/api_list and all the other combinations I've tried. Should I be changing .htaccess? Should my Flask app be located in the same directory as WordPress? I've already tried moving the app directory to public_html and adding the rule RewriteCond %{REQUEST_URI} !^/newsletter to the .htaccess file but it just shows the .py and .wsgi files, not running the script.


Here's how I solved it:

  • I was changing the non-SSL .conf file. certbot (from Let's Encrypt) automatically creates a new .conf with the new SSL settings
  • mod_wsgi was using Python 2, for which Flask was not installed
  • I uninstalled mod_wsgi, updated Apache with apt-get and reinstalled mod-wsgi with pip3 install mod_wsgi. More details here
  • I checked error.log to sort out the remaining errors

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.