Controlling Apache

Introduction

There are various ways of controlling an Apache web server on Windows. This page looks at several options available of stopping, starting, and pausing the Apache service or console server, and how then can be used in PowerShell scripts and batch files.

Most of the commands given need to be run with administrator privileges.

This page does not list all the switches for the given commands. Some of the best places to see those are Computer Hope, Learn Microsoft PowerShell, Learn Microsoft Windows Commands, and SS64.


Apachectl

When looking at Apache web server documentation you will often find references to a program named Apachectl. The Windows versions of Apache, at least the ones I've seen, do not use Apachectl which is a command for Unix-like systems. On Windows, you can address httpd.exe directly. You may have to be in the Apache bin folder and open with a command line as an administrator to access httpd.exe


HTTPD

Apache on Windows can be run as either a console application, or more usually, and as I do, as a service. My sites are non-critical and updates I make to the server and its configuration are minor and do not take long. For these reasons, I make the changes then restart the service.

I have read all sorts of things about the HTTPD program and how loads the Apchhe configuration files and the server service. Some of what I read on various sites seemed contradictory and not right, so I took a look at what was actually happening.

httpd.exe should be in the bin folder of the Windows Apache installation folders. Running it at a command line as httpd with /? or -? or /h or -h will show he list of available switches for the program and the one I was interested in is httpd -k restart which tells Apache to do a graceful restart. What Apache does after receiving this command is detailed in the Apache documentation.

Practically this is what happens:

The program process identifier (PID) is updated

The server status page is not shut down and the values recorded do not restart. According to the documentation, this is because the software has to keep track of the server requests to minimize the offline time.

Any changes made to the configuration files take effect when the server restarts

I repeatedly refreshed the Windows Service Management Console but did not see the Apache service actualy stopped. Apache's error log, shows that it did:

[Tue Mar 12 22:46:40.790908 2024] [core:notice] [pid 16448:tid 408] AH00094: Command line: 'c:\\Apache24\\bin\\httpd.exe -d C:/Apache24'
[Tue Mar 12 22:46:40.790908 2024] [mpm_winnt:notice] [pid 16448:tid 408] AH00418: Parent: Created child process 15260
[Tue Mar 12 22:46:41.782885 2024] [mpm_winnt:notice] [pid 15260:tid 456] AH00354: Child: Starting 64 worker threads.
[Tue Mar 12 22:46:42.794904 2024] [mpm_winnt:notice] [pid 14128:tid 424] AH00364: Child: All worker threads have exited.
The 'Apache2.4' service is restarting.
The 'Apache2.4' service has restarted.
winnt:notice] [pid 16448:tid 408] AH00424: Parent: Received restart signal -- Restarting the server.
[Tue Mar 12 22:47:22.614928 2024] [mpm_winnt:notice] [pid 16448:tid 408] AH00455: Apache/2.4.54 (Win64) OpenSSL/1.1.1s configured -- resuming normal operations
[Tue Mar 12 22:47:22.614928 2024] [mpm_winnt:notice] [pid 16448:tid 408] AH00456: Server built: Nov  4 2022 20:49:29

If the Apache service is not runing, then the command starts it.


Apache Service Monitor

ApacheMonitor.exe is a program provided with the Apache web server and is found in Apache's bin folder. When running it adds an icon to the Windows Task Bar System Tray and provides an additional method of controlling the Apache service.

Apache Service Monitor running in the Windows Task Bar

Apache Service Monitor running in the Windows Task Bar System Tray (bottom right)

Left clicking on the Apache Service Monitor icon

Left clicking on the Apache Service Monitor icon

Right clicking on the Apache Service Monitor icon

Right clicking on the Apache Service Monitor icon


Window Service Names

If you open the Windows Service Manager, right click on any of the srvices and open its properties you'll see the services have two names, a "service name" and a "display name". The service name is the "real" name of the service and what is recorded in the registry and used in the NET and SC (service control) commands. The display name is the more human-readable name of the service.

Apache service properties

Apache service properties

In this instance the Apache service and display names are the same - Apache2.4

Net Command

The NET command is limited to only displaying the display names of the services that are running, to do this use:

net start

SC Command

To get a list of all Windows services, running or stopped, with both their display and service names and write them to file use:

