Running Linux/Unix Commands With PHP
PHP can be used to run Unix and Linux commands on the server.
Last week, while working on a project for a client, I needed to copy a software installation — complete with plain text files, DBM file databases, and encrypted password — to another location on the client's server. The Linux/Unix cp
is perfect for that.
However, I did not have SSH access to type the command into the shell. (The shell is a plain text interface with the server where you type in commands to run certain software on the server and to tell the software what to do. Commands have to be typed because it is not a graphical interface that recognizes the mouse.)
The solution was to build a PHP script that runs the command for me.
It required ten minutes to build and test the PHP script. Two minutes to upload and run it. And anther five minutes or so to verify the copy was accomplished without errors.
Further below is the PHP script you can use to run Linux/Unix commands on your server.
Hosting companies can limit which Linux/Unix commands may be run via PHP.
Have a look at Basic Unix Commands for an overview of the types of commands that may be available for you.
Be that as it may, the cp
command can come in very handy when you need to copy a file or set of files, especially if they include binary databases.
Another command that may come in handy is unzip
. With unzip
, you can, for example, upload the WordPress ZIP file and use
unzip ./wordpress-5.5.5.zip
to unzip it right on the server. (Change the file information, colored blue in the example, according to its name and location.)
If the PHP script is in the same directory as the WordPress ZIP file, ./
informs the script of that fact. If in another directory, the full server path needs to be provided. Example (to unzip in the /wp directory):
unzip {$_SERVER['DOCUMENT_ROOT']}/wp/wordpress-5.5.5.zip
It's much faster to upload a ZIP file and a short PHP script than to upload over a thousand files. If you're a developer and need to frequently install WordPress, this technique can save a great deal of time.
There can be no continuing interaction with the command, no back-and-forth interaction with the server. Only run-once commands will work. In other words, you won't be able to answer questions that the command software might ask (when it asks for a password, for example, or whether you mean to overwrite a file).
To get an idea of all the Linux/Unix commands that may be available to you, go to the Linux and Unix Commands page. Scroll a bit more than halfway down for a list of hundreds of commands. Each command is linked to more information about the command and how to use it.
A fun command is wc
, one that counts lines, words, and characters contained in files. The format is: wc filelocation
Example, using a text file I made for this purpose:
wc ./untitledText.txt
The above responds with:
1603 7914 66728 ./untitledText.txt
The first number is the number of lines in the file. The second is the number of words. And the third is the number of characters. The last item of information is the file that was scanned.
The wc
command can be used for scripts, web pages — any text files. Binary files like ZIP files and images can also be scanned, but the response won't be reliable.
A useful command is ps -e -l
to list the processes that are running on the server, how long they have been running, and other information. It can be a handy reference if the server is slow and you're wondering what is hogging the CPU.
The PHP script is set up to run the ps -e -l
command. After you run the script, you'll understand the information it provides can be useful.
<?php
$command = "ps -e -l";
$output = array();
exec("$command 2>&1",$output);
echo "<p><i>Command:</i> <nobr>$command</nobr></p>";
echo '<pre>',print_r($output,true),'</pre>';
?>
The ps -e -l
command (colored blue) can be replaced with other Linux/Unix commands.
Replace the ps -e -l
command with your own. Upload the PHP script. And type its URL into your browser.
If the command results in an error, the PHP script prints the error message. When there is no error message to print, the software prints any response the command itself replies with.
Running Linux/Unix commands with PHP can be both fun and useful.
(This article first appeared in Possibilities ezine.)
Will Bontrager