How do you setup a Django website in a shared hosting?
I've checked the django installation and it is ok
>>> import django
>>> django.VERSION
>>> (1, 4, 0, 'final', 0)
I've followed http://www.djangobook.com/en/2.0/chapter12/ section Running Django on a Shared-Hosting Provider with Apache to no avail. Currently the website is like this
/home/django_projects/WebsiteName
/sites/WebsiteName.co.id/www/.htaccess
/sites/WebsiteName.co.id/www/dispatch.fcgi
The .htaccess file is like this
AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ $1 [QSA,L]
RewriteRule ^(admin_media/.*)$ $1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
and the dispatch.fcgi is like this
#!/usr/bin/python
import sys, os
sys.path = ['$HOME/lib/python/Django-1.4'] + sys.path
sys.path = ['$HOME/django_projects'] + sys.path
os.chdir("$HOME/django_projects")
#from flup.server.fcgi import WSGIServer
from django.core.servers.fastcgi import runfastcgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'WebsiteName.settings'
runfastcgi(["method=threaded", "daemonize=false"])
It keeps generating error 500, Internal Error when I access the website. What I tried so far are,
[1] Changing AddHandler fastcgi-script .fcgi to AddHandler fcgid-script .fcgi
[2] Removing AddHandler fastcgi-script .fcgi
[3] Putting the website directory to /sites/WebsiteName.co.id/www/WebsiteName instead of /home/django_projects/WebsiteName/
[4] Do no. 3 and move dispatch.fcgi to /sites/WebsiteName.co.id/www/WebsiteName
I'm sorry if this question is so foolish. I'm new to Django. Btw, no 1-4 is not in order. It's just to number what things I've tried so far. Also if I do no. 4, the website shows the content of dispatch.fcgi.
It is more easy deploying with mod_wsgi on shared hosting. I work on Quijost and we offer a built-in package with Django 1.4 and Python 2.7 under mod_wsgi using nginx as backend.
We wrote a small tutorial in our forums for mod_wsgi and maybe it is useful for your example with fastcgi.
Response from GoDaddy from this thread:
"FastCGI is accessible from Python scripts for our Linux hosting
accounts. We do not allow, however, the addition of a custom FastCGI
handler in our shared hosting accounts."
So I think you're pretty much out of luck. As am I, although I'm trying to convince the customer that having a webfaction account is a good idea, as that supports mod_wsgi.
Most of the shared hosting platforms that support python use phusion passenger to run python apps. You can upload your django app to your hosting and use wsgi to run it. As for now, django 2.2 causes problems so you will have to use django 2.1 or lower. I have written a step by step tutorial about it which can be accessed here.
Related
I'm working with a shared hosting account which uses apache 2.4 , trying to deploy a flask app using http://fgimian.github.io/blog/2014/02/14/serving-a-python-flask-website-on-hostmonster . I've put the code and the fcgi script in public_html folder The contents of the folder are in the screenshot above:
The manage_apache.fcgi script is:
#!/home/username/anaconda2/bin/python
import sys,os
from flup.server.fcgi import WSGIServer
sys.path.insert(0, '/home/username/public_html')
from myflaskapp.settings import Config, SharedConfig
from myflaskapp.app import create_app
if __name__ == '__main__':
app = create_app(SharedConfig)
WSGIServer(app).run()
I've gotten to the last step and while testing it at the command line using putty to SSH in:
[~/public_html]# ./manage_apache.fcgi
I can see the correct web page being generated, so I assume that fast cgi is supported by my host. I'm not getting any python errors.
The .htaccess file out of the article is :
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ manage_apache.fcgi/$1 [QSA,L]
In the browser when I surf to mysite.org I am getting
Not Found
The requested URL /manage_apache.fcgi/ was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
according to support The .htaccess file is redirecting to manage_apache.fcgi/$1
-rwxr-xr-x 1 myusername myusername Nov 22 17:26 manage_apache.fcgi*
How can I fix this?
I suspect
sys.path.insert(0, '/home/username/public_html')
is an absolute path, but flask application is looking to a relative path respect to flask gateway, and cannot find it.
Have you tried to wrap the libraries in the app instance - move the absolute path in the app instance?
As an example, see http://werkzeug.pocoo.org/:
from werkzeug.wrappers import Request, Response
#Request.application
def application(request):
return Response('Hello World!')
if __name__ == '__main__':
from werkzeug.serving import run_simple
# move absolute path here
run_simple('localhost', 4000, application)
I suspect that fcgi is not supported on that host. Just because a host lets you run a Python script on the command line does not mean that they have configured mod_fcgi in Apache.
Try this: apachectl -t -D DUMP_MODULES | grep cgi. You should see fcgi_module or fastcgi_module, or possibly a cgi_module.
If you only see a cgi_module, then you should be able to use AddHandler cgi-script .py instead of AddHandler fcgid-script .fcgi.
If you see none of those, then you can try wsgi: apachectl -t -D DUMP_MODULES | grep wsgi. If you see wsgi_module, then you know you can use wsgi. At that point, you might be able to follow instructions here under .htaccess.
Summary
Django 1.6
Python 2.6
Apache 2.2.27
PostgreSQL 8.4.20
psycopg2
flup
Viewing the FastCGI wrapper page for my Django site via the command line seems to work fine, but I always get a 404: Page Not Found when viewing my site via a browser. What am I doing wrong? I suspect it has to do with my Apache setup.
Versions and Setup
The Django 1.8 documentation says fastcgi support is deprecated as of Django 1.7, and I can't use mod_wsgi, so I am using Django 1.6. Python 2.7 and later are unavailable, so I am using Python 2.6. And the hosting company won't install Python packages for me, but I managed to install flup and psycopg2 myself. (Note that I had to compile psycopg2 on my Linux64 machine and upload it to the server.)
Django, flup, and psycopg2 are installed in /home/account/public_html/sitename/site-packages/. My Django site is located in /home/account/public_html/sitename/sitename/. Issuing set | grep -e PYTHON -e DJANGO at the command prompt returns no results (i.e., no PYTHON or DJANGO environment variables are set).
Full Story
I am trying to set up Django on shared hosting. And before anyone suggests it: unfortunately, mod_wsgi is out of the question.
I followed the directions here: https://docs.djangoproject.com/en/1.6/howto/deployment/fastcgi/#apache-shared-hosting and ended up with this /home/account/public_html/.htaccess:
AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ sitename/site.fcgi/$1 [QSA,L]
and this /home/account/public_html/sitename/site.fcgi:
#!/usr/bin/python
import sys, os
# Add a custom Python path.
sys.path.insert(0, "/home/account/public_html/sitename")
sys.path.insert(0, "/home/account/public_html/sitename/site-packages")
# Switch to the directory of your project. (Optional.)
os.chdir("/home/account/public_html/sitename")
# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "sitename.settings"
# From Django
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="prefork", daemonize="false", debug="true")
When I do cd /home/account/public_html/sitename/ ; ./site.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: 200 OK
X-Frame-Options: SAMEORIGIN
Content-Type: text/html; charset=utf-8
<!DOCTYPE html>
<html>
<head>
...etc...
which is the correct index page for my Django site. So that works. (I am ignoring the WSGIServer errors for now, because calling site.fcgi from the command prompt is, obviously, outside the WSGI environment.)
However, when I navigate my browser to http://example.com/, I get a 404: Page Not Found error, and the server error log shows only:
[error] File does not exist: /home/account/public_html/sitename/site.fcgi/
Navigating to http://example.com/valid-page also produces a 404, with the server error:
[error] File does not exist: /home/account/public_html/sitename/site.fcgi/valid-page
What am I doing wrong? Or how does WSGI work, so I can debug this better?
Variations I've Tried
Use the HostGator approach to site.fcgi by doing this instead:
# From Django
#from django.core.servers.fastcgi import runfastcgi
#runfastcgi(method="prefork", daemonize="false", debug="true")
# From HostGator
from flup.server.fcgi import WSGIServer
from django.core.wsgi import get_wsgi_application
WSGIServer(get_wsgi_application()).run()
Same result: 404. This makes sense, because manually walking the code shows that the From Django code is just a wrapper for the From HostGator code.
Use the fcgid-script handler in .htaccess instead of fastcgi-script:
#AddHandler fastcgi-script .fcgi
AddHandler fcgid-script .fcgi
Use a RewriteCond in .htaccess that I found in another tutorial:
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(sitename/site.fcgi)
RewriteRule ^(.*)$ sitename/site.fcgi/$1 [QSA,L]
Pass the WSGI URI as command-prompt-style parameter (note the space instead of /):
#RewriteRule ^(.*)$ sitename/site.fcgi/$1 [QSA,L]
RewriteRule ^(.*)$ sitename/site.fcgi $1 [QSA,L]
Ensure the REQUEST_URI environment variable is set:
SetEnv REQUEST_URI %{REQUEST_URI}
Even make site.fcgi a CGI script, just to see if I could reproduce the success of running site.fcgi from the command prompt (probably not a permanent solution):
#AddHandler fastcgi-script .fcgi
#RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^(.*)$ sitename/site.fcgi/$1 [QSA,L]
Options -Indexes +ExecCGI
AddHandler cgi-script .fcgi
and then navigating to http://example.com/sitename/site.fcgi.
None of the above variations worked, nor did many combinations of several of them. Many of them returned 404: Page Not Found, and some returned more glaring errors, while others just displayed the source of site.fcgi.
Edit
I am getting the same/similar results for shared hosting with both Arvixe and LunarPages. The above description is using only my Arvixe account, because my LunarPages account does not have SSH access and is therefore harder to debug.
I'd even be willing to entertain low-cost alternative hosting plans (e.g., other hosting companies) that would enable this to work. This is just a personal website, not generating any money, so I don't want to spend the ~$20/month that Arvixe and LunarPages are asking to have dedicated hosting in order to have mod_wsgi access.
Thanks in advance for any help or suggestions!
I ran into the same problem (posted here) and ended up determining that it was due to restrictions on the shared hosting account I was subscribed to with bluehost. I switched a dreamhost shared hosting plan and was up and running in no time.
I have a shared hosting from Namecheap ( No ssh access). There I can run any python file in the CGI-bin directory when I type the full path including the .py extension. I want to know how to run a flask app in such an environment. Should I change the .htaccess or make a .cgi or fcgi or wsgi? I am not sure what these are or what they do. If someone can explain these too.
Check out http://flask.pocoo.org/docs/deploying/cgi/
If your CGI-app is available at http://example.com/cgi-bin/myapp.py, you have to put the following in a .htaccess (assuming you're using Apache) to make the app available at http://example.com/:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f # Don't interfere with static files
RewriteRule ^(.*)$ /cgi-bin/myapp.py/$1 [L]
When I'm executing
django-admin.py startproject site
it works.
But if I'm only copying site folder it doesn't work.
Why?
<VirtualHost *:80>
ServerName django.stanislavfeldman.com
# Django settings
WSGIScriptAlias / /var/www/django/wsgi_handler.py
WSGIDaemonProcess django.stanislavfeldman.com maximum-requests=200 stack-size=524288
ErrorLog /var/www/django/error.log
LogLevel warn
</VirtualHost>
wsgi_handler.py:
import os, sys
sys.path.append('/var/www/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'site.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If you have something like this in apache configs:
WSGIScriptAlias /path /base/path/devel/your_project.wsgi
And this inside your_project.wsgi:
sys.path.append('/base/path')
os.environ['DJANGO_SETTINGS_MODULE'] = 'devel.settings'
Then apache will look at /base/path/devel/settings.py. If you move or copy /base/path/devel to /base/path/production you have to edit DJANGO_SETTINGS_MODULE at your_project.wsgi pointing to 'production.settings'.
Ensure you have read:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
and also watch this presentation:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
Your problem is going to be a sys.path or permissions issue which are both covered by the above.
That you are using 'maximum-requests=200 stack-size=524288' options to WSGIDaemonProcess directive makes me question whether you have referred to the mainstream documentation as basic instructions don't tell you to use them. Instead looks like you have used some arbitrary persons blog post for how to set it up, or relying on some folklore given to you on an IRC channel. :-)
Check your python path to make sure that WSGI can reference it.
I had a problem with a symlink not being followed from the site-packages dir. Double check your apache config and symlinks as well.
This doesn't appear to be the problem in your case, but I ran smack into the same ImportError when I used the WSGIPythonPath directive (instead of the .wsgi file) to set up sys.path. That worked fine until I switched to running WSGI in daemon mode. Once you do that, you have to use the python-path argument to the WSGIDaemonProcess directive instead.
I am new on django. I tried this but I can't deploy. How can I do
#!/usr/bin/python
import sys
import os
base = os.path.dirname(os.path.abspath(__file__)) + '/..'
sys.path.append(base)
os.environ['DJANGO_SETTINGS_MODULE'] = 'myfirstapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(adminmedia/.*)$ - [L]
RewriteCond %{REQUEST_URI} !(cgi-bin/myproject.fcgi)
RewriteRule ^(.*)$ mysite.fcgi/$1 [L]
Here's the alwaysdata wiki entry for setting up Django with fastcgi. Only down-side: it's written in French.
Well, I don't speak French, but what it basically says is:
Create a directory named public in the folder of your django project.
In that directory create the file django.fcgi with the following content:
#!/usr/bin/python
import os, sys
_PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, _PROJECT_DIR)
sys.path.insert(0, os.path.dirname(_PROJECT_DIR))
_PROJECT_NAME = _PROJECT_DIR.split('/')[-1]
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings" % _PROJECT_NAME
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
Next, create a .htaccess in the public folder with the following content:
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ django.fcgi/$1 [QSA,L]
If you're planning to include the django admin interface, create this symbolic link in your public directory:
ln -s /usr/local/alwaysdata/python/django/1.1/django/contrib/admin/media/ media
In the end your folder tree hierarchy should somehow look like this:
myproject/
__init__.py
manage.py
public/
django.fcgi
.htaccess
media/
settings.py
urls.py
myapp/
views.py
models.py
Hope this helps. I talked with the admin, and he said he will soon provide an English wiki. Let's hope this is going to happen anytime soon.
UPDATE: There is an English wiki article now.
You are trying to mix two different web server integration methods: fcgi (fast cgi) and wsgi.
Your first snippet is for a wsgi interface with the web server and is the recommended method for integrating Django with Apache. Very good resources (including examples) to help you set this up correctly can be found in the official Django docs How to use Django with Apache and mod_wsgi and the mod_wsgi docs Integration with Django
The second snippet (with AddHandler line) is for fcgi. This is the kind of interface that is more typically used to interface Django with the lighttpd and nginx web servers. Resources for setting up fcgi interface can be found in official Django docs How to use Django with FastCGI, SCGI, or AJP.
Since it looks like alwaysdata.com only uses FastCGI (fcgi) interface you are stuck with this method. It looks like there are examples on their wiki page Déployer une application Django and particulary you'll need to replace your first (wsgi) snippet with this:
#!/usr/bin/python
import os, sys
_PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, _PROJECT_DIR)
sys.path.insert(0, os.path.dirname(_PROJECT_DIR))
_PROJECT_NAME = _PROJECT_DIR.split('/')[-1]
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings" % _PROJECT_NAME
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
We got now (since a couple of months) an article in english:
Django on alwaysdata.com
Regards,