0

Overview: I am trying to have a simple Python bottle application to be available as a backend to a chatbot which sends json response back when called. I have chosen the solution Apache2 + mod WSGI(4.8.0) and Python3.6 after doing some research.

Issue: When accessed the python app through browser I get 500 Internal server error. Below is the module not found error from Apache2 logs

Error in Apache2 logs:

[Wed Jun 16 17:38:56.152523 2021] [wsgi:error] [pid 8878:tid 140503309027072] [remote 10.137.190.184:51146] mod_wsgi (pid=8878): Failed to exec Python script file '/opt/software/apache/httpd-webserver/httpd/wsgi-scripts/myapp.wsgi'. [Wed Jun 16 17:38:56.153460 2021] [wsgi:error] [pid 8878:tid 140503309027072] [remote 10.137.190.184:51146] mod_wsgi (pid=8878): Exception occurred processing WSGI script '/opt/software/apache/httpd-webserver/httpd/wsgi-scripts/myapp.wsgi'. [Wed Jun 16 17:38:56.154245 2021] [wsgi:error] [pid 8878:tid 140503309027072] [remote 10.137.190.184:51146] Traceback (most recent call last): [Wed Jun 16 17:38:56.155012 2021] [wsgi:error] [pid 8878:tid 140503309027072] [remote 10.137.190.184:51146] File "/opt/software/apache/httpd-webserver/httpd/wsgi-scripts/myapp.wsgi", line 6, in [Wed Jun 16 17:38:56.155605 2021] [wsgi:error] [pid 8878:tid 140503309027072] [remote 10.137.190.184:51146] import bottle [Wed Jun 16 17:38:56.156093 2021] [wsgi:error] [pid 8878:tid 140503309027072] [remote 10.137.190.184:51146] ModuleNotFoundError: No module named 'bottle'

Current Apache,modWSGI and Python system settings: Apache version:2.4.41 Mod WSGi version: 4.8.0 Python version : System has 2 versions, python2.7 and python3.6

I intended to use python3.6 so compiled modwsgi with python3 as below. I have another sandbox with similar config and did not explicitly mention python3.6 as below which made WSGI compile against python2.7 and the my bottle python script works fine there.

sudo ./configure --with-apxs=/opt/software/apache/httpd-webserver/httpd/bin/apxs --with-python=/usr/bin/python3.6

httpd.conf setting related to wsgi: (using WSGIDAEMON mode)

WSGIDaemonProcess wsgi-scripts user=webadm group=webgrp processes=1 threads=5
WSGIProcessGroup wsgi-scripts
WSGIScriptAlias /myapp "/opt/software/apache/httpd-webserver/httpd/wsgi-scripts/myapp.wsgi"
<Directory /opt/software/apache/httpd-webserver/httpd/wsgi-scripts>
<IfVersion < 2.4>
  WSGIProcessGroup wsgi-scripts
  WSGIApplicationGroup %{GLOBAL}
  Order allow,deny
  Allow from all
</IfVersion>
<IfVersion >= 2.4>
  WSGIProcessGroup wsgi-scripts
  WSGIApplicationGroup %{GLOBAL}
  Require all granted
</IfVersion>
</Directory>

Apache2 logs show that Apache is able to load the required versions of python and modwsgi

[Wed Jun 16 17:57:51.974604 2021] [mpm_worker:notice] [pid 10040:tid 140640770721664] AH00292: Apache/2.4.41 (Unix) OpenSSL/1.0.2k-fips mod_wsgi/4.8.0 Python/3.6 configured -- resuming normal operations

However when the script is accessed through UI, I see the above bottle module not found error in apache logs. Bottle module is available in both 2.7 and 3.6 versions of python on the system.

Also, Verified by running the below test WSGI script to confirm that Apache is using python3.6

#!/bin/python3
import sys

def application(environ, start_response):
  status = '200 OK'

  output = u''
  output += u'sys.version = %s\n' % repr(sys.version)
  output += u'sys.prefix = %s\n' % repr(sys.prefix)
  output += u'sys.path = %s' % repr(sys.path)
  response_headers = [('Content-type', 'text/plain'),
                    ('Content-Length', str(len(output)))]
  start_response(status, response_headers)
  return [output.encode('UTF-8')]

Output of the test WSGI script: sys.version = '3.6.8 sys.prefix = '/usr' sys.path = ['/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/local/lib64/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']

Looks like Apache/python is unable to load the required module during runtime, however not able to figure out how to enable python/apache to find the required module. I ran through several posts and tried few things which did not work out. Any help would be greatly appreciated. thank you!

Below is my bottle and wsgi code: hello.py:

#!/bin/python3
from bottle import route, run, template,error
import json

headers = {
'Accept': 'application/json',
}

environment = 'qa'
def verification(user):
 user = "profile of user " + user + " for environment " + environment + " looks good"
 ops=[{'fname':'john','lastname':'Doe','Remark':'updated'}]
 return json.dumps(ops)


@route('/troubleshoot/<user>')
def troubleshoot(user):
 return verification(user)
 ops=[{'fname':'harsha','lastname':'sri','Remark':'updated'}]


@error(404)
def error404(error):
 return '404, Please use the right context /troubleshoot/<user>'

myapp.wsgi:

#!/bin/python3
import sys

sys.path.insert(0, "/opt/software/apache/httpd-webserver/httpd/wsgi-scripts")
import bottle
import hello
application = bottle.default_app()

1 Answer 1

0

First thing I noticed was you need to add your page to the bottle.app() I am not sure however if the .wsgi file is supposed to be in Python?

hello.py:

helloRoute = Bottle()

myapp.wsgi:

from hello import helloRoute
application.merge(helloRoute)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.