Apache Web Server是目前linux系统下使用最广泛的处理静态网页的免费web服务器。 
   
 
  在CentOS 6.5,默认安装了Apache web服务器。 
   
 
  1 安装apache  服务  
   
 
  查看是否安装了httpd服务  
   
  [root@localhost ~]# rpm -qa|grep httpd 
httpd-tools-2.2.15-39.el6.centos.i686 
httpd-devel-2.2.15-39.el6.centos.i686 
httpd-manual-2.2.15-39.el6.centos.noarch 
httpd-2.2.15-39.el6.centos.i686 
 
 
  如果没有安装,可以使用yum来进行安装  
   
  [root@localhost ~]# yum install -y httpd-* 
   
 
  httpd的配置目录结构  
   
 
  [root@localhost ~]# tree /etc/httpd/ 
/etc/httpd/ 
├── conf 
│   ├── httpd.conf(最主要的配置文件) 
│   └── magic 
├── conf.d (存放模块配置文件)        
│   ├── manual.conf 
│   ├── mod_dnssd.conf 
│   ├── README 
│   └── welcome.conf 
├── logs -> ../../var/log/httpd(存放日志信息) 
├── modules -> ../../usr/lib/httpd/modules(存放用到的模块) 
└── run -> ../../var/run/httpd(存放主服务的进程id等相关信息) 
   
 
  网站根目录结构  
   
 
  [root@localhost ~]# tree -d /var/www/ 
/var/www/ 
├── cgi-bin 
├── error 
│   └── include 
├── html(网页存放根目录) 
├── icons 
│   └── small 
└── manual 
    ├── developer 
    ├── faq 
    ├── howto 
    ├── images 
    ├── misc 
    ├── mod 
    ├── platform 
    ├── programs 
    ├── rewrite 
    ├── ssl 
    ├── style 
    │   ├── css 
    │   ├── lang 
    │   ├── latex 
    │   └── xsl 
    │       └── util 
    └── vhosts 
 
24 directories 
   
 
  apache启动用户  
   
 
  apache安装完成之后,将会创建一个名为apache的用户。这个用户用于启动apache相关的子进程。 
  [root@localhost ~]# cat /etc/passwd | grep apache  
apache:x:48:48:Apache:/var/www:/sbin/nologin 
   
 
  2 apache  服务管理 
   
 
   
  [root@localhost ~]# service httpd  
Usage: httpd {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest} 
 
   
 
  服务常用选项  
   
 
  start:启动服务 
  stop:停止服务 
  restart:重启服务 
 
  graceful:在处理完所有用户的请求之后再重启服务。(不希望用户感觉到网站突然中断) 
  graceful-stop(apache独有的):在处理完所有用户的请求之后再关闭服务 
   
 
  启动apache服务  
   
 
  [root@localhost ~]# /etc/init.d/httpd start 
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName 
                                                           [  OK  ] 
 
  如有需要可以设置为开机启动  
  [root@localhost ~]# chkconfig --level 35 httpd on 
   
 
  启动后在浏览器中输入 http://localhost:80/,将会显示apache的首页。 
 
   
 
  3 apache进程管理   
   
 
  apache默认监听TCP协议80端口; 
  apache服务默认会启动一个主进程(控制进程)和多个子进程。 
   
 
  查看apache相关的进程  
  [root@localhost ~]# ps aux|grep httpd 
root      5827  0.0  0.6  11780  3428 ?        Ss   13:16   0:00 /usr/sbin/httpd 
apache    5830  0.0  0.5  11912  2816 ?        S    13:16   0:00 /usr/sbin/httpd 
apache    5831  0.0  0.5  11912  2876 ?        S    13:16   0:00 /usr/sbin/httpd 
apache    5832  0.0  0.5  11912  2832 ?        S    13:16   0:00 /usr/sbin/httpd 
apache    5833  0.0  0.5  11912  2816 ?        S    13:16   0:00 /usr/sbin/httpd 
apache    5834  0.0  0.5  11912  2832 ?        S    13:16   0:00 /usr/sbin/httpd 
apache    5835  0.0  0.4  11780  2164 ?        S    13:16   0:00 /usr/sbin/httpd 
apache    5836  0.0  0.5  11912  2832 ?        S    13:16   0:00 /usr/sbin/httpd 
apache    5837  0.0  0.4  11780  2164 ?        S    13:16   0:00 /usr/sbin/httpd 
 
   
 
  其中,以root身份运行的是主进程,以apache身份运行的是子进程,主进程的进程ID保存在/etc/httpd/run/httpd.pid文件中。 
   
 
  查看使用的端口  
   
 
  [root@localhost ~]# netstat -tupln|grep httpd 
