Question
PHP CGI vs PHP CLI What's the difference?
Answer
The Official PHP Documentation explains the differences very well:
PHP Docs - PHP-CLI Differences to other SAPIs
In summary:
- PHP-CLI does not provide headers by default
- Some INI configurations are overridden
- A number of constants are defined for I/O streams
- PHP-CLI does not change the working directory to the script being executed
For any of the above points that need further explanation please see the docs link above.
It is worth mentioning that it is possible to pass the -C flag when using the CGI binary to emulate the behavior of the CLI binary, but it is preferred to just use the correct binary to start with. Read on to learn about using the correct binary.
** Critical Security Notice **
IMPORTANT: When running PHP scripts on the command line *DO NOT* use the root user to execute those scripts. Executing PHP scripts via the command line introduces your server to serious security vulnerabilities. If a user-level compromise has occurred on the account that contains the script, and the script you run as the root user has been modified, you would be giving the attacker full root access to do whatever they want with your server.
Before executing any PHP script via cron or command line, switch to the account where the script is located with the su command in combination with the -l flag to set up the user's environment. The -s flag below is optional, and would only be used if SSH has been disabled by default for that user.
su -l -s /bin/bash cpanelusernamehere
How to determine which version is in use
** Before proceeding, read the security notice above
If you're trying to execute PHP on the command line, you may have noticed that there are two different kinds of PHP available. For example, on a default installation of cPanel you'll see the two following paths when running the whereis command:
$ whereis php
php: /usr/bin/php /usr/local/bin/php
Further, you'll notice that one of them is CLI, and the other is CGI:
$ /usr/local/bin/php -v | head -1
PHP 7.4.16 (cli) (built: Mar 9 2021 17:57:39) ( NTS )
$ /usr/bin/php -v | head -1
PHP 7.4.16 (cgi-fcgi) (built: Mar 9 2021 17:58:00)
You can use the full path to whichever version best suits your needs.
Using the "PHP" command on the command line
If you use the "php" command via SSH or Terminal, it will use the /usr/local/bin/php binary which is the CLI version of PHP:
$ which php
/usr/local/bin/php
$ php -v | head -1
PHP 7.4.16 (cli) (built: Mar 9 2021 17:57:39) ( NTS )
$ /usr/local/bin/php -v | head -1
PHP 7.4.16 (cli) (built: Mar 9 2021 17:57:39) ( NTS )
Using the "php" command with cron
If you use the "php" command in one of your cronjobs, it will use the CGI version instead of the CLI version. This can break your cronjob. In order to prevent this from happening, be sure to use the full path to the PHP binary instead of "php". The following guide has more info on this:
How to troubleshoot user level cron issues
Comments
0 comments
Article is closed for comments.