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
-
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
- Path to PHP-CLI
/usr/local/bin/php
-
Run PHP Scripts: To execute a PHP script file, use:
php script.php
-
Run Inline PHP Code: You can run PHP code directly from the command line:
php -r 'echo "Hello from PHP-CLI!\n";'
-
Interactive Shell: Enter the PHP interactive shell:
php -a
To exit, press
Ctrl+D
. -
Pass Arguments to PHP Script: Use
$argv
to handle command-line arguments. For example:php script.php arg1 arg2
-
Useful Commands:
- Check PHP version:
php -v
- Syntax check a script:
php -l script.php
- Check PHP version:
-
Configure PHP-CLI: To view or modify the PHP-CLI configuration, use:
php --ini