Friday, February 18, 2022

[SOLVED] How to properly serve Flask App on Ubuntu via Apache 2 and mod_wsgi? 404 Not Found Error

Issue

My Flask App works great on local loopback with the Flask server, but now I am trying to get apache2 to serve the application on my private 192.168.0.0/24 network (my server's IP is 192.168.0.18).

EDIT3: I have followed most of Mr. Dumpleton's suggestions with the exception of moving the .wsgi out of document root (I plan to do that after all is running properly). I am able to get the hellow world script to run. Any machine on my private LAN can see it via my server's private IP. However when actually trying to get my site to run, a 404 is returned. There are no errors in /var/log/apache2/error.log. I suspect the issue is in my flaskapp.wsgi script. It seems a lot different than the one used for the simple "hello world" page.

Any suggestions? Thank you Stack Overflow

root@server:/var/www/flaskapp# cat /etc/hosts
127.0.0.1   www.flaskapp.com
127.0.1.1   server


root@server:/etc/apache2/sites-enabled# ls -l
total 0
lrwxrwxrwx 1 root root 32 Jun 29 23:27 flaskapp.conf -> ../sites-available/flaskapp.conf


root@server:/var/www/flaskapp# ls -l
total 8
drwxr-xr-x 8 root root 4096 Jun 30 18:14 flaskapp

-rw-r--r-- 1 root root 1397 Jun 30 19:02 flaskapp.wsgi

root@server:/var/www/flaskapp/flaskapp# ls -l
total 92
drwxr-xr-x 2 root root     4096 Jun 29 15:19 bin
drwxr-xr-x 2 bin  root     4096 Jun 29 15:08 include
-rw-r--r-- 1 root root       64 Jun 30 00:19 __init__.py
drwxr-xr-x 3 bin  root     4096 Jun 29 15:08 lib
drwxr-xr-x 2 bin  root     4096 Jun 29 15:08 local
-rw-r--r-- 1 bin  robobot 10113 Jun 30 19:12 main.py
-rw-r--r-- 1 bin  robobot 10066 Jun 28 11:54 main.py.bak
-rw-r--r-- 1 bin  root       61 Jun 29 15:08 pip-selfcheck.json
-rw-r--r-- 1 bin  robobot    80 Jun 28 16:03 requirements.txt
-rw-r--r-- 1 bin  robobot  7525 Jun 25 11:46 rss_gen27.py
-rw-r--r-- 1 bin  robobot  7525 Jun 25 11:46 rss_gen27.py.bak
-rw-r--r-- 1 bin  robobot  7520 Jun 25 11:42 rss_gen.py
-rw-r--r-- 1 bin  robobot  7334 Jun 20 16:01 rss_gen.py.bak
drwxr-xr-x 2 bin  robobot  4096 Jun 29 09:24 static
drwxr-xr-x 2 bin  robobot  4096 Jun 28 11:53 templates


root@server:/var/www/flaskapp/flaskapp# cat /etc/apache2/sites-enabled/flaskapp.conf 
<VirtualHost *:80>
    ServerName www.flaskapp.com
    ServerAlias flaskapp.com
    DocumentRoot /var/www/flaskapp
    <Directory /var/www/flaskapp>
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>
    WSGIDaemonProcess flaskapp python-path=/var/www/flaskapp:/var/www/flaskapp/flaskapp/lib/python2.7/site-packages
    WSGIProcessGroup flaskapp
    WSGIScriptAlias / /var/www/flaskapp/flaskapp.wsgi
    Alias /static /var/www/flaskapp/flaskapp/static
    <Directory /var/www/flaskapp>
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


root@server:/var/www/flaskapp# cat flaskapp.wsgi 
#!/usr/bin/python
activate_this = '/var/www/flaskapp/flaskapp/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
import logging
import os
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/flaskapp")
from flaskapp import app as application
application.secret_key = "random string of chars "


root@server:/var/www/flaskapp/flaskapp# cat __init__.py 
from flask import Flask
app = Flask(__name__)
import main


main.py:

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def home():
    return render_template("index.html")

if __name__ == '__main__':
    app.run(debug=True)

Solution

The credit belongs to Mr. Dumpleton... here's what worked for me in order to get the flask app served on a private LAN:

root@server:/var/www/flaskapp# pwd
/var/www/flaskapp
root@server:/var/www/flaskapp# ls -l
total 96 
drwxr-xr-x 2 root root     4096 Jun 29 15:19 bin 
-rw-r--r-- 1 root root      750 Jul  1 00:14 flaskapp.wsgi
drwxr-xr-x 2 bin  root     4096 Jun 29 15:08 include
-rw-r--r-- 1 root root       64 Jun 30 00:19 __init__.py
drwxr-xr-x 3 bin  root     4096 Jun 29 15:08 lib
drwxr-xr-x 2 bin  root     4096 Jun 29 15:08 local
-rw-r--r-- 1 root root    10113 Jun 30 23:34 main.py
-rw-r--r-- 1 bin  robobot 10113 Jun 30 23:20 main.py.bak
-rw-r--r-- 1 bin  root       61 Jun 29 15:08 pip-selfcheck.json
-rw-r--r-- 1 bin  robobot    80 Jun 28 16:03 requirements.txt
-rw-r--r-- 1 bin  robobot  7525 Jun 25 11:46 rss_gen27.py
-rw-r--r-- 1 bin  robobot  7525 Jun 25 11:46 rss_gen27.py.bak
-rw-r--r-- 1 bin  robobot  7520 Jun 25 11:42 rss_gen.py
-rw-r--r-- 1 bin  robobot  7334 Jun 20 16:01 rss_gen.py.bak
drwxr-xr-x 2 bin  robobot  4096 Jun 29 09:24 static
drwxr-xr-x 2 bin  robobot  4096 Jun 28 11:53 templates

root@server:/var/www/flaskapp# cat __init__.py ; cat flaskapp.wsgi 
from flask import Flask
app = Flask(__name__)
import main
#!/usr/bin/python
import sys
import logging
import os


this_dir = os.path.dirname(__file__)
sys.path.insert(0, this_dir)
activate_this = '/var/www/flaskapp/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
this_dir = os.path.dirname(__file__)
logging.basicConfig(stream=sys.stderr)
from main import app as application
application.secret_key = "random string here"


Logs are located in /var/log/apache2/error.log

/etc/apache2/sites-enabled/flaskapp.conf:
<VirtualHost *:80>
    ServerName www.flaskapp.com
    ServerAlias flaskapp.com
    DocumentRoot /var/www/flaskapp
    <Directory /var/www/flaskapp>
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>
    WSGIDaemonProcess flaskapp.com processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup flaskapp.com
    WSGIScriptAlias / /var/www/flaskapp/flaskapp.wsgi
    Alias /static /var/www/flaskapp/static
    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


root@server:/var/www/flaskapp# cat /etc/hosts
127.0.0.1   www.flaskapp.com
127.0.1.1   server

Originally I thought the secret key was not needed in the  .wsgi file and in the file with the routes as well... However I needed the key in both places 


Answered By - user9915877
Answer Checked By - Terry (WPSolving Volunteer)