How to manage mod_wsgi logs in apache? - python

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.

Related

mod_wsgi daemon mode on OS X Server

I've successfully deployed my Django webapp on OS X Server 10.9 using the config .plist and extra httpd.conf using the built-in server manager.
(as per: How does one deploy a django application on OS X Server?, Deploying Django on OS 10.9 Server)
Now I'm trying to get it running using mod_wsgi's daemon mode, rather than the default embedded mode.
So I went back to bare basics: running the included OS X Server mod_wsgi tester bare Python script, which has a httpd conf file with the single line:
WSGIScriptAlias /wsgi /Library/Server/Web/Data/WebApps/hello.wsgi
Changed it to:
WSGIDaemonProcess wsgi_test processes=2 threads=15
WSGIProcessGroup wsgi_test
WSGIScriptAlias /wsgi /Library/Server/Web/Data/WebApps/hello.wsgi
However, it fails when attempting to turn the site back on, with the following message to the apache error log:
[Tue Sep 09 11:56:08 2014] [notice] caught SIGTERM, shutting down
[Tue Sep 09 11:56:12 2014] [crit] (17)File exists: mod_rewrite: Parent could not create RewriteLock file /var/log/apache2/rewrite.lock
Configuration Failed
Then the site 'unticks' itself in the Server admin. I've checked, and the rewrite.lock file doesn't exist when the webserver is turned off.
Should it work? My other concern is whether daemon mode supported out of the box, or I need a new mod_wsgi.
I had a similar error "caught SIGTERM, shutting down" in the Apache error logs on Yosemite. Unfortunately, I can't speak to the mod_rewrite issue.
The SIGTERM seems to occur when the python-path is not set in the WSGIDaemonProcess directive.
Here's a configuration that worked for me for a basic Django app on OSX:
WSGIDaemonProcess myapp processes=2 threads=15 python-path=/path/to/django/app/
WSGIProcessGroup myapp
WSGIScriptAlias /myapp /path/to/django/app/wsgi.py process-group=myapp
<Directory "/path/to/django/app/">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
I'm not sure if the <Directory> config is required or not. However, setting the python-path in WSGIDaemonProcess solved the SIGTERM issue for me.
Hope this helps.

mod_wsgi configuration with flask

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!

Flask + mod_wsgi: client denied by server configuration

I've been trying out quite a many things to the level of high frustration when trying to get Flask run with Apache + mod_wsgi.
Basically I've done the following tutorials:
http://flask.pocoo.org/docs/deploying/mod_wsgi/#installing-mod-wsgi
http://www.lonesomedev.com/?p=169
But on browser I'm getting the following error:
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
And in Apache error.log the following:
[Fri May 03 17:17:06 2013] [error] [client ::1] client denied by server configuration: /home/user1/Develop/flask_dbadmin.wsgi
I'm running OpenSuse 11.4.
If you tried things mentioned Apache2: 'AH01630: client denied by server configuration' here and it still doesn't work chances are your wsgi script alias path is outside path mentioned in <Directory> block.
You need a <Directory> or <Location> block for every Alias.
So for example,
for
WSGIScriptAlias / /home/stark/FlaskApp/flaskapp.wsgi
you would need
<Directory /home/stark/FlaskApp>
Require all granted
</Directory>
It sounds like Apache doesn't have access to the object in question. Make sure you have an account set up for this specific reason and give the files access this account. Then use chown to set the access to these files for that user. In a development environment that can be for example the Apache account.
chown -R wwwrun:www /home/user1/Develop/
Or you could give everyone access, but I wouldn't recommend this.
chmod 777 -R /home/user1/Develop/
If that doesn't work you may need to manually allow access to the wsgi files in your apache config.
It should look something like this.
WSGIDaemonProcess flask_dbadmin user=wwwrun group=www threads=5
<VirtualHost *:80>
........
<Directory /home/user1/Develop/ >
Order allow,deny
Allow from all
</Directory>
<Files flask_dbadmin.wsgi>
Order allow,deny
Allow from all
</Files>
</VirtualHost>

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