mod_wsgi configuration with flask - python

I have a problem with mod_wsgi and flask Im not sure what it is after almost 48 hours of trying I've finally given up and need some help. Everything seems fine and working properly I know this because my www.example.com domain routes to example.com/login if the user is not logged in.
When I access example.com the routing does happen and I can see the url change to example.com/login
Here is my configuration:
killerapp.wsgi:
from main import app as application
and the apache virtual host:
NameVirtualHost *:8080
<VirtualHost *:8080>
ServerName example.com
WSGIDaemonProcess killerapp user=apache group=apache threads=5
WSGIScriptAlias / /var/www/wsgi/killerapp.wsgi
<Directory /var/www/wsgi>
WSGIProcessGroup killerapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
I was getting the following error all along
ImportError: No module named app
when I did
chmod 755 app
on the app folder I got the following in the log file indicating no errors:
[Wed Nov 06 17:25:29 2013] [info] [client xx.xx.x.xxx] mod_wsgi (pid=3823, process='killerapp', application=''): Loading WSGI script '/var/www/wsgi/killerapp.wsgi
But i still get the following error when I access the page:
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
I suspect its a ownership/security issue but that's just my guess I wouldn't know at this point. If its of any help Im running python 2.6.6 on Centos 6.4 and mod_wsgi 3.2.3
Any help would really be appreciated.
Thank you

With many thanks to Mark Hildreth for showing me the 'way' I managed to finally see what the problem was. It was a permission issue
In my python app I turned on debugging after initializing the flask app like so:
app = Flask(__name__)
app.debug = True
This allowed Flask to display the error messages in the apache error log. In my case the problem was Jinja was unable to access the templates directory because the apache user was not set as owner of that directory so the following fixed it:
chown apache:apache templates
Once again credit and thanks goes to Mark Hildreth for showing me the light!

Related

Flask app running on apache resulting in 404 error

I'm using mac os x and trying to get the official flask tutorial to work with Apache, but when I attempt to open the site in localhost on my browser it results in a 404: The requested URL / was not found on this server. I followed these directions for configuring mod_wsgi for flask. I've run the app using the built in server with flask so I know the app itself works.
The following is the resulting error in the Apache error log
[negotiation:error] [pid 7563] [client ::1:50509] AH00687: Negotiation: discovered file(s) matching request: /Library/WebServer/Documents/flaskr/flaskr.wsgi (None could be negotiated).
Here is what I have in my flaskr.wsgi file located in /Library/WebServer/Documents/flaskr/flaskr.wsgi
import sys
sys.path.insert(0, '/Library/WebServer/Documents/flaskr')
from flaskr import app as application
I have the following in my httpd.conf file
Listen 80
<VirtualHost *>
ServerName localhost
WSGIDaemonProcess flaskr user=_www group=_www threads=5
WSGIScriptAlias / /Library/WebServer/Documents/flaskr/flaskr.wsgi
<Directory /Library/WebServer/Documents/flaskr>
WSGIProcessGroup flaskr
WSGIApplicationGroup %{GLOBAL}
Require all granted # httpd 2.4 syntax as noted in the guide
</Directory>
</VirtualHost>
LoadModule wsgi_module /usr/local/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-darwin.so
WSGIPythonHome /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6
From terminal when I run sudo apachectl -M it displays wsgi_module (shared). If I comment out the code in my httpd.conf file that I've listed above, the wsgi_module goes away from the enabled modules list, so unless there's something I'm not aware of mod_wsgi seems to be enabled successfully, but as I've mentioned I get a 404 message when trying to view the site.
EDIT
I don't think there's anything wrong with my Apache configuration. I tried this tutorial for getting flask up and running on Apache and it worked. I'll update once I figure out what I did wrong with the "flaskr" app.
Edit - found the problem
Turns out my flaskr.wsgi file was in the wrong directory. It was supposed to be in /Library/WebServer/Documents/flaskr/flaskr/flaskr.wsgi.
The official Flask tutorial I referenced uses the following directory structure
flaskr <----Initially put my `flaskr.wsgi` file in this directory
init.py
flaskr <----Changed to this directory and it worked
flaskr.py
flaskr.db
flaskr.wsgi
Also changed the system path to sys.path.append('/Library/WebServer/Documents/flaskr/flaskr')

How to manage mod_wsgi logs in apache?

