PHP is a versatile tool that millions of sites use to power their content management systems and user interfaces. Its flexibility and dynamic nature make it deal for the era of the Internet that has been dubbed “Web 2.0”. It is, therefore, no surprise that the highly praised LAMP (Linux+Apache+MySQL+PHP) stack includes PHP.
What some may not realize is that you can also put PHP to work for you in your daily system administrator tasks on your dedicated server. If you have invested a good amount of time learning the ins and outs of PHP, there is no reason to master some other language just to run routine command-line tasks. All you need is the PHP Command Line Interface (CLI).
In many Linux distributions, php-cli is a standard package found in the distro repository. It installs a php binary executable program that allows you to run php scripts directly from the Linux or Unix shell. To run a script, first make the script executable:
chmod +x name-of-script.php
then execute by typing:
If you want to avoid having to type “php” before any script you want to execute, just add a shebang to the beginning of your script. For example:
This will tell the script to execute PHP, so that you can now just type the name of the script to execute it:
With your new ability to run PHP from the command line, you can execute more complex tasks, test your web PHP scripts, and schedule your scripts to run at specified times using Cron. For more information about the PHP command line interface, consult the PHP documentation.