一. httpd-apache.2.4.9安装过程
编译需要依赖 pcre-devel-7.8-6.el6.bz2 apr-1.5.0.bz2 apr-util-1.5.3.bz2
按需进行依次编译安装
pcre-devel
|
1
2
3
4
|
tar
xf pcre-devel-7.8-6.el6.bz2
cd
pcre-devel-7.8-6
.
/configure
--prefix=
/usr/local/pcre
make
&&
make
install
|
apr
|
1
2
3
4
|
tar
xf apr-1.5.0.bz2
cd
apr-1.5.0
.
/configure
--prefix=
/usr/local/apr
make
&&
make
install
|
apr-util
|
1
2
3
4
|
tar
xf apr-util-1.5.3.bz2
cd
apr-util-1.5.3
.
/configure
--prefix=
/usr/local/apr-util
--with-apr=
/usr/local/apr
make
&&
make
install
|
httpd
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 安装之前请确保系统之前预装的httpd已被卸载
tar
xf httpd-2.4.9.bz2
cd
httpd-2.4.9
# 参数依次是: httpd安装路径 httpd配置文件存放路径 启用模块化方式 启用ssl安全连接
# 启用cgi脚本功能 启用url重写 启用服务器压缩 启用正则表达式支持 apr安装路径
# apr util安装路径 启用常用模块以确保apache正常工作 将多进程模型非静态化
# 启用事件异步模型
.
/configure
--prefix=
/usr/local/apache
--sysconfdir=
/etc/httpd
--
enable
-so --
enable
-ssl --
enable
-cgi --
enable
-rewrite --with-zlib --
with-pcre=
/usr/local/pcre
--with-apr=
/usr/local/apr
--with-apr-
util=
/usr/local/apr-util/
--
enable
-modules=most --
enable
-mpms-
shared=all --with-mpm=event
make
&&
make
install
|
附: SerV风格的启动脚本
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible \
# server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
.
/etc/rc
.d
/init
.d
/functions
if
[ -f
/etc/sysconfig/httpd
];
then
.
/etc/sysconfig/httpd
fi
HTTPD_LANG=${HTTPD_LANG-
"C"
}
INITLOG_ARGS=
""
apachectl=
/usr/local/apache/bin/apachectl
# 修改apachectl路径
httpd=
/usr/local/apache/bin/httpd
# 修改httpd bin路径
prog=httpd
pidfile=${PIDFILE-
/var/run/httpd
.pid}
lockfile=${LOCKFILE-
/var/lock/subsys/httpd
}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
start() {
echo
-n $
"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] &&
touch
${lockfile}
return
$RETVAL
}
stop() {
echo
-n $
"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] &&
rm
-f ${lockfile} ${pidfile}
}
reload() {
echo
-n $
"Reloading $prog: "
if
! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&
/dev/null
;
then
RETVAL=6
echo
$
"not reloading due to configuration syntax error"
failure $
"not reloading $httpd due to configuration syntax error"
else
# Force LSB behaviour from killproc
LSB=1 killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
if
[ $RETVAL -
eq
7 ];
then
failure $
"httpd shutdown"
fi
fi
echo
}
# See how we were called.
case
"$1"
in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart|try-restart)
if
status -p ${pidfile} $httpd >&
/dev/null
;
then
stop
start
fi
;;
force-reload|reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo
$
"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
RETVAL=2
esac
exit
$RETVAL
|
二 .实战: 配置CGI、虚拟主机、https、mod_deflate、mod_status
A) 配置CGI、(在部分业务中常见,例如邮箱)
具体配置如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 在 /etc/named/named.conf 文件中找到 alias_module 模块
# 替换cgi-bin的输出路径
<IfModule alias_module>
# 在此路径下可编写shell脚本实现cgi方式输出html内容
ScriptAlias
/cgi-bin/
"/usr/local/apache/cgi-bin/"
<
/IfModule
>
# cgi-bin示例
#!/bin/bash
#
userinfo=`
cat
/etc/passwd
|
grep
^root`
cat
<< EOF
Content-Type: text
/html
<pre>
The
hostname
is: `
hostname
`.
The
time
is: `
date
`.
The User Information: ${userinfo}.
<
/pre
>
EOF
|
运行效果如下:
B) 虚拟主机: (提高主机生产力)
一机多站点成为可能,需要取消中心主机在2.4.x + 的版本上配置虚拟主机已经不需要支持NameVirtualHost
具体配置如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# 在 httpd.conf 中开启vhost子配置文件
Include
/etc/httpd/extra/httpd-vhosts
.conf
vim
/etc/httpd/extra/httpd-vhosts
.conf
# 在此要注意关闭之前的主机配置也就是 httpd.conf
# 中的<Directory>节点
<VirtualHost *:80>
DocumentRoot
"/var/www/html/a.com"
ServerName www.a.com
ServerAlias www.aa.com
<Directory />
AllowOverride none
Require all granted
<
/Directory
>
ErrorLog
"logs/dummy-host.example.com-error_log"
CustomLog
"logs/dummy-host.example.com-access_log"
common
<
/VirtualHost
>
<VirtualHost *:80>
DocumentRoot
"/var/www/html/b.org"
ServerName www.b.org
ErrorLog
"logs/dummy-host2.example.com-error_log"
<Directory />
AllowOverride none
Require all granted
<
/Directory
>
CustomLog
"logs/dummy-host2.example.com-access_log"
common
<
/VirtualHost
>
|
运行效果如下:
C) mod_deflate (节约带宽成本)
启用压缩模块,gzip,deflate
具体配置如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# 在 httpd.conf 中开启压缩模块
LoadModule deflate_module modules
/mod_deflate
.so
# 在 httpd.conf 中编写如下配置
<IfModule deflate_module>
AddOutputFilterByType DEFLATE text
/plain
AddOutputFilterByType DEFLATE text
/html
AddOutputFilterByType DEFLATE application
/xhtml
+xml
AddOutputFilterByType DEFLATE text
/xml
AddOutputFilterByType DEFLATE application
/xml
AddOutputFilterByType DEFLATE application
/x-javascript
AddOutputFilterByType DEFLATE text
/javascript
AddOutputFilterByType DEFLATE text
/css
# 压缩等级(0-9),数值越大CPU压力也就越大
DeflateCompressionLevel 9
# 非Mozilla4标准浏览器仅开启gzip压缩
BrowserMatch ^Mozilla
/4
gzip
-only-text
/html
# Mozilla4.06,4.07,4.08版本不开启压缩
BrowserMatch ^Mozilla
/4
\.0[678] no-
gzip
# IE浏览器不开启压缩
BrowserMatch \bMSI[E] !no-
gzip
!
gzip
-only-text
/html
<
/IfModule
>
|
运行效果如下:
D) setHandler (apache自带探测功能)
可访问服务器信息,为服务器监控提供便利
具体配置如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 需要注意: SetHandler中的内容必要要经过用户认证
# htpasswd -c /etc/httpd/conf/.htpasswd -M tom
# -c 生成 .htpasswd 文件 -M md5加密用户密码
# 在 httpd.conf 中编写如下配置
<Location
/server-status
>
SetHandler server-status
AuthType Basic
AuthName
"Server Status"
AuthUserFile
"/etc/httpd/conf/.htpasswd"
Require valid-user
Order deny,allow
Allow from all
<
/Location
>
|
# 其他SetHandler可设置的值及对应模块
default-handler: 使用 default_handler() 发送文件,它是用来处理静态内容的处理器(核心)。
send-as-is: 直接发送,不增加 HTTP 头(mod_asis)。
cgi-script: 按 CGI 脚本处理(mod_cgi)。
imap-file: 按 imagemap 规则处理(mod_imagemap)。
server-info: 取得服务器配置信息(mod_info)。
server-status: 取得服务器状态报告(mod_status)。
type-map: 用于内容协商,按类型映射文件处理(mod_negotiation)。
运行效果如下:
E) https (为私密信息保驾护航)
https链接采用证书认证流程,客户端发送链接请求与服务器进行握手,协商加密方式,
服务器发送带有证书签发者与自身信息的证书给客户端验证,客户端验证完毕即建立
https安全链接通信.
具体配置如下:
1) 证书服务器自签证书阶段(需要密钥对一组,自签证书一张)
|
1
2
3
4
5
|
cd
/etc/pki/CA
openssl genrsa -out private
/cakey
.pem 2048
openssl req -new -x509 -key private
/cakey
.pem -out cacert.pem -days 3650
touch
index.txt serial calnumber
echo
01 > serial
|
2) Web服务器安装mod_ssl模块
|
1
|
yum -y
install
mod_ssl
|
3) Web服务器申请证书阶段
|
1
2
3
|
cd
/var/www/html
openssl genrsa -out httpd.key 1024
openssl req -new -key httpd.key -out httpd.csr
|
4) 证书服务器签署证书
|
1
|
openssl ca -
in
/var/www/html/httpd
.csr -out
/var/www/html/httpd
.crt -days 365
|
5) 启用ssl模块与配置文件
|
1
2
3
4
5
6
7
8
9
10
11
12
|
# 在 httpd.conf 中开启ssl_module模块和ssl的配置文件
# LoadModule ssl_module modules/mod_ssl.so
# Include /etc/httpd/extra/httpd-ssl.conf
vim
/etc/httpd/extra/httpd-ssl
.conf
<VirtualHost *:443>
DocumentRoot
"/var/www/html"
ServerName www.king.com:443
ErrorLog
"/usr/local/apache/logs/error_log"
TransferLog
"/usr/local/apache/logs/access_log"
SSLEngine on
SSLCertificateFile
"/var/www/html/httpd.crt"
SSLCertificateKeyFile
"/var/www/html/httpd.key"
|
6) 重启apachectl服务
|
1
|
apachectl restart
|
运行效果如下:
# 没有将服务器证书导入到客户端受信任机构列表
# 下载 httpd.crt 将其导入安装在客户端
# 将证书安放在受信任的根证书办法机构(12306买过票的童鞋都知道 :)
# 导入成功后此处信息应为绿色,显示了链接到www.king.com的加密信息
本文转自My_King1 51CTO博客,原文链接:http://blog.51cto.com/apprentice/1380490,如需转载请自行联系原作者












