Valid for Linux Web Hosting.

Although the PHP scripting language was primarily developed for the creation of dynamic websites, PHP scripts can also be executed completely independently of the web browser. For this purpose, the corresponding PHP file can be called with the so-called PHP Command Line Interpreter (PHP CLI for short) on the command line with the desired PHP version. In the following you will learn how to use PHP CLI to execute PHP files in your webspace.

Requirements

To execute PHP files with PHP CLI, you need:

  • An SFTP account with SSH support. Further information can be found in the article Creating an SFTP/SSH account.
  • The connection data for your SFTP account. Further information can be found in the article Connection data for Secure FTP.
  • A device with an installed SSH program (SSH client). For computers with a Windows operating system, we recommend the PuTTY program. Download PuTTY.

Available PHP versions

The following table lists all PHP versions that can be used with PHP CLI. The Path column contains the path for calling the corresponding PHP CLI program (you can find out more about this in the next section).

PHP versions that are no longer updated by the PHP Group (the organization responsible for the development and maintenance of the PHP programming language) should therefore no longer be used, and are marked with the addition "(obsolete"). The version recommended by IONOS is marked with the addition "(recommended)".

Version Path
PHP 8.4 /usr/bin/php8.4-cli
PHP 8.3 (recommended) /usr/bin/php8.3-cli
PHP 8.2 /usr/bin/php8.2-cli
PHP 8.1 /usr/bin/php8.1-cli
PHP 8.0 (deprecated) /usr/bin/php8.0-cli
PHP 7.4 (deprecated) /usr/bin/php7.4-cli
PHP 7.3 (deprecated) /usr/bin/php7.3-cli
PHP 7.2 (deprecated) /usr/bin/php7.2-cli
PHP 7.1 (deprecated) /usr/bin/php7.1-cli
PHP 7.0 (deprecated) /usr/bin/php7.0-cli
PHP 5.6 (deprecated) /usr/bin/php5.6-cli
PHP 5.5 (deprecated) /usr/bin/php5.5-cli
PHP 5.4 (deprecated) /usr/bin/php5.4-cli
PHP 5.2 (deprecated) /usr/bin/php5.2-cli
PHP 4.4 (deprecated) /usr/bin/php4.4-cli

Note

Only use outdated PHP versions such as PHP 7.4 if it is not possible to update the script, and it is necessary to use the older PHP version for compatibility reasons.

Executing PHP files with PHP CLI

  • Start your SSH client and connect to your webspace. A terminal window with the Linux command line will open. Carry out the following steps in this terminal window.

    Tip: The following article describes how to connect to your web space with the SSH client PuTTY: Connecting to your webspace via Secure Shell (SSH) using PuTTY.
     
  • Change to the directory in which the PHP file you want to execute is located. Execute the command cd in the terminal according to the scheme cd <directory path >. For example, to change to the relative scripts directory, enter the following command and then press the Enter key to execute the command.

    cd scripts

  • To execute a PHP file, enter the path to the PHP CLI program followed by the name of the PHP file in the terminal. The path for the desired PHP version can be found in the table above. For example, to execute the PHP file my_script.php with PHP 8.3, execute the following command:

    /usr/bin/php8.3-cli my_script.php

Please Note

  • Do not execute PHP files with the php or /usr/bin/php command, as this will result in the script being executed with the outdated PHP version 4.4.

  • This also applies to the configuration of web applications: If your web application contains a setting for the path to the PHP program, check whether /usr/bin/php is entered there. If so, replace /usr/bin/php with the path of the PHP CLI version to be used.

Further information