1

I am trying to run a flask based REST api through Apache mod wsgi.

I am developing the project in virtual environment. So the project structure looks like the following:

home
  user
    python_projects
        project_name
            venv
               project_folders 
                   api_folder
                   other_source_code_folders

The api folder actually hosts a flask application. This folder looks like:

api_folder
    flask_app.wsgi
    __init__.py
    my_flask.py

The flask_app.wsgi source code is:

activate_this = '/home/user/python_projects/project_name/venv/bin'
execfile(activate_this, dict(__file__=activate_this))

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/home/user/python_projects/project_name/venv/project_folders/api_folder/")

from api_folder import app as application

I have created the following file:

/etc/apache2/sites-available/MyApp.conf

MyApp.conf looks like:

<VirtualHost *>
    ServerName my-project.dev

    WSGIScriptAlias / /home/user/python_projects/project_name/venv/project_folders/api_folder/flask_app.wsgi

    <Directory /home/user/python_projects/project_name/venv/project_folders/api_folder/>
        Order deny,allow
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I have also inserted the following in my host file:

127.0.0.1 my-project.dev

I ran the commands also:

sudo a2ensite MyApp
sudo service apache2 reload
sudo service apache2 restart

After all these steps, when I open my-project.dev, the apache homepage only opens up. I don't think my flask app is running properly.

The apache error log also doesn't say any error:

[Sat Nov 05 17:05:39.380213 2016] [core:notice] [pid 14656] AH00094: Command line: '/usr/sbin/apache2'
[Sat Nov 05 17:11:55.648767 2016] [mpm_prefork:notice] [pid 14656] AH00169: caught SIGTERM, shutting down
[Sat Nov 05 17:11:56.761456 2016] [wsgi:warn] [pid 25785] mod_wsgi: Compiled for Python/2.7.11.
[Sat Nov 05 17:11:56.761553 2016] [wsgi:warn] [pid 25785] mod_wsgi: Runtime using Python/2.7.12.
[Sat Nov 05 17:11:56.764083 2016] [mpm_prefork:notice] [pid 25785] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Sat Nov 05 17:11:56.764153 2016] [core:notice] [pid 25785] AH00094: Command line: '/usr/sbin/apache2'
[Sat Nov 05 17:15:25.337951 2016] [mpm_prefork:notice] [pid 25785] AH00171: Graceful restart requested, doing restart
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
[Sat Nov 05 17:15:25.389644 2016] [wsgi:warn] [pid 25785] mod_wsgi: Compiled for Python/2.7.11.
[Sat Nov 05 17:15:25.389737 2016] [wsgi:warn] [pid 25785] mod_wsgi: Runtime using Python/2.7.12.
[Sat Nov 05 17:15:25.389860 2016] [mpm_prefork:notice] [pid 25785] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations

Please let me know where I am going wrong.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
coderx
  • 509
  • 6
  • 22
  • Have you verified that ``MyApp.conf`` now appears in the ``sites-enabled`` directory as a symlink to the same file in ``sites-available``? Do you have any other ``VirtualHost`` entries using the same value of ``ServerName``? Have you tried using the preferred ``*:80`` in the ``VirtualHost`` instead of just ``*``. – Graham Dumpleton Nov 05 '16 at 19:30
  • Yes, I have already tried everything. MyApp.conf is available in the sites-enabled directory symlinking to the same in sites-available dir. I don't have any more at all as VirtualHost. – coderx Nov 05 '16 at 20:36
  • Add a syntax error, line with ``xxx`` by itself, in the site file and restart Apache. Does Apache fail to start? This will confirm that your site file is in fact being read. – Graham Dumpleton Nov 05 '16 at 22:44
  • Hi, I am in same boat. Did you find any solution please? – Gauthier Buttez Apr 02 '21 at 10:53

0 Answers0