In my PHP file, I ran var_dump(exec('echo $PATH')); and got /usr/bin:/bin:/usr/sbin:/sbin
Then I ran echo $PATHin terminal, I got /Library/PostgreSQL/9.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
How can't I add $PATH variable to Xampp?
Your $PATH represents folders that the system is already aware of, and can access the contents directly. That is all it does. This is a system level thing, not a PHP thing, but anything in your $PATH is already available to PHP.
You should be aware that Mac OSX and Linux both run on a UNIX command line, and it works exactly the same on both.
Say for example I have a directory like:
/usr/local/foobar
I want to use foobar for something. If it is not in my $PATH, then I have to access it with
/usr/local/foobar
If it is in my $PATH, then I can access it with
foobar
The $PATH is separated by : so if my $PATH is /usr/local:/usr/bin, then I have two folders that are known to the system automatically, being:
/usr/local
/usr/bin
And anything in those can be directly accessed.
If I want a list of them in PHP, I would use the following to get a list of the folders:
$path = explode(':', `echo $PATH`);
On the command line, you just do echo $PATH
If you need to add a new folder to it, you would do (from the command line) either:
export $PATH=$PATH:/path/to/new/folder
or
export $PATH=/path/to/new/folder:$PATH
I would use the first one if I want the new folder to be at the end of the results, which would be the case if I were overriding an existing value in it with something else I want it to use instead. I would use the second option if I want my new folder to be put at the beginning of the list.
DO NOT FORGET TO PREFIX OR SUFFIX THE NEW VALUE WITH $PATH, OR YOU WILL LOSE EVERYTHING YOU ALREADY HAD AND PROBABLY BORK YOUR SYSTEM
If you add stuff to the $PATH, you also need to source your .bashrc file to tell the system to reload the path and be aware of the changes, like this:
source ~/.bashrc
The reason your path is different, is because from the command line, you are seeing the $PATH for your user, and from the script, you are seeing the $PATH available to the user the webserver is running under. To resolve this, you have two options:
Run the webserver as your user (which is a bad idea for security reasons, so I'm not going to bother explaining how to do this)
Edit the .bashrc for the user that the webserver runs under (which is a better option)
First, in your script, you want to do
echo `whoami`;
This will tell you what user the webserver is running under.
You can then use sudo su webservername (use the value you got from the above command in place of webservername), then use the above export $PATH=... and source ~/.bashrc to fix the path of the webserver account as needed, then exit to go back to your own account, or just close the shell window. Then go back to your script and do
var_dump( explode(':', `echo $PATH`) );
and you should see the correct values (in addition to whatever other folders the webserver had that you do not on your own $PATH).
You can also use vim or vi to accomplish this, by doing
vi ~/.bashrc
and put one folder per line in that file, then save and close, and do source ~/.bashrc as you normally would to reload it. If you are not familiar with vim, use the export approach instead.
Related
I've been trying and searching about a small issue I have in our php application. We use a which to find the full path of a program, however it returns with
which: no bla in ((null))
on Centos, on our own debian boxes it works just fine. I've figured out since then that (obviously) the PATH is not available in the shell from PHP. But what I can't find out is why that is the case. I've replaced the command with all sorts of commands to find out what the environment is in which I run in.
If I run an echo $PATH I actually see the directories I've set in the .bash_profile. If I run echo $SHELL I know I am using bash, if I run whoami I found out I am not the apache user but a different user, whom I changed the .bash_profile for. As I've read there's a difference between shells, so I thought it might not load the .bash_profile so I've also added the export PATH to the .bashrc.
I can get it to work with a change in the code, I could replace the which, but I am just genuinely interested in why this is not working on this centos configuration. I've now added an export PATH in the exec function before the which and it works, and should also work on other systems, but still I think it shouldn't be necessary.
Anyone know what could cause this behaviour?
I have two remote servers that need to have a certain codebase in sync. I have it all set up on the first, but while setting it up on the second, I ran into a problem:
They don't have hg installed in the same location. I can't just run hg <command> because it returns the error:
sh: hg: command not found
So I had been using the full path, but that won't work with two separate hg locations.
I thought it would be clever to run which hg and use the path from the response, so here's the new code:
$hg = trim(`which hg`, "\n");
$output = `{$hg} pull -u`
But $hg is NULL, so that doesn't work! I can ssh into the 2nd server and see that which hg definitely does work. I even appended 2>> path/to/log to see if there were any errors with the which command, but there was not. I made sure it was writing to the log, so it wasn't related to that.
I am not running in safe mode, and I am definitely allowed to run shell_exec because other commands work.
I know I could just create a symlink so they both had the same path and just hardcode the path, but it's driving me crazy why shell_exec('which hg') isn't working!!!
which hg will only work if hg is already in your path, otherwise it will return the empty string.
Obviously this is not what you want, since if it were already in your path, you could just use hg.
If you're using many machines with different configs, you could just create a bin directory in your home directory, add it to your path and make links to the commands you need in that directory. That will allow you to use the same commands on all machines.
For PHP, you'd need to put it in a directory accessible to the web server (but preferably outside the document root), besides that, you can use the same technique there by giving an absolute path to the common directory where you created your links.
try
shell_exec('/usr/bin/which hg');
instead. $PATH may not be set and that is a typical location of which
I'm using git bash on a windows 7 machine. When I try to use mysqldump I'm getting an error:
"sh.exe": mysqldump: command not found". I am very new to this. Can somebody help me?
You'll need mysqldump on the path that bash is using to find commands. UN*X shells, like bash, and also windows cmd, use a PATH environment variable to manage this.
To see your current path, type this in bash:
$ echo $PATH
If you've installed MySQL, it's typically located at c:\Program Files\MySql, and the support programs, like mysqldump, are in c:\Program Files\MySql\MySQL Server 5.5\bin\. You'll need to have that directory on your path. If MySQL has been installed somewhere else on your machine then you'll need to find the bin directory there and use that path in these instructions.
The best way to do this in windows is by changing the user or system environment variables. To do this, in Windows Explorer:
Right-click Computer and select Properties from the menu.
In the resulting window, choose Advanced System Settings in the sidebar.
In the resulting dialog, click the Advanced tab.
Near the bottom, click the Environment Variables button.
There are 2 sections, user at the top, and system below. Either will work, but let's change the system settings.
Scroll the list until you see the variable named Path in the Variable column.
Double-click this Path row.
In the resulting dialog, in the Variable value field, scroll all the way to the end of the field, and at the end, add this: ;c:\Program Files\MySql\MySQL Server 5.5/bin/. Note the leading semicolon: it won't work without it.
Click OK and now you can close all the dialogs/windows.
Once you've done all that, close the Git bash window, and open another one. Type:
$ mysqldump
You should get Usage instructions from the program.
If not, look at the path again by typing:
$ echo $PATH
Do you see anything like what you added? Any MySQL directories there? It's likely you've just mistyped something, or missed the semicolon. Go over the steps again and make sure everything is correct.
i am trying to run this piece of php code on my server:
<?php
$cmd = 'echo "this is a test" > /home/ubuntu/scripts/test_file';
echo exec($cmd);
?>
From my understanding it should add the piece of text to the file test_file . The file exists in the appropriate location and i have tried chmod 755 and chmod 777 on the php file. But i dont see the text being added to the text_file . I tried running the linux command directly on the server and it works. Could some one tell me what i am doing wrong?
Also, i am trying to create a virtual host file on the server through a php script. Rather than running the commands through php exec() , i thought it would be better to run a shell script, with the shell script reading the required parameters from a text file and setting the directory path in the virtual host file. I am new to linux, is this a good approach or is there a better way in going about this? All this is being done to setup a magento based site programatically. Thanks.
Your code is OK. The problem probably either lies with your php being in safe mode (though it's deprecated, see link) or with file/directory permissions.
No need to give the file permissions 0777 since that makes the file executable, 0666 should suffice. It is not enough however for the file to have the right permissions, each directory on the path must be traversable. Try a different directory to which the user with whose privileges the php code runs has access, /tmp is a good start.
General way to debug problems like this is to execute a different command which gives you extra information about the context in which echo is executed, e.g.
<?php
echo exec("id");
echo "<br/>";
echo exec("ls -l /home/ubuntu/scripts/test_file");
?>
(remember exec() only returns the last line of command's output, these display just one line though). These commands will tell you the user which runs the code and whether they can see the file at all.
As the comment already said: this is actually bad way to accomplish what you're trying to do, as writing Apache configuration based on user input through web could open you up to multiple issues.
What you might consider, is to have the PHP side write the required information to a file, or a database, which is then polled every now and then via a cron script or similar by a different process that does the actual configuration changes. This eliminates the need to exec() from PHP (which is always bad). With this, your process that runs PHP wouldn't need to have write permissions to important system files.
I am currently working on my own little project, but I have a little problem: I want to set the $PATH environment variable to ./bin, so that when I use exec() and similar functions, it would only search for binary files in that directory (unless I explicitly tell it otherwise).
I have already tried putenv(), which won't work unless I have safe-mode enabled, which I'd prefer not to; and I also tried apache_setenv(), but that didn't seem to work either.
Are there any other solutions I might want to try?
(I am using a Linux machine with PHP 5.3.2)
If you want to set it only in specific circumstances, you can do:
exec("PATH=/my/path ./bin");
The way to alter the PATH used by apache on Mac OS X is described here:
http://lists.apple.com/archives/macos-x-server/2008/Sep/msg00433.html
As stated in that post:
[A]dd the following text into [the file
/System/Library/LaunchDaemons/org.apache.httpd.plist] at the fifth
line:
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin</ string>
</dict>
See the man page for launchd.plist(5) for details on the syntax I'm
using here.
If you need to run your PHP commands as CLI sessions, you'll also
probably need to add /opt/local/bin as a new path under /etc/paths.d
work. For instance, something like this:
shell> sudo echo "/opt/local/bin" >> /etc/paths.d/macports
See the man page for path_helper(8).
instead of setting the path to bin and calling foo, why don't you just explicitly invoke bin/foo?
If You have path set for Yours user AND if Your scripts run as Yours user, only thing You should do, is to set up this path for Yours shell, but is Your's scripts run as ie. apache user (www-data in debian-like systems) for this to work, You should set this PATH for that user explicte