How to restart apache2 from laravel? - php

I am working on a multi-tenant package Hyn/Multitenant for laravel and after creating a tenant I need to restart apache2. Within the package, I have a command as:
apache2ctl graceful
But when this command executes, it throws an error that it can't bind to the port.
So far I have a tried editing visudo with this command:
www-data ALL=(ALL) NOPASSWD: /etc/init.d/apache2 restart
But it still throws error that it can't bind to port 80.

php exec() method may help you -
exec("/etc/init.d/apache2 restart");
http://php.net/manual/en/function.exec.php

Related

Restarting apache from PHP

I know that there are a lot of similar questions already on the website or even in the another ones but I didn't get the solution even looking for all the information of this questions.
From PHP code, I'm trying:
exec('/etc/init.d/apache2 restart');
And on my sudoers file, I added the following line:
www-data ALL=(ALL) NOPASSWD: ALL
I tried a lot of combinations here like for exaple creating Cmnd_Alias but nothing works.
If I execute this exec, I get "Restarting apache2 (via systemctl): apache2.service failed!". And If I execute this exec command but with sudo, I get an error "Failed to restart apache2.service: Interactive authentication required." (seen on apache error.log). I have seen this error from browser console because I'm running the code from an AJAX post.
But if I try from the cmd, this command ask for a password (I don't understand why if I added the line I explained before on sudoers file):
sudo -u www-data /etc/init.d/apache2 restart
And of course if I add sudo, it works:
sudo -u www-data sudo /etc/init.d/apache2 restart
So I really don't know what is wrong here. If someone can help I will appreciate it because I am quite lost...
Regards

Restart apache2 via PHP script

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

Restart apache on Docker

I am trying to update my .htaccess file on a Docker container. After updating the file I need to restart Apache. Whenever I try to restart Apache: with the command service apache2 restart I get the following error:
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
Action 'start' failed.
The Apache error log may have more information.
...fail!
When I got to the error log it doesn't have any new errors.
This is what my Dockerfile looks like:
FROM ubuntu:12.04
# Install dependencies
RUN apt-get update -y
RUN apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql php5-curl vim
# Install app
RUN rm -rf /var/www/ *
ADD src /var/www
# Configure apache
RUN a2enmod rewrite
RUN chown -R www-data:www-data /var/www
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
Actually you don't need to restart Apache in order to apply changes defined in .htaccess - those are applied during runtime. If you're modifying apache config file (like virtual host definition or something in httpd.conf) you can also reload config without restarting apache using
sudo /etc/init.d/apache2 reload
It's because you are (correctly) not starting apache as a service when you docker run the container. The line:
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
Starts apache in the foreground.
I'm guessing you are then using docker exec to execute a shell in the container to edit the file and restart apache? If so this would explain why the second time you start apache it complains about the existing process.
I think if you are using containers in this way then you are really missing out on the benefit of containers which comes when you treat them as immutable and keep the data outside the container (either on your host or in volumes) so that you can easily replace the container.
In your case if you need to modify the .htaccess file I think it would be more normal to mount that file into the container by using a command like:
docker run -d --name apache -v $(pwd)/.htaccess:/path/to/.htaccess -p 80:80 image:tag
Then if you have to change the file and need to restart apache you can use:
docker restart apache
Although it may be worth investigating the suggestion from Charlotte Dunois that you might not even need to restart apache.

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