How can I run this from PHP?
sudo /etc/init.d/apache2 restart
I don't think that's a good idea.
Also, have a look to the following discussions:
Can PHP restart Apache?
http://www.linuxforums.org/forum/linux-security/3068-using-php-restart-apache.html
You can use the exec command. http://ch.php.net/manual/en/function.exec.php
If you want to use sudo, just add the user Apache is running under (usually www) in the /etc/sudoers file.
Related
I want to restart apache2 when I load a page with the following code:
exec('/etc/init.d/apache2 reload', $output, $return);
if(!$return) {
$result = "<script>console.log('can not restart apache2');</script>";
echo $result;
echo $output;
} else {
$result = "<script>console.log('restart apache2 successfuly');</script>";
echo $result;
}
And in file etc/sudoers I add this lines:
Cmnd_Alias RESTART_APACHE = /etc/service apache2 restart
www-data ALL=NOPASSWD: RESTART_APACHE
But the result return can not restart apache2.
Am I do something wrong?
#tuanptit
<?php echo shell_exec('service httpd restart &'); ?>
You might have permissions problems with such a script attempting to do this though. It sounds like you're in a full-on dev environment though, so it shouldn't matter for you to give elevated privileges to it.
But the best way the best way to handle this, IMHO, is to give the user that Apache runs under access to restart Apache via the sudo command.
You'll want to edit your /etc/sudoers file and add lines similar to the following:
Cmnd_Alias RESTART_APACHE = /sbin/service apache2 restart
www-data ALL=NOPASSWD: RESTART_APACHE
You may need nobody instead of www-data, it depends on the user which Apache runs under. On Debian, Apache typically runs under user www-data, whereas under Red Hat, often Apache runs under user nobody. Also, the /sbin/service apache2 restart may need to be /sbin/service apache restart or maybe /sbin/service httpd restart. All depends on your system's configuration.
Once that's done, in PHP you can use the code:
exec('/sbin/service apache2 restart');
(Obviously changing that if the command to restart Apache differs on your server.)
Please note: this could very well be considered a security risk! If you do this, you fully trust the sudo binary, the service binary, and your system to obey the rules and not let an Apache/PHP process get a root shell. I highly recommend asking on http://serverfault.com for the implications of what you're doing here.
The Apache service probably dont have rights to restart itself
The solution already discussed here.
How do you restart Apache with a (web) button click?
In the sudoers files you have "restart", in php file you have "reload"
Check if you need to use /sbin/service instead of /etc/service
Make sure the commands match in PHP and sudoer file
I am trying to build a vm, where exactly one user (as sudo or not) can brew and start a custom php build.
The current "default" php version should be started as phpbrew fpm process on system start. I was putting something like
phpbrew fpm start
in /etc/rc.local which seems to result in starting up the process. But the process is terminated as soon, as the rc.local script finished. It seems, that without an active login session, the process can't live.
Any suggestions, as how to make the fpm process survive?
Many thanks in advance.
You can modify this config according your needs.
phpbrew 1.22.0 now supports "fpm setup --systemctl" command to help you setup the service.
be sure to enable --with-fpm-systemd when building your php.
In phpbrew build directory it is exists ready file. For example:
~/.phpbrew/build/php-7.0.22/sapi/fpm/init.d.php-fpm
But more comfortable way is: temporary change owner of /etc/init.d to current user, then run command
phpbrew fpm setup --initd
then return back
sudo chown root:root /etc/init.d
sudo chown root:root /etc/init.d/phpbrew-fpm
and then
sudo update-rc.d phpbrew-fpm defaults
If you run it in macOS.
sudo PHPBREW_PHP=$PHPBREW_PHP phpbrew fpm setup --launchctl
and then add it to your launchctl list.
# Activate a system-wide daemon to be loaded whenever the system boots up (even if no user logs in):
sudo launchctl load /Library/LaunchDaemons/<path_to_phpbrew>.plist
I have a working shell script using killall to kill all instances of a program like below:
killall abc
Now, I write a php webpage to execute this script using shell_exec function:
shell_exec('sh ./myscript.sh');
Problem is that my php code works correct on commandline with "php myscript.php", but not works in browsers!. However, I know that the user in commandline is "root" and in php is "apache" (I get this with 'whoami').
The linux distribution is Centos 6 which uses SElinux. I changed the status of selinux to permissive.
Things I've checked:
PHP safe_mode is off
shell_exec() is not present in disable_functions in php.ini
Is there a way to run scripts with kill command using php?
Thank you for your help.
you either have to run apache as root (insecure) or, which would be much safer, you have to run the commands you try to kill as 'apache', or you configure your sudoers file to grant apache rights to killall command:
# vim /etc/sudoers
apache localhost=(ALL) NOPASSWD:/usr/bin/killall
and then change the myscript.sh to do sudo killall abc
Well, i have this program i need to run via either functions however it is located on my dekstop (this ubuntu 11.04).
I moved it to /home/Username, but no dice.
I run
$blah = exec('sudo | echo mypassword | /home/server1/program commandhere', $test);
var_dump($test);
var_dump($blah); ?>
The output is nothing.
I was told if i wanted to run it via sudo i needed to add the Apache user which is www-data to the sudoers list, i added it, but no luck again.
Basically, i've tried A LOT of things, it just wont run. Why?
EDIT:
If i paste that into the terminal it works great, just not with exec,system nor passtrhu.
Use echo mypassword | sudo -S instead.
It also depends on which user has sudo privileges. If you want to run this from the apache process, you need to give the apache user sudo privileges as well.
Also, just to clarify, the command should be:
echo mypassword | sudo -S /home/server1/program commandhere
Look into your security log. Not sure where this is on Ubuntu, possibly /var/log/secure or /var/log/messages. I'm betting that you find a message there similar to sudo requires a TTY, or sorry, you must have a TTY to run sudo indicating that sudo is configured not to work without a real interactive shell. That is, sudo won't permit you to use it in a script or to be called by an external program.
I recently dealt with this issue myself while trying to bind a Gnome keyboard shortcut to a sudo command.
If this is the case, you'll need to comment out the following line in /etc/sudoers
#Defaults requiretty
I am newbie in PHP. I have successfully installed PHP on Ubuntu, now I want start my first program. I am using gPHPEdit as IDE.
Where should I save .php files that I create? And how to run/test them?
Make sure you have LAMP installed. Do a sudo tasksel and select lamp then hit enter, its gotta be the most simple *amp install ever made. Its a good idea to install phpmyadmin: sudo apt-get install phpmyadmin. After that just copy the files to /var/www/ and then they will show up on http://localhost. I recommended using Eclipse PDT or the Netbeans build for PHP.
You should pick up a book or start following some good tutorials on the web.
If you are just scripting using php, you can save them anywhere and run the php on the terminal using the php command line interpreter.
If you are trying write web scripts (and I think you are), you need to install and configure a web server (typically apache) and save your scripts in the server's document root (typically /var/www). Also, I highly recommend you to read up a little about servers and HTTP and figure out how all this works on the inside before learning to building websites in php.
If you cannot save or copy to var/www/html, to run your php scripts on your browser. If you are using Ubuntu 14.04.
I followed these steps and it worked for me.
Execute sudo su on the terminal.
Enter your password
Execute sudo subl /etc/apache2/sites-available/000-default.conf on your terminal to open this file. Note you can change the subl to any text editor to open the file e.g sudo nano /etc/apache2/sites-available/000-default.conf.
Change DocumentRoot /var/www/html to /home/user/yoursubdir
Save the file and close it.
Execute sudo subl /etc/apache2/apache2.conf on your terminal to open this file.
Add the following to end of the file
<Directory /home/user/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Save and Close the file.
Execute sudo service apache2 restart
Go to your browser, type the URL of your script e.g 127.0.1.1/directory/document.php.
I hope this helps.
remove the index.html file from /var/www/
$ sudo rm index.html
create a new php file there:
$ sudo gedit /var/www/index.php
write in it:
<?php
print_r(phpinfo());
?>
Restart your Apache2 Server :
$ sudo service apache2 restart
OR
$ sudo /etc/init.d/apace2 restart
and point to yout localhost and /index.php
if err arises visit : http://www.allaboutlinux.eu/how-to-run-php-on-ubuntu/
https://www.php.net/manual/en/features.commandline.webserver.php
this is an easy way to test your files in php.
$ cd ~/public_html
$ php -S localhost:8000
then you can go to your browser and enter localhost:8000/myfile.php.