Question

I'm trying to restart my Apache2.2 webserver using a bat file that is being called by a PHP running in IIS using exec(), Apache and IIS is in the same server.

PHP code

exec("cmd /c restart.bat");

bat file

"PATH_TO_APACHE\bin\httpd.exe" -k restart -n Apache2.2

When I load my php via http to restart, I get an "Unable to open logs" error in my Apache error log file.

Majority of my search results say that there is a port 80 conflict however when I do a netstat there is none, my IIS is listening to a different port. Double-clicking the .bat file works, but running it from the PHP gets the error. Any ideas? Please advise.

Was it helpful?

Solution

IIS runs all executable scripts under the context of a specified user, usually this is the IUSR_[MACHINENAME] account. This account can be changed in the IIS configuration in the pool configuration.

The problem you're running into is probably that this user is not allowed to access the log file location.

To track this problem down I would: 1. (dangerous, use only for testing): put the IUSR account in the local administrators group. test again. if it works, you know it is a permission problem. 2. Try using the runas command to run cmd.exe as the IUSR user and execute your command in the console window, that way you see all error messages and test more easily. 3. allow IUSR to write to the apache log location.

A whole different attempt would be to create a marker file in PHP, and use a scheduled task run as Administrator to restart apache if that marker file exists. That way you do not need to give the IUSR account more permissions and have a separate code handling the restart.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top