I am working on flask application and i have used mod_wsgi and Apache to host the flask app. I have setup the logging in app it, but i observe that there are same logs are getting written in apache/logs/error.log
like
[wsgi:error] ... BACK TO THE BROWSER
[wsgi:error] ... Selected records from table
[wsgi:error] ... mod_wsgi (pid=1654): Exception occurred processing WSGI script '/tmp/mod_wsgi-localhost:8000:0/htdocs/'.
[wsgi:error] ... ID 1123 Inserted in table
Due to this error.log is getting polluted with wsgi:error and apache errors its getting difficult to analyse the log file.
Is their any way to stop getting written wsgi.errors in error.log files or may we way to redirect these specific logs to seperate log files ?
I typically add an ErrorLog to my VirtualHost. Here's an example Apache VirtualHost I use:
<VirtualHost *:443>
ServerName yourservername.com
ErrorLog /home/yourusername/apache_errors.log
WSGIDaemonProcess yourproject-https python-home=/home/yourusername/.virtualenvs/yourproject
WSGIScriptAlias /yourproject /var/www/html/yourproject/yourproject/wsgi.py process-group=yourproject-https application-group=yourproject-https
WSGIProcessGroup yourproject-https
Alias /yourproject/static/ /var/www/html/yourproject/static/
SSLENGINE on
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
SSLProtocol all -SSLv2
</VirtualHost>
Would that do the trick? You can choose a path other than your home directory, of course.
First, you should add ErrorLog within the VirtualHost, Then you should use flask.logging.default_handler to log your message.
And be careful that if your request application does not exist in your threadLocal environment, flask will use std.strerr to log the message, and this will due to the message be cached by the main log instead of VirtualHost log.

Apache server in Linux error forbidden access

I'm a beginner in web development. When I run Apache server give me this error in web page:
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
If you think this is a server error, please contact the webmaster.
Error 403
localhost
Apache/2.4.20 (Unix) mod_wsgi/4.4.22 Python/2.7.11
I'm developing using Django 1.9.1 web app and use mode_wsgi with apache version 2. Here is my wsgi.py file :
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "newSite.settings")
application = get_wsgi_application()
and my httpd.conf :
WSGIScriptAlias /newsite "/home/hello/django/newSite/newSite/wsgi.py"
WSGIPythonPath "/home/hello/django/newSite"
<Directory "/home/hello/django/newSite/newSite/">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
thanks guys'
im succeed to run the server truely .
just change target directory permission to 775 and change the user and group in httpd.conf to my current user .
and for custom folder for sites root , change "DocumentRoot" of httpd.conf and set permission.
sory for my english.
thanks again

django, apache, mod_wsgi and python py-scrypt not working together

I am running a ec2 instance to host some Django websites. The websites are being served by Apache with the use of mod_wsgi
Since a few days I am trying to deploy a new webplatform we are developing but I am running into a problem that seems impossible for me to solve. For security reasons we use scrypt 0.4 to secure the users personal information like passwords.
On the development server everything works like a charm but when we deploy to our live server we get a 500 Internal Server error. The apache log gives me the following message
Premature end of script headers: socialmarketingplatform.wsgi, referer:
When I uncomment the line where the scrypt module is used everything works fine. Also when I run the server delivered with Django with scrypt enabled everything works on the live server. So it is a combination of mod_wsgi, django and scrypt that generates the error.
I am using the following wsgi file:
import os
import sys
path = '/var/www/vhosts/[sub_domain]'
if path not in sys.path:
sys.path.append(path)
sys.path.append(path + '/socialmarketingplatform')
os.environ['DJANGO_SETTINGS_MODULE'] = 'socialmarketingplatform.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
And the following virtualhost config:
<VirtualHost *:80>
#Basic setup
ServerAdmin [removed email]
ServerName luxdevelopment.net
ServerAlias [sub domain]
DocumentRoot /var/www/vhosts/[sub domain]/socialmarketingplatform/
Alias /media/admin /usr/lib/python2.6/site-packages/Django-1.3-py2.6.egg/django/contrib/admin/media
Alias /media /var/www/vhosts/[sub domain]/socialmarketingplatform/media
<Location media="">
SetHandler None
</Location>
LogLevel warn
ErrorLog /var/log/httpd/smp_error.log
CustomLog /var/log/httpd/smp_access.log combined
WSGIDaemonProcess luxdevelopment.net user=apache group=apache threads=25
WSGIProcessGroup luxdevelopment.net
WSGIScriptAlias / /var/www/cgi-bin/socialmarketingplatform.wsgi
</VirtualHost>
I hope someone can help me with this problem. If there are any further question let me know.
See:
http://code.google.com/p/modwsgi/wiki/FrequentlyAskedQuestions#Apache_Process_Crashes
The message 'premature end of script headers' is usually indicative of your code crashing the daemon process. You can verify this by looking for segmentation fault or similar message in main Apache error log file. If you enable 'LogLevel info' in main Apache config and VirtualHost then mod_wsgi will log more about daemon process restarts.
A quick remedy if running only application in that daemon process group is to add:
WSGIApplicationGroup %{GLOBAL}
This will work around crashes caused by broken third party extension modules for Python which aren't written properly to work in sub interpreters.
Other than that, can be shared library version mismatches as described in the FAQ.

