Run shell script from PHP

Running a shell script or command from PHP offers a number of conveniences to an administrator. When properly set up, shell scripts and commands can be run by simply requesting a web page over the Internet. The web server would receive the request and then pass it on to the PHP interpreter. By using special PHP functions, we are then able to make PHP run the shell script or command for us.

This technique is very useful as an administrator would no longer be required to login to the server via SSH or some other means in order to run shell scripts and commands on the server. The PHP tutorial below shows the proper way to run shell scripts and commands from PHP.

How to run shell commands from PHP

The PHP shell_exec command allows us to execute shell commands from within PHP. This is the function we will be using to run commands and shell scripts. For example, if we wanted a directory listing of the current directory we may use the following code:

<?php
 
// for Linux
$result = shell_exec('ls -altp');
 
// for Windows
// $result = shell_exec('dir');
 
echo "<pre>$result</pre>";
 
?>

Note how we use HTML Pre Tags to preserve the formatting of the output we receive. The result would be a directory listing, similar to what we would see if we ran the same command in a terminal:

drwxr-xr-x  6 root root 4096 Jan 27 10:51 ./
drwxr-xr-x 29 root root 4096 Feb 25 13:25 ../
-rwx------  4 root root 8900 Aug 21 23:50 backup-script.sh
drwxr-xr-x  8 root root 4096 Dec 20 13:56 temp/

How to run a shell script from PHP

The principle for running a shell script from PHP is the same. We first create a shell script and save it somewhere on our server. Let's say our shell script was located at /path/to/sample.sh and contains:

#!/bin/bash
echo "Tutorial Arena rocks"

We would first need to make our shell script executable by running the following command:

chmod a+x sample.sh

We then use our PHP script to execute the shell script that we just created. It is a good idea to specify an absolute path to the shell script as shown below:

<?php
 
$result = shell_exec('/path/to/sample.sh');
echo $result;
 
?>

When we request our PHP script from within a browser, we will get the following output:

Tutorial Arena rocks

And there you have it, we have successfully run a shell script from PHP.

Security implications from running shell commands from PHP

As you saw above, it is fairly easy to run a shell script or execute shell commands from within PHP. Take care to follow proper coding practices so that your web server remains safe. The last thing you want is an attacker being able to run shell commands from your application. Don't do something silly like pass the commands to be run via a POST or GET method.

The best thing to do is hard-code the shell commands or scripts to be run, and then protect the PHP script itself which runs these commands by putting it in a directory protected by Basic Authentication. That would be the bare minimum we would recommend.

Conclusion

We hope this PHP tutorial has been helpful and that you fully understand how to run shell scripts from PHP. As you saw, running shell commands from PHP are done in a similar way to running shell scripts. Happy coding. Check out articles related to this one below.

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...