There are two options.
Access the php file through a local webserver(ie thru a local website). The web-server will deal with the requested php file. It will use either,
- Inbuilt PHP module to interpret the php file, or
PHP through CGI (eg.CGI, FastCGI)
If your apache(check if apache is running usingservice apache2 status!!) is set to the default configuration, this could be as simple ashttp://localhost/path/to/your.phpRemember by default, the base directory for apache is
/var/www/html/, so you need not include this in the url.
Use the php binary directly from a terminal.
php /path/to/your/file.php
There are two options.
Access the php file through a local webserver(ie thru a local website). The web-server will deal with the requested php file. It will use either,
- Inbuilt PHP module to interpret the php file, or
PHP through CGI (eg.CGI, FastCGI)
If your apache(check if apache is running usingservice apache2 status!!) is set to the default configuration, this could be as simple ashttp://localhost/path/to/your.phpRemember by default, the base directory for apache is
/var/www/html/, so you need not include this in the url.
Use the php binary directly from a terminal.
php /path/to/your/file.php
After installation of Lamp system in Ubuntu. Please follow the below two steps to run your php file.
- Place your php file (.php) in /var/www/html/ (default path)
- Please run url as localhost/withfilename.php
Example : I have placed welcome.php file in the /var/www/html/welcome.php
then url will be http://localhost/welcome.php
Videos
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
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.
To open an interactive php shell, just type in a terminal:
php -a
As for opening a file, just:
php filename.php
Environment variables are set in /etc/environment. You will find the $PATH variable in this file. This variable stores the path to binaries in various locations.
To add /opt/lampp/bin to the location searched for binary files, just append this path preceded by a : to the path variable.
For example, if the $PATH variable was:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
add /opt/lampp/bin to the end of it, so that it becomes:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/lampp/bin
After doing this, do a source /etc/environment.