19

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.

Zanna
  • 69,223
  • 56
  • 216
  • 327
sud_the_devil
  • 855
  • 6
  • 12
  • 19

4 Answers4

28

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

php -a

As for opening a file, just:

php filename.php
animaletdesequia
  • 8,306
  • 4
  • 26
  • 43
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
  • 166,822
  • 48
  • 327
  • 400
jobin
  • 27,142
  • 16
  • 100
  • 116
  • @sud_the_devil: Just edited my answer. – jobin Apr 13 '14 at 14:32
  • I opened the file by issuing **sudo subl /opt/lampp/bin/php** and then appended the location as you asked me to do . Here's the link to the opened file. [PATH](http://i.imgur.com/bywNrYr.png) Do I need to restart my system after that?? – sud_the_devil Apr 13 '14 at 14:38
  • 1
    No, you don't need to restart your system. Can you paste the output of `echo $PATH` after you have added the path and done a `source /etc/environment` and `ls /opt/lampp/bin/php`? – jobin Apr 13 '14 at 14:41
  • This is the output of **echo $PATH**: /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games And this is the output of **ls /opt/lampp/bin/php**: /opt/lampp/bin/php – sud_the_devil Apr 13 '14 at 14:46
  • 1
    @sud_the_devil you are required to edit the environment file and not the php executable. Your command should be `sudo subl /etc/environment` – Srihari Apr 13 '14 at 14:47
  • 2
    @sud_the_devil Add only `/opt/lampp/bin`, not `/opt/lampp/bin/php` which is the executable file. – Radu Rădeanu Apr 13 '14 at 14:48
  • @ Srihari @RaduRădeanu Thank you guys, my bad ... It works now – sud_the_devil Apr 13 '14 at 14:50
  • @Jobin : I have added your response as the answer . – sud_the_devil Apr 13 '14 at 14:51
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
  • 69,223
  • 56
  • 216
  • 327
Sylvain Pineau
  • 61,564
  • 18
  • 149
  • 183
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
  • 69,223
  • 56
  • 216
  • 327
RN Kushwaha
  • 109
  • 4