1

I have written REST APIs using python flask and now I want to run them using apache to put them into production and getting errors.

Here is the structure:

/opt/myapp/venv/api/app-code.py

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

/var/www/html/myapp/myapp.wsgi

import sys
sys.path.insert(0, "/opt/myapp/venv/api")
from app-code import app
application = app

/etc/httpd/conf.d/ssl.conf

<VirtualHost *:443>
    ServerName hostname.com
    DocumentRoot "/var/www/html/myapp"
    SSLEngine on

    WSGIDaemonProcess myapp  python-path=/opt/myapp/venv/lib/python3.6/site-packages/ threads=5

    <Directory "/var/www/html/myapp">
            Order allow,deny
            Allow from all
    </Directory>
    WSGIScriptAlias / /var/www/html/myapp/myapp.wsgi

    ErrorLog /var/log/httpd/ssl_error_log
    TransferLog /var/log/httpd/ssl_access_log
    LogLevel debug

    SSLCertificateFile /opt/ssl-certs/ssl-cert.pem
    SSLCertificateKeyFile /opt/ssl-certs/ssl-key.pem
    SSLCertificateChainFile /opt/ssl-certs/ssl-certchain.pem
</VirtualHost>

when I am making API calls, getting below error:

[Sun Jan 14 03:22:50 2018] [info] Initial (No.1) HTTPS request received for child 2 (server hostname.com:443)
[Sun Jan 14 03:22:50 2018] [info] [client 103.3.43.163] mod_wsgi (pid=24539, process='', application='hostname.com|'): Loading WSGI script '/var/www/html/myapp/myapp.wsgi'.
[Sun Jan 14 03:22:50 2018] [error] [client 103.3.43.163] mod_wsgi (pid=24539): Target WSGI script '/var/www/html/myapp/myapp.wsgi' cannot be loaded as Python module.
[Sun Jan 14 03:22:50 2018] [error] [client 103.3.43.163] mod_wsgi (pid=24539): Exception occurred processing WSGI script '/var/www/html/myapp/myapp.wsgi'.
[Sun Jan 14 03:22:50 2018] [error] [client 103.3.43.163] Traceback (most recent call last):
[Sun Jan 14 03:22:50 2018] [error] [client 103.3.43.163]   File "/var/www/html/myapp/myapp.wsgi", line 8, in <module>
[Sun Jan 14 03:22:50 2018] [error] [client 103.3.43.163]     from app-code import app
[Sun Jan 14 03:22:50 2018] [error] [client 103.3.43.163]   File "/opt/myapp/venv/api/app-code.py", line 3, in <module>
[Sun Jan 14 03:22:50 2018] [error] [client 103.3.43.163]     from flask import Flask, abort, request, jsonify, g, url_for
[Sun Jan 14 03:22:50 2018] [error] [client 103.3.43.163] ImportError: No module named flask
[Sun Jan 14 03:22:50 2018] [debug] ssl_engine_kernel.c(1894): OpenSSL: Write: SSL negotiation finished successfully

I have gone through many posts and tried different options but couldn't get it running.

2 Answers2

0

The critical thing here is the ImportError. Either flask isn't installed in your virtual environment or apache isn't handling the venv correctly. I think you may want to use python-home rather than python-path in your WSGIDaemonProcess directive. The [http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html](mod_wsgi documentation for virtual environments) should help.

Nathan Vērzemnieks
  • 5,495
  • 1
  • 11
  • 23
0

after giving it some more try, I was able to get it working.

I was using incorrect wsgi module as mod_wsgi.so, after changing it to mod_wsgi_python3.6.so, everything started working.