How can I simply run a PHP script in the command line? Also is it possible to run it like it would be if the URL was typed in?
Asked
Active
Viewed 2.6k times
2 Answers
7
As long as you have php installed, you run a PHP file using
/usr/bin/php /path/to/php/file.php
Or if your $PATH is set up properly to include /usr/bin, then simply
php /path/to/php/file.php
You can check if PHP is installed, by running
which php
Brandon Johnson
- 114
- 4
1
Use the php5 command:
php5 /path/to/php/script
It's part of the php5-cli package.
The php command is part of the alternatives system, so it always points to something else. Depending on the version of PHP your script is in, it would be better to use the versioned command.
Running a PHP script the way it is called by a webserver is a bit complicated. The server sets up quite a few variables, which may or may not be used by the script. It would be much more simpler to run a webserver itself.