Cannot solve mod_wsgi exception in Django setup

I'm working with my hosting provider to get a Django application up and running, but neither of us are very experienced and we've basically hit a complete dead end.
I don't have direct access to the conf file but here's how its contents have been described to me:
<IfModule mod_wsgi.c>
WSGIScriptAlias /fredapp/ /home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi
WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup fred
WSGIApplicationGroup %{GLOBAL}
</IfModule>
Alias /robots.txt /home/fred/public_html/fred-site/robots.txt
Alias /favicon.ico /home/fred/public_html/fred-site/favicon.ico
Alias /settings/media/ /home/fred/public_html/fred-site/media/
My "django.wsgi" script is nothing fancy:
import os, sys
sys.path.append('/home/fred/public_html/cgi-bin/')
sys.path.append('/home/fred/public_html/cgi-bin/fredapp/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'fredapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
So my understanding is that all this means that if a request comes in for domain.com/fredapp/ that it should be turned over to the application via django.wsgi. However, the only response I get is:
[Fri Jan 22 18:46:08 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain.com/500.shtml
[Fri Jan 22 18:46:08 2010] [error] [client xx.xxx.xx.xx] mod_wsgi (pid=26760): Exception occurred processing WSGI script '/home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi'.
[Fri Jan 22 18:46:03 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain.com/404.shtml
[Fri Jan 22 18:46:03 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain
This is running under Apache on Linux. I have tried running each line of the .wsgi script in the Python interpreter on the server, and none of them return any errors. I also tried the sys.stdout = sys.stderr trick and got no further output than what is above. The File does not exist errors have to do with the rest of the site's set-up and occur on any request. I haven't finished setting all that up properly (error pages and index pages and so on) because I'm just trying to get the app itself to run.
I've gotten this app up and running under Apache on my own machine, though NOT in Daemon mode, but it's my first Django app, and I don't think my hosting provider has ever configured one before, so we're flying a little blind. If anyone has any suggestions, I'd be very grateful. Thank you!
If the quoted configuration about is what you are using, the error is rather obvious actually. You have:
WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup scratchf
It should be:
WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup fred
That is, the name of the process group must match.
You should though have seen an error message:
No WSGI daemon process called 'scratchf' has been configured
This would likely be before the logged error:
Exception occurred processing WSGI script
This is why it is important that you supply all the error log messages and don't assume that they aren't relevant.
Alternatively, you have quoted configuration different to what you are using or not all of the configuration.
UPDATE 1
It looks like you may have ErrorDocument directive enabled in Apache to redirect errors to a specific URL. Because however you have mounted Django at root of web server and not excluded those error URLs from being passed through to Django, then when an error is generated Django gets the redirect for the error document but it cannot resolve the URL and subsequently generates a 404. Because Apache saw a 404 for error page redirect, it then returns a 500 default error page. The end result is that true original error and any information is lost.
Thus, go into Apache configuration and comment out the ErrorDocument directives.
UPDATE 2
Change configuration to:
WSGIScriptAlias /fredapp /home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi
You should not have trailing slash on the second value on line. Missed that you were actually trying to mount at sub URL and not at root of web server.
Is it possible that your starting directory is not the one project is in?
Today I was also setting up Apache+mod_wsgi+Django app and after adding to django.wsgi:
os.chdir('/home/user/my_django_project')
everything started to work like a charm.
We had the same error when the user running Apache had not the rights to read the files.

Categories

Resources