Friday, July 04, 2008

How to run simple local php script files without apache

Some said it's not possible. I've busted my head trying to figure out an easy way to test simple php/html scripts, which would run locally using only php, but wouldn't require running an apache server locally.




Well using two packages (which can be stripped to only one) I managed to create a bash script that will take the php script file and transform it to html (which is saved temporarily in the current directory), thus being able to run it easily in firefox. Here we go!

Firstly, install the packages, in Ubuntu:

sudo apt-get install php5-cli debianutils

From these packages we'll use the php command (php5-cli) and the tempfile command (debianutils)



Next we create the bash "phpview" convert script. Fire up the terminal (Applications > Accessories > Terminal):

gedit $HOME/phpview.sh

Inside place the following code:

#!/bin/bash
file=`tempfile -d . -s .html`
echo "Creating file: $file"
php5 $1 > $file
firefox $file
echo "Press any key to delete the temporary html file or ctrl-c to stop this script and keep it"
read
echo "Removing $file"
rm $file



Save and close the gedit text editor and make phpview.sh executable:

chmod +x $HOME/phpview.sh



That's about it, we can create a test php/html file:

gedit $HOME/Desktop/hi.php

Place in the following:

#!/usr/bin/php5 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head><title>Moo</title></head>
<body><?php echo "Hello world!"; ?></body>
</html>



Save and close.

Now let's test it!!

cd $HOME/Desktop
~/phpview.sh hi.php



Woo!

2 comments:

Unknown said...

Hello,

I am the webmaster of www.pinhole-glasses-direct.com

I was reading your blog and I see you have a very impressive way of describing things. The information you provide is very helpful. So I was wondering if you could take a look at our product pinhole glasses and write a review about and post it in your blog.

I would really be interested to know if you would be able to post the review in your blog with links to our site.



Regards,
Sandy Williams
visionresident@gmail.com

Ventrix said...

Πολύ χρήσιμη είναι και η wget με τα flags --post-data --post-file για να κάνεις post/get στο αρχείο.