How to execute php scripts using command line in Linux

How to execute php scripts using command line in Linux, if you want to learn how to run php scripts in linux, I’ll show you in this article. Before proceeding further Install Apache and PHP5 in Linux. After installing create a simple PHP file and executing that file through Command Line Interface in Linux. Here I’m using ubuntu Linux, you can use any Linux distribution that doesn’t make any difference.

You can run PHP5 scripts from command line. To run PHP5 scripts from command line you should install php5-cli package. To install php5-cli you can enter the following command in the terminal prompt:
[bash] sudo apt-get install php5-cli

After installing test it by using this command, if it goes correct then you will see a message with version and all details.
[bash] php -v

Create a simple php file for executing, save it anywhere but remember the path. You will use this path in next step.
[php] <?php
echo "testing through command line";
echo "\n";

Then after doing all the above steps, run php script through command line shell in Linux.
[bash] php your-path-to-php-file.php

then you can see output in browser itself.

If you have any problem executing or running a php script form command line, let me know using comment form.

Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.