sc query state=all > c:\services.txt

The Apache entry in the file produced is this:

SERVICE_NAME: Apache2.4
DISPLAY_NAME: Apache2.4
        TYPE               : 10  WIN32_OWN_PROCESS  
        STATE              : 4  RUNNING 
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

SC QUERY commands are:

sc query - returns running services only
sc query state=all - returns all services, running or stopped
sc query state=inactive - returns stopped services only

WMIC Command

According to Learn Microsoft, WMIC is depreciated in 2016 as of Windows 10, 21H1, but I found it was still working on Windows 11, 23H2 in March 2024.

To get a list of the services, their display name (caption when using WMIC) , service name (name when using WMIC), and whether they are running or not use:

wmic service get name, caption, started > c:\services.txt

To get a list of only the running services use:

wmic service where "started='TRUE'" get name, caption, started 2>nul > c:\services.txt

To get a list of only the non-running services use:

wmic service where "started='FALSE'" get name, caption, started 2>nul > c:\services.txt

The 2>nul part of the command is because the command line uses stream 2 for error messages, this command suppresses them.

PowerShell

To get a list of all services, the server names, their display names and status, use:

Get-Service -Name * | ft -auto > c:\services.txt

To get a list of running services with their server names, their display names, and status, use:

get-service | where-object {$_.Status -eq 'Running'} | ft -auto > c:\services.txt

To get a list of stopped services with their server names and their display names, and status, use:

get-service | where-object {$_.Status -eq 'Stopped'} | ft -auto > c:\services.txt

Sometimes you need a list of services that are not running or stopped such as paused or startpending. To find these use:

get-service | where-object {$_.Status -ne 'Running' -and $_.Status -ne 'Stopped'} | ft -auto > c:\services.txt

The output from the script can be sorted and the column order changed, for example:

get-service | where-object {$_.Status -eq 'Running'} | Sort-Object -Property DisplayName | Format-Table DisplayName, Name, Status | ft -auto > d:\services.txt

In the above lines ft -auto is used. This is used to clear the default column width that PowerShell uses. If not used then long names will be truncated and end with an ellipsis.


Windows Service Manager

Windows Service Manager displays and controls Windows services and there are several methods of opening it. I prefer to just type services in the Windows search bar, but it can also be opened by using a command prompt or Windows Run command (Win key + R) using services.msc.

Once the console is open the various services can be started, stopped or restarted. The Apache service, in my case Apache2.4, should automatically start when the computer starts.

The Apache service in the Services Console

The Apache service in the Services Console

Right clicking in area to the right of the listed services will bring up another menu which includes "Export List...". The information shown on the screen can be saved in various text or CSV formats.


Windows Task Manager

Windows Task Manager can be opened by eitehr typing Task Manager into the Windows search in the Task Bar or by pressing the Ctrl + Shift + Esc keys. The Task Manager provides a lot of infrmation and control over the running processes and services.

In the Processes tab of the Task Manager two instances of httpd.exe can be seen running. This is normal and happens because of the way Apache works. Apache for Windows is multithreaded, it does not use a separate process for each request as it does in Unix. Instead there are usually only two Apache processes running: a parent process, and a child which handles the requests. Within the child process each request is handled by a separate thread.

Apache running two processes in Windows

Apache running two processes in Windows

More information about these two processes can be seen in the Details tab, incliding the PID of each. One process will be using a lot more memory than the other.

The Details tab in Task Manager

The Details tab in Task Manager

The Task Manager can also show the running and available wervices. They can be controlled in the same way they can from the Services Manager.

The Services tab in Task Manager

The Services tab in Task Manager


Stopping and Starting the Apache Service from the Command Line

Windows has several commands that can be used to control services, some can control more than just Windows services and there are some important differences between them. The batch file and PowerShell scripts here will check if apache.2.4, my Apache service, is running, stop it, then start it.

NET Command

NET accepts the display name of a service. On my system, apache2.4 is both the service and display names.

NET can only start, stop or pause services.

NET will send a command and wait for the command to be carried out before returning to the command line or script. This means the following short batch file will work properly:

net stop apache2.4
net start apache2.4

Apache takes a couple of seconds to stop, but as NET waits for Apache to stop, or until it timeouts, before returning control to the script and restarting it.

