Apache - Python not executing - python

I am having difficulty in getting Python to execute on my Apache web server.
My file looks like this
#! /usr/bin/python2.6
# enable debugging
import cgitb
cgitb.enable()
print "Content-Type: text/plain\r\n\r\n"
print "Hello World!"
I have enabled permissions for CGI execution and Python addHandler in http.conf
Apache is serving exactly what you see above.
Any ideas?

Related

Status 500 on Python file calls

I have a very basic Python script being run as CGI, I am able to run it with only print statements and they execute and display as HTML. When I try to do any file system calls such as using logging, making files or using subprocesses I get an error 500.
#!/usr/bin/python
import cgitb; cgitb.enable()
import cgi
import sys
from subprocess import call
call('ls') #Error here, if I remove I get no error 500 and see "Hello"
form = cgi.FieldStorage()
print "Content-Type: text/html"
print
print "<html><body>Hello"
print "</body></html>"
The CGI script has execute permission:
chmod a+x example.cgi
In my Apache configuration I have the virtual hosts to execute CGI setup:
<Directory "/path/to/cgi/cgi-bin/">
AddHandler cgi-script cgi pl
Options +FollowSymLinks +ExecCGI
AllowOverride None
</Directory>
And setup the CGI module for Apache:
sudo a2enmod cgi
If I run the cgi script directly on the server, all the file manipulation works perfectly, if I try to access it from the browser, i.e example.com/test.cgi it gives me the 500 error. I also ran dos2unix to make sure it's formatted correctly.
Try to use os.system('ls') instead a call, or use it before you enabled the cgitb.

Deploying flask app to Apache shared hosting

I am trying to deploy a simple flask application in the Apache shared hosting server.
I am not sure what is wrong here.
I am stuck at the .cgi file for now.
The flask app - hello.py:
#!/usr/bin/python
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!\n"
if __name__ == "__main__":
app.run()
The myapp.cgi file:
#!/usr/bin/python
import os
from wsgiref.handlers import CGIHandler
from hello import app
os.environ['SERVER_NAME'] = '127.0.0.1'
os.environ['SERVER_PORT'] = '5000'
os.environ['REQUEST_METHOD'] = 'GET'
os.environ['PATH_INFO'] = ""
CGIHandler().run(app)
Both the files are placed in the /home/username/public_html/cgi-bin directory
The same cgi-bin has the directory named myenv - it's a virtualenv I have created. The virtualenv is active.
Now,
I navigate to the cgi-bin directory and run -
python hello.py
I get this :
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
So this is fine. Now I am running the myapp.cgi file:
python myapp.cgi
I get this :
Status: 301 MOVED PERMANENTLY
Content-Type: text/html; charset=utf-8
Content-Length: 251
Location: http://127.0.0.1:5000/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: http://127.0.0.1:5000/. If not click the link.
How can I make this status as 200 OK,
Please suggest.
Thanks!
I had to make few changes in the .cgi file. Below is the final file.
import os
from wsgiref.handlers import CGIHandler
from hello import app
CGIHandler().run(app)
and added these lines in my hello.py file:
import os
import sys
sys.path.insert(0, '/home/username/public_html/cgi-bin/myenv/lib/python2.6/site-packages')
Refer this - https://medium.com/#mohdejazsiddiqui/deploy-flask-app-in-apache-shared-hosting-5b3c82c8fd5e
I think you have 2 big misunderstandings about how apache works with flask with the help of cgi.
Apache uses the system directorys for the python interpreter. You can in fact change the sys.path Like here descriped. But that is far from ideal.
you don't have to call python for your cgi file. The Server will do that when you did your config correctly
in the cgi doc of flask are some ways how you get the server to work with the cgi file.
Since you say you want it to upload at shared hosting writing a .htaccess file for your needs would be the most promesing way, since most of those services only allow you to work from your public dircectory. In this case you also have to use a shared hoster where python is on the server or be willed to install python with all the packages you need for you, since you can't install any packages by yourself.
You could try the changing of the interpreter path, but i have no experience if that would work on shared hosting.

