温馨提示:
由于上一篇己经对http编译安装完成,这篇将介绍CGI、https、压缩功能的启用。
一、启用CGI功能(CGI是什么?)
由于是编译安装,在编译时己经指加载的了大多数的模块,所以在配置CGI的过程中,只要编辑/etc/httpd24/httpd.conf就可以了。
1、启用CGI模块
2、启用别名模块
别名模块的作用就是将ServerRoot中的cgi-bin目录指向自定义位置
3、设置cgi-bin的别名目录
提示:
什么是处理器(Handler)
"处理器"是当一个文件被调用时,Apache所执行操作的内部表现。文件一般都有基于其文件类型的隐含处理器。通常,文件都只是被服务器简单的提交,只有某些文件类型会被特别地"处理"。
常用指令
AddHandler:在文件扩展名与特定的处理器之间建立映射
SetHandler:强制所有匹配的文件被一个指定的处理器处理
利用上一个篇中的服务脚本启动服务
1
|
#service httpd24 start
|
4、书写测试CGI脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#mkdir -p /web/www/ess/
#cd /web/www/ess/
#vim test.sh
# cat /web/www/ess/test.sh
#!/bin/bash
cat
<< EOF
Content-Type: text
/html
<pre>
hello
new Time is : `
date
`
current user:\033[0m `
whoami
`
current direcory:`
pwd
`
<
/pre
>
EOF
#chmod +x test.sh
|
5、测试CGI功能
竟然出错了,好吧,看错误日志
注意:
APACHE错误日志:Premature end of script headers,或 malformed header from script 'filename': Bad header:XXX,这种情况,还是检查一下CGI输出的第一句话是啥。应该是形如:
Content-type:text/html\n\n
修改后格式
再次测试
总结:
编写CGI程序
编写CGI程序和"常规"程序之间有两个主要的不同。Content-type: text/html
注:MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准
二、https
1、生成证书
2、开启ssl功能
1
2
3
4
|
#vim /etc/httpd24/httpd.conf
LoadModule ssl_module modules
/mod_ssl
.so
# Secure (SSL/TLS) connections
Include
/etc/httpd24/extra/httpd-ssl
.conf
|
3、注销根目录限制,因为在后面要自定义站点目录
1
2
3
4
|
#<Directory />
# AllowOverride none
# Require all denied
#</Directory>
|
注:
如果在/etc/httpd24/httpd.conf文件中没有启用LoadModule socache_shmcb_module modules/mod_socache_shmcb.so,在后面配置完/etc/httpd24/extra/httpd-ssl.conf启动服务会报错,SSLSessionCache需要这个模块的支持。
3、修改配置文件(/etc/httpd24/extra/httpd-ssl.conf)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<VirtualHost _default_:443>
DocumentRoot
"/web/www/"
#指定目录
ServerName www.essun.com:443
#指定服务名字
ErrorLog
"/usr/local/apache/logs/error_log"
TransferLog
"/usr/local/apache/logs/access_log"
SSLEngine on
启用SSL功能
SSLCertificateFile
"/etc/httpd24/ssl_key/www.crt"
#指定web服务证书位置
SSLCertificateKeyFile
"/etc/httpd24/ssl_key/www.key"
#指定私钥位置
<
/VirtualHost
>
|
4、启动服务
1
|
#service httpd24 start
|
5、访问测试
提示:
在测试时要设置hosts文件
win7 路径为:C:\Windows\System32\drivers\etc
IP 主机名
点继续。
将CA的证书与web服务器的证书导出并添加到受信任根证书
提示:
1、将CA的证书导出后,将后缀改为.crt,双击安装,指定存储路径,添加到受信任的根证书颁发机构。
2、安装web服务器证书
3、查看证书信息
三、虚拟主机
1、基于不同的FQDN访问。
前提:
在服务器端配置正向DNS解析,将测试机的DNS指向DNS服务器的IP地址。
如果是自定义目录,请注销/etc/httpd24/httpd.conf中
1
2
3
4
|
#<Directory />
# AllowOverride none
# Require all denied
#</Directory>
|
如果没有关闭,将会无法访问自定义目录
设置主配置文件(/etc/httpd24/httpd.conf),启用虚拟主机配置文件
1
2
|
# Virtual hosts
Include
/etc/httpd24/extra/httpd-vhosts
.conf
|
在/etc/httpd24/extra/httpd-vhosts
.conf书写配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
DocumentRoot
"/web/html/ess/"
ServerName www.jgpserver.com
# ServerAlias www.dummy-host.example.com
ErrorLog
"logs/dummy-host.example.com-error_log"
CustomLog
"logs/dummy-host.example.com-access_log"
common
<
/VirtualHost
>
<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot
"/web/html/test/"
ServerName www.jgpserver.com.cn
ErrorLog
"logs/dummy-host2.example.com-error_log"
CustomLog
"logs/dummy-host2.example.com-access_log"
common
<
/VirtualHost
>
|
创建目录与默认页面
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
|
#mkdir -p /web/html/{ess,test}
#cd /web/html/ess
#vim index.html
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
>
<html>
<
head
>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=gb2312"
/>
<title>FQDN<
/title
>
<
/head
>
<body>
<p>
hostname
is :www.jgpserver.com<
/p
>
<p>ip is:192.168.1.114<
/p
>
<
/body
>
<
/html
>
#---------------------------------------------
#vim /web/html/test/index.html
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
>
<html>
<
head
>
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=gb2312"
/>
<title>FQDN_cn<
/title
>
<
/head
>
<body>
<p>
hostname
is :www.jgpserver.com.cn<
/p
>
<p>ip is:192.168.1.114<
/p
>
<
/body
>
<
/html
>
|
重启服务,测试
1
|
#service httpd24 start
|
测试www.jgpserver.com
测试www.jgpserver.com.cn
2、基于IP地址的虚拟主机
前提:
在Web服务端添加多块虚拟网卡,以便测试
修改配置文件(/etc/httpd24/extra/httpd-vhosts.conf )
重启服务,测试
1
|
#service httpd24 restart
|
测试ip:192.168.1.234
测试ip:192.168.1.123
3、基于端口的虚拟主机
在主配置文件(/etc/httpd24/httpd.conf)中添加监听端口
修改虚拟主机配置文件(/etc/httpd24/extra/httpd-vhosts.conf )
语法检查、重启服务
查看端口是否开启
测试端口80
测试端口4800
测试5800
四、文本压缩
好处:
经过压缩后实际上降低了网络传输的字节数,最明显的好处就是可以加快网页加载的速度。网页加载速度加快的好处不言而喻,除了节省流量,改善用户的浏览体验
Web服务器处理HTTP压缩的过程如下:
① Web服务器接收到浏览器的HTTP请求后,检查浏览器是否支持HTTP压缩
(Accept-Encoding 信息);
② 如果浏览器支持HTTP压缩,Web服务器检查请求文件的后缀名;
③ 如果请求文件是HTML、CSS等静态文件,Web服务器到压缩缓冲目录中检查是否已经存在请求文件的最新压缩文件;
④ 如果请求文件的压缩文件不存在,Web服务器向浏览器返回未压缩的请求文件,并在压缩缓冲目录中存放请求文件的压缩文件;
⑤ 如果请求文件的最新压缩文件已经存在,则直接返回请求文件的压缩文件;
1、在主配置文件/etc/httpd24/httpd.conf文件中启用 deflate模块
MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准
2、定义压缩类型、压缩等级、对特殊浏览器不支持压缩的定义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# Level of compression (Highest 9 - Lowest 1)
DeflateCompressionLevel 9
# Netscape 4.x has some problems.
BrowserMatch ^Mozilla
/4
gzip
-only-text
/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla
/4
\.0[678] no-
gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSI[E] !no-
gzip
!
gzip
-only-text
/html
<IfModule deflate_module>
AddOutputFilterByType DEFLATE text
/plain
AddOutputFilterByType DEFLATE text
/html
AddOutputFilterByType DEFLATE text
/xml
AddOutputFilterByType DEFLATE text
/css
AddOutputFilterByType DEFLATE text
/javascript
AddOutputFilterByType DEFLATE application
/xhtml
+xml
AddOutputFilterByType DEFLATE application
/xml
AddOutputFilterByType DEFLATE application
/rss
+xml
AddOutputFilterByType DEFLATE application
/atom_xml
AddOutputFilterByType DEFLATE application
/x-javascript
AddOutputFilterByType DEFLATE application
/x-httpd-php
<
/IfModule
>
|
3、查看结果
如果想了解请求报文响应报文属性的,请参阅http://essun.blog.51cto.com/721033/1379932
五、利用mod_status查看apache服务相关信息
1、启用状态模块、info文件
1
2
3
4
|
#vim /etc/httpd24/httpd.conf
LoadModule status_module modules
/mod_status
.so
# Real-time info on requests and configuration
Include
/etc/httpd24/extra/httpd-info
.conf
|
2、修改/etc/httpd24/extra/httpd-info
.conf文件
Require [host|ip]对那一个ip或主机响应
3、重启服务、测试
======================================完=============================================