I am trying to use Bottle.py and mod_wsgi together but I am getting a strange error that I cannot fix.
When I try to make a curl request ("curl http://localhost/myapi/hello1") I get the error: "ImportError: No module named server". However, if I take out the "import server" line in my app.wsgi file and try "curl http://localhost/myapi/hello2" it works just fine. I have tried adding to the WSGIPythonPath, WSGIDaemonProcess, etc. and nothing has worked. Does anyone see anything I may be doing wrong?
Note: The "/usr/local/www/wsgi-script" path is successfully being added to sys.path, so that isn't the issue. I also do have a init.py file in the "/usr/local/www/wsgi-scripts" directory and the permissions are all 777.
[root#my_vm httpd]# ls -lt /usr/local/www/wsgi-scripts/
total 8
-rwxrwxrwx. 1 root root 95 Mar 21 14:42 server.py
-rwxrwxrwx. 1 root root 318 Mar 21 14:37 app.wsgi
-rwxrwxrwx. 1 root root 0 Mar 20 19:53 __init__.py
Using Versions:
Apache/2.4.6 (Red Hat Enterprise Linux)
Python 2.7.5
Bottle 0.12.9
/usr/local/www/wsgi-scripts/server.py
import bottle
from bottle import route
#route('/hello1')
def hello():
return "Hello World!"
/usr/local/www/wsgi-scripts/app.wsgi
import os
import sys
import bottle
from bottle import route
server_loc = "/usr/local/www/wsgi-scripts"
if not server_loc in sys.path:
sys.path.insert(0, server_loc)
os.chdir(os.path.dirname(__file__))
print(sys.path)
#route('/hello2')
def hello():
return "Hello World!\n"
import server
application = bottle.default_app()
/etc/httpd/conf.d/myapi.conf
<VirtualHost *:80>
ServerName localhost
WSGIScriptAlias /myapi /usr/local/www/wsgi-scripts/app.wsgi
<Directory /usr/local/www/wsgi-scripts>
Require all granted
</Directory>
</VirtualHost>
EDIT: Adding python traceback
/etc/httpd/logs/error_log
[Thu Mar 22 13:39:18.234573 2018] [core:notice] [pid 18172] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Thu Mar 22 13:39:29.628352 2018] [:error] [pid 18174] ['/usr/local/www/wsgi-scripts', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
[Thu Mar 22 13:39:29.628514 2018] [:error] [pid 18174] [client ::1:35838] mod_wsgi (pid=18174): Target WSGI script '/usr/local/www/wsgi-scripts/app.wsgi' cannot be loaded as Python module.
[Thu Mar 22 13:39:29.628525 2018] [:error] [pid 18174] [client ::1:35838] mod_wsgi (pid=18174): Exception occurred processing WSGI script '/usr/local/www/wsgi-scripts/app.wsgi'.
[Thu Mar 22 13:39:29.628539 2018] [:error] [pid 18174] [client ::1:35838] Traceback (most recent call last):
[Thu Mar 22 13:39:29.628556 2018] [:error] [pid 18174] [client ::1:35838] File "/usr/local/www/wsgi-scripts/app.wsgi", line 18, in <module>
[Thu Mar 22 13:39:29.628625 2018] [:error] [pid 18174] [client ::1:35838] import server
[Thu Mar 22 13:39:29.628645 2018] [:error] [pid 18174] [client ::1:35838] ImportError: No module named server
Adding the solution that I found worked for me:
When I moved the server.py into another directory inside the directory (aka /usr/local/www/wsgi-scripts/server.py --> /usr/local/www/wsgi-scripts/inner_dir/server.py), and do "import inner_dir.server" instead of "import server", it started working.
Related
I deploy flask with apache on CentOS7
And I want Oracle database to insert or select when user access.
So I install cx_Oracle
pip install cx_Oracle
And run flask on virtual env.
flask run
It work with no error.
But I access to localhost:80 using apache, It occur Internal Server Error 500.
In error_log
[Fri Oct 23 09:12:34.623473 2020] [mpm_prefork:notice] [pid 475] AH00170: caught SIGWINCH, shutting down gracefully
[Fri Oct 23 09:12:35.678302 2020] [lbmethod_heartbeat:notice] [pid 510] AH02282: No slotmem from mod_heartmonitor
[Fri Oct 23 09:12:35.681622 2020] [mpm_prefork:notice] [pid 510] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
[Fri Oct 23 09:12:35.681665 2020] [core:notice] [pid 510] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Fri Oct 23 09:12:38.074902 2020] [:error] [pid 513] [client 172.17.0.1:33640] mod_wsgi (pid=513): Target WSGI script '/var/www/hitme/wsgi.py' cannot be loaded as Python module.
[Fri Oct 23 09:12:38.074946 2020] [:error] [pid 513] [client 172.17.0.1:33640] mod_wsgi (pid=513): Exception occurred processing WSGI script '/var/www/hitme/wsgi.py'.
[Fri Oct 23 09:12:38.074973 2020] [:error] [pid 513] [client 172.17.0.1:33640] Traceback (most recent call last):
[Fri Oct 23 09:12:38.074994 2020] [:error] [pid 513] [client 172.17.0.1:33640] File "/var/www/hitme/wsgi.py", line 10, in <module>
[Fri Oct 23 09:12:38.075058 2020] [:error] [pid 513] [client 172.17.0.1:33640] from app import app as application
[Fri Oct 23 09:12:38.075070 2020] [:error] [pid 513] [client 172.17.0.1:33640] File "/var/www/hitme/app/__init__.py", line 4, in <module>
[Fri Oct 23 09:12:38.075138 2020] [:error] [pid 513] [client 172.17.0.1:33640] import cx_Oracle
[Fri Oct 23 09:12:38.075162 2020] [:error] [pid 513] [client 172.17.0.1:33640] ImportError: No module named cx_Oracle
I followed this article for deploy flask with apache.
https://dev.to/sm0ke/flask-deploy-with-apache-on-centos-minimal-setup-2kb7
and install cx_Oracle and I copied it where I thought I needed it.
Here my tree
/etc/httpd/modules/
|-cx_Oracle.cpython-36m-x86_64-linux-gnu.so
|-cx_Oracle-8.0.1.dist-info
/var/www/hitme/lib/python3.6/site-packages
|-cx_Oracle.cpython-36m-x86_64-linux-gnu.so
|-cx_Oracle-8.0.1.dist-info
/usr/local/lib/python3.6/site-packages
|-cx_Oracle.cpython-36m-x86_64-linux-gnu.so
|-cx_Oracle-8.0.1.dist-info
But it still gives the error.
Is there any other way?
Ensure that the required Oracle Instant Client is installed to the Apache user by downloading
https://download.oracle.com/otn_software/linux/instantclient/199000/instantclient-basic-linux.x64-19.9.0.0.0dbru.zip
and unzip ut to a folder Apache user owns or is accessible for read and execute - lets say
/apache/oracle
After that, ensure PATH, ORACLE_HOME and LD_LIBRARY_PATH ist set properly as Apache User - i.e.
export ORACLE_HOME=/apache/oracle
export PATH=$PATH:$ORACLE_HOME
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
If you start Apache as service then you may need to set the variables to root or user who starts apache.
Hi Today I started using flask
I m trying to configure two blueprints
my project structure is as image below
Here is all my project codes
init.py
from flask import Flask
app = Flask(__name__)
from mod_image.controllers import mod_image
from mod_home.home import mod_home
#the app config
#app.config.from_object('config')
#declaring image registration module/blueprint
#from app.mod_image.controllers import mod_image as image_module
# Register blueprint(s)
app.register_blueprint(mod_home)
app.register_blueprint(mod_image)
if __name__ == "__main__":
app.run()
controllers.py
from flask import Blueprint
mod_image = Blueprint('mod_image', __name__)
#mod_image.route('/register')
def register():
return "This is an example app"
home.py
from flask import Blueprint
mod_home = Blueprint('mod_home', __name__)
#mod_home.route('/')
def showHome():
return "This is a home"
Here is the error log
[Mon Jul 06 17:24:05.338680 2020] [wsgi:error] [pid 15407] [client ::1:38506] mod_wsgi (pid=15407): Failed to exec Python script file '/var/www/wanasissmarteye/wanasissmarteye.wsgi'.
[Mon Jul 06 17:24:05.338731 2020] [wsgi:error] [pid 15407] [client ::1:38506] mod_wsgi (pid=15407): Exception occurred processing WSGI script '/var/www/wanasissmarteye/wanasissmarteye.wsgi'.
[Mon Jul 06 17:24:05.338764 2020] [wsgi:error] [pid 15407] [client ::1:38506] Traceback (most recent call last):
[Mon Jul 06 17:24:05.338805 2020] [wsgi:error] [pid 15407] [client ::1:38506] File "/var/www/wanasissmarteye/wanasissmarteye.wsgi", line 7, in
[Mon Jul 06 17:24:05.338860 2020] [wsgi:error] [pid 15407] [client ::1:38506] from wanasissmarteye import app as application
[Mon Jul 06 17:24:05.338889 2020] [wsgi:error] [pid 15407] [client ::1:38506] File "/var/www/wanasissmarteye/wanasissmarteye/init.py", line 11, in
[Mon Jul 06 17:24:05.339028 2020] [wsgi:error] [pid 15407] [client ::1:38506] from mod_home.home import mod_home
[Mon Jul 06 17:24:05.339063 2020] [wsgi:error] [pid 15407] [client ::1:38506] ImportError: No module named mod_home.home
An __init__.py file is required for python to recognize your mod_home and mod_image as
modules and import submodules from them.
Add one in the root of each folder you want to import from.
See this post: Importing files from different folder
OK
since I m a newbie to Python. addind init.py fixed the issue
after I googled
The init.py files are required to make Python treat the directories as containing packages;
I a trying to run a "hello world" python application from the server using Apache web server port 80. Here, I have setup python virtual environment compacting with python version 3.8. created virtual host, wsgi file and now trying to call the URL on browser, I am getting 500 internal server error. Apache error log file details provided here.
1. Ubuntu 18.04,
2. Python 3.8,
3. Apache 2.4
Virtual host configuration
#WSGIDaemonProcess MyApp python-home=/usr/local/env/myApp/venv threads=5i
WSGIDaemonProcess Myapp user=www-data group=www-data python-home=/usr/local/env/myApp/venv python-path=/usr/local/env/myApp threads=5
WSGIScriptAlias /project_wsgi /usr/local/env/myApp/myapp.wsgi
#WSGIProcessGroup myApp
WSGIApplicationGroup Myapp
#WSGIImportScript %{GLOBAL}
#WSGIPythonHome /usr/local/env/myApp/venv
<VirtualHost *:80>
<Directory /usr/local/env/myApp>
Require all granted
Order allow,deny
Allow from all
LogLevel info
</Directory>
</VirtualHost>
WSGI file
import os
import sys
sys.path.insert(0, '/usr/local/env/myApp/')
python_home = '/usr/local/env/myApp/venv'
#activate_this = '/usr/local/env/myApp/venv/bin/activate_this.py'
#with open(activate_this) as file_:
# exec(file_.read(), dict(__file__=activate_this))
from app.myApp import app as application
apache error logs
[Fri Jan 31 16:00:28.990978 2020] [wsgi:info] [pid 58589:tid 140712013437888] mod_wsgi (pid=58589): Starting process 'Myapp' with uid=33, gid=33 and threads=5.
[Fri Jan 31 16:00:28.991819 2020] [wsgi:info] [pid 58589:tid 140712013437888] mod_wsgi (pid=58589): Python home /usr/local/env/myApp/venv.
[Fri Jan 31 16:00:28.991863 2020] [wsgi:info] [pid 58589:tid 140712013437888] mod_wsgi (pid=58589): Initializing Python.
[Fri Jan 31 16:00:28.991926 2020] [mpm_event:notice] [pid 102854:tid 140712013437888] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Fri Jan 31 16:00:28.991937 2020] [mpm_event:info] [pid 102854:tid 140712013437888] AH00490: Server built: 2019-09-16T12:58:48
[Fri Jan 31 16:00:28.991952 2020] [core:notice] [pid 102854:tid 140712013437888] AH00094: Command line: '/usr/sbin/apache2'
[Fri Jan 31 16:00:28.993170 2020] [wsgi:info] [pid 58591:tid 140712013437888] mod_wsgi (pid=58591): Initializing Python.
[Fri Jan 31 16:00:28.993874 2020] [wsgi:info] [pid 58590:tid 140712013437888] mod_wsgi (pid=58590): Initializing Python.
[Fri Jan 31 16:00:29.061111 2020] [wsgi:info] [pid 58589:tid 140712013437888] mod_wsgi (pid=58589): Attach interpreter ''.
[Fri Jan 31 16:00:29.088271 2020] [wsgi:info] [pid 58589:tid 140712013437888] mod_wsgi (pid=58589): Adding '/usr/local/env/myApp' to path.
[Fri Jan 31 16:00:29.089075 2020] [wsgi:info] [pid 58591:tid 140712013437888] mod_wsgi (pid=58591): Attach interpreter ''.
[Fri Jan 31 16:00:29.089966 2020] [wsgi:info] [pid 58590:tid 140712013437888] mod_wsgi (pid=58590): Attach interpreter ''.
[Fri Jan 31 16:02:22.375687 2020] [wsgi:info] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] mod_wsgi (pid=58591, process='', application='Myapp'): Loading WSGI script '/usr/local/env/myApp/myapp.wsgi'.
[Fri Jan 31 16:02:22.381101 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] mod_wsgi (pid=58591): Target WSGI script '/usr/local/env/myApp/myapp.wsgi' cannot be loaded as Python module.
[Fri Jan 31 16:02:22.381132 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] mod_wsgi (pid=58591): Exception occurred processing WSGI script '/usr/local/env/myApp/myapp.wsgi'.
[Fri Jan 31 16:02:22.381374 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] Traceback (most recent call last):
[Fri Jan 31 16:02:22.381400 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] File "/usr/local/env/myApp/myapp.wsgi", line 10, in <module>
[Fri Jan 31 16:02:22.381405 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] from app.myApp import app as application
[Fri Jan 31 16:02:22.381411 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] File "/usr/local/env/myApp/app/myApp.py", line 1, in <module>
[Fri Jan 31 16:02:22.381414 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] from flask import Flask
[Fri Jan 31 16:02:22.381436 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] ModuleNotFoundError: No module named 'flask'
I have installed flask on the virtual environment and trying to activate from the WSGI file execution. However, this is not working for me.
FYI : The commented lines on virtual host is already I have tried.
Tried multiple solutions online but couldnt get it working !
Basically my application is deployed but http response is "Internal Server Error".
On investigating EB logs i found the following traceback.
[Sat Jun 08 10:28:56.880823 2019] [:error] [pid 4776] [remote 172.31.4.251:180] Traceback (most recent call last):
[Sat Jun 08 10:28:56.880846 2019] [:error] [pid 4776] [remote 172.31.4.251:180] File "/opt/python/current/app/modelforms/modelforms/wsgi.py", line 12, in <module>
[Sat Jun 08 10:28:56.880863 2019] [:error] [pid 4776] [remote 172.31.4.251:180] from django.core.wsgi import get_wsgi_application
[Sat Jun 08 10:28:56.880878 2019] [:error] [pid 4776] [remote 172.31.4.251:180] ModuleNotFoundError: No module named 'django'
[Sat Jun 08 10:29:10.828039 2019] [:error] [pid 4776] [remote 172.31.42.54:180] mod_wsgi (pid=4776): Target WSGI script '/opt/python/current/app/modelforms/modelforms/wsgi.py' cannot be loaded as Python module.
[Sat Jun 08 10:29:10.828089 2019] [:error] [pid 4776] [remote 172.31.42.54:180] mod_wsgi (pid=4776): Exception occurred processing WSGI script '/opt/python/current/app/modelforms/modelforms/wsgi.py'.
My requirements.txt is placed in all directories and yet django module is not recognised. It seems it hasn't been installed at first place.
The file's contents are:
Django==2.2.1
Pillow==6.0.0
pytz==2019.1
sorl-thumbnail==12.5.0
sqlparse==0.3.0
Any help is appreciated.
EDIT1:
I dont have ebextentions setup, however the same config's are present when i run " eb config"
aws:elasticbeanstalk:container:python:
NumProcesses: '1'
NumThreads: '15'
StaticFiles: /static/=static/
WSGIPath: modelforms/modelforms/wsgi.py
aws:elasticbeanstalk:container:python:staticfiles:
/static/: static/
aws:elasticbeanstalk:environment:
EnvironmentType: LoadBalanced
ExternalExtensionsS3Bucket: null
ExternalExtensionsS3Key: null
LoadBalancerType: application
ServiceRole: aws-elasticbeanstalk-service-role
Try adding your DJANGO_SETTINGS_MODULE and PYTHONPATH to the config
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "modelforms.settings"
"PYTHONPATH": "/opt/python/current/app/modelforms:$PYTHONPATH"
Also, check the Health dashboard and make sure it shows as deployed before testing
Apache 2.4.10 and mod-wsgi 4.4 environments.
Below is apache error log file :
[Wed Nov 02 11:15:21.360055 2016] [wsgi:error] [pid 3729:tid 140033629607680] [remote 10.10.200.99:7425] mod_wsgi (pid=3729): Target WSGI script '/home/service/was/test/test_app.wsgi' cannot be loaded as Python module.
[Wed Nov 02 11:15:21.360060 2016] [wsgi:error] [pid 3729:tid 140033629607680] [remote 10.10.200.99:7425] mod_wsgi (pid=3729): Exception occurred processing WSGI script '/home/service/was/test/test_app.wsgi'.
[Wed Nov 02 11:15:21.360067 2016] [wsgi:error] [pid 3729:tid 140033629607680] [remote 10.10.200.99:7425] Traceback (most recent call last):
[Wed Nov 02 11:15:21.360075 2016] [wsgi:error] [pid 3729:tid 140033629607680] [remote 10.10.200.99:7425] File "/home/service/was/test/test_app.wsgi", line 6, in <module>
[Wed Nov 02 11:15:21.360119 2016] [wsgi:error] [pid 3729:tid 140033629607680] [remote 10.10.200.99:7425] from test_app import app as application
[Wed Nov 02 11:15:21.360125 2016] [wsgi:error] [pid 3729:tid 140033629607680] [remote 10.10.200.99:7425] File "/home/service/was/test/test_app.py", line 3, in <module>
[Wed Nov 02 11:15:21.360141 2016] [wsgi:error] [pid 3729:tid 140033629607680] [remote 10.10.200.99:7425] from flask import Flask
[Wed Nov 02 11:15:21.360158 2016] [wsgi:error] [pid 3729:tid 140033629607680] [remote 10.10.200.99:7425] ImportError: No module named flask
And I set httpd-virtualhost.conf:
Listen 10003
<VirtualHost *:10003>
# mod_wsgi global settings
WSGIApplicationGroup %{RESOURCE}
ErrorLog "|/usr/local/server/apache/bin/rotatelogs /home/log/apache/error-%Y-%m-%d.log 86400 +540"
TransferLog "|/usr/local/server/apache/bin/rotatelogs /home/log/apache/checkout/access-%Y-%m-%d.log 86400 +540"
#mod-wsgi : test
WSGIScriptAlias /test /home/service/was/test/test_app.wsgi
WSGIDaemonProcess test_app processes=4 threads=4 display-name=%{GROUP}
<Directory /home/service/was/test/ >
WSGIProcessGroup test_app
Require all granted
</Directory>
</VirtualHost>
And my wsgi script, test_App.wsgi and test_app.py:
#test_app.wsgi
import os, sys
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
sys.path.insert(0, '/home/service/was/test/')
print os
print sys
from test_app import app as application
In this setting, I run test_app.py in my dev-server.
Why generate 'Target WSGI script '/home/service/was/test/test_app.wsgi' cannot be loaded as Python module.' message?
Why do not import python module installed by pip?