tcp        0      0 :::80                       :::*                        LISTEN      5827/httpd 
   
 
  4Apache DSO   
   
  apache是一个模块化设计的服务,核心只包含主要功能,扩展功能通过模块来实现。不同模块可以被静态的编译进程序,也可以动态加载。 
   
 
  模块的动态加载通过DSO(Dynamic Share Object)来实现。 
   
 
  查看模块  
  [root@localhost ~]# httpd -M 
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName 
Loaded Modules: 
 core_module (static) 
 mpm_prefork_module (static) //进程管理模块 
 http_module (static) 
 so_module (static) //加载动态模块 
 auth_basic_module (shared) 
 auth_digest_module (shared) 
 authn_file_module (shared) 
 authn_alias_module (shared) 
 authn_anon_module (shared) 
 authn_dbm_module (shared) 
 authn_default_module (shared) 
 authz_host_module (shared) 
 authz_user_module (shared) 
 authz_owner_module (shared) 
 authz_groupfile_module (shared) 
 authz_dbm_module (shared) 
 authz_default_module (shared) 
 ldap_module (shared) 
 authnz_ldap_module (shared) 
 include_module (shared) 
 log_config_module (shared) 
 logio_module (shared) 
 env_module (shared) 
 ext_filter_module (shared) 
 mime_magic_module (shared) 
 expires_module (shared) 
 deflate_module (shared) 
 headers_module (shared) 
 usertrack_module (shared) 
 setenvif_module (shared) 
 mime_module (shared) 
 dav_module (shared) 
 status_module (shared) 
 autoindex_module (shared) 
 info_module (shared) 
 dav_fs_module (shared) 
 vhost_alias_module (shared) 
 negotiation_module (shared) 
 dir_module (shared) 
 actions_module (shared) 
 speling_module (shared) 
 userdir_module (shared) 
 alias_module (shared) 
 substitute_module (shared) 
 rewrite_module (shared) 
 proxy_module (shared) 
 proxy_balancer_module (shared) 
 proxy_ftp_module (shared) 
 proxy_http_module (shared) 
 proxy_ajp_module (shared) 
 proxy_connect_module (shared) 
 cache_module (shared) 
 suexec_module (shared) 
 disk_cache_module (shared) 
 cgi_module (shared) 
 version_module (shared) 
 dnssd_module (shared) 
Syntax OK 
   
 
  查看静态编译入程序的模块  
   
 
  [root@localhost ~]# httpd -l 
Compiled in modules: 
  core.c 
  prefork.c 
  http_core.c 
  mod_so.c 
   
 
  5 Apache MPM(Multi Process Modules)  
   
 
  MPM主要负责实现网络监听、请求处理等功能,MPM有多种,目的是为了在不同平台环境下实现最优化的性能及稳定性。 
平台 
 MPM 
 Windows 
 mpm_winnt 
 Linux 
 prefork, worker 
 
  Prefork 是非线程、预生成进程型MPM,prefork会预先启动一些子进程,每个子进程一个时间点只能处理一个请求,并根据并发请求的数量动态生成更多子进程。 
   
 
  Worker 是多线程化、多进程的MPM,每个进程可以生成多个线程,每个线程处理一个请求。 
   
 
  prefork会消耗更多内存,比worker快一些,并且不用考虑线程安全性问题,worker则需要考虑线程安全性问题。 
 
   
查看当前服务使用的MPM  
   
 
  [root@localhost ~]# httpd -V 
Server version: Apache/2.2.15 (Unix) 
Server built:   Oct 16 2014 14:45:47 
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:   32-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" 
 
   
 
  对Prefork进行压力测试  
   
  查看当前apache当前进程: 
  [root@localhost ~]# watch -n 1 'ps aux|grep httpd' 
   
 
  使用Apache Bench来并发访问 
  [root@localhost ~]# ab -c 32 -n 30000 http://localhost/ 
This is ApacheBench, Version 2.3  
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ 
Licensed to The Apache Software Foundation, http://www.apache.org/ 
 
Benchmarking localhost (be patient) 
Completed 3000 requests 
Completed 6000 requests 
Completed 9000 requests 
Completed 12000 requests 
Completed 15000 requests 
Completed 18000 requests 
Completed 21000 requests 
Completed 24000 requests 
Completed 27000 requests 
Completed 30000 requests 
Finished 30000 requests 
 
 
Server Software:        Apache/2.2.15 
Server Hostname:        localhost 
Server Port:            80 
 
Document Path:          / 
Document Length:        4954 bytes 
 
Concurrency Level:      32 
Time taken for tests:   13.860 seconds 
Complete requests:      30000 
Failed requests:        0 
Write errors:           0 
Non-2xx responses:      30014 
Total transferred:      154632128 bytes 
HTML transferred:       148689356 bytes 
Requests per second:    2164.48 [#/sec] (mean) 
Time per request:       14.784 [ms] (mean) 
Time per request:       0.462 [ms] (mean, across all concurrent requests) 
Transfer rate:          10895.13 [Kbytes/sec] received 
 
Connection Times (ms) 
              min  mean[+/-sd] median   max 
Connect:        2    7   3.2      6      93 
Processing:     4    8   2.9      7      91 
Waiting:        0    7   2.7      6      90 
Total:          9   15   4.9     13     120 
 
Percentage of the requests served within a certain time (ms) 
  50%     13 
  66%     15 
  75%     16 
  80%     17 
  90%     19 
  95%     22 
  98%     27 
  99%     30 
 100%    120 (longest request) 
 
 
 
  发送请求前 
   
 
 
  发送请求之后,可以看到子进程数要比原来多几倍(子进程以2的指数增长)。 
   
 
  在/ect/httpd/conf/httpd.conf中可以对prefork进行配置(必须观察每个子进程内存使用情况来设定相应的参数) 
 
   
 
  ## 
## Server-Pool Size Regulation (MPM specific) 
##  
 
# 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 
# ServerLimit: maximum value for MaxClients for the lifetime of the server 
# MaxClients: maximum number of server processes allowed to start 
# MaxRequestsPerChild: maximum number of requests a server process serves 
 
StartServers       8 
MinSpareServers    5 
MaxSpareServers   20 
ServerLimit      256 
MaxClients       256 
MaxRequestsPerChild  4000 
 
 
  MaxClients 
 
 
 
  
运维网声明 
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003 
2、本站所有主题由该帖子作者发表,该帖子作者与运维网  享有帖子相关版权 
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版 
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任 
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任 
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任 
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决 
8、联系人Email:admin@iyunv.com  网址:www.yunweiku.com