run python scripts on apache (linux and windows)

I need help with how to run scripts test.cgi for example in apache ?
I created test script:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
print "Content-Type: text/plain;charset=utf-8"
print
print "Hello World!
When I runlocalhost/cgi-bin/test.cgi
It gives me error.
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster#localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Apache/2.2.22 (Ubuntu) Server at localhost Port 80
Python is installed properly.
Test.cgi chmod is 755
I am new to linux. On wndows it gives me error too .
On windows it gives me:
End of script output before headers: python_test.cgi
I use this script on windows:
#!"C:\Python34\python.exe"
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
print "Content-Type: text/plain;charset=utf-8"
print
print "Hello World!"
Any idea ?
Add these to your configuration file:
<Directory "/opt/lampp/htdocs/xampp/python">
Options +ExecCGI
AddHandler cgi-script .cgi .py
Order allow,deny
Allow from all
</Directory>
and replace your code with:
import cgitb
cgitb.enable()
print ("Content-Type: text/plain;charset=utf-8")
print ()
print ("Hello World!")
It worked for me. In Python 3.x print is a function, so you have to pass the string to be printed as an arguement.

python 2.7.3 not executing on apache 2.2

Using Apache2.2
Python 2.7.3
in httpd.conf using LoadModule wsgi_module modules/mod_wsgi-win32-ap22py27-3.3.so
python executes perfectly , but not on localhost
in localhost the following script is not being processed, nor are any errors appearing in error.log
What else to check for?
using test script
#!/usr/bin/python2.7.3
# enable debugging
import cgitb
cgitb.enable()
print "Content-Type: text/plain\r\n\r\n"
print
print "Hello World!"
mod_wsgi is a Apache module for running WSGI applications. The example script you've provided is not a WSGI application, it's just regular CGI script.
Hello world in WSGI looks like this:
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
yield 'Hello World\n'
On the other hand, if you want to use old-fashioned CGI, you should use Apache's mod_cgi. Note, that this is quite obsolete and inefficient way to do server-side programming, so I wouldn't recommend it for anything serious.

How do I set up a Python CGI server?

I'm running Python 3.2 on Windows. I want to run a simple CGI server on my machine for testing purposes. Here's what I've done so far:
I created a python program with the following code:
import http.server
import socketserver
PORT = 8000
Handler = http.server.CGIHTTPRequestHandler
httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()
In the same folder, I created "index.html", a simple HTML file. I then ran the program and went to http://localhost:8000/ in my web browser, and the page displayed successfully. Next I made a file called "hello.py" in the same directory, with the following code:
import cgi
import cgitb
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print()
print("""<html><body><p>Hello World!</p></body></html>""")
Now if I go to http://localhost:8000/hello.py, my web browser displays the full code above instead of just "Hello World!". How do I make python execute the CGI code before serving it?
Take a look at the docs for CGIHTTPRequestHandler, which describe how it works out which files are CGI scripts.
Though not officialy deprecated, the cgi module is a bit clunky to use; most folks these days are using something else (anything else!)
You can, for instance, use the wsgi interface to write your scripts in a way that can be easily and efficiently served in many http servers. To get you started, you can even use the builtin wsgiref handler.
def application(environ, start_response):
start_response([('content-type', 'text/html;charset=utf-8')])
return ['<html><body><p>Hello World!</p></body></html>'.encode('utf-8')]
And to serve it (possibly in the same file):
import wsgiref.simple_server
server = wsgiref.simple_server.make_server('', 8000, application)
server.serve_forever()
simplest way to start a cgi server for development is following:
create a base directory with all your html and other files
create a subdirectory named cgi-bin with all your cgi files
cd to the base directory
run python -m http.server --cgi -b 127.0.0.1 8000
Now you can connect to http://localhost:8000 and tst your html code with cgi scripts

Categories

Resources