The following batch file is a little longer, and confirms that the service is stopped and started by looking in the list returned by NET START.

@echo off
NET START | FIND "Apache2.4" >NUL && GOTO running || GOTO notrun
:running
CHOICE /M "The Apache service is running, do you want to stop and restart it?" /C "YN"
IF ERRORLEVEL 2 GOTO end
IF ERRORLEVEL 1 GOTO stopservice
:stopservice
NET STOP "Apache2.4"
NET START | FIND "Apache2.4" >NUL && GOTO err1 || ECHO Confirmed: Apache is stopped

NET START "Apache2.4"
NET START | FIND "Apache2.4" >NUL && ECHO Confirmed: Apache is running || GOTO err2
GOTO end
:notrun
ECHO Apache service is not running
GOTO end
:err1
ECHO Could not stop Apache service
GOTO end
:err2
ECHO Could not start Apache service
GOTO end
:end
pause

Some sources say that the service name must be enclosed in quotes when using the NET command. I have found that it does not matter.

SC Command

SC only accepts the service name of a service. On my system, apache2.4 is both the service and display names.

SC has more advanced controls, can not only start, stop or pause services but also query their state, create and delete them, change their configuration and security.

SC will send a command and return immediately to the command line or script. This means the fllowing short batch file will not work properly:

SC STOP apache2.4
SC START apache2.4

SC will send the stop command to the service but return immediately to the command line or script. It will then try to restart the service even if it has not stopped, which will return an error. Instead, SC QUERY can be used to test if the service is actually stopped, such as this:

@ECHO OFF
ECHO Stopping the Apache service 
SC STOP apache2.4 >nul
:stopping
SC QUERY apache2.4 | FIND "STOPPED" >nul 2>&1
IF ERRORLEVEL 1 (
Timeout /T 1 /Nobreak >nul
GOTO stopping
)
ECHO Apache service is stopped

ECHO.
ECHO Starting the Apache service
SC START apache2.4 >nul
:running
SC QUERY apache2.4 | FIND "RUNNING" >nul 2>&1
IF ERRORLEVEL 1 (
Timeout /T 1 /Nobreak >nul
GOTO running
) 
ECHO Apache service has started
ECHO.
pause

Commands can be run across a network

WMIC Command

Like the SC command, WMIC returns execution when the command is sent, not when the service is stopped or started. A loop is required to test the state of the service before continuing.

@ECHO OFF
ECHO Stopping the Apache service
wmic service apache2.4 call StopService >nul
:false
wmic service where "name='apache2.4'" get started | FIND "FALSE" >nul 2>&1
IF ERRORLEVEL 1 (
Timeout /T 1 /Nobreak >nul
GOTO false
)
ECHO Apache service is stopped

ECHO.
ECHO Starting the Apache service
wmic service apache2.4 call StartService >nul
:true
wmic service where "name='apache2.4'" get started | FIND "TRUE" >nul 2>&1
IF ERRORLEVEL 1 (
Timeout /T 1 /Nobreak >nul
GOTO true
) 
ECHO Apache service has started
ECHO.
pause

PowerShell

New-Variable -Name serviceName -Value 'Apache2.4' -Option Constant
Write-Output "Stopping Apache"
Stop-Service -name $serviceName
$service = Get-Service -Name $serviceName
$service.WaitForStatus('Stopped')
Write-Output "Apache is stopped"

Write-Output "Apache is restarting"
Start-Service -Name $serviceName
$service.WaitForStatus('Running')
Write-Output "Apache has restarted"
Exit

Sources and Resources

Apache Server Status for brisray.com
Choice
Choice command
CHOICE.exe
Find
Find
Find command
Finding the process ID
How to open and use Windows Services Manager (Services.msc)
httpd - Apache Hypertext Transfer Protocol Server - Command switches for the httpd program
Net command
NET.exe
PowerShell Managing Services
PowerShell Get-Service
SC command
SC.exe
Sc.exe query
Stopping and Restarting Apache HTTP Server
Using Apache HTTP Server on Microsoft Windows - Apache notes on Windows usage
WMI queries from the command line
Wmic command
WMIC: WMI command-line utility
WMIC.exe