1
votes

I want to deploy a Flask app using Python 3. I am running Ubuntu 16.04, Apache2.

I ran sudo apt-get install libapache2-mod-wsgi-py3 to install wsgi.

I followed the instruction here. I have a Flask App on my Linode server at /var/www/html/hxueh.net/finance. Inside the finance folder there is one file and one folder. The structure looks like this.

|--------finance
|----------------finance
|-----------------------static
|-----------------------templates
|-----------------------venv
|-----------------------application.py
|----------------finance.wsgi

In venv/bin:

activate activate_this.py flask pip3.5 python3.5
activate.csh easy_install pip python python-config
activate.fish easy_install-3.5 pip3 python3 wheel

The finance.wsgi is:

#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/html/hxueh.net/finance/")

from finance import app as application

And my Apache2 config is:

<VirtualHost *:80>
        ServerName finance.hxueh.net
        ServerAdmin [email protected]
        WSGIScriptAlias / /var/www/html/hxueh.net/finance/finance.wsgi
        <Directory /var/www/html/hxueh.net/finance/finance/>
                Order allow,deny
                Allow from all
        </Directory>
        <Directory /var/www/html/hxueh.net/finance>
                WSGIProcessGroup finance
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>
        Alias /static /var/www/html/hxueh.net/finance/finance/static
        <Directory /var/www/html/hxueh.net/finance/finance/static/>
                Order allow,deny
                Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I have a Wordpress app running on the same server, with Certbot enabling Let's encrypt certificate.

When I visit my server it return Error 500. In error.log it shows:

[Sun Jan 21 10:40:56.310304 2018] [mpm_prefork:notice] [pid 26281] AH00169: caught SIGTERM, shutting down
[Sun Jan 21 10:41:21.236671 2018] [ssl:warn] [pid 26747] AH01909: hxueh.net:443:0 server certificate does NOT include an ID which matches the server name
[Sun Jan 21 10:41:21.276195 2018] [ssl:warn] [pid 26748] AH01909: hxueh.net:443:0 server certificate does NOT include an ID which matches the server name
[Sun Jan 21 10:41:21.276370 2018] [wsgi:warn] [pid 26748] mod_wsgi: Compiled for Python/3.5.1+.
[Sun Jan 21 10:41:21.276378 2018] [wsgi:warn] [pid 26748] mod_wsgi: Runtime using Python/3.5.2.
[Sun Jan 21 10:41:21.278888 2018] [mpm_prefork:notice] [pid 26748] AH00163: Apache/2.4.18 (Ubuntu) OpenSSL/1.0.2g mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Sun Jan 21 10:41:21.278910 2018] [core:notice] [pid 26748] AH00094: Command line: '/usr/sbin/apache2'
[Sun Jan 21 10:44:02.826408 2018] [wsgi:error] [pid 26751] [client xxx.xxx.xxx.xxx:xxx] No WSGI daemon process called 'finance' has been configured: /var/www/html/hxueh.net/finance/finance.wsgi

UPDATE: Problem Solved.

I rewrite the structure and put wsgi file inside the project.

|--------Finance
|----------------static
|----------------templates
|----------------venv
|----------------application.py
|----------------finance.wsgi

Also I rewrite the Apache 2 file. I disable WSGIProcessGroup since I don't need it.

<VirtualHost *:80>
        ServerName finance.hxueh.net
        ServerAdmin [email protected]
        WSGIScriptAlias / /var/www/html/hxueh.net/Finance/finance.wsgi
        <Directory /var/www/html/hxueh.net/Finance/>
                Order allow,deny
                Allow from all
        </Directory>
        <Directory /var/www/html/hxueh.net/Finance>
                Order deny,allow
                Allow from all
        </Directory>
        Alias /static /var/www/html/hxueh.net/Finance/static
        <Directory /var/www/html/hxueh.net/Finance/static/>
                Order allow,deny
                Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Make sure to enable mod_wsgi in /etc/apache2/mods-available/mod.load. Just run mod_wsgi-express module-config and put the output inside. Then run sudo a2enmod wsgi and sudo service apache2 restart.

My finance.wsgi is:

#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/html/hxueh.net/Finance/")

from application import app as application

Finally, thanks Graham Dumpleton for helping me to deploying my first web app.

1

1 Answers

1
votes

You have:

WSGIProcessGroup finance

to tell mod_wsgi to send requests to WSGI application running in a daemon process group but haven't configured the daemon process group.

Add before the WSGIScriptAlias directive:

WSGIDaemonProcess finance

Also read:

to learn more about daemon process groups.

BTW, version of mod_wsgi 4.3.0 is very old. You should avoid using system provided packages for mod_wsgi on Debian/Ubuntu as they are usually hopelessly out of date and are also unsupported. Suggest you uninstall the system package and install from source code yourself using pip install method. See: