Today i decided to install Python in Ubuntu OS and to use PHP and Python both together. If i hadn’t installed php and wanted to have python as only localhost in my OS, so it was easy, there are plenty of examples which show how to do it(for example). But if you have already installed php and you want to have python, then let’s go on. In this sample we will need two different ports if we want to do so. And i could easily install and test it. So i am sharing how i did it:

You have installed apache2-php in your Ubuntu(or other Linux based OS) and you want to install python for having python localhost. If python has not installed in your OS, do it. Let’s open Terminal:

sudo apt-get install python

Now you have python installed. Now we need apache mod for python. Let’s install it :

sudo apt-get install libapache2-mod-python

Then we must make some apache operations in terminal:

cd /etc/apache2/sites-available/

sudo gedit default 

(default is the name of default apache conf file. )

You must see <virtualhost:*.80> …. </virtualhost> there.

Copy all block and paste it at the bottom. Then you will have two <virtualhost:*.80> …. </virtualhost> blockes. Then find in pasted block(second one) this:


Options Indexes FollowSymLinks MultiViews

AllowOverride AuthConfig

Order allow,deny

allow from all

Add these lines to new line after “allow from all” :


AddHandler mod_python .py

PythonHandler mod_python.publisher

PythonDebug On

Then go to first line of pasted part and edit

<virtualhost:*.80>

to

Listen 81
<virtualhost:*.81>

Save and close the file. Restart apache:


sudo /etc/init.d/apache2 restart

It is ready. Both PHP and Python work together in your OS. PHP has 80, Python has 81 port.

PHP – http://127.0.0.1 or http://127.0.0.1:80

Python – http://127.0.0.1:81

You can test python with simple test file which contains these 2 strings:


def index(req):

return "Python works!";

In default apache virtualhost root localhost folder is /var/www. It is better to use different folders for Php and Python. For clear undersanding i am sharing my apache default conf file:

<virtualhost:*.80>

ServerAdmin webmaster@localhost

DocumentRoot /home/user/public_html/

Options FollowSymLinks

AllowOverride All

Options Indexes FollowSymLinks MultiViews

AllowOverride All

Order allow,deny

allow from all

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None

Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

Order allow,deny

Allow from all

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,

# alert, emerg.

LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"

Options Indexes MultiViews FollowSymLinks

AllowOverride None

Order deny,allow

Deny from all

Allow from 127.0.0.0/255.0.0.0 ::1/128

</virtualhost>

Listen 81

<virtualhost:*.81>

ServerAdmin webmaster@localhost

DocumentRoot /var/www/

Options FollowSymLinks

AllowOverride All

Options Indexes FollowSymLinks MultiViews

AllowOverride AuthConfig

Order allow,deny

allow from all

AddHandler mod_python .py

PythonHandler mod_python.publisher

PythonDebug On

# Uncomment this directive is you want to see apache2′s

# default start page (in /apache2-eh) when you go to /

#RedirectMatch ^/$ /apache2-eh/

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None

Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

Order allow,deny

Allow from all

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,

# alert, emerg.

LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"

Options Indexes MultiViews FollowSymLinks

AllowOverride None

Order deny,allow

Deny from all

Allow from 127.0.0.0/255.0.0.0 ::1/128

</virtualhost>

You can see from last sample that i use /home/user/public_html folder for PHP, and /var/www folder for Python.

That’s all. If any question, you can add it to comments.

15 thoughts on “Using Python and PHP together

  1. Pingback: Elvin Haci’s Blog: Mit Python und PHP zusammen | PHP Boutique

  2. Pingback: Elvin Haci’s Blog: Using Python and PHP together | Scripting4You Blog

  3. Pingback: Elvin Haci’s Blog: Using Python and PHP together

  4. avatar

    Maybe an use case for letting these two scripting languages do their magic together would´ve been nice… Like: Using Pythons CLI & thread magic do the trick for getting around long running PHP processes or so. But enjoy the idea!

    Reply
  5. avatar

    Thanks for the writeup. I’ll just add a couple of points.

    Firstly, mod_python is as good as deprecated. So it’s no longer supported for major Python web projects such as Django. mod_wsgi is the recommended successor.

    Secondly, I’m not sure why you say “Of course we need two different ports”. It’s fairly simple to run mod_wsgi and mod_php in the same instance of HTTPD, on the same port. This would let you even run PHP and Python on the same *site* if required.

    I’m not sure why you would want to do any of this of course :), but I know we all have our edge cases – and certainly the requirement for a further open port is erroneous.

    Reply
  6. avatar

    Thanks all for opinions.
    Dave S. Thank you for comment. I completely agree with you.
    1. In sample i used mod_python because you see, thios article is ggenerally for php developers, who want to start with Python. So they don’t need Django or more advanced tools. It is just for learning, not for professional development.
    2. Yes with httpd it is possible to do plenty of things, also using same port. But for easiness it is better to use different ports, for starters at least. Of course you are right about no need requirement, it is not required, and it was a little mistake by me as i described it so that it seemed as required thing. But although it is not required, it is better for starters :)

    Reply
  7. Pingback: PHP: Links, News and Resources (1) « Angel “Java” Lopez on Blog

  8. Pingback: Python: Links, News and Resources (1) « Angel ”Java” Lopez on Blog

  9. avatar

    When I used
    sudo /etc/init.d/apache2 restart command
    I got this error

    apache2: Syntax error on line 230 of /etc/apache2/apache2.conf: Syntax error on line 51 of /etc/apache2/sites-enabled/000-default: Expected but saw
    Action ‘configtest’ failed.
    The Apache error log may have more information.
    …fail!

    pls help me and i want to use php with django instead of html

    Reply
  10. avatar

    This is really useful to me because the new laptop that I’m using is ubuntu and linux right now. Still not use to it though. I already installed python on my system.

    Anyhow thanks for sharing this information probably I can use it sometime

    Reply
  11. Pingback: Programowanie w PHP » Blog Archive » Elvin Haci’s Blog: Using Python and PHP together

Leave a reply

required

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>