天天看点

web服务其四启用CGI、https、压缩功能

温馨提示:

由于上一篇己经对http编译安装完成,这篇将介绍CGI、https、压缩功能的启用。

由于是编译安装,在编译时己经指加载的了大多数的模块,所以在配置CGI的过程中,只要编辑/etc/httpd24/httpd.conf就可以了。

1、启用CGI模块

<a href="http://s3.51cto.com/wyfs02/M01/22/E1/wKioL1Mqi_CwefO5AABuBbN_NaE582.jpg" target="_blank"></a>

2、启用别名模块

<a href="http://s3.51cto.com/wyfs02/M00/22/E0/wKiom1MqjBfQ8W1xAABD90dtS8Y594.jpg" target="_blank"></a>

别名模块的作用就是将ServerRoot中的cgi-bin目录指向自定义位置

3、设置cgi-bin的别名目录

<a href="http://s3.51cto.com/wyfs02/M02/22/E0/wKiom1MqlAWCpQt3AAJIei6wg1w901.jpg" target="_blank"></a>

提示:

什么是处理器(Handler)

"处理器"是当一个文件被调用时,Apache所执行操作的内部表现。文件一般都有基于其文件类型的隐含处理器。通常,文件都只是被服务器简单的提交,只有某些文件类型会被特别地"处理"。

常用指令

AddHandler:在文件扩展名与特定的处理器之间建立映射

SetHandler:强制所有匹配的文件被一个指定的处理器处理

利用上一个篇中的服务脚本启动服务

1

<code>#service httpd24 start</code>

4、书写测试CGI脚本

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<code>#mkdir -p /web/www/ess/</code>

<code>#cd /web/www/ess/</code>

<code>#vim test.sh</code>

<code># cat /web/www/ess/test.sh</code>

<code>#!/bin/bash</code>

<code>cat</code> <code>&lt;&lt; EOF</code>

<code>Content-Type: text</code><code>/html</code>

<code>&lt;pre&gt;</code>

<code>hello</code>

<code>new Time is : `</code><code>date</code><code>`</code>

<code>current user:\033[0m `</code><code>whoami</code><code>`</code>

<code>current direcory:`</code><code>pwd</code><code>`</code>

<code>&lt;</code><code>/pre</code><code>&gt;</code>

<code>EOF</code>

<code>#chmod +x test.sh</code>

5、测试CGI功能

<a href="http://s3.51cto.com/wyfs02/M01/22/E2/wKioL1MqmLeCqwtDAAE1Ycx48Mk136.jpg" target="_blank"></a>

竟然出错了,好吧,看错误日志

<a href="http://s3.51cto.com/wyfs02/M02/22/E2/wKioL1MqmWfQyCnZAADMy1QjeHw314.jpg" target="_blank"></a>

注意:

APACHE错误日志:Premature end of script headers,或 malformed header from script 'filename': Bad header:XXX,这种情况,还是检查一下CGI输出的第一句话是啥。应该是形如:

Content-type:text/html\n\n

修改后格式

<a href="http://s3.51cto.com/wyfs02/M02/22/E1/wKiom1MqnBaRb_bYAACXS2Itx1Y562.jpg" target="_blank"></a>

再次测试

<a href="http://s3.51cto.com/wyfs02/M00/22/E1/wKiom1MqnHaCPm37AACpUn-3OlI626.jpg" target="_blank"></a>

总结:

编写CGI程序

编写CGI程序和"常规"程序之间有两个主要的不同。

首先,在CGI程序的所有输出前面必须有一个HTTP的MIME类型的头,对客户端指明所接收内容的类型,大多数情况下,像这样:

Content-type: text/html

注:MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准

其次,输出要求是HTML形式的,或者是浏览器可以显示的其他某种形式。多数情况下,输出是HTML形式的,但偶然也会输出一个gif图片或者其他非HTML的内容。

除了这两点,编写CGI程序和编写其他程序大致相同。

二、https

2、开启ssl功能

<code>#vim /etc/httpd24/httpd.conf</code>

<code>LoadModule ssl_module modules</code><code>/mod_ssl</code><code>.so</code>

<code># Secure (SSL/TLS) connections</code>

<code>Include </code><code>/etc/httpd24/extra/httpd-ssl</code><code>.conf</code>

3、注销根目录限制,因为在后面要自定义站点目录

<code>#&lt;Directory /&gt;</code>

<code>#    AllowOverride none</code>

<code>#    Require all denied</code>

<code>#&lt;/Directory&gt;</code>

注:

如果在/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)

