wsgi.py:
import os,sys
sys.path.append('/var/www/html/yuyyu/yuyyu')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yuyyu.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
apache.conf:
...
WSGIDaemonProcess yuyyu python-path=/var/www/html/yuyyu
WSGIProcessGroup yuyyu
WSGIScriptAlias / /var/www/html/yuyyu/yuyyu/wsgi.py \
process-group=yuyyu application-group=%{GLOBAL}
...
here is my error in error.log:
[Fri Dec 02 19:07:55.862266 2016] [:error] [pid 1414] [remote 213.14.244.3:2784] mod_wsgi (pid=1414): Target WSGI script '/var/www/html/yuyyu/yuyyu/wsgi.py' cannot be loaded as Python module.
[Fri Dec 02 19:07:55.862323 2016] [:error] [pid 1414] [remote 213.14.244.3:2784] mod_wsgi (pid=1414): Exception occurred processing WSGI script '/var/www/html/yuyyu/yuyyu/wsgi.py'.
[Fri Dec 02 19:07:55.862353 2016] [:error] [pid 1414] [remote 213.14.244.3:2784] Traceback (most recent call last):
[Fri Dec 02 19:07:55.862393 2016] [:error] [pid 1414] [remote 213.14.244.3:2784] File "/var/www/html/yuyyu/yuyyu/wsgi.py", line 23, in <module>
[Fri Dec 02 19:07:55.862398 2016] [:error] [pid 1414] [remote 213.14.244.3:2784] from django.core.wsgi import get_wsgi_application
[Fri Dec 02 19:07:55.862418 2016] [:error] [pid 1414] [remote 213.14.244.3:2784] ImportError: No module named 'django'
my project path is /var/www/html/yuyyu/
my wsgi file path is /var/www/html/yuyyu/yuyyu/
im get also 500 Internel Server Error and
im not using virtualenv so what can i do for solve this problem?
WSGIDaemonProcess yuyyu python-path=/var/www/html/yuyyu
I think that your problem is here, the python-path shouldn't be the path to your django project but to your python libraries (for instance the path to your python's site-packages directory where django is installed).
For me, for instance, this path is /usr/lib/python3.5/site-packages.
In wsgi.py try to load the path where you have Django. In my configuration it is located inside of virtualenv and the path looks like that:
import site
site.addsitedir('~/virtualenvs/myenv/lib/python3.5/site-packages')
here is my solition my django is installed to
/usr/lib/python2.7/dist-packages
and i change my .conf file like this
WSGIDaemonProcess yuyyu python-path=/usr/local/lib/python2.7/dist-packages
WSGIProcessGroup yuyyu
WSGIScriptAlias / /var/www/html/yuyyu/yuyyu/wsgi.py
Related
I'm experimenting to publish my django project with Apache mod_wsgi. I created a simple django3 project named sampleapp in virtualenv. Then, I configured my /etc/apache2/sites-enabled/000-default.conf file as follows
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIDaemonProcess sampleapp python-home=/var/www/html/sampleapp/env python-path=/var/www/html/sampleapp:/var/www/html/sampleapp/env/lib/python3.7/site-packages
WSGIProcessGroup sampleapp
WSGIScriptAlias /sampleapp /var/www/html/sampleapp/sampleapp/wsgi.py process-group=sampleapp
</VirtualHost>
Although my virtualenv's python version is 3.7, mod_wsgi is used system's python version(3.5), so it doesn't see my virtualenv and doesn't use its python version. My apache error log as follows:
[Sat Feb 20 15:36:02.390188 2021] [mpm_prefork:notice] [pid 20090] AH00169: caught SIGTERM, shutting down
[Sat Feb 20 15:36:03.226065 2021] [mpm_prefork:notice] [pid 20204] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Sat Feb 20 15:36:03.226278 2021] [core:notice] [pid 20204] AH00094: Command line: '/usr/sbin/apache2'
[Sat Feb 20 15:36:03.294321 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Sat Feb 20 15:36:03.295568 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '/var/www/html/sampleapp/env/lib/python3.7/site-packages'.
[Sat Feb 20 15:36:07.084368 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Sat Feb 20 15:36:07.084455 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '/var/www/html/sampleapp/env/lib/python3.7/site-packages'.
[Sat Feb 20 15:36:07.085660 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] mod_wsgi (pid=20207): Target WSGI script '/var/www/html/sampleapp/sampleapp/wsgi.py' cannot be loaded as Python module.
[Sat Feb 20 15:36:07.085769 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] mod_wsgi (pid=20207): Exception occurred processing WSGI script '/var/www/html/sampleapp/sampleapp/wsgi.py'.
[Sat Feb 20 15:36:07.086188 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] Traceback (most recent call last):
[Sat Feb 20 15:36:07.086266 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] File "/var/www/html/sampleapp/sampleapp/wsgi.py", line 12, in <module>
[Sat Feb 20 15:36:07.086280 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] from django.core.wsgi import get_wsgi_application
[Sat Feb 20 15:36:07.086321 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] ImportError: No module named 'django'
How do I configure mod_wsgi to use the python version of my venv? Or where is my fault? Thanks for the answers.
So the mod_wsgi is compiled to a specific version of Python for Apache. You cannot make it run a different version of python. You can either rebuild that mod_wsgi to the correct version, or change the virtual environment. This deletes virtualenv wrapper but keeps project files.
I think that "changing the virtualenv" would be easier.
rmvirtualenv env-name
mkvirtualenv -p python3.5 env-name
setvirtualenvproject env-name
When you use 'mkvirtualenv' use the -r tag for a requirements.txt
Or rebuild the mod_wsgi:
Documentation
I'm trying to run a simple Flask web app that displays a pygal graph. When I run it with the python development server, the application runs fine. However, when I try to run it in apache2, I get the following error:
[Thu Dec 10 19:55:22.745469 2020] [wsgi:error] [pid 17741] [client ::1:34366] mod_wsgi (pid=17741): Failed to exec Python script file '/var/www/FlaskApp/FlaskApp.wsgi'.
[Thu Dec 10 19:55:22.745521 2020] [wsgi:error] [pid 17741] [client ::1:34366] mod_wsgi (pid=17741): Exception occurred processing WSGI script '/var/www/FlaskApp/FlaskApp.wsgi'.
[Thu Dec 10 19:55:22.745553 2020] [wsgi:error] [pid 17741] [client ::1:34366] Traceback (most recent call last):
[Thu Dec 10 19:55:22.745586 2020] [wsgi:error] [pid 17741] [client ::1:34366] File "/var/www/FlaskApp/FlaskApp.wsgi", line 8, in <module>
[Thu Dec 10 19:55:22.745650 2020] [wsgi:error] [pid 17741] [client ::1:34366] from FlaskApp import app as application
[Thu Dec 10 19:55:22.745665 2020] [wsgi:error] [pid 17741] [client ::1:34366] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 2, in <module>
[Thu Dec 10 19:55:22.745707 2020] [wsgi:error] [pid 17741] [client ::1:34366] import pygal
[Thu Dec 10 19:55:22.745723 2020] [wsgi:error] [pid 17741] [client ::1:34366] File "/var/www/FlaskApp/FlaskApp/venv/lib/python3.8/site-packages/pygal/__init__.py", line 28, in <module>
[Thu Dec 10 19:55:22.745786 2020] [wsgi:error] [pid 17741] [client ::1:34366] import pkg_resources
[Thu Dec 10 19:55:22.745838 2020] [wsgi:error] [pid 17741] [client ::1:34366] File "/var/www/FlaskApp/FlaskApp/venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1365
[Thu Dec 10 19:55:22.745850 2020] [wsgi:error] [pid 17741] [client ::1:34366] raise SyntaxError(e) from e
[Thu Dec 10 19:55:22.745857 2020] [wsgi:error] [pid 17741] [client ::1:34366] ^
[Thu Dec 10 19:55:22.745864 2020] [wsgi:error] [pid 17741] [client ::1:34366] SyntaxError: invalid syntax
Here is the code that I'm running
# -*- coding: utf-8 -*-
import pygal
from flask import Flask, Response
app = Flask(__name__)
#app.route('/')
def index():
""" render svg on html """
return """
<html>
<body>
<h1>hello pygal</h1>
<figure>
<embed type="image/svg+xml" src="/graph/" />
</figure>
</body>
</html>'
"""
#app.route('/graph/')
def graph():
""" render svg graph """
bar_chart = pygal.Bar()
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
return Response(response=bar_chart.render(), content_type='image/svg+xml')
if __name__ == '__main__':
app.run()
(Note that this code is just a sample copied from github)
Here is my apache site configuration:
<VirtualHost *:443>
ServerName flaskapp.localhost
ServerAdmin me#email.com
WSGIScriptAlias / /var/www/FlaskApp/FlaskApp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
DocumentRoot /var/www/FlaskApp
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
ErrorLog ${APACHE_LOG_DIR}/FlaskApp-error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/FlaskApp-access.log combined
AddDefaultCharset utf-8
</VirtualHost>
Here is my wsgi file:
python_home = '/var/www/FlaskApp/FlaskApp/venv'
activate_this = python_home + '/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
sys.path.insert(0,"/var/www/FlaskApp/FlaskApp/venv/lib/python3.8/site-packages/")
from FlaskApp import app as application
application.secret_key = 'secret key stuff'
Here are some details about my environment:
Ubuntu 20.04.1 LTS
Apache/2.4.41 (Ubuntu)
Python 3.8.5 (installed in a virtual environment)
Any thoughts would be greatly appreciated!
It appears that the python interpreter is compiled into the mod_wsgi.so file. An old version, that included python2, was being pointed to by Apache. To fix it I did the following:
Removed the old version of mod_wsgi by doing sudo apt-get remove libapache2-mod-wsgi
modified /etc/apache2/mods-available/mod_wsgi.load to point to the library (.so) file in my virtual environment. The contents of mod_wsgi looked like this:
LoadModule wsgi_module /var/www/FlaskApp/FlaskApp/venv/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so
I suspect that modifying it here points all Apache virtual hosts to this python virtual environment, which isn't really ideal. It would be better if each Apache virtual host pointed to its own python environment.
Modified my wsgi file to work with the new version of python. Here are the contents:
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
sys.path.insert(0,"/var/www/FlaskApp/FlaskApp/venv/lib/python3.8/site-packages/")
from FlaskApp import app as application
application.secret_key = 'secret key stuff'
Changed the local imports in my __init__.py file to be preceeded by a dot(.)
Following these changes the site is now working as expected.
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
I'm trying run flask on apache server and following the instructions on
this page but getting No module named 'flask' error
here's error.log
[Tue Aug 14 20:53:59.945197 2018] [wsgi:error] [pid 16744] [remote 189.46.121.20:52032] mod_wsgi (pid=16744): Target WSGI script '/var/www/FlaskApp/FlaskApp/flask-admin/examples/forms/flaskapp.wsgi' cannot be loaded as Python module.
[Tue Aug 14 20:53:59.945263 2018] [wsgi:error] [pid 16744] [remote 189.46.121.20:52032] mod_wsgi (pid=16744): Exception occurred processing WSGI script '/var/www/FlaskApp/FlaskApp/flask-admin/examples/forms/flaskapp.wsgi'.
[Tue Aug 14 20:53:59.945394 2018] [wsgi:error] [pid 16744] [remote 189.46.121.20:52032] Traceback (most recent call last):
[Tue Aug 14 20:53:59.945425 2018] [wsgi:error] [pid 16744] [remote 189.46.121.20:52032] File "/var/www/FlaskApp/FlaskApp/flask-admin/examples/forms/flaskapp.wsgi", line 8, in <module>
[Tue Aug 14 20:53:59.945431 2018] [wsgi:error] [pid 16744] [remote 189.46.121.20:52032] from app123 import app as application
[Tue Aug 14 20:53:59.945438 2018] [wsgi:error] [pid 16744] [remote 189.46.121.20:52032] File "/var/www/FlaskApp/FlaskApp/flask-admin/examples/forms/app123.py", line 4, in <module>
[Tue Aug 14 20:53:59.945442 2018] [wsgi:error] [pid 16744] [remote 189.46.121.20:52032] from flask import Flask, url_for, flash
[Tue Aug 14 20:53:59.945466 2018] [wsgi:error] [pid 16744] [remote 189.46.121.20:52032] ModuleNotFoundError: No module named 'flask'
FlaskApp.conf file:
<VirtualHost *:80>
ServerName 1**.1**.**.**
WSGIDaemonProcess app123
WSGIScriptAlias / /var/www/FlaskApp/FlaskApp/flask-admin/examples/forms/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/flask-admin/examples/forms>
WSGIProcessGroup app123
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
wsgi - file:
#!/usr/bin/python3
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/FlaskApp/flask-admin/examples/forms/")
#from FlaskApp
from app123 import app as application
application.secret_key = 'j****'
activate_this = 'py3env/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
and the app123.py file is the simple flask-admin example with forms. Absolutely the same. So, the question is: where is the problem?
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?