Setting Django on Shared Host with FastCGI - django

I know this might be a question often asked, but I tried my best for almost a day to launch my first django website with no results, so please anyone experienced with shared hosting and django to help. Here is any relevant info:
python 2.6 installed on the remote machine at $HOME/.local/lib/
flup and django at same place, all is done by setup.py install --user.
my django project file structure:
$HOME/
projects/django/bw_python/
manage.py
bw_python/
__init__.py
settings.py
urls.py
wsgi.py
site_main/
django app with urls,models, etc...
In $HOME/.bashrc I have the following lines:
export PATH=$HOME/.local/bin:$HOME/.local/usr/bin:$PATH
export PYTHONPATH=$HOME/projects/django:$HOME/projects/django/bw_python:$HOME/projects/django/bw_python/bw_python:$HOME/projects/django/bw_python/site_main:$PYTHONPATH
If I run python interpreter django and flup are working, manage.py runserver works without any errors.
Now in the public_html I have the following bw_python.fcgi script:
#!/usr/bin/env python
import sys, os
# Add a custom Python path.
sys.path.insert(0, "/home5/bwinnova/.local/lib/python")
# Switch to the directory of your project. (Optional.)
os.chdir("/home5/bwinnova/projects/django/bw_python")
# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "bw_python.settings"
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
If I execute ./bw_python.fcgi I get:
WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 301 MOVED PERMANENTLY
Content-Type: text/html; charset=utf-8
Location: http://localhost/home/
The .htaccess:
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*)$ bw_python.fcgi/$1 [QSA,L]
The chmod on both of these files and including the $HOME/projects/django/bw_python is 755.
And sorry but from the tutorial I'm stuck here? What is the next step to be done? I always get a 500 server error and on the server error log it is only this:
[Fri Jul 27 19:29:14 2012] [warn] RewriteCond: NoCase option for non-regex pattern '-f' is not supported and will be ignored.
Thanks in advance for any help or guidance from here.

Related

Django Response Delay for every request

currently, I move my Django application on a production server. The problem is that each request to the server is slower by 1 second, then locallhost. (I don't mean static files. For testing I made ​​a simple VIEW, that not access the database and not contain python code only return string) I did get this values(delay 1 second) ​​of Chrome / Network/ Property - request - Waiting. Property Sending/receiving is normal (cca 1-10ms)
My .htaccess:
AuthType none
Satisfy Any
Allow from All
AddHandler fast-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /mysite.cgi/$1 [QSA,L]
My mysite.cgi:
#!/usr/bin/python
# -*- coding: utf8 -*-
import sys, os
# to suppress browser output, you should explicitly say so.
import cgitb
cgitb.enable(display=True)
# Add a custom Python path.
# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ.setdefault("DJANGO_SETTINGS_MODULE","Web.settings")
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="False")
I'm not server admin, so haven´t permission for setting Apache etc.
It is possible that the application not uses FastCGI but CGI? Or something like that?
Thanks
Problem solved by server admin, the server has not installed mod_fcgid.

Error: Target WSGI script not found or unable to stat when run django on apache

i have a problem when run django on apache:
htdocs/blog/apps/homepage/urls.py:
url(r'^$', 'index', name="h_index"),
url(r'^about/$', 'about', name="h_about"),
url(r'^contact/$', 'contact', name="h_contact"),
url(r'^archive/$', 'archive', name="h_archive"),
htdocs/blog/urls.py
(r'^', include('apps.homepage.urls')),
django.wsgi:
import os
import os.path
import sys
sys.path.append('D:/Coding/xampp/htdocs')
sys.path.append('D:/Coding/xampp/htdocs/blog')
os.environ['DJANGO_SETTINGS_MODULE'] = 'blog.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
httpd.conf:
Alias /static/ "D:/Coding/xampp/htdocs/blog/static/"
WSGIScriptAlias /blog/ "D:/Coding/xampp/htdocs/blog/django.wsgi"
when i run "localhost/blog", it's working. But run "localhost/blog/about/" or other, it's error:
[error] [client ::1] Target WSGI script not found or unable to stat: .../htdocs/blog/django.wsgiabout, referer: http://localhost/blog/
Please notice how your apache configuration doesn't match the documented syntax for mod_wsgi's WSGIScriptAlias.
WSGIScriptAlias /blog/ "D:/Coding/xampp/htdocs/blog/django.wsgi"
should be:
WSGIScriptAlias /blog "D:/Coding/xampp/htdocs/blog/django.wsgi"
(notice no trailing slash after the 2nd token, "/blog")
I just resolved the same issue just now and found this thread Googling. Hope this helps you and future users.
For more information:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines
I would suspect you are not loading the wsgi module. If you look closely at your error message Apache is treating it like a file and adding about to the name of the wsgi script file it looks for - thus it doesn't find it.

Django project with fcgi

