Execute a PHP 5 Script From the Command Line in Ubuntu Linux

To execute a PHP script from the command line in Ubuntu you must have php5-cli installed. (CLI stands for “Command Line Interface”.)

sudo apt-get install php5-cli

Once cli is installed, you can execute your script like so:

php5 ./myScript.php

Inside the script you can run other commands using your choice of the following PHP functions:

Execute Directly
To execute your script directly, you’ll have to specify the script interpreter on the first line of your script. (AKA a she-bang.)

#!/usr/bin/php5
<?php 
// myScript.php 
echo 'hello world!'.PHP_EOL; 
?>

Make your script executable,

chmod +x myScript.php

and now you can execute it at your convenience.

./myScript.php

This entry was posted in Linux, System Administration and tagged , , , . Bookmark the permalink.

4 Responses to Execute a PHP 5 Script From the Command Line in Ubuntu Linux

  1. somesh says:

    thanx a lot

  2. Akhilesh Saini says:

    How we can run the php scrip in browser using the redis server and php redis…reply plz.

    Thanks in Advance.

  3. Pingback: Установка базы IP адресов со странами и городами « Интересно почитать…

  4. Mehrdad says:

    then use the “cd” command to head to your php5 script directory, and do:
    Code:
    php your-file.php

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>