logfile causes permission error in django application deployed on apache - python

I have deployed a Django 1.3.1 app on apache2 in my machine using mod_wsgi.I used to log some values in the app using python logging module and these were written to a mylog.txt file in the application base directory.However I deleted this file and commented out every log() call in my code.In the django standalone server the app runs without any problem.But when I start the app in apache,I get this apache log entry
[Sat Mar 31 09:34:40 2012] [error] [client 127.0.0.1] TemplateSyntaxError: Caught IOError while rendering: [Errno 13] Permission denied: '/home/me/dev/python/django/myapp/mylog.txt'
I cannot understand why this happens ,since I commented out every log related line in the code and even the variable which tells the logfile's name in settings.py
I tried clearing the cache..but even that didn't help
can someone help me figure this out?
The module details are as below
Apache/2.0.63 (Unix) DAV/2 mod_wsgi/3.3 Python/2.6.5 Server at 127.0.0.1 Port 80

what are the unix/linux permissions on your mylog.txt file?
ls -la /home/me/dev/python/django/myapp/mylog.txt should show the unix/linux perms. Can you paste the results of this command? (update it in your question above if possible)
Apache2 user on your system (depending on which linux distro of if you use Mac) are most likely 'www-data' or 'http'. You will need to make sure that mylog.txt has the correct unix/linux owner and the correct permissions mode so that apache2 can write into this log file.

I also met this problem in Webpy+Apache. And I changed the Apache logs directory owner to Apache user then the problem solved.

Related

Python Internal Server Error

I'm new to Python Web Development and I have written the following code and uploaded into the root of my server (public_html) folder:
import web
urls = ('/','index')
app = web.application(urls, globals())
class index:
def GET(self):
greeting = "Hello World"
return greeting
app.run()
My .htaccess file is:
AddType text/html py
AddHandler cgi-script .py .cgi
When I open my site, I get the following error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster#example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
My cPanel Error Log shows:
[Thu Jul 30 10:44:42 2015] [error] [client 1.1.1.1] client denied by server configuration: /home/user/public_html/.htaccess
[Thu Jul 30 10:36:53 2015] [error] [client 1.1.1.1] attempt to invoke directory as script: /home/user/public_html/cgi-bin/, referer: http://example.com/
Most of the places I searched for said that the .py file should be given permission 755. I did the same and in addition, even gave 755 permission to .htaccess file.
What may be the probable solution?
I believe its neither issue with py or .htaccess file permissions, its all about provide access to the files under .htaccess in the configuration section... check that out!!!!

Flask app on Apache, CentOs throws mod_fcgid: error reading data from FastCGI server

I am developing a Flask application on CentOS 6.6 on Apache and Mysql. It is modified from The Flask Megatutorial. I am able to create normally the database, however when I try to access it from my browser I get 500 internal server error and this in the error_log file:
content type: text/html
<h1>Hello world!</h1>
[Sun May 03 18:39:53 2015] [error] [client my.ip.add.res] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[Sun May 03 18:39:53 2015] [error] [client my.ip.add.res] Premature end of script headers: runp-mysql.fcgi
This is after I've edited down the runp-mysql.fcgi file to this:
#!flask/bin/python
#encoding=UTF-8
#import os
print "content type: text/html\n\n"
print ""
print "<h1>Hello world!</h1>"
Running this from the command line completes correctly.
My httpd.conf file ends with this:
FcgidIPCDir /tmp
AddHandler fcgid-script .fcgi
<VirtualHost *:80>
DocumentRoot /home/apps/my_app/app/static
Alias /static /home/apps/my_app/app/static
ScriptAlias / /home/apps/my_app/runp-mysql.fcgi/
</VirtualHost>
I was doing the same and a major issue was the permissions on the file. make sure it is executable and not read only.
Also I do not think that the runp-mysql.fcgi is where you want to do your printing. It should be in the views file. This is where you should have the connection to the your mysql database then start the wscgi server.
This is a very loose answer based on my actual problem and I am only uploading it in case someone else follows the same tutorial and also has a problem with deployment.
The gist of it is that under the python version I used (2.7) for deployment the flipflop module did not work and I had to use flup instead. For anyone getting the same error - try it, it might work.

500 Internal Server Error on .py execution

I am trying to have a python script execute on the click of an image but whenever the python script gets called it always throws a 500 Internal Server Error? Here is the text from the log
[Sun Feb 15 20:31:04 2015] [error] (2)No such file or directory: exec of '/var/www/forward.py' failed
[Sun Feb 15 20:31:04 2015] [error] [client 192.168.15.51] Premature end of script headers: forward.py, referer: http://192.168.15.76/Testing.html
I don't understand why it is saying Premature end of script header? I can execute a basic python script that just prints an html header or text. The file I am trying to execute just has some basic wiringPi code that executes fine from the sudo python forward.py command?
EDIT
This is the script I'm trying to execute:
#!/usr/bin/python
import time
import wiringpi2 as wiringpi
wiringpi.wiringPiSetupPhys()
wiringpi.pinMode(40, 1)
wiringpi.digitalWrite(40, 1)
time.sleep(2)
wiringpi.digitalWrite(40, 0)
wiringpi.pinMode(40, 0)
Things to check:
Make sure the script is executable (chmod +x forward.py) and that it has a she-bang line (e.g. #!/usr/bin/env python).
Make sure the script's owner & group match up with what user apache is running as.
Try running the script from the command line as the apache user. I see that you're testing it with sudo. Does it really need sudo? If so whatever user is running as apache will also need sudoers access.
Since it looks like you're using CGI, try adding: import cgitb; cgitb.enable(); to the top of your script. This will catch any exceptions and return it as a response instead of causing the script to die.
In addition to #lost-theory's recommendations, I would also check to make sure:
You have executable permission on the script.
Apache has permission to access the folder/file of the script. For example:
<Directory /path/to/your/dir>
<Files *>
Order allow,deny
Allow from all
Require all granted
</Files>
</Directory>

Python webscript will not run - generates 403 error

I am trying to run a python (.py) script on my website.
Whenever I direct my browser to http://mydomain.co.uk/cgi-bin/index.py I get a 403 error.
I have checked, and everyone has execute permissions.
I have checked the error log, and got this:
[Thu May 10 13:17:03 2012] [error] [client 82.132.248.44] client denied by server configuration: /home/liamwli2/public_html/cgi-bin/index.py
I am using a shared hosting package.
Can anyone give me a solution? Thanks.

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