I am trying to deploy a second Django project on a dedicated server using fcgi. However if I run python manage.py runfcgiin my project I get the following output:
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 500 INTERNAL SERVER ERROR
Vary: Accept-Language, Cookie
Content-Type: text/html; charset=utf-8
Content-Language: en-us
followed by the correct html output.
My .htaccess is like this:
RewriteEngine On
RewriteRule ^media/(.*)$ /home/username/public_html/my_project/media/$1 [L]
RewriteRule ^admin_media/(.*)$ /home/username/public_html/my_project/admin_media/$1 [L]
RewriteRule ^(django\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ cgi-bin/django.fcgi/$1 [L]
And my django.fcgi is:
#!/usr/local/bin/python2.6
import sys
import os
# insert PYTHONPATH values here, including the path to your application
sys.path.insert(0, '/home/darwinfo/lib/python/')
sys.path.insert(0, '/home/darwinfo/django-projects/my_project/')
# location of your application's settings file.
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_project.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method = "threaded", daemonize = "false", maxchildren=3, minspare=0, maxspa
re=1)
And when I navigate to the url I get a 500 error. I don't believe this a Django error as my admin mail is configured correctly and I get no notifications.
Any suggestions what I can do to remedy this situation
Do you have a flup installed as suggested by: https://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/?from=olddocs ?
You have to have flup installed in virt_env for your new project.

WSGIServer errors when trying to run Django app

Firstly, here's my script:
#!/usr/bin/python
import sys, os
sys.path.append('/home/username/python')
sys.path.append("/home/username/python/flup")
sys.path.append("/home/username/python/django")
# more path stuff
os.environ['DJANGO_SETTINGS_MODULE'] = "project.settings"
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
As was described here.
And here's the error I get when trying to run it from shell:
WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI!
WSGIServer: missing FastCGI param SERVER_NAME required by WSGI!
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!
Status: 404 NOT FOUND
Content-Type: text/html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<!-- more html which looks to be the correct output -->
My question is, why aren't those params passed automatically by FastCGI? What am I doing wrong? Running the script from my web server just gives me an internal server error.
Instead of the last two lines of my script, I can use
from flup.server.fcgi import WSGIServer
from django.core.handlers.wsgi import WSGIHandler
WSGIServer(WSGIHandler()).run()
But I still get the exact same error...
Solved it. This .htaccess file did the trick, for whatever reason. I swear I tried all this before...
AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(adminmedia/.*)$ - [L]
RewriteCond %{REQUEST_URI} !(cgi-bin/myproject.fcgi)
RewriteRule ^(.*)$ cgi-bin/myproject.fcgi/$1 [L]
The script expects those params to be passed as environment variables. Since they are not present in your shell environment, and the script is not running in the apache fastcgi environment (which provides them), it complains.
Do you have access to apache error logs? What do they say?
Does your host have mod_wsgi support? If so, you could use Django's wsgi handler:
import sys
import os
base = os.path.dirname(os.path.abspath(__file__)) + '/..'
sys.path.append(base)
os.environ['DJANGO_SETTINGS_MODULE'] = 'yourproject.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Further instructions can be found on the modwsgi wiki, and the Django docs.

Django MOD_PYTHON ERROR

I have had django running with mod_python for awhile now but today I get the following error
MOD_PYTHON ERROR
ProcessId: 4551
Interpreter: 'thatch'
ServerName: 'thatch'
DocumentRoot: '/var/www/thatch'
URI: '/'
Location: '/'
Directory: None
Filename: '/var/www/thatch/'
PathInfo: ''
Phase: 'PythonHandler'
Handler: 'django.core.handlers.modpython'
Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 1202, in _process_target
module = import_module(module_name, path=path)
File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 304, in import_module
return __import__(module_name, {}, {}, ['*'])
ImportError: No module named django.core.handlers.modpython
This is my virtual host configuration
<VirtualHost *:80>
ServerName thatch
DocumentRoot /var/www/thatch
<Location "/">
SetHandler python-program
PythonPath "['/var/www/thatch'] + sys.path"
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE thatch.settings
PythonOption django.root /var/www/thatch
PythonDebug On
</Location>
<Location "/media">
SetHandler None
</Location>
<LocationMatch "\.(jpg|gif|png|css|js)$">
SetHandler None
</LocationMatch>
</VirtualHost>
Any ideas why this is happening?
I can only guess what your problem might be, but here are two standard checks:
Import modpython in shell, from a couple different working directories
Change users (if you have any besides root set up) and repeat step 1
If #2 looks like more trouble than it's worth, then try very, very carefully examining permissions for each folder along the path to modpython to make sure that "other" has read access at every stage along the way. If you've moved django into your home directory (like I did once...not sure why, but got the same error) that'll be the problem. You either need to move it back into a directory where apache can read or you need to add read permission for other.
In my case pytonpath and access rights was ok.
This error disappeared after recompilation python.
Configuration:
Gentoo
Django 1.1
Apache:
www-servers/apache-2.2.15 USE="doc ssl suexec threads -debug -ldap (-selinux) -static" APACHE2_MODULES="alias auth_basic authn_alias authn_anon authn_default authn_file authz_default authz_host authz_owner autoindex cache dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias -actions -asis -auth_digest -authn_dbd -authn_dbm -authz_dbm -authz_groupfile -authz_user -cern_meta -charset_lite -dbd -dumpio -ident -imagemap -log_forensic -proxy -proxy_ajp -proxy_balancer -proxy_connect -proxy_ftp -proxy_http -substitute -version" APACHE2_MPMS="worker -event -itk -peruser -prefork"
Python 2.6.4