Search Google

Sunday 26 April 2015

Python CGI setup (for Hostgator hosting)

To install mod_wsgi in XAMPP Apache and serve Django (Windows) 

 

 

 



 DJANGO

In this post we will enable Python CGI for Hostgator
Create a .htaccess file and put following contents in this file:


This will make the web server to interprete .py file as Python CGI script.

Next create a python file (named test.py here) with following code:
And set this file’s permission setting so that Owner, Group and Public have “execute” permission for this file (Or set 755 permission simply)
Visit http://server_address/app_path/test.py, you will see a page with “hello” printed.

Perl and Python scripts

These scripts must have 755 permissions.
Perl = .pl
Python = .py
Example perl script:
#!/usr/bin/perl -w
print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>CGI Test</title>";
print "</head><body>";
print "<p>Test page using Perl</p>";
print "</body></html>";
Example python script:
#!/usr/bin/python
print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>CGI Test</title>";
print "</head><body>";
print "<p>Test page using Python</p>";
print "</body></html>";
Activate Python:
Please note that you have to add a line in the .htaccess file to use Python.
The recommended directive is:
Addhandler cgi-script .py .pl .cgi
(This directive covers Perl, Python and CGI files.)


Getting django up and running on hostgator

 

I thought it would be fun this year to start learning Django, so here’s a bit about how to get it set up using Hostgator (I have a shared hosting account).
I am using Terminal on the MAC and the File Manager on the cPanel.
The first thing you have to do is enable SSH (shell access). To do this, log into your cPanel and go to “Billing”:

Then choose, “View Hosting Packages”:

Then, click the link that says, “Enable Shell Access“.
Next, write down your IP as shown in the cPanel (I blurred mine out for security purposes, but you can see what the section looks like here):

Also, write down your cPanel username and password, you will need this.
Open Terminal and the File Manager in cPanel. I keep both open at the same time to see what I am doing.
In your File Manager, you should see /home/your-username/. Then look for a folder called django. You should have this. If not, in Terminal, type the following to SSH to your account:
ssh -p 2222 your-username-goes-here@your-ip-goes-here
If this works (you might have to wait a moment while it connects to your site), you will be prompted for your password.
Type in your password (you won’t see it at all) then hit enter and wait.
If this is your first time, you will be asked about a key. Just say “yes”.
You might then get some message like, “The authenticity of this host can’t be established” and “RSA key fingerprint is blah blah blah”.
Don’t worry about this, if you like, write down the key in case you need it later. That’s it. You are now connected to your logged in to your account via SSH. Now type the following.
mkdir ~/django
cd ~/django
svn co http://code.djangoproject.com/svn/django/trunk/django
mkdir creates a folder called “django”.
cd changes where you are to inside of the “django” folder.
The last line gets the latest stable version of django and installs it into the “django” folder. You need to have subversion installed for this to work.
I can’t really remember if I had to install subversion on my version of OS X, I think it was already installed, but if not, you’ll have to install it. It’s free.
Then, create a directory for your projects inside of the django folder by typing:
mkdir ~/django/projects
You should now have a folder called “django” with two folders inside:
Now, to test if django is running, you make a test project.
In Terminal, type:
cd projects
This will switch you into the projects folder.
Then type:
django-admin.py startproject newproject
This tells django to create a new project called, “newproject”.
Then type:
cd newproject
chmod +x manage.py
./manage.py startapp newapp

If you go inside of your “newproject” folder you should now see the following:

This is your “newapp”. Yay, django is working…sort of
Next, you have to enable fastcgi (or, I suppose, django will run really slowly).
Create a new file called index.fcgi in your public folder:

Then change the permissions of the file to 755:

Next, edit the file and add the following (be sure to change “username” with your username for your account):
#!/usr/local/bin/python
import sys, os, user

# sys.path.insert(0, "/usr/lib/python2.4")
sys.path.insert(0, "/home/your-username-goes-here/django")
sys.path.insert(0, "/home/your-username-goes-here/django/projects")
sys.path.insert(0, "/home/your-username-goes-here/django/projects/newproject")

# Switch to the directory of your project.
os.chdir("/homeyour-username-goes-here/django/projects/newproject")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "newproject.settings"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
Next, open the .htaccess that is in the same folder as index.fcgi.
AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On

RewriteRule (media/.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin/.*)$ index.fcgi/$1 [L]
Next (and this is important) check your website. If you are getting a 500 error, open Terminal and log in to your account:
ssh -p 2222 your-username-goes-here@your-ip-goes-here
Then type:
which python
This should give you a message like:
/usr/bin/python
If you are getting something different (for example, user/local/bin/python/), update your script to reflect this:
#!/usr/local/bin/python
If all goes well, you should see this:

Below is my working code for hostgator
Below are my working code based on Robert code.
1. For curlgen.fcgi – change permission to 755
code is :
#!/usr/bin/python
import sys, os, user
# sys.path.insert(0, “/usr/lib/python2.7″)
sys.path.insert(0, “/home/r5tpl/django/projects”)
# Switch to the directory of your project.
os.chdir(“/home/r5tpl/django/projects/newproject”)
# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ[‘DJANGO_SETTINGS_MODULE’] = “newproject.settings”
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method=”threaded”, daemonize=”false”)
For .htaccess code is
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ curlgen.fcgi/$1 [QSA,L]

 

No comments:

Post a Comment