I have installed python3.6 on root, created python_app folder inside public_html directory running this .py file
#! /usr/bin/python
print "Content-type: text/html\n\n"
print "Hello world!!"
but it is rendered as plain text in browser,
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Any solution
You need to configure Apache to run your Python scripts as CGI.
Create or add this to .htaccess:
Options +ExecCGI
AddHandler cgi-script .py
Make sure your Python file has a she-bang line at the very top so it can be run on its own:
#! /usr/bin/env python3
Make sure your file is executable:
chmod +x myfile.py
You can verify that it is set up properly by executing it from a command line:
./myfile.py
If that works, then Apache should be able to find it.
While the suggestion to use ExecCGI is good, I find it's better to use a WSGI implementation such as uWSGI or gunicorn instead.
Related
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'm trying to run simple python script in my server (hostgator) and I'm getting error 500 when run it in the url.
this is my .py file: (in public_html folder) CHMOD:755
#!/usr/bin/env 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>";
and this is my .htaccess: (in public_html folder)
AddType text/html py
AddHandler cgi-script .py
Couple of things that might be wrong:
AllowOverride FileInfo is not set for this directory. 500 may indicate that apache has an issue with "malformed" .htaccess
no executable bit on file:
$ chmod +x my.py
suEXEC gets in the way. Simple test for this is:
$ SUEXEC=$(which suexec)
$ mv ${SUEXEC} ${SUEXEC}.orig
$ service httpd restart
In case of suEXEC there are several things you can do:
trace the permissions and other items that might be preventing suexec from normal operation
disable suexec completely by removing/renaming it's executable
use ScriptAlias to define location for your scripts (this seems to sidestep suEXEC completely)
I have a running Apache2 server on Ubuntu, with PHP installed.
My root folder is /usr/local/apache2/htdocs.
I have put a test python script in /usr/local/apache2/cgi-bin, named test.py.
test.py
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
print "Content-Type: text/plain\r\n\r\n"
print 'hi'
I access this in the browser via http://localhost/cgi-bin/test.py. At the moment, it just displays the Python code that I just typed out, rather than executing it. I have looked extensively at online documentations and other stackoverflow questions that address this issue.
From that research I changed my httpd.conf file to include a few Directory's.
httpd.conf (stuff added to end of it)
ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
<Directory "/usr/local/apache2/cgi-bin">
Options ExecCGI
SetHandler cgi-script
</Directory>
<Directory "/usr/local/apache2">
AddHandler cgi-script .py
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
I have restarted/hard-started/stopped Apache and was able to run the Python file successfully through the command-line, but still no luck with actually executing the python file through the browser. I have even checked the Apache error log, but saw no indication of the problem. Can anyone offer some advice?
Thank you
You need to chmod 755 cgi-bin folder(and/or subfolder) where you script is.
Then chmod 755 your script file.
and finally..
Open script in notepad of your preference and save with encoding UTF-8 without BOM, and EOL(end of line) conversion must be set for UNIX systems. (in Notepad++ latter is in Edit menu)
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]
I am trying to execute a Python program using Apache. However, Apache will only serve the file and not actually execute it. The permissions on the file are r/w/x and it is in /var/www. I will post the contents of httpd.conf and the program code after. I also tried to running the python script as a .cgi file but that did not work as well. I have both the mod_python and mod_wsgi modules loaded into apache as well.
Python sample:
#!/usr/bin/python
# enable debugging
import cgitb
cgitb.enable()
print "Content-Type: text/plain\r\n\r\n"
print
print "Hello World!"
httpd.conf:
AddHandler cgi-script .cgi .pl
AddHandler python-program .py
I know its a small httpd.conf file but when I installed apache, there was nothing in the file. I should also mention that this is just so that I learn the basics of running python in apache. It is not meant for production.
Thanks for the help!
Edit
The OS I'm using is Ubuntu 10.04 and the version of apache is 2. I have python version 2.6 which is automatically used when #!/usr/bin/env python is invoked.
I am getting two main errors, the first is that the file is not being found even though the permissions of the file and folder are 777. The error from the log is
[Sun Feb 05 13:29:44 2012] [error] [client 192.168.1.3] File does not exist: /var/www/poit-0.1
This error is for a different python script that I did not write. What is weird is that the file shows up in the index of the folder when accessed from a browser window. However, when I navigate to the file, I get the above error.
The other error that I am getting is premature end of headers. The error is below:
[Sun Feb 05 12:10:19 2012] [error] (8)Exec format error: exec of '/var/www/pyth.py' failed
[Sun Feb 05 12:10:19 2012] [error] [client 192.168.1.3] Premature end of script headers: pyth.py
The first line of httpd.conf: AddHandler cgi-script .cgi .pl is irrelevant, since you're testing python scripts and not perl scripts. And you should define those directives within the location of your python script, and tell apache that it should execute cgi scripts in that location: Options +ExecCGI. This snippet would be a start:
<Directory /path/to/sample.py />
Options +ExecCGI
AddHandler cgi-script .py
</Directory>
Addendum 1:
As per my last comment, try this script. It should spit information about the cgi environment.
#!/usr/bin/python
import cgi
cgi.test()
Addendum 2:
I got your script to work with the above configuration. The problem is that script is written in python2. And the default interpreter apache is invoking to execute the script, is python3 (at least in my case, and chances are this would be the same for you too).
This is a python3 version of the hello world script:
#!/usr/bin/env python
# enable debugging
import cgitb
cgitb.enable()
print("Content-Type: text/plain;charset=utf-8")
print()
print("Hello World!")
Addendum 3:
For the first error, make sure the permission and the ownership of whatever directory and files you're attempting to deploy are properly set. And try adding those directives to httpd.conf:
Order allow,deny
Allow from all
Which will get you this:
<Directory /path/to/sample.py />
Options +ExecCGI
AddHandler cgi-script .py
Order allow,deny
Allow from all
</Directory>
For the second error, unless I am missing something, it looks like apache is invoking python 3 interpreter to execute your script. To rule out this possibility, you might try the following:
ls -al /usr/bin/python*
This will list the python interpreters available on your system. If you have more than one interpreter you'll get something similar to this output:
/usr/bin/python -> python3*
/usr/bin/python2.6*
/usr/bin/python3*
If not, it would be this output:
/usr/bin/python -> python2.6*
/usr/bin/python2.6*
To make sure, this is not the issue you're having, try with this modified sample script:
#!/usr/bin/python2.6
# enable debugging
import cgitb
cgitb.enable()
print "Content-Type: text/plain\r\n\r\n"
print
print "Hello World!"
You'll notice that I explicitly mentioned the version of the interpreter apache should invoke, which is ugly. But for the sake of testing, you can do it. Of course you should map #!/usr/bin/python2.6, to whatever binary you have on your server, and make sure you don't mix python 3 comtipable code with python 2 interpreter and vice versa.
Re: The Exec format error.
I've run in to this myself a couple of times before. I had the exact same (cryptic) error message.
I was developing Python (3) scripts to use via CGI in Notepad++ on my Windows machine, and then uploading them to my Linux server.
After much frustration, I discovered that this issue is related to line endings and you need to convert Windows line endings (\r\n) to UNIX line endings (\n).
In Notepad++ (6.1.5), you can achieve this by going to the Edit menu and selecting the EOL conversion option and then saving the file.
**For apache2 version 2.4
sudo apt-get install python
sudo apt-get install apache2
edit file /etc/apache2/conf-enables/serve-cgi-bin.conf
====comment old section and add code below:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">
AddHandler cgi-script .py
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
=========================================
edit file /etc/apache2/apache2.conf
***add code below:
<Directory "/var/www/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
<Directory "/var/www/cgi-bin">
Options All
</Directory>
<Directory /usr/local/apache2/cgi-bin>
Require all granted
</Directory>
Note!
use for apache2.4
file python keep in directory: /var/www/cgi-bin/
You can test :go to http://localhost/cgi-bin/<namefile>.py
referrence
https://www.linux.com/blog/configuring-apache2-run-python-scripts
In my case, it was a trivial issue. I had to add this line:
#!/usr/bin/python3
to the top of every .py file I wanted to run.
Then, everything started working correctly.
I had the same symptom and my config looked okay compared to the answers above.
I found that my new install was not configured to load mod_cgi.so
Loading the required module looks a bit like this. If restarting the server will give you an error that the file was not found, figure out where the file is adjust the path accordingly.
LoadModule cgi_module modules/mod_cgi.so
I'm assuming you are using firefox. I read somewhere else on the 'net that it could be related to the firefox addons installed on a machine.
I was getting this error and I use firefox 20. Switched to Opera and I did not get any more errors and the python scripts seemed to execute just fine.
Edit: It was actually an eval() method call I had overlooked that had appended "(" and ")" to the results of the script that caused my failure. Once I removed them, it worked for me.
I noticed you did not post the javascript, html, or whatever it is you use to call the script in the first place. Perhaps you could post it for us? I did a direct copy/paste example from a blog site without looking at what I had copied. That was my error.