Restart apache2 via PHP script - php

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

Related

Starting PHPBrew FPM on system start

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

php7 fpm sock file ownership on service restart

I have just installed PHP7
https://github.com/kasparsd/php-7-debian
Everything works except each time I do service php7-fpm restart I need manually to set ownership on file /run/php7-fpm.sock to www-data
After setting the permissions everything works.. But how to avoid this on every restart?
The ownership is always root after restart
php-fpm.ini
user = www-data
group = www-data
Ownership of the file socket is determined by the listen.owner and listen.group directives in the FPM config file.
If thats your local development environment, just add www-data to the sudo. That should make things easy.
Also try reinstalling PHP
Following tutorial might be helpful.
https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-ubuntu-14-04

Different php result between terminal and browser

My server is running php-fpm with nginx. I had applied these permissions.
sudo chgrp -R www-data /usr/share/nginx/html
sudo chmod -R g+rw /usr/share/nginx/html
sudo chmod g+s /usr/share/nginx/html
I have this code.
if(function_exists("my_void_function")){
echo "exist";
}else{
echo "none exist";
}
I am getting different result that simple code. you can see screenshot of my pc from this image.
As you have added the function via a custom extension, you must load the extension with the respective php.ini file of the PHP engine. PHP CLI has a different php.ini than PHP-FPM/mod_php.
Also, after changing php.ini of PHP-FPM, you must restart the PHP-FPM service (usually sudo service php-fpm restart on Debian-like systems). Those who are using mod_php with Apache need to restart the Apache service.

launch sudo from shell with php

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.

Reloading Apache2 via Terminal in MAC OS X

Am trying to reload Apache 2 via /init.d/apache2 reload command in terminal but I get a command not found error and when I look at the etc/ directory in Finder I can't see any init.d folder. Is there somewhere else this init.d folder might be?
Just type in the terminal:
sudo apachectl graceful
graceful reloads the configuration files and gracefully restarts. Any current connections are allowed to complete.
For more info on the apachectl command just type:
man apachectl
And if that doesn't work, you can try sudo apachectl restart. Just for reference, OS X doesn't use init.d (although it used to); it uses launchd instead. See http://launchd.macosforge.org/.

Categories