<code>&lt;VirtualHost _default_:443&gt;</code>

<code>DocumentRoot </code><code>"/web/www/"</code>

<code>#指定目录</code>

<code>ServerName www.essun.com:443</code>

<code>#指定服务名字</code>

<code>ErrorLog </code><code>"/usr/local/apache/logs/error_log"</code>

<code>TransferLog </code><code>"/usr/local/apache/logs/access_log"</code>

<code>SSLEngine on</code>

<code>启用SSL功能</code>

<code>SSLCertificateFile </code><code>"/etc/httpd24/ssl_key/www.crt"</code>

<code>#指定web服务证书位置</code>

<code>SSLCertificateKeyFile </code><code>"/etc/httpd24/ssl_key/www.key"</code>

<code>#指定私钥位置</code>

<code>&lt;</code><code>/VirtualHost</code><code>&gt;</code>

4、启动服务

5、访问测试

在测试时要设置hosts文件

win7 路径为:C:\Windows\System32\drivers\etc

IP        主机名

<a href="http://s3.51cto.com/wyfs02/M01/22/E4/wKioL1MqvQLhI-g2AAItGvSDwpI131.jpg" target="_blank"></a>

点继续。

<a href="http://s3.51cto.com/wyfs02/M02/22/E4/wKioL1MqvVHyJQBmAAEh1RjiDYQ450.jpg" target="_blank"></a>

将CA的证书与web服务器的证书导出并添加到受信任根证书

<a href="http://s3.51cto.com/wyfs02/M00/22/E4/wKioL1MqvrCyneTpAAEu9Z8lQ1Y524.jpg" target="_blank"></a>

1、将CA的证书导出后,将后缀改为.crt,双击安装,指定存储路径,添加到受信任的根证书颁发机构。

2、安装web服务器证书

3、查看证书信息

<a href="http://s3.51cto.com/wyfs02/M02/22/E4/wKioL1MqwKqgLQw6AAE772M-a94219.jpg" target="_blank"></a>

三、虚拟主机

1、基于不同的FQDN访问。

前提:

如果是自定义目录,请注销/etc/httpd24/httpd.conf中

如果没有关闭,将会无法访问自定义目录

设置主配置文件(/etc/httpd24/httpd.conf),启用虚拟主机配置文件

<code># Virtual hosts</code>

<code>Include </code><code>/etc/httpd24/extra/httpd-vhosts</code><code>.conf</code>

在<code>/etc/httpd24/extra/httpd-vhosts</code><code>.conf书写配置文件</code>

<code></code>

<code>&lt;VirtualHost *:80&gt;</code>

<code>#    ServerAdmin [email protected]</code>

<code>    </code><code>DocumentRoot </code><code>"/web/html/ess/"</code>

<code>    </code><code>ServerName www.jgpserver.com</code>

<code>#    ServerAlias www.dummy-host.example.com</code>

<code>    </code><code>ErrorLog </code><code>"logs/dummy-host.example.com-error_log"</code>

<code>    </code><code>CustomLog </code><code>"logs/dummy-host.example.com-access_log"</code> <code>common</code>

<code>#    ServerAdmin [email protected]</code>

<code>    </code><code>DocumentRoot </code><code>"/web/html/test/"</code>

<code>    </code><code>ServerName www.jgpserver.com.cn</code>

<code>    </code><code>ErrorLog </code><code>"logs/dummy-host2.example.com-error_log"</code>

<code>    </code><code>CustomLog </code><code>"logs/dummy-host2.example.com-access_log"</code> <code>common</code>

创建目录与默认页面

16

17

18

19

20

21

22

23

24

25

26

27

<code>#mkdir -p /web/html/{ess,test}</code>

<code>#cd /web/html/ess</code>

<code>#vim index.html</code>

<code>&lt;!DOCTYPE html PUBLIC </code><code>"-//W3C//DTD XHTML 1.1//EN"</code> <code>"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"</code><code>&gt;</code>

<code>&lt;html&gt;</code>

<code>        </code><code>&lt;</code><code>head</code><code>&gt;</code>

<code>                </code><code>&lt;meta http-equiv=</code><code>"Content-Type"</code> <code>content=</code><code>"text/html; charset=gb2312"</code> <code>/&gt;</code>

<code>                </code><code>&lt;title&gt;FQDN&lt;</code><code>/title</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>/head</code><code>&gt;</code>

<code>        </code><code>&lt;body&gt;</code>

<code>                </code><code>&lt;p&gt;</code><code>hostname</code> <code>is :www.jgpserver.com&lt;</code><code>/p</code><code>&gt;</code>

<code>                </code><code>&lt;p&gt;ip is:192.168.1.114&lt;</code><code>/p</code><code>&gt;</code>

<code>        </code><code>&lt;</code><code>/body</code><code>&gt;</code>

<code>&lt;</code><code>/html</code><code>&gt;</code>

<code>#---------------------------------------------</code>

<code>#vim /web/html/test/index.html</code>

<code>                </code><code>&lt;title&gt;FQDN_cn&lt;</code><code>/title</code><code>&gt;</code>

<code>                </code><code>&lt;p&gt;</code><code>hostname</code> <code>is :www.jgpserver.com.cn&lt;</code><code>/p</code><code>&gt;</code>

<code></code>重启服务,测试

测试www.jgpserver.com

<a href="http://s3.51cto.com/wyfs02/M01/22/E6/wKioL1Mq_uDBsi0kAAB4jdfQqHM334.jpg" target="_blank"></a>

测试www.jgpserver.com.cn

<a href="http://s3.51cto.com/wyfs02/M01/22/E5/wKiom1Mq_zaDd4dvAAB-2HK3tR4539.jpg" target="_blank"></a>

2、基于IP地址的虚拟主机

在Web服务端添加多块虚拟网卡,以便测试

<a href="http://s3.51cto.com/wyfs02/M00/22/E6/wKioL1MrAsmj15CnAAO2O6EO_rQ860.jpg" target="_blank"></a>

修改配置文件(/etc/httpd24/extra/httpd-vhosts.conf )

<a href="http://s3.51cto.com/wyfs02/M00/22/E5/wKiom1MrBMaR56mPAAKEm1QgLGM283.jpg" target="_blank"></a>

重启服务,测试

<code>#service httpd24 restart</code>

测试ip:192.168.1.234

<a href="http://s3.51cto.com/wyfs02/M01/22/E6/wKioL1MrBOejUSybAABfpr_0beI522.jpg" target="_blank"></a>

测试ip:192.168.1.123

<a href="http://s3.51cto.com/wyfs02/M00/22/E6/wKioL1MrBTXQfq1kAABqpteuX14030.jpg" target="_blank"></a>

3、基于端口的虚拟主机

在主配置文件(/etc/httpd24/httpd.conf)中添加监听端口

<a href="http://s3.51cto.com/wyfs02/M01/22/E6/wKioL1MrBjCS9K6PAABFzh246Iw196.jpg" target="_blank"></a>

修改虚拟主机配置文件(/etc/httpd24/extra/httpd-vhosts.conf )

语法检查、重启服务

<a href="http://s3.51cto.com/wyfs02/M02/22/E6/wKioL1MrB5yApnR6AACYPYBdSH8774.jpg" target="_blank"></a>

查看端口是否开启

<a href="http://s3.51cto.com/wyfs02/M01/22/E5/wKiom1MrB-rzCgHsAACFuDqL4a8806.jpg" target="_blank"></a>

测试端口80

<a href="http://s3.51cto.com/wyfs02/M00/22/E5/wKiom1MrC-mAjl96AABafsygNO8559.jpg" target="_blank"></a>

测试端口4800

测试5800

<a href="http://s3.51cto.com/wyfs02/M02/22/E5/wKiom1MrDDKwrD6yAACCcbw5o3s817.jpg" target="_blank"></a>

