首页 > 代码库 > linux服务安装 -- web服务(Apache)

linux服务安装 -- web服务(Apache)

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


4  Apache 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 <$Revision: 655654 $>
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)

wKiom1RV0rzSL9LuAALcfHfp69Q599.jpg

发送请求前


wKioL1RV0xyDOlndAARhzO2UBaE847.jpg

发送请求之后,可以看到子进程数要比原来多几倍(子进程以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
<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      256
MaxClients       256
MaxRequestsPerChild  4000
</IfModule>

MaxClients<=ServerLimit;

当进程处理的请求超过MaxRequestsPerChild,该进程会被杀死,生成新的进程。

MaxSpareServers:最大空闲的进程数,之前作为压力测试之后,httpd的子进程应该为20.


观察每个子进程内存使用情况:

[root@localhost ~]# top -u apache
wKiom1RV19PwXtfSAAUNjmjMWPw327.jpg


6 Apache主配置文件


主配置文件在 /etc/httpd/conf/httpd.conf


# The configuration directives are grouped into three basic sections:
#  1. Directives that control the operation of the Apache server process as a
#     whole (the ‘global environment‘).
#  2. Directives that define the parameters of the ‘main‘ or ‘default‘ server,
#     which responds to requests that aren‘t handled by a virtual host.
#     These directives also provide default values for the settings
#     of all virtual hosts.
#  3. Settings for virtual hosts, which allow Web requests to be sent to
#     different IP addresses or hostnames and have them handled by the
#     same Apache server process.


linux服务安装 -- web服务(Apache)