Django Response Delay for every request - django

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.

Related

URL configuration issue with Django app using Bluehost

I'm in the process of deploying my Django app to a production server using Bluehost but keep seeing my site's 404 page. The issue seems to be my URL configuration. When I type http://www.example.com/ into my browser I receive the following :
Request URL: http://www.example.com/public_html/
^main/
^admin/
^accounts/
^media/(?P<path>.*)$
etc..
The current URL, public_html/, didn't match any of these
public_html/ is automatically added to the end of my URL I would like to eliminate the public_html/ entirely so my URL pattern will work as expected. For example
Request URL: http://www.example.com/
Which would then redirect to
Request URL: http://www.example.com/main/
How would I go about changing the default from http://www.example.com/public_html/ to http://www.example.com/ ?
My fcgi file:
AddHandler fcgid-script .fcgi
RewriteEngine on
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(adminmedia/.*)$ - [L]
RewriteCond %{REQUEST_URI} !(cgi-bin/mysite.fcgi)
RewriteRule ^(.*)$ cgi-bin/mysite.fcgi/$1 [L]
My .htaccess file:
#!/home/username/python/bin/python
import sys, os
sys.path.insert(0, "/home/username/python")
sys.path.insert(13, "/home/username/MySite")
os.environ['DJANGO_SETTINGS_MODULE'] = 'MySite.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
Is there a possibility this could be an issue with where my files are located on my server? If anyone has experienced this kind of issue before I would greatly appreciate some advice. Thanks!
You have to define following url in your urls.py,
url(r'^$', 'views.home', name='home')
url(r'^main/$', 'views.main', name='main')
then in views.py
def home(request):
return HttpResponseRedirect('/main/')
def main(request):
#some code here.

Django redirect loop in form submit

Just to clarify, this is the first time I'm putting a django website to production.
I have a simple website in django that works locally. But when I deploy it in the production server it enters in redirect loop when I submit a form. Then the browser just tells me that a redirect loop occurred and asks to empty the cache and so on...
This problem happens in the admin. I don't know if it would happen in the frontend because it just doesn't have any form in the frontend.
Notes:
I don't have any funky middlewares.
I tested in a virtual machine with more or less the same specs of
the production server and it works fine.
The submitted data is correctly handled and saved
The rest of the website is fine
I'm using apache with wsgi in a shared host, through a .htaccess.
At this point I don't rule out the chance of a dns misconfiguration, but I already checked and looks fine.
.htaccess:
AddHandler wsgi-script .wsgi
DirectoryIndex app22.wsgi
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app22.wsgi/$1 [QSA,PT,L]
</IfModule>
app22.wsgi:
import os, sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'BTT.settings'
path = '/home/bttmonch/apps'
if path not in sys.path:
sys.path.append(path)
path = '/home/bttmonch/apps/ENV/lib/python2.7/site-packages'
if path not in sys.path:
sys.path.append(path)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
At this point I created a fresh new app with just the admin activated, to see if it was some codding issue and put it on the server. And the same thing happens.
I am using the same .htaccess and .wsgi files that I mentioned before.
My urls.py is just the basic:
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
I am accessing the app via test.domainname.com.
I already tried accessing it through test.domainname.com, test.domainname.com/app22.wsgi, domainname.com/test/ and domainname.com/test/app22.wsgi...
After poking around in the server I stopped getting a redirect loop. But now every time I save a form I get a 403 error. Than I clear the browser cookies and all comes back to normal, and the form data is successfully saved in the database.
At this point a just don't now what else to try...

Django FastCGI and 500 Internal Server Error

I tried to install Django with FastCGI but without success!. It gives me a 500 Error, but if I execute my ./dispatch.fcgi on shell it gives me the "it works!" HTML page of Django.
Here is my url http://mydjango.webmashing.com
and my dispatch.fcgi file is
#!/usr/bin/python
import sys, os
sys.path.insert(0, "/home/webmashi/.local/lib/python")
os.chdir("/home/webmashi/.local/lib/python/myproject")
os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings"
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
.htaccess file:
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
I update my python version from 2.4 to 2.6 and it's worked :)

FCGI htaccess handler

I'm trying to setup django on a shared hosting provider. I followed the instructions on http://helpdesk.bluehost.com/index.php/kb/article/000531
and almost have it working.
The problem I'm facing now is that the traffic is properly routed throught the fcgi file, but the file itself shows up as plain text in the browser. If I run ./mysite.fcgi in the ssh shell, I do get the default django welcome page.
my .htaccess is:
AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L]
and mysite.fcgi:
#!/usr/bin/python2.6
import sys, os
os.environ['DJANGO_SETTINGS_MODULE'] = "icm.settings"
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
thanks.
Try removing AddHandler, or changing it to:
AddHandler fcgid-script .fcgi

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.