21

I have installed XAMPP v1.8.3 for my PHP development. I am new to Ubuntu, so I don't know how to set environment variable for PHP which is located at /opt/lampp/bin/php.

I can run PHP scripts from localhost just fine, but I wanted to run them from the command line as well.

I want to set this variable for every user, since I am the only one who uses this system.

sotirov
  • 4,379

5 Answers5

29

To open an interactive php shell, just type in a terminal:

php -a

As for opening a file, just:

php filename.php
21

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.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
jobin
  • 28,567
13

As an alternative to /opt/lampp/bin/php, to run a php script from the command line, you just need to install php5-cli:

sudo apt-get install php5-cli

And run your script with:

php myscript.php

editor's note: depending on your version, you may need to install php7.0-cli etc instead

Zanna
  • 72,312
0

as 2025 and Ubuntu 24, I was able to run a php script using php or php -f. However, it is important for the scrip to at least start with

<?php

Without this tag, just the script's code will be shown.

0

You can use

php /var/www/html/yourProjctFolder/yourFile.php

This will call your php file and output if you have written echo or print statement

Zanna
  • 72,312