Executing PHP Scripts in the Command Line

For Linux Hosting

Although the PHP scripting language has been mainly developed for the creation of dynamic websites, you can also execute PHP scripts completely independent of the web server. For this purpose, you can call the PHP script on the command line with the so-called PHP Command Line Interpreter (short: PHP CLI).

Requirements:

To run PHP scripts on the command line, you need:

  • A Linux-based web hosting package with shell access (SSH).
  • A device with an SSH client installed. For computers with Windows operating system we recommend the program Putty.
  • Your IONOS FTP server name and the access data for your main FTP user.

To execute PHP files from the command line, do the following:

  • Start the SSH client and connect to your web space.
    Connecting to Your Web space via Secure Shell (SSH)

    You can now enter commands on the Linux command line in the terminal window.
  • If the PHP script you want to execute is located in a subdirectory, use the command cd <directory path> to change to the appropriate directory. For example, to change to the Scripts directory, type the following command:
    cd Scripts
  • You can now execute your PHP script by typing the command to call the PHP CLI program on the command line, specifying the name of the PHP file to be executed. For example, to execute the PHP file my_skript.php, enter the following command:

    /usr/bin/php7.4-cli my_script.php

Note:

The file you want to execute does not require a certain file name extension. For example, your PHP script does not need to end in .php.

Set PHP version

In the example above, you have run your script with the latest version of PHP, PHP 7.4. If your script is not compatible with PHP 7.4, you can also use older versions of PHP. 

The following table shows the PHP versions available in IONOS web hosting with PHP CLI. The Path column specifies the command that must be entered on the command line for each version of PHP.

 

Version Path
PHP 8.0 /usr/bin/php8.0-cli
PHP 7.4 (recommended) /usr/bin/php7.4-cli
PHP 7.3 /usr/bin/php7.3-cli
PHP 7.1 (deprecated) /usr/bin/php7.1-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:

Use outdated PHP versions only if an update of the script is not possible and therefore the use of the older PHP version is necessary for compatibility reasons.

Examples:
  • To execute the file my_script.php with PHP 7.4, enter the following command: /usr/bin/php7.4-cli my_script.php
  • To run the file my_script.php with PHP 7.3, enter the following command:
    /usr/bin/php7.3-cli my_script.php

For more information on how to use PHP CLI, please visit: https://www.php.net/manual/en/features.commandline.php