Execute PHP Script from command line in Ubuntu

Some times there is a great requirement to execute some codes of PHP immediately, but for that we have to format the output according to HTML format since generally PHP scripts are executed using LAMP.

For example if we want to fetch some data from a table of RDB using PHP and need to see the value then we will have to code lots to see the data in HTML browsers.

There is a way by which we can execute a PHP script directly without using web browser in Ubuntu.

Steps are as below –

  1. Be conform that PHP5 is installed and configured in your Linux Box.

    Install php5-cli by the following command

    sudo apt-get install php5-cli

  2. Now we need to change the file mcrypt.ini, for that use the following command.

    sudo gedit /etc/php5/cli/conf.d/mcrypt.ini

    and change the line

    # configuration for php MCrypt module

    to

    ; configuration for php MCrypt module

    sudo gedit /etc/php5/cli/conf.d/imap.ini

    and change the line

    # configuration for php Imap module

    to

    ; configuration for php Imap module

  3. Now to test our work create a file file_name.php from anywhere. now we are ready to test our PHP scripts by executing from command line!! and then go to file location in the terminal and type

    php file_name.php

Permanent link to this article: https://blog.openshell.in/2011/01/execute-php-script-from-command-line-in-ubuntu/

Leave a Reply

Your email address will not be published.