2

I was following this tutorial: https://pythonprogramming.net/creating-first-flask-web-app/?completed=/flask-web-development-introduction/

On my local server (home machine on local network). Flask itself starts properly

>>> python /var/www/FlaskApp/FlaskApp/__init__.py 
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [31/Oct/2016 22:56:29] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [31/Oct/2016 22:56:29] "GET /favicon.ico HTTP/1.1" 404 -

flask starts But Apache is showing its default webpage no matter what.

sudo a2enmod wsgi

Module wsgi already enabled

cat /var/www/FlaskApp/FlaskApp/init.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def homepage():
    return "Hi There, how you're doin?"
if __name__ == "__main__":
    app.run()(debug=True)

cat /etc/apache2/sites-available/FlaskApp.conf

<VirtualHost *>
                ServerName dagzserv
                ServerAdmin my@email.com
                WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
                <Directory /var/www/FlaskApp/FlaskApp/>
            Require all granted
                </Directory>
                Alias /static /var/www/FlaskApp/FlaskApp/static
                <Directory /var/www/FlaskApp/FlaskApp/static/>
            Require all granted
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

sudo a2ensite FlaskApp

Site FlaskApp already enabled

I have tried adding app.run(host='0.0.0.0', port=5000) I do have /etc/hosts dagserv as my ip in localnetwork (10.0.1.xxx) Also tried ServerName as localhost.

One thing i did not understood is how exactly Apache is finding that "FlaskApp.conf" but i assumed that it is scanning its config directory (/etc/apache2/sites-available/FlaskApp.conf)

I am getting no errors in /var/logs/apache2

cat access.log

10.0.1.14 - - [31/Oct/2016:22:46:52 -0700] "GET / HTTP/1.1" 200 3524 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
10.0.1.14 - - [31/Oct/2016:22:49:14 -0700] "GET / HTTP/1.1" 200 3524 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
10.0.1.14 - - [31/Oct/2016:22:49:15 -0700] "GET / HTTP/1.1" 200 3523 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
10.0.1.101 - - [31/Oct/2016:22:56:18 -0700] "GET /favicon.ico HTTP/1.1" 404 499 "-" "Mozilla/5.0 (X11; Ubuntu; Linux armv7l; rv:49.0) Gecko/20100101 Firefox/49.0"

cat error.log

[Mon Oct 31 22:45:47.288264 2016] [wsgi:warn] [pid 982] mod_wsgi: Compiled for Python/2.7.11.
[Mon Oct 31 22:45:47.288623 2016] [wsgi:warn] [pid 982] mod_wsgi: Runtime using Python/2.7.12.
[Mon Oct 31 22:45:47.314433 2016] [mpm_prefork:notice] [pid 982] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Mon Oct 31 22:45:47.314616 2016] [core:notice] [pid 982] AH00094: Command line: '/usr/sbin/apache2'
[Mon Oct 31 22:48:52.171206 2016] [mpm_prefork:notice] [pid 982] AH00169: caught SIGTERM, shutting down
[Mon Oct 31 22:48:53.766824 2016] [wsgi:warn] [pid 2360] mod_wsgi: Compiled for Python/2.7.11.
[Mon Oct 31 22:48:53.766940 2016] [wsgi:warn] [pid 2360] mod_wsgi: Runtime using Python/2.7.12.
[Mon Oct 31 22:48:53.775947 2016] [mpm_prefork:notice] [pid 2360] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Mon Oct 31 22:48:53.776083 2016] [core:notice] [pid 2360] AH00094: Command line: '/usr/sbin/apache2'
tmdag
  • 531
  • 6
  • 17
  • Have you tried putting your virtual host config at the very top of the 000-default.conf? Also use *:80 instead of * in the opening VirtualHost tag. – Apoorv Kansal Nov 01 '16 at 06:13
  • When using mod_wsgi ``app.run()`` is not used. What is the URL you are using? Because you have ``VirtualHost`` and ``ServerName`` of ``dagzserv`` the URL would have to be ``http://dagzerv/``. If you are using an IP address instead of that, it will go through to default ``VirtualHost`` instead. – Graham Dumpleton Nov 01 '16 at 06:33
  • @GrahamDumpleton thx for that app.run() tip, wasn't aware of that. I've tried http://dagzserv/ as well as 10.0.1.101 ip. Both are just showing clean, default apache web (the ubuntu version if that changes anything) – tmdag Nov 01 '16 at 07:18
  • Try using ````. You would usually always have the port. Also add a syntax error to config file to verify that Apache is actually reading it. – Graham Dumpleton Nov 01 '16 at 07:21
  • @ApoorvKansal Haven't tried that yet (didn't want to mess with default apache settings. Overall it sounds wierd to me that you have to make apache config for each single app you make :) – tmdag Nov 01 '16 at 07:22
  • You are not doing it for each app, you are doing it for each unique ``VirtualHost``. If you are using ``ServerName`` for the same host and port elsewhere thinking you need one for each app, then it would use the first it finds. – Graham Dumpleton Nov 01 '16 at 07:23
  • @GrahamDumpleton *:80 - that was there originally. I was just testing if that will make any difference for me - it didn't :( – tmdag Nov 01 '16 at 07:24
  • But what about adding a syntax error? Is the file read for sure. – Graham Dumpleton Nov 01 '16 at 07:25
  • ok - so putting it on top of 000-default.conf DID the trick (yay!) but i guess that is not clean solution, isn't it ? is the 000-default.conf compelatly taking over any other confs ? (and i set back to *:80 as well) – tmdag Nov 01 '16 at 07:35

1 Answers1

7

1. How Apache is finding that "FlaskApp.conf"

There one line in /etc/apache2/apache2.conf to include virtual host configurations:

# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

Apache2 server will load apache2.conf and also include the virtual host configurations.

2. Why Apache is showing its default webpage no matter what.

If you access the server via IP address, Apache will return based on its default site configuration in /etc/sites-available/000-default.conf. So if you want to access your WSGI server via IP address, you should disable the Apache default site configuration first:

sudo a2dissite 000-default.conf
service apache2 restart
Yingbo Cui
  • 259
  • 3
  • 5