ããã¯ããªã«ãããããŠæžãããã®ïŒ
Apacheã®MPMã®èšå®âŠæ¥ç¶æ°ãããã»ã¹æ°ãã¹ã¬ããæ°ãŸããã®èšå®ãã¡ããã¡ããèŠãå²ã«ã¯èŠããŠããããªãã®ã§ã
1床ã¡ãããšèŠãŠãããããªãšããããšã§ã
ç°å¢
ä»åã®ç°å¢ã¯ããã¡ãã
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.2 LTS Release: 20.04 Codename: focal $ uname -srvmpio Linux 5.4.0-65-generic #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Ubuntu Linux 20.04 LTSã§ãã
ãã¡ãã«Apacheãã€ã³ã¹ããŒã«ããŸãã
$ sudo apt install apache2
èµ·åã
$ sudo systemctl enable apache2 $ sudo systemctl start apache2
Apache 2.4.41ãã€ã³ã¹ããŒã«ãããããã§ãã
$ apachectl -V Server version: Apache/2.4.41 (Ubuntu) Server built: 2020-08-12T19:46:17 Server's Module Magic Number: 20120211:88 Server loaded: APR 1.6.5, APR-UTIL 1.6.1 Compiled using: APR 1.6.5, APR-UTIL 1.6.1 Architecture: 64-bit Server MPM: event threaded: yes (fixed thread count) forked: yes (variable process count) Server compiled with.... -D APR_HAS_SENDFILE -D APR_HAS_MMAP -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE -D APR_USE_PTHREAD_SERIALIZE -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD -D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=256 -D HTTPD_ROOT="/etc/apache2" -D SUEXEC_BIN="/usr/lib/apache2/suexec" -D DEFAULT_PIDLOG="/var/run/apache2.pid" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="mime.types" -D SERVER_CONFIG_FILE="apache2.conf"
ããã©ã«ãã®MPMã¯ãeventã®ããã§ããã
Server MPM: event threaded: yes (fixed thread count) forked: yes (variable process count)
ããã§ãApacheã®çšæã¯ã§ããŸããã
Apacheã®MPMã®çš®é¡ãšããã¥ã¡ã³ã
Apacheã®MPMãšããã®ã¯ãMulti-Processing Modulesã®ç¥ã§ãããã¹ãã®ããŒãã«ãã€ã³ãããŠãªã¯ãšã¹ããåãä»ãã
ãªã¯ãšã¹ããåŠçããããã«åã«ãã£ã¹ãããããã¢ãžã¥ãŒã«ã®ããšãèšããŸãã
Apache HTTP Server 2.0 extends this modular design to the most basic functions of a web server. The server ships with a selection of Multi-Processing Modules (MPMs) which are responsible for binding to network ports on the machine, accepting requests, and dispatching children to handle the requests.
Multi-Processing Modules (MPMs) - Apache HTTP Server Version 2.4
ã©ã®MPMãããã©ã«ãã§äœ¿ããããã¯ç°å¢ã«ãã£ãŠæ±ºãŸã£ãŠããããã§ãããUnixã©ã€ã¯ãªOSã ãšpreforkãworkerãeventã®ã©ããã«
ãªãããã§ããã
èªåã®ç°å¢ã ãšãeventã«ãªããŸããããšã
netwareãos2ãwinntãšããMPMããããŸããããã¡ãã¯ä»åã¯çœ®ããŠãããŸãããã
workerãeventãpreforkã®åMPMã®èª¬æã®ãµããªãŒãèŠãŠã¿ãŸããä»ã®ããã©ã«ãã®MPMãèŠããševentãworkerãpreforkã®é ã
æ£è§£ãªæ°ãããŸãããeventãworkerã®å
容ãèŠãŠãããšå
ã«workerãèŠãæ¹ãããæ°ãããã®ã§ã
worker MPMã¯ããã«ãããã»ã¹ãµãŒããŒããã«ãã¹ã¬ãããµãŒããŒã®ãã€ããªãããªå®è£
ã§ããã¹ã¬ããã䜿çšããããšã§
ããã»ã¹ããŒã¹ã®ãµãŒããŒãããå°ãªããªãœãŒã¹ã§å€ãã®ãªã¯ãšã¹ããåŠçã§ããŸãããã ããããããå€ãã®ã¹ã¬ããã管çãã
ããã»ã¹èªäœãè€æ°æã€ããšã§å®å®æ§ãä¿ã£ãŠããŸãã
This Multi-Processing Module (MPM) implements a hybrid multi-process multi-threaded server. By using threads to serve requests, it is able to serve a large number of requests with fewer system resources than a process-based server. However, it retains much of the stability of a process-based server by keeping multiple processes available, each with many threads.
worker - Apache HTTP Server Version 2.4
ãªã®ã§ããã«ãããã»ã¹ãµãŒããŒãšãã«ãã¹ã¬ãããµãŒããŒã®ãã€ããªãããªããã§ããããã®mpmã§éèŠãªãã£ã¬ã¯ãã£ãã¯
åããã»ã¹ããšã®ã¹ã¬ããæ°ãã³ã³ãããŒã«ããThreadsPerChild
ãããããèµ·åã§ããæ倧ã¹ã¬ããæ°ãã³ã³ãããŒã«ãã
MaxRequestWorkers
ã®ããã§ãã
event MPMã¯ããªã¹ããŒã¹ã¬ããã«ããã€ãã®åŠçãæž¡ãããšã§ã¯ãŒã«ãŒã¹ã¬ãããéæŸããããå€ãã®ãªã¯ãšã¹ããåŠçã§ããããã«
èšèšãããMPMã§ãã
The event Multi-Processing Module (MPM) is designed to allow more requests to be served simultaneously by passing off some processing work to the listeners threads, freeing up the worker threads to serve new requests.
event - Apache HTTP Server Version 2.4
èšå®ã¯worker MPMã»ãŒåããªã®ã§ãããAsyncRequestWorkerFactor
ãšãããã£ã¬ã¯ãã£ããå°å
¥ãããŠããããã¡ãã§
ããã»ã¹ããšã«åãå
¥ããããæ¥ç¶æ°ãã³ã³ãããŒã«ã§ããŸãã
prefork MPMã¯ããã®åã®éãããªãã©ãŒã¯ãªWebãµãŒããŒã§ãã芪ããã»ã¹ãå®éã«ãªã¯ãšã¹ããåŠçããåããã»ã¹ã管çãã
ã¢ãã«ã§ããApacheã«çµã¿èŸŒãã¢ãžã¥ãŒã«ãã©ã€ãã©ãªã«ã¹ã¬ããã»ãŒãã§ã¯ãªããã®ãå«ãŸããŠããå Žåãäºææ§ã®ããã«
䜿çšãããããªäœçœ®ã¥ãã§ãããŸãããªã¯ãšã¹ããããã»ã¹åäœã§åé¢ãããŠããã®ã§ããããªã¯ãšã¹ããä»ã®ãªã¯ãšã¹ãã«
圱é¿ãäžããããšã¯ãããŸãããæãéèŠãªãã£ã¬ã¯ãã£ãã¯MaxRequestWorkers
ïŒä»¥åã¯MaxClients
ãšåŒã°ããŠãããã®ïŒã§ãã
prefork - Apache HTTP Server Version 2.4
MPMã«é¢ããå ±éçãªãã£ã¬ã¯ããã£ãã¯ããã¡ãã«æžãããŠããŸãã
mpm_common - Apache HTTP Server Version 2.4
åMPMã®æŠèŠã¯ããã£ãããããªæãã§ããã
MPMãèšå®ãã
ãããã®MPMã®èšå®ãè¡ãããšã§ãApacheã§åãä»ããããæ¥ç¶æ°ããããŠãªã¯ãšã¹ããåŠçããã®ã«äœ¿ãããã»ã¹æ°ãã¹ã¬ããæ°ã
èšå®ããããã§ãããããã1床æŽçããããªãšã
åMPMããšã«ãããã¥ã¡ã³ããèªãã§æžããŠãããŸãã
worker MPM
ãŸãã¯worker MPMã®èšå®ãèŠãŠãããŸãããã
worker - Apache HTTP Server Version 2.4
ãã£ã¬ã¯ãã£ãå | 説æ |
---|---|
ThreadsPerChild |
ããã»ã¹ããšã«çæããã¹ã¬ããæ° |
ThreadLimit |
ããã»ã¹ããšã«æãŠãã¹ã¬ããæ°ã®äžé ïŒThreadsPerChild ãšåãããããã¯å€§ããå€ã«ããïŒ |
StartServers |
æåã«èµ·åããããã»ã¹æ° |
MaxRequestWorkers |
åæã«åŠçã§ããæ倧æ°ïŒåããã»ã¹ããšã®ã¹ã¬ããæ°ã®åã®äžéïŒ |
ServerLimit |
ããã»ã¹æ°ã®äžéïŒMaxRequestWorkers / ThreadsPerChild ãã倧ããå€ã§ããå¿
èŠãããïŒ |
MinSpareThreads |
åŸ æ©ã¹ã¬ããã®æå°æ° |
MaxSpareThreads |
åŸ
æ©ã¹ã¬ããã®æ倧æ°ïŒMinSpareThreads ãšThreadsPerChild ã®åã«çããããããã¯å€§ããå€ã§ããå¿
èŠãããïŒ |
Serverãããã»ã¹ã«é¢ããèšå®ãæããThreadãã¹ã¬ããã«é¢ããèšå®ãæãããšèŠãŠããã°è¯ãããã§ãã
åºæ¬çã«ã¯ããã¡ãã«æžãããŠããå 容ã§ãã
mpm_common - Apache HTTP Server Version 2.4
ãªããããã»ã¹æ°ã®æå®ãããã£ã¬ã¯ãã£ãããããŸããããMaxRequestWorkers
ã®å€ãThreadsPerChild
ã§å²ã£ãå€ã§ç®åºãããŸãã
Ubuntu Linux 20.04 LTSïŒApache 2.4.41ã§ã®ãworker MPMã®ããã©ã«ãèšå®ã¯ãã¡ãã§ãã
/etc/apache2/mods-available/mpm_worker.conf
<IfModule mpm_worker_module> StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule>
MaxConnectionsPerChild
ãšããã®ã¯ãåããã»ã¹ãåŠçãããªã¯ãšã¹ãæ°ã§ãã0以å€ã®å€ãæå®ããŠããå Žåã
ããã»ã¹ã¯MaxConnectionsPerChild
ã§æå®ãããæ°ã ããªã¯ãšã¹ããåŠçãããšãããã»ã¹ãçµäºããŸãã
MaxConnectionsPerChild Directive
ã¡ã¢ãªãªãŒã¯åé¿ã®ããã«äœ¿ããããµãŒããŒããã»ã¹ãäœãçŽãããå Žåã«äœ¿ãæãã§ããã
0ã®å Žåãããã»ã¹ã¯ãªã¯ãšã¹ãã®åŠçæ°ã§çµäºããªããªããŸãã
ããšãServerLimit
ã¯æžãããŠããŸããããworker MPMããããã¯event MPMã®å Žåãããã©ã«ãå€ã¯16ã§ãã
event MPM
event MPMã®èšå®ã¯ãworker MPMãšã»ãŒåãã§ããAsyncRequestWorkerFactor
ãå¢ããã ãã§ããã
event - Apache HTTP Server Version 2.4
ãã£ã¬ã¯ãã£ãå | 説æ |
---|---|
AsyncRequestWorkerFactor |
ããã»ã¹ããšã®åææ¥ç¶æ°ã®å¶éå€ |
ThreadsPerChild |
ããã»ã¹ããšã«çæããã¹ã¬ããæ° |
ThreadLimit |
ããã»ã¹ããšã«æãŠãã¹ã¬ããæ°ã®äžé ïŒThreadsPerChild ãšåãããããã¯å€§ããå€ã«ããïŒ |
StartServers |
æåã«èµ·åããããã»ã¹æ° |
MaxRequestWorkers |
åæã«åŠçã§ããæ倧æ°ïŒåããã»ã¹ããšã®ã¹ã¬ããæ°ã®åã®äžéïŒ |
ServerLimit |
ããã»ã¹æ°ã®äžéïŒMaxRequestWorkers / ThreadsPerChild ãã倧ããå€ã§ããå¿
èŠãããïŒ |
MinSpareThreads |
åŸ æ©ã¹ã¬ããã®æå°æ° |
MaxSpareThreads |
åŸ
æ©ã¹ã¬ããã®æ倧æ°ïŒMinSpareThreads ãšThreadsPerChild ã®åã«çããããããã¯å€§ããå€ã§ããå¿
èŠãããïŒ |
event MPMã¯ãäžéšã®æ¥ç¶ãéåæã§åŠçããŸãããã®ã±ãŒã¹ã§ã¯ãã¯ãŒã«ãŒã¹ã¬ããã¯å¿
èŠã«å¿ããŠçæéã ãå²ãåœãŠããã
ãã®ä»ã®ã±ãŒã¹ã ãšæ¥ç¶ããšã«ã²ãšã€ã®ã¯ãŒã«ãŒã¹ã¬ããã䜿çšãããŸããã¯ãŒã«ãŒã¹ã¬ããã䜿ãå°œãããå Žåã§ãéåæåŠçã
ã§ããããã«ãéåæåŠççšã®ã¯ãŒã«ãŒã¹ã¬ãããã©ã®ãããçšæããããšããèšå®ã§ãã
event MPMãã©ã®ãããã®æ¥ç¶æ°ãæ±ãããã¯ã以äžã«èšç®åŒãæžãããŠããŸãã
AsyncRequestWorkerFactor Directive
åããã»ã¹ã¯ãæ¥ç¶æ°ã以äžã®å€ãããäœããã°ãæ°ããæ¥ç¶ãåãä»ããŸãã
ThreadsPerChild
ïŒ ïŒAsyncRequestWorkerFactor
à ã¢ã€ãã«ç¶æ
ã®ã¯ãŒã«ãŒã¹ã¬ããæ°ïŒ
ã¢ã€ãã«ç¶æ ã®ã¯ãŒã«ãŒã¹ã¬ããã®å¹³åå€ããããå Žåãå šããã»ã¹ã§ã®æ倧æ¥ç¶æ°ã¯ä»¥äžã®åŒã§ç®åºã§ããŸãã
ïŒThreadsPerChild
ïŒïŒ AsyncRequestWorkerFactor
à ã¢ã€ãã«ç¶æ
ã®ã¯ãŒã«ãŒã¹ã¬ããæ°ïŒïŒ à ServerLimit
ããã©ã«ãã§ã¯AsyncRequestWorkerFactor
ã®å€ã¯2ãªã®ã§ãã¢ã€ãã«ç¶æ
ã®ã¯ãŒã«ãŒã¹ã¬ããã®2åãéåæçšã®ã¹ã¬ãããšããŠ
æ±ããããšã«ãªãããã§ããã
â» ãªã®ã§ããããã®åŸã§è©ŠããŠã¿ããïŒåŸè¿°ïŒãã¡ãã£ãšããããããªããªããŸããâŠ
Ubuntu Linux 20.04 LTSïŒApache 2.4.41ã§ã®ãevent MPMã®ããã©ã«ãèšå®ã¯ãã¡ãã§ãã
/etc/apache2/mods-available/mpm_event.conf
<IfModule mpm_event_module> StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule>
prefork MPM
æåŸã¯ãprefork MPMã§ãã
prefork - Apache HTTP Server Version 2.4
ãã£ã¬ã¯ãã£ãå | 説æ |
---|---|
StartServers |
æåã«èµ·åããããã»ã¹æ° |
MaxRequestWorkers |
åæã«åŠçã§ããæ倧æ°ïŒåããã»ã¹ããšã®ã¹ã¬ããæ°ã®åã®äžéïŒ |
ServerLimit |
ããã»ã¹æ°ã®äžéïŒMaxRequestWorkers ããã倧ããå€ã§ããå¿
èŠãããïŒ |
MinSpareServers |
åŸ æ©ããã»ã¹ã®æå°æ° |
MaxSpareServers |
åŸ æ©ããã»ã¹ã®æå€§æ° |
worker MPMãevent MPMãšæ¯ã¹ããšãã¹ã¬ããããªãåã ãã·ã³ãã«ã«ãªããŸããããåŸ
æ©åã®èšå®ããã¹ã¬ããã§ã¯ãªãããã»ã¹
ïŒãã£ã¬ã¯ãã£ããšããŠã¯Server
ïŒã«ãªã£ãŠããŸãã
Ubuntu Linux 20.04 LTSïŒApache 2.4.41ã§ã®ãprefork MPMã®ããã©ã«ãèšå®ã¯ãã¡ãã§ãã
/etc/apache2/mods-available/mpm_prefork.conf
<IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule>
prefork MPMã®å ŽåãServerLimit
ã®ããã©ã«ãå€ã¯256ã§ãã
åMPMã§ãèµ·åããããã»ã¹ãã¹ã¬ãããåãä»ããããæ¥ç¶æ°ãå®éã«è©ŠããŠç¢ºèªããŠã¿ã
ãããŸã§ããã¥ã¡ã³ããèªã¿ã€ã€åMPMãèŠãŠãããŸãããããã®èšå®ãšå¹æãå®éã«åãããŠç¢ºèªããŠã¿ãããšæããŸãã
以äžã®å 容ã§ç¢ºèªããŠã¿ãŸãããã
- ç°¡åãªHTTPãµãŒããŒãApacheãšã¯å¥ã«ç«ãŠãApacheããã¯
mod_proxy_http
ã§ãããã·ãã- ãã®ã¢ããªã±ãŒã·ã§ã³ã¯ãApacheã®
MaxRequestWorkers
以äžã®æ¥ç¶æ°ãåãå ¥ãããããã®ãšãã - ãã€ããšãŠãåŠçãé ããã®ãšãã
- ãã®ã¢ããªã±ãŒã·ã§ã³ã¯ãApacheã®
- Apacheã®MPMãåãæ¿ããªãããApacheã«å¯ŸããŠå€æ°ã®ã¢ã¯ã»ã¹ãè¡ã
- ãããã·å ãé ãã®ã§å¯èœãªæ¥ç¶æ°ã䜿ãåãã¯ããªã®ã§ããã®æã®æ¥ç¶æ°ãããã»ã¹æ°ãã¹ã¬ããæ°ãªã©ã確èªããŠã¿ã
- Apacheã®åMPMã®èšå®ã¯ãUbuntu Linux 20.04 LTSïŒApache 2.4.41ã§ã®ããã©ã«ãèšå®ãšãã
ç°¡åãªHTTPãµãŒããŒã¯ãJavaã§äœããŸããJavaã®ããŒãžã§ã³ã¯ãã¡ãã
$ java --version openjdk 11.0.9.1 2020-11-04 OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04) OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)
ããã°ã©ã ã¯ããããããã®ãçšæããŸããã
SimpleHttpd.java
import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; public class SimpleHttpd { public static void main(String... args) throws IOException { ExecutorService es = Executors.newFixedThreadPool(200); HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0); server.setExecutor(es); server.createContext("/", new SimpleHttpHandler()); server.start(); System.out.println(LocalDateTime.now() + " server startup."); } static class SimpleHttpHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { try { TimeUnit.SECONDS.sleep(60); } catch (InterruptedException e) { // ignore } byte[] message = "Hello World!!".getBytes(StandardCharsets.UTF_8); exchange.sendResponseHeaders(200, message.length); try (OutputStream os = exchange.getResponseBody()) { os.write(message); } } } }
èµ·åã¯ããããªæãã§ã
$ java SimpleHttpd.java 2021-02-07T14:21:33.252448 server startup.
ãHello World!!ããšè¿ãã ãã®ç°¡åãªãµãŒããŒã§ãããæ¥ç¶æ°ã¯200ã1ãªã¯ãšã¹ãããšã«1åã®ã¹ãªãŒããè¡ããŸãã
ãã®ãµãŒããŒã«å¯ŸããŠãmod_proxy_http
ã§ãããã·ããŸãããã
mod_proxy_http
ãæå¹ã«ããŠ
$ sudo a2enmod proxy_http
Apacheããããã·ããããã«èšå®ã
$ grep -vE '^\s*#' /etc/apache2/sites-enabled/000-default.conf <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined ProxyPass / http://localhost:8080 ProxyPassReverse / http://localhost:8080 </VirtualHost>
確èªã
$ time curl -i localhost HTTP/1.1 200 OK Date: Sun, 07 Feb 2021 05:30:04 GMT Server: Apache/2.4.41 (Ubuntu) Content-length: 13 Hello World!! real 1m0.584s user 0m0.013s sys 0m0.025s
OKã§ãã
ã¢ã¯ã»ã¹æ°ãåºãããã®ããŒã«ãšããŠã¯ãVegetaã䜿ããŸããã
GitHub - tsenart/vegeta: HTTP load testing tool and library. It's over 9000!
ã€ã³ã¹ããŒã«ã
$ curl -sLO https://github.com/tsenart/vegeta/releases/download/v12.8.4/vegeta_12.8.4_linux_amd64.tar.gz $ tar xf vegeta_12.8.4_linux_amd64.tar.gz
ä»åã®ç°å¢ã§ã®ãããã¯ãã°ã®äžéã確èªããŠãããŸãã
$ sysctl net.core.somaxconn net.core.somaxconn = 4096
Apacheã®èšå®ãšããŠã¯ListenBackLog
ã䜿ãããã§ãããã©ã«ãã§511ã®ããã§ãïŒä»åã¯æªèšå®ïŒã
worker MPM
ãŸãã¯ãworker MPMãã確èªããŸãããã
event MPMããworker MPMãžåãæ¿ããApacheãåèµ·åããŸãã
$ sudo a2dismod mpm_event $ sudo a2enmod mpm_worker $ sudo systemctl restart apache2
worker MPMã«åãæ¿ãã£ãããšã確èªïŒServer MPMïŒã
$ apachectl -V Server version: Apache/2.4.41 (Ubuntu) Server built: 2020-08-12T19:46:17 Server's Module Magic Number: 20120211:88 Server loaded: APR 1.6.5, APR-UTIL 1.6.1 Compiled using: APR 1.6.5, APR-UTIL 1.6.1 Architecture: 64-bit Server MPM: worker threaded: yes (fixed thread count) forked: yes (variable process count) Server compiled with.... -D APR_HAS_SENDFILE -D APR_HAS_MMAP -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE -D APR_USE_PTHREAD_SERIALIZE -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD -D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=256 -D HTTPD_ROOT="/etc/apache2" -D SUEXEC_BIN="/usr/lib/apache2/suexec" -D DEFAULT_PIDLOG="/var/run/apache2.pid" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="mime.types" -D SERVER_CONFIG_FILE="apache2.conf"
worker MPMã®ä»åã®èšå®ã¯ãã¡ãã§ãïŒåæ²ïŒã
<IfModule mpm_worker_module> StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule>
èµ·åããæç¹ã§ã®ããã»ã¹æ°ã¯ããããªã£ãŠããŸããStartServers
ã2ãªã®ã§ãåããã»ã¹ã2ã€ããç¶æ
ã§ããã
$ ps aux | grep apache2 | grep -v grep root 745 0.0 0.2 6752 4740 ? Ss 20:14 0:00 /usr/sbin/apache2 -k start www-data 746 0.0 0.2 1211648 4480 ? Sl 20:14 0:00 /usr/sbin/apache2 -k start www-data 747 0.0 0.2 1211648 4480 ? Sl 20:14 0:00 /usr/sbin/apache2 -k start
ã¹ã¬ããæ°ãèŠãŠã¿ãŸãããã次ã®ãã®ã¯ãPIDåäœã§LWPãã°ã«ãŒãã³ã°ãã«ãŠã³ããããã®ã§ãããªã®ã§ãããã»ã¹èªèº«ã®æ°ã
å«ãŸããŸãã
$ ps aux -L | grep apache2 | grep -v grep | perl -awln -F'\s+' -e 'print "$F[0] $F[1]"' | sort | uniq -c 1 root 1778 27 www-data 1779 27 www-data 1780
ã§ãããããã»ã¹æ°ã®åãå·®ãåŒããŠã26ãããŸããThreadsPerChild
ãMinSpareThreads
ã25ãªã®ã§ããã
ã²ãšã€å€ãã§ããâŠã
ãŸãããã®å èš³ã«ã¯ä»åã¯ãã ãããé²ã¿ãŸãããã
Vegetaã§ã¯ãŒã«ãŒæ°ã300ïŒä»åã®MaxRequestWorkers
ã®åïŒã«ããŠãåŸ
ã¡æéã§ã¿ã€ã ã¢ãŠãããªãããã«èšå®ãã€ã€ã¢ã¯ã»ã¹ã
$ echo 'GET http://localhost' | ./vegeta attack -duration 600s -max-workers 300 -timeout 120s
Vegetaã§ã¢ã¯ã»ã¹ããŠããéã®ãApacheã®ç¶æ ãèŠãŠã¿ãŸãã
åããã»ã¹ã®æ°ã6ã«ãªããŸãããMaxRequestWorkers
ïŒ150ïŒ / ThreadsPerChild
ïŒ25ïŒã¯6ã«ãªãã®ã§ãèšå®ãšåã£ãçµæã«
ãªã£ãŠããŸããã
$ ps aux | grep apache2 | grep -v grep root 745 0.0 0.2 6752 4740 ? Ss 20:14 0:00 /usr/sbin/apache2 -k start www-data 746 0.0 0.2 1213632 4480 ? Sl 20:14 0:00 /usr/sbin/apache2 -k start www-data 747 0.0 0.2 1213632 4480 ? Sl 20:14 0:00 /usr/sbin/apache2 -k start www-data 1646 0.2 0.2 1213632 4540 ? Sl 20:27 0:00 /usr/sbin/apache2 -k start www-data 1701 0.2 0.2 1213632 4540 ? Sl 20:27 0:00 /usr/sbin/apache2 -k start www-data 1702 0.2 0.2 1213632 4604 ? Sl 20:27 0:00 /usr/sbin/apache2 -k start www-data 1807 0.3 0.2 1213632 4540 ? Sl 20:27 0:00 /usr/sbin/apache2 -k start
äžæ¹ã§ã¹ã¬ããæ°ãèŠãŠã¿ããšããããªã£ãŠããŸãã
$ ps aux -L | grep apache2 | grep -v grep | perl -awln -F'\s+' -e 'print "$F[0] $F[1]"' | sort | uniq -c 1 root 745 27 www-data 1646 27 www-data 1701 27 www-data 1702 27 www-data 1807 27 www-data 746 27 www-data 747
Apacheã®ãšã©ãŒãã°ãèŠããšãMaxRequestWorkers
ã«éããããšãåºåãããŠããŸãã
/var/log/apache2/error.log
[Sun Feb 07 20:27:11.011377 2021] [mpm_worker:error] [pid 745:tid 139631994985536] AH00286: server reached MaxRequestWorkers setting, consider raising the MaxRequestWorkers setting
ããã»ã¹ãããã®ã¹ã¬ããæ°ã®ïŒ1åã¯ã¡ãã£ãšããã£ãŠããŸããããæŠã説æã®ãšããã§ããã
Apacheã®ããã¯ãã°ãèŠãŠã¿ãŸããããã¢ã¯ã»ã¹å ã®300ã¯ãŒã«ãŒã®ãã¡ã150ãRecv-Qã«å ¥ã£ãŠããã®ã§ãã¡ããšãåã£ãŠããã§ãã
$ ss -tnl | grep -E 'State|:80 ' State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 150 511 *:80 *:*
event MPM
次ã¯ãevent MPMã確èªããŸãããã
event MPMã«åãæ¿ããŸãã
$ sudo a2dismod mpm_worker $ sudo a2enmod mpm_event $ sudo systemctl restart apache2
確èªã
$ apachectl -V Server version: Apache/2.4.41 (Ubuntu) Server built: 2020-08-12T19:46:17 Server's Module Magic Number: 20120211:88 Server loaded: APR 1.6.5, APR-UTIL 1.6.1 Compiled using: APR 1.6.5, APR-UTIL 1.6.1 Architecture: 64-bit Server MPM: event threaded: yes (fixed thread count) forked: yes (variable process count) Server compiled with.... -D APR_HAS_SENDFILE -D APR_HAS_MMAP -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE -D APR_USE_PTHREAD_SERIALIZE -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD -D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=256 -D HTTPD_ROOT="/etc/apache2" -D SUEXEC_BIN="/usr/lib/apache2/suexec" -D DEFAULT_PIDLOG="/var/run/apache2.pid" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="mime.types" -D SERVER_CONFIG_FILE="apache2.conf"
event MPMã®èšå®ã¯ããã¡ãã§ãã
<IfModule mpm_event_module> StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule>
ããã»ã¹ã確èªãworker MPMã®æãšåãã§ããã
$ ps aux | grep apache2 | grep -v grep root 2350 0.0 0.2 6772 4712 ? Ss 21:02 0:00 /usr/sbin/apache2 -k start www-data 2352 0.0 0.2 1211668 4436 ? Sl 21:02 0:00 /usr/sbin/apache2 -k start www-data 2353 0.0 0.2 1211668 4468 ? Sl 21:02 0:00 /usr/sbin/apache2 -k start
ããã»ã¹ããšã®ã¹ã¬ããæ°ã«é¢ããŠãåãã§ãã
$ ps aux -L | grep apache2 | grep -v grep | perl -awln -F'\s+' -e 'print "$F[0] $F[1]"' | sort | uniq -c 1 root 2350 27 www-data 2352 27 www-data 2353
ã§ã¯ãè² è·ããããŠã¿ãŸãã
$ echo 'GET http://localhost' | ./vegeta attack -duration 600s -max-workers 300 -timeout 120s
ãã®ç¶æ ã§ããã»ã¹æ°ãèŠããšãworker MPMã®æãããå€ã9ã«ãªã£ãŠããŸãïŒworker MPMã¯6ïŒã
$ ps aux | grep apache2 | grep -v grep root 2350 0.0 0.2 6772 4840 ? Ss 21:02 0:00 /usr/sbin/apache2 -k start www-data 2352 0.0 0.2 1213588 4436 ? Sl 21:02 0:00 /usr/sbin/apache2 -k start www-data 2353 0.0 0.2 1213660 4468 ? Sl 21:02 0:00 /usr/sbin/apache2 -k start www-data 2481 0.0 0.2 1213660 4472 ? Sl 21:04 0:00 /usr/sbin/apache2 -k start www-data 2534 0.0 0.2 1213340 4472 ? Sl 21:04 0:00 /usr/sbin/apache2 -k start www-data 2535 0.0 0.2 1211900 4472 ? Sl 21:04 0:00 /usr/sbin/apache2 -k start www-data 2614 0.0 0.2 1213660 4472 ? Sl 21:04 0:00 /usr/sbin/apache2 -k start www-data 2615 0.0 0.2 1213660 4472 ? Sl 21:04 0:00 /usr/sbin/apache2 -k start www-data 2616 0.0 0.2 1213020 4472 ? Sl 21:04 0:00 /usr/sbin/apache2 -k start www-data 2617 0.2 0.2 1213660 4472 ? Sl 21:04 0:00 /usr/sbin/apache2 -k start
ã¹ã¬ããæ°ããã®èšç®ã ãšã225ã¹ã¬ããããããšã«ãªããŸããã
$ ps aux -L | grep apache2 | grep -v grep | perl -awln -F'\s+' -e 'print "$F[0] $F[1]"' | sort | uniq -c 1 root 2350 27 www-data 2352 27 www-data 2353 27 www-data 2481 27 www-data 2534 27 www-data 2535 27 www-data 2614 27 www-data 2615 27 www-data 2616 27 www-data 2617
ãšã©ãŒãã°ãèŠããšãMaxRequestWorkers
ã«éããããšãåºåãããŠããŸãã
/var/log/apache2/error.log
[Sun Feb 07 21:04:43.278710 2021] [mpm_event:error] [pid 2350:tid 139858399816768] AH00484: server reached MaxRequestWorkers setting, consider raising the MaxRequestWorkers setting
ããã¯ãã°ãèŠããš110ãªã®ã§ã150ã ã£ãworker MPMããã40æžã£ãŠããŸããã
$ ss -tnl | grep -E 'State|:80 ' State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 110 511 *:80 *:*
ã€ãŸããworker MPMã®èšå®ãšåããªã®ã«40åã®æ¥ç¶ãåãå ¥ããŠããããšã«ãªããŸããã
ããŠãããããèšç®ãããããªããªããŸãããmod_status
ã§èŠããšã225ã®ã¹ã¬ãããããžãŒãªã¯ãŒã«ãŒã¹ã¬ãããšèªèãããŠãã
ããã ã£ãã®ã§ããããã®225ãã©ãèšç®ãããåºãŠããã®ããããããããŸããâŠãããã¯ãã°ãšMaxRequestWorkers
ã®å·®ã§ãã
40ããããªã®ã§ããâŠã
AsyncRequestWorkerFactor Directive
worker MPMããå€ãã«ãªãã®ã¯ããã¥ã¡ã³ãããããããã®ã§ããâŠãã®ç¢ºèªã¯ãä»åã¯ãã¹ããŸãããã
prefork MPM
æåŸã¯prefork MPMã§ãã
prefork MPMã«åãæ¿ããŸãã
$ sudo a2dismod mpm_event $ sudo a2enmod mpm_prefork $ sudo systemctl restart apache2
確èªã
$ apachectl -V Server version: Apache/2.4.41 (Ubuntu) Server built: 2020-08-12T19:46:17 Server's Module Magic Number: 20120211:88 Server loaded: APR 1.6.5, APR-UTIL 1.6.1 Compiled using: APR 1.6.5, APR-UTIL 1.6.1 Architecture: 64-bit Server MPM: prefork threaded: no forked: yes (variable process count) Server compiled with.... -D APR_HAS_SENDFILE -D APR_HAS_MMAP -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE -D APR_USE_PTHREAD_SERIALIZE -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD -D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=256 -D HTTPD_ROOT="/etc/apache2" -D SUEXEC_BIN="/usr/lib/apache2/suexec" -D DEFAULT_PIDLOG="/var/run/apache2.pid" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="mime.types" -D SERVER_CONFIG_FILE="apache2.conf"
prefork MPMã®èšå®ã¯ãããã§ããã
<IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxRequestWorkers 150 MaxConnectionsPerChild 0 </IfModule>
StartServers
ã5ãªã®ã§ãèµ·åæã®ããã»ã¹æ°ã5ã§ãããšã
$ ps aux | grep apache2 | grep -v grep root 4674 0.0 0.2 6500 4452 ? Ss 21:33 0:00 /usr/sbin/apache2 -k start www-data 4675 0.0 0.1 6772 3824 ? S 21:33 0:00 /usr/sbin/apache2 -k start www-data 4676 0.0 0.1 6772 3824 ? S 21:33 0:00 /usr/sbin/apache2 -k start www-data 4677 0.0 0.1 6772 3824 ? S 21:33 0:00 /usr/sbin/apache2 -k start www-data 4678 0.0 0.1 6772 3824 ? S 21:33 0:00 /usr/sbin/apache2 -k start www-data 4679 0.0 0.1 6772 3824 ? S 21:33 0:00 /usr/sbin/apache2 -k start
ã¹ã¬ããã«é¢ããŠã¯ã確èªããæå³ããããŸããã
$ ps aux -L | grep apache2 | grep -v grep | perl -awln -F'\s+' -e 'print "$F[0] $F[1]"' | sort | uniq -c 1 root 4674 1 www-data 4675 1 www-data 4676 1 www-data 4677 1 www-data 4678 1 www-data 4679
ã§ã¯ãè² è·ããããŠã¿ãŸãã
$ echo 'GET http://localhost' | ./vegeta attack -duration 600s -max-workers 300 -timeout 120s
åããã»ã¹ã150ãŸã§å¢ããŸããã
$ ps aux | grep apache2 | grep -v grep | wc -l 151
ãšã©ãŒãã°ã確èªãããšãMaxRequestWorkers
ã«éããããšãåºåãããŠããŸãã
/var/log/apache2/error.log
[Sun Feb 07 21:41:02.467235 2021] [mpm_prefork:error] [pid 5049] AH00161: server reached MaxRequestWorkers setting, consider raising the MaxRequestWorkers setting
ããã¯ãã°ã«ã¯ã150ãããŸããã
$ ss -tnl | grep -E 'State|:80 ' State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 150 511 *:80 *:*
ã ããããé°å²æ°ã¯ããã£ãã®ã§ã¯ãªãã§ããããã
ãŸãšã
ããApacheã®MPMã®èšå®ãããããªããªãã®ã§ããã®æ©äŒã«ã¡ãã£ãšç¢ºèªããŠã¿ãŸããã
event MPMã«ã€ããŠã¯ã¡ãã£ãšããããããªããšãããããã®ã§ãããæ°ãåãããããã¡ãã£ãšç¢ºèªããŠã¿ããããªãšâŠã
ãã£ãããé°å²æ°ã¯ããã£ãã®ã§è¯ããšããŸãããã