Currently, I have PHP and Perl running fine on my machine. What I want to do is run Python websites (Development). I have Python installed. I looked online for ways of doing this, but unfortunately can't run my hello.py file that prints "Hello World" on the screen. (Screen shows the source code, and I have #!/usr/bin/python on the top of the file). I tried to modify my httpd.conf file but it crashed the server and nothing would load. (I followed this tutorial: How do you set up Python scripts to work in Apache 2.0?)
What are the things I need to do/change to successfully run Python website on my Mac?
The easiest way to get a simple "Hello, World!" script running is to enable cgi handling of the scripts. Basically you would modify your httpd.conf and search for an "AddHandler cgi-script" line. Add .py to the end of it so that it looks something like this:
AddHandler cgi-script .cgi .py
Also, you may need to enable cgi handling for that filetype in that folder.
<Location /some/path/*.py>
Options +ExecCGI
</Location>
Finally, make sure you're serving the right content type and headers and out comes your content:
print "Content-type: text/html\n\n"
print "<html>Hello world!</html>"
If you use a framework like Flask, your Python app can self-host for development.
Related
I have a very simple Python script that I want to run though my website. Here is my script:
#!/usr/bin/python
print("This line will be printed.")
Suppose my script is called 'hello.py' and my website is 'mywebsite.com'. My web hosting is provided by BlueHost, and I can access the server through FileZilla. I place 'hello.py' in the public_html directory on the server (which also contains my website html files). Now I try to run the Python script though the browser, so in my web browser I go to 'mywebsite.com/hello.py'. In the web browser, the source code of 'hello.py' is printed. Is there a way to execute the Python script instead?
The following modifications should be made for success:
(1) Python script slightly modified to the following:
#!/usr/bin/python
print("Content-Type: text/html")
print
print("This line will be printed.")
(2) Add the following into the 'IfModule mod_rewrite.c' field of your .htaccess file:
<IfModule mod_rewrite.c>
Options +ExecCGI
AddHandler cgi-script .py
</IfModule>
Here is the successful output result on my browser, after searching 'mywebsite.com/hello.py':
Contrast this to the output result if either instructions (1) or (2) are not followed - the browser simply outputs the Python source code. For instance, violating instruction (2) yields:
Note that the BlueHost Python or CGI guides do not provide this information. My solution was attained after a series of guess-and-checks, and there might be a more proper way of doing this.
I am running XAMPP control panel 3.2.2 in order to have Apache and MySQL hosted, and I have correctly imported PyMySQL and cgi, which I have verified to be installed.
However, when I try and run a test script to make sure that everything is working together, it prints part of the program correctly, but not the other stuff. Images attached.
How do I fix this? I have tried Googling solutions but nothing to solve it has come up, and the python.exe location is definitely correct.
Modules verified
Program displayed
Test program
XAMPP running
Alright I fixed it, here is how I did it for anyone looking at this question:
Open the Apache httpd.conf configuration file from XAMPP
Locate the AddHandler block of code using CTRL-F
Where it says
AddHandler cgi-script .cgi .pl .asp
Add .py to the end of the list so it instead becomes:
AddHandler cgi-script .cgi .pl .asp .py
Save the file with .py appended onto it
Restart XAMPP and then launch the file from a web browser, where it should be working
It took me quite a while to figure out how to make Apache2.4 run my "Hello, world!" python script. I have finally figured out what sequence of commands I have to run in the command line for the script to work. Unfortunately, I still don't understand what is happening when I run those commands. I would like to know why they make my script work. I know it's all in the documentation, but so far I find it a bit hard to comprehend what's written there.
Here goes the list of commands I used.
sudo apt-get install apache2
sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
sudo service apache2 restart
sudo a2enmod cgi
sudo service apache2 restart
Any comments on steps 2, 3 and 5 would be highly appreciated.
After that I create script.py in /usr/lib/cgi-bin:
#! /usr/bin/python
print "Content-type: text/html\n\n"
print "Hello, world!"
For some reason the first two lines of the script.py are absolutely necessary. There is no way the code is going to run without them.
And finally I run:
sudo chmod +x /usr/lib/cgi-bin/script.py #why do I need this? how come it is not executable by default?
sudo service apache2 restart
When I call http://localhost/cgi-bin/script.py I get my Hello, world!
I didn't even have to modify apache2.conf, serve-cgi-bin.conf or 000-default.conf
If there is a more obvious/better/correct way to run a python script using Apache24, I would really love to learn it.
P.S. Some people recommend adding AddHandler cgi-script .py .cgi to /etc/apache2/conf-enabled/serve-cgi-bin.conf if you encounter a problem when running a script on Apache. But for some reason it doesn't make any difference in my case. Why?
P.P.S. I use Ubuntu 14.04.
The event Multi-Processing Module (MPM) is designed to allow more
requests to be served simultaneously by passing off some processing
work to supporting threads, freeing up the main threads to work on new
requests.
http://httpd.apache.org/docs/2.2/mod/event.html
This Multi-Processing Module (MPM) implements a non-threaded,
pre-forking web server. Each server process may answer incoming
requests, and a parent process manages the size of the server pool. It
is appropriate for sites that need to avoid threading for
compatibility with non-thread-safe libraries. It is also the best MPM
for isolating each request, so that a problem with a single request
will not affect any other.
http://httpd.apache.org/docs/current/mod/prefork.html
5)
a2enmod is a script that enables the specified module within
the apache2 configuration.
http://manpages.ubuntu.com/manpages/lucid/man8/a2enmod.8.html
The name a2enmod stands for apache2 enable module.
For some reason the first two lines of the script.py are absolutely
necessary.
The first one tells apache how to execute your cgi script. After all, there are other server side languages, like php, perl, ruby, etc. How is apache supposed to know which server side language you are using?
The second line outputs an HTTP header, which is the simplest header you can use. The headers are required to be output before the body of the request--that's the way the http protocol works.
sudo chmod +x /usr/lib/cgi-bin/script.py
why do I need this? how come it is not executable by default?
A file cannot be executed unless an administrator has given permission to do so. That is for security reasons.
If there is a more obvious/better/correct way to run a python script
using Apache24, I would really love to learn it.
Most of the commands you listed are to setup the apache configuration. You shouldn't have to run those commands every time you execute a cgi script. Once apache is configured, all you should have to do is start apache, then request a web page.
P.S. Some people recommend adding:
AddHandler cgi-script .py .cgi
to /etc/apache2/conf-enabled/serve-cgi-bin.conf if you encounter a
problem when running a script on Apache. But for some reason it
doesn't make any difference in my case. Why?
See here:
AddHandler handler-name extension [extension]
Files having the name extension will be served by the specified
handler-name. This mapping is added to any already in force,
overriding any mappings that already exist for the same extension. For
example, to activate CGI scripts with the file extension .cgi, you
might use:
AddHandler cgi-script .cgi
Once that has been put into your httpd.conf file, any file containing
the .cgi extension will be treated as a CGI program.
http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler
So it appears that when you add the AddHandler line it is overriding a configuration setting somewhere that does the same thing.
Response to comment:
ScriptInterpreterSource Directive
This directive is used to control how Apache httpd finds the
interpreter used to run CGI scripts. The default setting is Script.
This causes Apache httpd to use the interpreter pointed to by the
shebang line (first line, starting with #!) in the script
http://httpd.apache.org/docs/current/mod/core.html
On the same page, there is this directive:
CGIMapExtension Directive
This directive is used to control how Apache httpd finds the
interpreter used to run CGI scripts. For example, setting
CGIMapExtension sys:\foo.nlm .foo will cause all CGI script files with
a .foo extension to be passed to the FOO interpreter.
The mpm stands for Multi-Processing Module; basically you replaced the event based approach with the prefork; this is used internally by Apache and often does not affect anything beyond performance (each of these have different performance characteristics), but some things are not compatible with some MPM's and then you need to change them.
The cgi module is an additional module that provides the Common Gateway Interface; it is not included in Apache by default anymore.
The first line of the script is the shebang; it tells Unix/Linux kernel what program to use as the interpreter; that is; "use /usr/bin/python to run this file please". The file extensions do not mean anything in *nix w.r.t executability.
The second line are the headers. The CGI specification says that the output shall be headers followed by an empty line, followed by the content. 1 header is mandatory: the Content-Type. Here you are telling the webserver and browser, that what follows is a document of type text/html. '\n' stands for a newline. (Technically you should write
print "Content-type: text/html\n\n",
with a comma there, otherwise you get one newline too much).
Files in *nix don't have the +x execute bit on by default - this is a security feature; a conscious decision is required to make something executable.
As for the preferred method, since you control the server, use the Apache mod_wsgi with any web framework - Pyramid, Flask, Django, etc; WSGI applications are much more efficient than the CGI.
I have been dabbling in Python over the past few weeks and thought it might be fun to mess around with writing some output in python on a website. Understanding that apache isn't the "ideal" environment to run python on the web, I would like to try to put some simple junk on a website. Maybe connect to a database and write a very simple blog or something.
How do I make apache on my shared hosting account read my python file? I have created hello.py with this content:
#! /usr/bin/python
print "Content-type: text/html"
print ""
print "Hello, World!"
and dropped this .htaccess:
Options +ExecCGI
AddHandler cgi-script .py
but when i try to visit domain.com/test.py I get a 500 Internal Server Error.
Any ideas?
you mentioned that you created hello.py and so you are visiting domain.com/test.py that seems wrong in that case after making hello.py executable using sudo chmod +x hello.py command you should visit domain.com/hello.py
I am new to python cgi programming. I have installed apache 2.2 server on linux mint and I have my html form in var/www folder which is being displayed properly. Action to the form is a cgi script that I've put in the folder /usr/lib/cgi-bin. But on submit, it says "The requested URL /usr/lib/cgi-bin/file.cgi as not found on this server." Does anyone know the fix for this?
What is the name of your Python program? Is it /usr/lib/cgi-bin/file.cgi?
What are the rights of this file? Can it be read by apache? Can it be executed?
Is first line with #!/usr/bin/env python or similar good? Make sure that this file can be run from command line (ie. shebang is good)
Does apache receive request with that file? Look at apache logs, especially error.log and access.log (probably in /var/log/apache)
Make sure you have enabled ExecCGI for /usr/lib/cgi-bin/ directory in Apache configuration. For examples see at: http://opensuse.swerdna.org/suseapache.html. You can also use ScriptAlias directive.