Using Python and PHP together

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

&amp;lt;virtualhost:*.80&amp;gt;

to

Listen 81
&amp;lt;virtualhost:*.81&amp;gt;

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:

&amp;lt;virtualhost:*.80&amp;gt;

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

&amp;lt;/virtualhost&amp;gt;

Listen 81

&amp;lt;virtualhost:*.81&amp;gt;

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

&lt;/virtualhost&gt;

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.

21 comments on “Using Python and PHP together

  1. 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!

  2. 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.

  3. 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 🙂

  4. 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

  5. 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

  6. Do we need to mention the port on the browser to run the app?
    Please could you give me some example URLs of the same server, where PHP and Python apps are running?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.