I'm a LAMP developer trying out Python for the first time.. I'm okay with picking up the syntax, but I can't figure out how to run it on the server! I've tried the following
- uploading
filename.py
to a regular web/public directory. chmod 777, 711, 733, 773... (variations of execute) - putting the
filename.py
in cgi-bin, chmod same as above..
Typing up example.com/filename.py
simply loads a textfile - nothing appears to have been compiled/parsed/etc!
(I believe python is installed, as
whereis python
on my server shows /usr/bin/python
among several other directories)
Many words for a simple question - how do you run a python file on a CentOS server?
This is a big mental shift from PHP. Python files are not simply interpreted like .php files[1]. The simplest way I have found to get up & running with Python is the Bottle framework.
I recommend you spend a short while reading http://docs.python.org/howto/webservers.html. It's very informative.
[1]: Note: there is such a thing as Python Server Pages, but it's not widely used.
you can use cgi, but that will not have great performance as it starts a new process for each request.
More efficient alternatives are to use fastcgi or wsgi
A third option is to run a mini Python webserver and proxy the requests from apache using rewrite rules
I agree with the other comments that there may be more efficient ways to run a Python script. Here are some things to try if you'd just like to run a Python script by dropping it into the cgi-bin directory. You'll first want to locate your httpd.conf file. One way to do this is:
locate httpd.conf
Your httpd.conf file is probably located at /etc/httpd/conf/httpd.conf for CentOS. Edit the file and make sure Python files are recognized, especially if you will use them outside of the ScriptedAliased directories:
AddHandler cgi-script .cgi .py
Restart your Apache web-server:
apachectl restart
Create the following test.py file inside the /var/www/cgi-bin directory (default for CentOS):
#! /usr/bin/python
print "Content-type: text/html"
print ""
print "Hello, World!"
You'll want to make the file executable with:
chmod 775 test.py
That should be all that you'll need to do. You can now visit http://{your-domain}/cgi-bin/test.py and the resulting "Hello, World!" should appear.
精彩评论