Reloading Apache2 via Terminal in MAC OS X - php

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/.

Related

Reloading php-fpm after deploy via php-deployer without password

What I'm trying to achieve is, to reload php7.2-fpm service via php-deployer. According to php-deployer's documentation, it can be done like the code below:
task('reload:php-fpm', function () {
run('sudo /usr/sbin/service php7-fpm reload');
});
after('deploy', 'reload:php-fpm');
However, I face the error below while deploying:
➤ Executing task deploy:reload_php_fpm
↳ on [prod]
sudo service php7.2-fpm reload
In PhpSecLib.php line 124:
[RuntimeException]
sudo: no tty present and no askpass program specified
System info:
PHP Version 7.2
Ubuntu 18.04 LTS
PHP-Deployer Version 6.3.0
Do you have any idea how can I solve this issue?
After digging around, I finally found the proper answer to handle the situation.
I edited /etc/sudoers file by following command:
sudo visudo
At the end of the file, I added this line:
siamak ALL=(ALL) NOPASSWD:/etc/init.d/php7.2-fpm reload
It means that, the user siamak has permission to just reload php7.2-fpm without entering password. For any other administrative tasks, siamak must enter password.

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.

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

Why I cannot use my apache server?

I just installed apache server on my local machine. I executed the following command for that:
sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5
sudo /etc/init.d/apache2 restart
However, if I put my test.php file in /var/www and open it with my browser I see nothing (I supposed to see "Hello, world!").
If I put test.txt file in this directory with "Hello". I see "Hello" in my browser. My PHP file contains the following.
<?php
print "Hellow, World";
?>
If I open this file with browser I see nothing (no error messages). May be the reason is that before I force browser to open php files by /user/bin/php? How can I remove this setting?
You call it test.php at one point, and test.txt at another. Is it definitely named test.php?
What URL are you using to access the file? Are you using http://localhost/test.php, or file:///var/www/html/test.php?
At one point you say you see nothing, and at another point you say you see "It works"? Which is it?
A few things to check.
Do you see the "It works!" file apache creates?
Were there any install errors with aptitute
Check if the module is loaded
apache2 -t -D DUMP_MODULES

Categories