How to stop multiple httpd instance in macOS Catalina
In the default config httpd starts a parent process usually owned by root which binds to port 80 (a port < 1024), opens log files and spawns child processes. The child process(es) serve http requests. Depending on the number of requests and the level of concurrency they are dynamically added and released. It's completely normal to have several httpd processes in the process list.
You can tweak this by modifying several aspects of httpd.
Further reading with an explanation and pros & cons:
- Apache docs: Apache MPM Common Directives & Apache MPM prefork
- Liquidweb: Apache Performance Tuning: MPM Modules
The number of possible child processes in the apache installed by brew (currently Apache/2.4.43; Server MPM: prefork) can be managed in the Supplemental configuration chapter of the config file. The relevant item is the Server-pool management (MPM specific) part. The number of child processes can be determined in its config file in the Prefork MPM section:
-
Modify Server-pool management (MPM specific) (~line 488 of /usr/local/etc/httpd/httpd.conf) from
# Server-pool management (MPM specific) #Include /usr/local/etc/httpd/extra/httpd-mpm.conf
to
# Server-pool management (MPM specific) Include /usr/local/etc/httpd/extra/httpd-mpm.conf
-
Modify the prefork MPM config (starting at line 21 of /usr/local/etc/httpd/extra/httpd-mpm.conf)
from the default
# prefork MPM # StartServers: number of server processes to start # MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # MaxRequestWorkers: maximum number of server processes allowed to start # MaxConnectionsPerChild: maximum number of connections a server process serves # before terminating <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxRequestWorkers 250 MaxConnectionsPerChild 0 </IfModule>
Process list:
to e.g.
# prefork MPM # StartServers: number of server processes to start # MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # MaxRequestWorkers: maximum number of server processes allowed to start # MaxConnectionsPerChild: maximum number of connections a server process serves # before terminating <IfModule mpm_prefork_module> StartServers 1 MinSpareServers 1 MaxSpareServers 1 MaxRequestWorkers 250 MaxConnectionsPerChild 0 </IfModule>
Process list:
-
Restart apache:
sudo apachectl -k restart
Speedtest (with ab -c 100 -n 1000 http://localhost:8080/index.html
):
default prefork MPM config (StartServers/MinSpareServers/MaxSpareServers 5/5/10):
Server Software: Apache/2.4.43
Server Hostname: localhost
Server Port: 8080
Document Path: /index.html
Document Length: 45 bytes
Concurrency Level: 100
Time taken for tests: 1.075 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 289000 bytes
HTML transferred: 45000 bytes
Requests per second: 929.81 [#/sec] (mean)
Time per request: 107.549 [ms] (mean)
Time per request: 1.075 [ms] (mean, across all concurrent requests)
Transfer rate: 262.42 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 5.7 1 50
Processing: 3 103 68.3 82 310
Waiting: 2 101 67.1 81 309
Total: 10 106 69.4 83 317
Percentage of the requests served within a certain time (ms)
50% 83
66% 116
75% 150
80% 180
90% 216
95% 225
98% 260
99% 307
100% 317 (longest request)
modified prefork MPM config (StartServers/MinSpareServers/MaxSpareServers 1/1/1):
Server Software: Apache/2.4.43
Server Hostname: localhost
Server Port: 8080
Document Path: /index.html
Document Length: 45 bytes
Concurrency Level: 100
Time taken for tests: 1.325 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 289000 bytes
HTML transferred: 45000 bytes
Requests per second: 754.55 [#/sec] (mean)
Time per request: 132.529 [ms] (mean)
Time per request: 1.325 [ms] (mean, across all concurrent requests)
Transfer rate: 212.96 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 4 8.9 1 55
Processing: 10 125 56.4 116 231
Waiting: 1 124 56.2 116 231
Total: 20 129 53.1 119 232
Percentage of the requests served within a certain time (ms)
50% 119
66% 136
75% 170
80% 186
90% 214
95% 221
98% 229
99% 231
100% 232 (longest request)
This won't work for all apaches. I didn't get this to work with for example MAMP/MAMP Pro's apache!
In Terminal, have you tried running:
sudo apachectl stop
After you try stopping httpd
, then restart it, and see how may occurrences you have.
If in macOS Catalina, using the default install of Apache, if I run sudo apachectl start
followed by pgrep httpd
it will show multiple occurrences of httpd
, sometimes just two and other times four occurrences.
I actually never noticed it because on my system httpd
doesn't show up in Activity Monitor and why I used pgrep httpd
from Terminal. (Update: I had Activity Monitor showing only My Processes and changing it to All Processes httpd
now shows.)
I do not know why there are multiple occurrences of httpd
, however it seems normal.