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/

Inside place the following code:

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"
echo "Removing $file"
rm $file

Save and close the gedit text editor and make executable:

chmod +x $HOME/

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

gedit $HOME/Desktop/hi.php

Place in the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" dir="ltr" lang="en">
<body><?php echo "Hello world!"; ?></body>

Save and close.

Now let's test it!!

cd $HOME/Desktop
~/ hi.php


1 comment:

Ventrix said...

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