LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   Apache doesn't recognize python code (http://www.linuxquestions.org/questions/linux-server-73/apache-doesnt-recognize-python-code-649567/)

mohtasham1983 06-16-2008 03:09 AM

Apache doesn't recognize python code
 
Hi,

Currently I have php5 and apache2 running smoothly on my ubuntu hardy machine. I have started coding on python recently, so I tried to write a few small web based application on python. That is, I tried to install libapache2-mod-python. Then I wrote the following script and put it under /var/www/new-folder/hey.py

PHP Code:

#!/usr/bin/python2.5
print "Hey world" 

I restarted the apache server couple of times, but when I try to point the browser to the hey.py file, firefox tries to open it with the text editor.

Any idea how to execute web based python code?

aTxIvG4001 06-16-2008 03:59 AM

Quote:

Originally Posted by mohtasham1983 (Post 3185893)
Hi,

Currently I have php5 and apache2 running smoothly on my ubuntu hardy machine. I have started coding on python recently, so I tried to write a few small web based application on python. That is, I tried to install libapache2-mod-python. Then I wrote the following script and put it under /var/www/new-folder/hey.py

PHP Code:

#!/usr/bin/python2.5
print "Hey world" 

I restarted the apache server couple of times, but when I try to point the browser to the hey.py file, firefox tries to open it with the text editor.

Any idea how to execute web based python code?

you will need mod_python module in your apache OR
you need to run .py script in cgi-bin directory

Regards,
mark

mohtasham1983 06-16-2008 04:25 AM

Quote:

Originally Posted by aTxIvG4001 (Post 3185917)
you will need mod_python module in your apache

How can I add mode_python to apache? Isn't installing libapache2-mod-python using apt enough?

aTxIvG4001 06-16-2008 05:07 AM

Quote:

Originally Posted by mohtasham1983 (Post 3185926)
How can I add mode_python to apache? Isn't installing libapache2-mod-python using apt enough?

Hi,

When u run phpinfo(); can you see mod_python in the list?

to manually install mod_python

1. download latest mod_python from here
http://httpd.apache.org/modules/python-download.cgi

2. install like this
Code:

% tar xfz mod_python-3.3.1.tgz
% cd mod_python-3.3.1
% ./configure
% make
% su
% make install

3. in httpd.conf , add this
Code:

LoadModule python_module modules/mod_python.so
4. reload or restart httpd

regards,
Mark

mohtasham1983 06-17-2008 08:54 PM

Quote:

Originally Posted by aTxIvG4001 (Post 3185973)

When u run phpinfo(); can you see mod_python in the list?

Yes, phpinfo gives me the following output:

Code:

Apache/2.2.8 (Ubuntu) mod_python/3.3.1 Python/2.5.2 PHP/5.2.4-2ubuntu5.1 with Suhosin-Patch mod_wsgi/1.3
I'm using ubuntu, whose httpd.conf file is empty. However, if I go to mod-enabled directories, there is a file called mod_python.load which contains the following:

Code:

LoadModule python_module /usr/lib/apache2/modules/mod_python.so
Everything seems to be alright, but I don't know why I can't run my python program :(

aTxIvG4001 06-17-2008 09:05 PM

Quote:

Originally Posted by mohtasham1983 (Post 3187889)
Yes, phpinfo gives me the following output:

Code:

Apache/2.2.8 (Ubuntu) mod_python/3.3.1 Python/2.5.2 PHP/5.2.4-2ubuntu5.1 with Suhosin-Patch mod_wsgi/1.3
I'm using ubuntu, whose httpd.conf file is empty. However, if I go to mod-enabled directories, there is a file called mod_python.load which contains the following:

Code:

LoadModule python_module /usr/lib/apache2/modules/mod_python.so
Everything seems to be alright, but I don't know why I can't run my python program :(

ah, you may need AddHandler to httpd.conf or .htaccess

Code:

AddHandler mod_python .py
and here is some sample programs that runs on apache

http://www.modpython.org/live/curren.../tutorial.html

Regards,
Mark

mohtasham1983 06-17-2008 10:36 PM

Quote:

Originally Posted by aTxIvG4001 (Post 3187893)
ah, you may need AddHandler to httpd.conf or .htaccess

Code:

AddHandler mod_python .py

I added
Code:

AddHandler mod_python .py
to mod_pythong.load, but I still get the same result. In mods-enabled directory, there is a php5.load file which has only
Code:

LoadModule php5_module /usr/lib/apache2/modules/libphp5.so
That is, I don't think that I need to add the handler to mod_python.load file. In Debian based systems, the httpd.conf file is divided to small files, that's why I don't know which file is the right place :(

By the way, can't
PHP Code:

print "hey world" 

be a good example to test if my python program can be ran by apache?

BTW, yesterday I found out that my web host supports python as well. When I uploaded my python file there, when I pointed the browser to that file, it showed me the command print "hey world". That's because I didn't write it inside <? ?> like in PHP. I think running python applications is different from running php application on apache. I m really confused

aTxIvG4001 06-17-2008 11:29 PM

Quote:

Originally Posted by mohtasham1983 (Post 3187935)
BTW, yesterday I found out that my web host supports python as well. When I uploaded my python file there, when I pointed the browser to that file, it showed me the command print "hey world". That's because I didn't write it inside <? ?> like in PHP. I think running python applications is different from running php application on apache. I m really confused

Ok, I will confirm with my ubuntu and will give you feedback very soon, may b to night :D

Regards,
Mark

mohtasham1983 06-17-2008 11:31 PM

Quote:

Originally Posted by aTxIvG4001 (Post 3187973)
Ok, I will confirm with my ubuntu and will give you feedback very soon, may b to night :D

Regards,
Mark

Thank you very much. I really appreciate it :)

aTxIvG4001 06-18-2008 09:19 PM

Quote:

Originally Posted by mohtasham1983 (Post 3187975)
Thank you very much. I really appreciate it :)

Ok, I confirmed with my ubuntu, its working fine here.

1. In debian/ubuntu, httpd.conf is in /etc/apache2/apache2.conf And /etc/apache2/sites-available/default

2. pls find this in /etc/apache2/sites-available/default
Code:

<Directory /var/www/>
.....
</Directory>

Test 1,

To run A python script called /var/www/test.py
add the following 3 lines inside <Directory /var/www/> like this
Code:

<Directory /var/www/>
#.........
      AddHandler mod_python .py
      PythonHandler test #### test.py
      PythonDebug On
</Directory>

/var/www/test.py
Code:

from mod_python import apache

def handler(req):
        req.content_type = 'text/plain'
        req.write("Hello World!")
        return apache.OK

To Test in the web, go http://localhost/test.py , you will see
Code:

Hello World!
Test 2

Running multiple python scripts, for example, /var/python/*.py
Add the followin block in /etc/apache2/sites-available/default
Code:

ScriptAlias /python/ /var/python/
  <Directory "/var/python">
      SetHandler mod_python
      PythonHandler mod_python.publisher
      PythonDebug On
  </Directory>

/var/python/hello.py
Code:

#""" Publisher example """
def say(req, what="NOTHING"):
  return "I am saying %s" % what

To Test in the web,
1. http://localhost/python/hello.py/say , you will see
Code:

I am saying NOTHING
say is function name in python code as you see.
2. http://localhost/python/hello.py/say?what=Wow, you will see
Code:

I am saying Wow
you can pass parameter like that

Regards,
Mark

mohtasham1983 06-18-2008 10:07 PM

Mark,

That was a wonderful tutorial. I really appreciate it. Unfortunately, I didn't find such information anywhere. This was the best how to for creating web based hello world application.

You have to write it as a how to somewhere more appropriate, so that more people can find it.

Thanks a lot

aTxIvG4001 06-18-2008 11:19 PM

Quote:

Originally Posted by mohtasham1983 (Post 3188771)
Mark,
That was a wonderful tutorial. I really appreciate it. Unfortunately, I didn't find such information anywhere. This was the best how to for creating web based hello world application.

Glad to hear that :D

Quote:

Originally Posted by mohtasham1983 (Post 3188771)
You have to write it as a how to somewhere more appropriate, so that more people can find it.
Thanks a lot

Thanks for idea, I will find somewhere to put it.

Regards,
Mark


All times are GMT -5. The time now is 11:42 PM.