Using PHP-CLI on a Website Print

  • php-cli, php, cli, command line, easy apache, /usr/local/, binary, cron
  • 101

You would call '/usr/local/bin/php' if you need the php-cli binary, and '/usr/bin/php' if you need the CGI variant. 

If you call 'php' only via the CRON, you're going to end up using the CGI binary, since '/usr/bin' comes first in the CRON $PATH.

To use use php-cli you need to use the path /usr/local/bin/php

 

Using PHP-CLI on Linux

  1. Check if PHP-CLI is Installed: Run the following command to check if PHP is installed:

     
    php -v

    If not installed, you can install it with:

     
    sudo apt-get install php-cli # For Debian/Ubuntu sudo yum install php-cli # For CentOS/RedHat
  2. Path to PHP-CLI

    /usr/local/bin/php
  3. Run PHP Scripts: To execute a PHP script file, use:

    php script.php

  4. Run Inline PHP Code: You can run PHP code directly from the command line:

     
    php -r 'echo "Hello from PHP-CLI!\n";'
  5. Interactive Shell: Enter the PHP interactive shell:

     
    php -a

    To exit, press Ctrl+D.

  6. Pass Arguments to PHP Script: Use $argv to handle command-line arguments. For example:

     
    php script.php arg1 arg2
  7. Useful Commands:

    • Check PHP version:
       
      php -v
    • Syntax check a script:
       
      php -l script.php
  8. Configure PHP-CLI: To view or modify the PHP-CLI configuration, use:

     
    php --ini

Was this answer helpful?

« Back