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.

4 Answers 4

26

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

php -a

As for opening a file, just:

php filename.php
2
  • 1
    The OP problem is: I don't know how to set environment variable for php which is located at /opt/lampp/bin/php . Apr 13, 2014 at 17:08
  • 1
    @darent Thanks. This was the first search result I got from Google and php -a was exactly what I looked for. Feb 25, 2015 at 16:52
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.

8
  • @sud_the_devil: Just edited my answer.
    – jobin
    Apr 13, 2014 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 Do I need to restart my system after that?? Apr 13, 2014 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, 2014 at 14:41
  • 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, 2014 at 14:47
  • 2
    @sud_the_devil Add only /opt/lampp/bin, not /opt/lampp/bin/php which is the executable file. Apr 13, 2014 at 14:48
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

3
  • php5-cli is in a different package.
    – Wutaz
    Apr 13, 2014 at 15:30
  • 2
    @Wutaz On what planet? (hint)
    – e-sushi
    Jul 25, 2014 at 3:10
  • 3
    Doesn't work. Instead of execution a script it just prints file content like cat does
    – Green
    Jul 27, 2017 at 4:12
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

3
  • No, wrong.It just prints file content like cat does. No execution
    – Green
    Jul 27, 2017 at 4:16
  • @Green It seems that you don't have proper set up of php on your system. Jul 27, 2017 at 17:54
  • 2
    It seems that you have to type -f between php and filename to make it work => php -f file.php
    – Green
    Jul 28, 2017 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.