四、文本压缩

好处:

经过压缩后实际上降低了网络传输的字节数,最明显的好处就是可以加快网页加载的速度。网页加载速度加快的好处不言而喻,除了节省流量,改善用户的浏览体验

Web服务器处理HTTP压缩的过程如下:

① Web服务器接收到浏览器的HTTP请求后,检查浏览器是否支持HTTP压缩

(Accept-Encoding 信息);

② 如果浏览器支持HTTP压缩,Web服务器检查请求文件的后缀名;

③ 如果请求文件是HTML、CSS等静态文件,Web服务器到压缩缓冲目录中检查是否已经存在请求文件的最新压缩文件;

④ 如果请求文件的压缩文件不存在,Web服务器向浏览器返回未压缩的请求文件,并在压缩缓冲目录中存放请求文件的压缩文件;

⑤ 如果请求文件的最新压缩文件已经存在,则直接返回请求文件的压缩文件;

1、在主配置文件/etc/httpd24/httpd.conf文件中启用 deflate模块

<a href="http://s3.51cto.com/wyfs02/M00/22/E6/wKioL1MrDlvxTTsaAAB40VAGOyc120.jpg" target="_blank"></a>

MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准

2、定义压缩类型、压缩等级、对特殊浏览器不支持压缩的定义

<code># Level of compression (Highest 9 - Lowest 1)</code>

<code>DeflateCompressionLevel 9</code>

<code># Netscape 4.x has some problems.</code>

<code>BrowserMatch ^Mozilla</code><code>/4</code> <code>gzip</code><code>-only-text</code><code>/html</code>

<code># Netscape 4.06-4.08 have some more problems</code>

<code>BrowserMatch ^Mozilla</code><code>/4</code><code>\.0[678] no-</code><code>gzip</code>

<code># MSIE masquerades as Netscape, but it is fine</code>

<code>BrowserMatch \bMSI[E] !no-</code><code>gzip</code> <code>!</code><code>gzip</code><code>-only-text</code><code>/html</code>

<code>&lt;IfModule deflate_module&gt;</code>

<code>AddOutputFilterByType DEFLATE text</code><code>/plain</code>

<code>AddOutputFilterByType DEFLATE text</code><code>/html</code>

<code>AddOutputFilterByType DEFLATE text</code><code>/xml</code>

<code>AddOutputFilterByType DEFLATE text</code><code>/css</code>

<code>AddOutputFilterByType DEFLATE text</code><code>/javascript</code>

<code>AddOutputFilterByType DEFLATE application</code><code>/xhtml</code><code>+xml</code>

<code>AddOutputFilterByType DEFLATE application</code><code>/xml</code>

<code>AddOutputFilterByType DEFLATE application</code><code>/rss</code><code>+xml</code>

<code>AddOutputFilterByType DEFLATE application</code><code>/atom_xml</code>

<code>AddOutputFilterByType DEFLATE application</code><code>/x-javascript</code>

<code>AddOutputFilterByType DEFLATE application</code><code>/x-httpd-php</code>

<code>&lt;</code><code>/IfModule</code><code>&gt;</code>

3、查看结果

<a href="http://s3.51cto.com/wyfs02/M01/22/E6/wKioL1MrEYSx4bmoAAPFNqDyAeI734.jpg" target="_blank"></a>

五、利用mod_status查看apache服务相关信息

1、启用状态模块、info文件

<code>LoadModule status_module modules</code><code>/mod_status</code><code>.so</code>

<code># Real-time info on requests and configuration</code>

<code>Include </code><code>/etc/httpd24/extra/httpd-info</code><code>.conf</code>

2、修改<code>/etc/httpd24/extra/httpd-info</code><code>.conf文件</code>

Require [host|ip]对那一个ip或主机响应

3、重启服务、测试

<a href="http://s3.51cto.com/wyfs02/M02/22/E6/wKioL1MrFLTg83E_AAK2xXkpd8A586.jpg" target="_blank"></a>

======================================完=============================================

本文转自 jinlinger 51CTO博客,原文链接:http://blog.51cto.com/essun/1380687,如需转载请自行联系原作者

继续阅读