python 2.7.3 not executing on apache 2.2 - python

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.

Related

run python3 script on linux apache 2.4

I have looked everywhere and tried many suggested solutions, still without the required result: to run a python file from my lamp server. I can not seem to integrate all the pieces of the puzzle ... Complicating the story is that many solutions either use old apache version (<2.4), which changed the config files significantly. No more httpd.conf! so this executing-a-python-script-in-apache2 does not help; But also the python version being > 3 complicates matters.
specs:
linux Kubuntu, apache 2.4, python 3.5
apache is running
website files are in root/var/www/html/, I have sudo access to this folder.
apache2 cgi module enabled: a2enmod cgi
the python 3.5 path is usr/bin/env python3
the python script, simplest of scripts, has been made executable
#!/usr/bin/env python3
print ("Content-type: text/html\n")
print ("Hello world!")
lets boil it down to the simplest case: I would like to have apache interpret the spark.py script and spit out the html: "Hello world!"
Questions:
is the script file correct as is?
which config files do I need to change and what do I need to add to these config files?
I know for security reasons, you should not have apache run script in your root dir.
The python documentation for modwsgi seems to fit what you are asking for. The following webpage has a really simple example and the necessary configuration for a python3-apache2 setup.
http://modwsgi.readthedocs.io/en/develop/user-guides/quick-configuration-guide.html
You will need to install the mod_wsgi for the configuration to work. Take note of the different "_" underscore and "-" dash character used in apt and pip3.
$ sudo apt install apache2-dev libapache2-mod-wsgi-py3
$ sudo pip3 install mod_wsgi
libapache2-mod-wsgi-py3 and mod_wsgi seems to be the same thing. However, my test deployment only works after installing mod_wsgi. Could be configuration issue. The following are the details of the configuration I have tested on Ubuntu 16.04.2.
Application file /home/user/wsgi_sample/hello.wsgi:
def application(environ, start_response):
status = '200 OK'
output = b'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Apache2 configuration /etc/apache2/sites-available/000-test.conf
<VirtualHost *:80>
ServerName testmachine
<Directory /home/user/wsgi_sample>
Order allow,deny
Allow from all
Require all granted
</Directory>
WSGIScriptAlias /hello /home/user/wsgi_sample/hello.wsgi
</VirtualHost>
Enable the site in apache2.
sudo a2ensite 000-test.conf
Open your browser to testmachine/hello.
wsgi may also be deployed on Apache2 using passenger. It demands a slighter longer configuration. Ask a new question if passenger/python3 is desired.
Yes, your minimum code seem correct. The Apache config information is answered here
https://stackoverflow.com/a/57531411/4084546

Apache - Python not executing

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?

Flask not working in Virtualenv setup

SetUp
VirtualBox | Ubuntu Server 12.04.2
(flaskve)vks#UbSrVb:~/flaskve$ python --version
Python 2.7.3
ifconfig
192.168.1.100 (the bridge interface on which i interact with VirtualBox)
code I am trying to run.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='192.168.1.100', port=8080, debug=True)
When I do
(flaskve)vks#UbSrVb:~/flaskve$ python start.py
(flaskve)vks#UbSrVb:~/flaskve$
It does not run or do anything, it just returns back to command prompt. Although I am running in debug=True mode.
I then made a new VirtualEnv and install bottle in that. When I tried to run helloworld it shows the same behaviour.
However I then started the python shell on the same virtualenv, imported bottle modules and ran
>>> from bottle import route, run
>>> run(host='192.168.1.100', port=8081, debug=True)
Bottle v0.11.6 server starting up (using WSGIRefServer())...
Listening on http://192.168.1.100:8081/
Hit Ctrl-C to quit.
What could be problem here ?
Even debug does not show anything.
Following link is the output of python -v start.py
http://paste.ubuntu.com/5713138/
The first example uses Flask, not bottle. Maybe you are confusing your code snippets here? :)

Setting up Python with WSGI on Apache for a directory

I am trying to setup Python with WSGI for a particular directory on Apache but I am getting the following error:
mod_wsgi (pid=3857): Target WSGI script '/var/www/test/test.py' does not contain WSGI application 'application'.
My test.py contains:
print 'Hello, World!'
And my wsgi.conf contains:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /usr/local/bin/python2.7
Alias /test/ /var/www/test/test.py
<Directory /var/www/test>
SetHandler wsgi-script
Options ExecCGI
Order deny,allow
Allow from all
</Directory>
On top of all that, interestingly enough, the web browser returns a "404 Not Found" error but thankfully the error_log is a little more enlightening.
What am I doing wrong?
You're using WSGI as though it was CGI (strangely without headers).
What you need to do, for your immediate problem is adapt the following from http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
So that you have application present.
And from the referenced doc.
Note that mod_wsgi requires that the WSGI application entry point be
called 'application'. If you want to call it something else then you
would need to configure mod_wsgi explicitly to use the other name.
Thus, don't go arbitrarily changing the name of the function. If you
do, even if you set up everything else correctly the application will
not be found.

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