====== Apache Architecture ====== refer: http://stackoverflow.com/questions/17999140/prefork-vs-worker-module-for-apache-what-is-being-used\\ MPM(Multi Processing Module) ===== MPM Prefork Architecture ===== Basic configuration: * 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** * MaxClients: maximum number of server processes allowed to start => Limist number of httpd created and processing the request. For example: MaxClients 10, And clients send **100 concurrency requests to server**, The server only **create max 10 server process httpd** for processing, and **99 remain requests will waiting in the queues** {{:apache:preforkmodel.png|}} Config example: StartServers 6 MinSpareServers 6 MaxSpareServers 12 MaxClients 12 MaxRequestsPerChild 0 check config: * check number of httpd process with none client request: ps -eaf | grep httpd output: root 1629 1 0 20:56 ? 00:00:00 /usr/local/apache/bin/httpd apache 1631 1629 0 20:56 ? 00:00:00 /usr/local/apache/bin/httpd apache 1632 1629 0 20:56 ? 00:00:00 /usr/local/apache/bin/httpd apache 1633 1629 0 20:56 ? 00:00:00 /usr/local/apache/bin/httpd apache 1634 1629 0 20:56 ? 00:00:00 /usr/local/apache/bin/httpd apache 1635 1629 0 20:56 ? 00:00:00 /usr/local/apache/bin/httpd apache 1636 1629 0 20:56 ? 00:00:00 /usr/local/apache/bin/httpd => 7 processes = MinSpareServers + 1 * run load test with 20 concurrecy requests to server: ab -n 200 -c 20 http://shop.babies.vn/ And check number of httpd processes: ps -eaf | grep httpd root 1013 1 0 19:50 ? 00:00:00 /usr/local/apache/bin/httpd apache 1144 1013 0 20:10 ? 00:00:09 /usr/local/apache/bin/httpd apache 1210 1013 1 20:29 ? 00:00:05 /usr/local/apache/bin/httpd apache 1216 1013 2 20:30 ? 00:00:05 /usr/local/apache/bin/httpd apache 1219 1013 2 20:30 ? 00:00:06 /usr/local/apache/bin/httpd apache 1220 1013 2 20:30 ? 00:00:05 /usr/local/apache/bin/httpd apache 1226 1013 8 20:33 ? 00:00:06 /usr/local/apache/bin/httpd apache 1227 1013 8 20:33 ? 00:00:06 /usr/local/apache/bin/httpd apache 1228 1013 8 20:33 ? 00:00:06 /usr/local/apache/bin/httpd apache 1229 1013 7 20:33 ? 00:00:05 /usr/local/apache/bin/httpd apache 1230 1013 6 20:33 ? 00:00:04 /usr/local/apache/bin/httpd apache 1231 1013 8 20:33 ? 00:00:05 /usr/local/apache/bin/httpd apache 1288 1013 8 20:34 ? 00:00:03 /usr/local/apache/bin/httpd => max httpd processes = 13 = maxClients + 1 ===== MPM worker Architecture ===== * StartServers: initial number of server processes to start * MinSpareThreads: minimum number of worker threads which are kept spare * MaxSpareThreads: maximum number of worker threads which are kept spare * ThreadsPerChild: constant number of worker threads in each server process {{:apache:apacheworker.png|}} ===== Debug architecture of apache ===== ==== Get current MPM is running: MPM Prefork or MPM worker ==== apachectl -V Server version: Apache/2.2.15 (Unix) Server built: Apr 3 2014 23:56:16 Server's Module Magic Number: 20051115:25 Server loaded: APR 1.3.9, APR-Util 1.3.9 Compiled using: APR 1.3.9, APR-Util 1.3.9 Architecture: 64-bit Server MPM: Prefork threaded: no forked: yes (variable process count) Server compiled with.... -D APACHE_MPM_DIR="server/mpm/prefork" ........................... ==== compiled modules in MPM Prefork(httpd) and MPM worker(httpd.worker) ==== * httpd -V Server version: Apache/2.2.15 (Unix) Server built: Apr 3 2014 23:56:16 Server's Module Magic Number: 20051115:25 Server loaded: APR 1.3.9, APR-Util 1.3.9 Compiled using: APR 1.3.9, APR-Util 1.3.9 Architecture: 64-bit Server MPM: Prefork threaded: no forked: yes (variable process count) Server compiled with.... -D APACHE_MPM_DIR="server/mpm/prefork" -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=128 -D HTTPD_ROOT="/etc/httpd" -D SUEXEC_BIN="/usr/sbin/suexec" -D DEFAULT_PIDLOG="run/httpd.pid" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_LOCKFILE="logs/accept.lock" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="conf/mime.types" -D SERVER_CONFIG_FILE="conf/httpd.conf" * httpd.worker -V Server version: Apache/2.2.15 (Unix) Server built: Apr 3 2014 23:56:59 Server's Module Magic Number: 20051115:25 Server loaded: APR 1.3.9, APR-Util 1.3.9 Compiled using: APR 1.3.9, APR-Util 1.3.9 Architecture: 64-bit Server MPM: Worker threaded: yes (fixed thread count) forked: yes (variable process count) Server compiled with.... -D APACHE_MPM_DIR="server/mpm/worker" -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=128 -D HTTPD_ROOT="/etc/httpd" -D SUEXEC_BIN="/usr/sbin/suexec" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="conf/mime.types" -D SERVER_CONFIG_FILE="conf/httpd.conf" * Difference between "httpd -V" and "httpd.worker -V" * httpd -V Server MPM: Prefork threaded: no forked: yes (variable process count) Server compiled with.... -D APACHE_MPM_DIR="server/mpm/prefork" * httpd.worker -V Server MPM: Worker threaded: yes (fixed thread count) forked: yes (variable process count) Server compiled with.... -D APACHE_MPM_DIR="server/mpm/worker" * httpd -l Compiled in modules: core.c prefork.c http_core.c mod_so.c * httpd.worker -l Compiled in modules: core.c worker.c http_core.c mod_so.c ==== Debug processes and used memory, cpu ==== * netstat -anp | grep 80 | grep LISTEN tcp 0 0 :::80 :::* LISTEN 5141/httpd => http listen on port 80 with PID(ProcessID) 5141 * ps -eaf | grep 5141 ps -eaf | grep 5141 UID PID PPID C STIME TTY TIME CMD root 5141 1 0 15:38 ? 00:00:00 /usr/sbin/httpd apache 5176 5141 0 15:40 ? 00:00:01 /usr/sbin/httpd apache 5196 5141 0 15:42 ? 00:00:02 /usr/sbin/httpd apache 5353 5141 0 15:58 ? 00:00:00 /usr/sbin/httpd apache 5358 5141 0 15:58 ? 00:00:00 /usr/sbin/httpd apache 5360 5141 0 15:59 ? 00:00:00 /usr/sbin/httpd => PPID(Parent PID). the master process is 5141, and sub processes are 5176, 5196, 5353, 5358, 5360 with CMD is /usr/sbin/httpd * ps -aux | grep /usr/sbin/httpd ps -aux | grep /usr/sbin/httpd USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 5141 0.0 1.9 254904 9920 ? Ss 15:38 0:00 /usr/sbin/httpd apache 5176 0.1 9.0 290320 45340 ? S 15:40 0:01 /usr/sbin/httpd apache 5196 0.1 9.3 291896 46956 ? S 15:42 0:02 /usr/sbin/httpd apache 5353 0.1 6.8 279708 34160 ? S 15:58 0:00 /usr/sbin/httpd apache 5358 0.0 3.3 263124 16592 ? S 15:58 0:00 /usr/sbin/httpd apache 5360 0.1 7.6 283964 38312 ? S 15:59 0:00 /usr/sbin/httpd With VSZ is Virtual Memory Size(address space allocated) , and RSS is Resident Set Size (physically resident memory) * pmap -x 6726 | sort -nk +2 pmap -x 6812 | sort -nk +2 00007fcacac95000 2048 0 0 ----- mod_mime_magic.so 00007fcacb4a8000 2048 0 0 ----- mod_log_config.so 00007fcacc0c1000 2048 0 0 ----- mod_authz_user.so 00007fcacccd7000 2048 0 0 ----- mod_auth_digest.so 00007fcacd559000 2048 0 0 ----- libdl-2.12.so 00007fcacdb06000 2048 0 0 ----- libpthread-2.12.so 00007fcace4db000 2048 0 0 ----- libcrypt-2.12.so 00007fcad1562000 2384 2356 2356 rw--- [ anon ] 00007fcac723b000 3268 1672 0 r-x-- libphp5.so 00007fcad17b6000 47848 47488 47488 rw--- [ anon ] ===== Optimize apache and PHP config ===== ==== optimize load module apache ==== Config Optimize load modules for httpd with Explain directives with these modules: LoadModule authz_host_module modules/mod_authz_host.so#Order LoadModule access_compat_module modules/mod_access_compat.so#Require LoadModule log_config_module modules/mod_log_config.so#LogFormat,TransferLog LoadModule expires_module modules/mod_expires.so LoadModule deflate_module modules/mod_deflate.so LoadModule headers_module modules/mod_headers.so LoadModule env_module modules/mod_env.so LoadModule setenvif_module modules/mod_setenvif.so#BrowserMatch LoadModule mime_module modules/mod_mime.so#AddHandler LoadModule autoindex_module modules/mod_autoindex.so#IndexOptions LoadModule negotiation_module modules/mod_negotiation.so#LanguagePriority LoadModule dir_module modules/mod_dir.so#DirectoryIndex LoadModule alias_module modules/mod_alias.so#Alias LoadModule rewrite_module modules/mod_rewrite.so ==== optimize load module php ==== ===== Custom Config for Apache ===== ==== Config disable warning log from browser ==== With this config, the log will display in PHP error log but they don't display in browser side\\\ Update httpd.conf php_admin_flag display_errors off