2013-09-14 13:56:16
来 源
ITJS.CN
Apache
该篇讲述在FreeBSD上配置Apache+SSL,希望对于初学Apache服务器相关的朋友有帮助,更多Apache安装、配置、报错处理等资源请本站内搜索。
SSL(Secure Socket Layer)是一种在让可持有证书的浏览器软件(比如Internet Explorer、Netacpe Navigator)和WWW服务器(如Netscape的Netscape Enterprise Server 、ColdFusion Server等等)之间构造的安全通道中传输数据的协议,他运行在TCP/IP层之上、应用层之下,为应用程序提供加密数据通道。

  Apache,众所周知,是UNIX世界最为流行的WWW服务器软件,性能好,可扩展性强。同时他也有相应的Win32版本。

  在访问一些网站时,我们会注意到它的URL不是一般的HTTP://aa.bb.cc ,而是https://aa.bb.cc 这表明网站提供了HTTPS连接功能,即SSL over HTTP. 这保证了客户端与服务器的通信都是在SSL建立的加密通道中进行,绝对安全。为了让Apache服务器支持https,那么就必须配置mod_ssl模块。下文就开始就Apache+mod_ssl on FreeBSD进行讨论。

配置Apache服务器

首先,如果您的FreeBSD系统已经配置好Apache服务器,那么最好先备份一下,起码要把Apache的配置文件作一备份。如果需要直接往现有的安装上配置SSL支持,请阅读文章的最后一部分。

  · 安装port

  port的安装非常方便,由于笔者已经安装好了所有的port 框架,所以只需要 cd /usr/ports/www/apache13-modssl make install

  这样就安装好了Apache的二进制文件,但是这还不够。

  · Apache以什么方式运行?

  比如,笔者需要Apache以www用户组的www用户运行(这也是出于安全性考虑)。如果发现针对apache的入侵,那么这只是针对这个组/用户的。所以还是推荐这样配置用户。

可以用adduser命令添加用户,也可以直接vipw,添加:

www:*:99:99::0:0:apache Daemon:/nonexistent:/sbin/nologin

同时需要添加www组,在/etc/group里面添加一行

www:*:99

  · 配置文件

以下都需要在apache的配置文件中进行改变。缺省条件下,它是/usr/local/etc/apache/apache.conf

下列表格是笔者改动的一些设置

  · 测试配置

  配置好apache以后,运行# /usr/local/sbin/apachectl configtest Syntax OK

  表示已经工作正常

  · 启动和重起服务器

  现在,一切已经工作正常,可以启动apache服务器

  # /usr/local/sbin/apachectl startssl /usr/local/sbin/apachectl startssl: httpd started

  启动可能需要一些时间,因为需要产生随机数。

  如果你又做了一些配置上的改动,需要重起服务器,可以

  # /usr/local/sbin/apachectl graceful /usr/local/sbin/apachectl graceful: httpd gracefully restarted

  · 校错

  如果启动出现问题,那么最好去查阅log文件, 比如

  # tail apache_ssl_engine_log

  [notice] SIGUSR1 received. Doing graceful restart

[notice] Apache/1.3.14 (Unix) mod_ssl/2.7.1 OpenSSL/0.9.4

configured -- resuming normal operations

然后,看看SSL引擎的log

# tail apache_ssl_engine_log

[info] Init: 1st restart round (already detached)

[info] Init: Reinitializing OpenSSL library

[info] Init: Seeding PRNG with 1160 bytes of entropy

[info] Init: Configuring temporary RSA private keys (512/1024 bits)

[info] Init: Configuring temporary DH parameters (512/1024 bits)

[info] Init: Initializing (virtual) servers for SSL

[info] Init: Configuring server new.host.name:443 for SSL protocol

[warn] Init: (new.host.name:443) RSA server certificate CommonName

(CN) `www.snakeoil.dom' does NOT match server name!?

  问题就一幕了然了,其中snakeoil是安装的时候缺省的证书,以后会用我们自己的证书来代替之。

  · 别忘了自己的防火墙

  如果你的机器是在防火墙后面,别忘了让防火墙允许对443端口的访问,这就是https使用的端口。

  · 浏览一下

  然后在浏览器里面输入http://server server是你的服务器的IP地址,然后会出现以下的字样:

   Hey, it worked !

   The SSL/TLS-aware Apache webserver wassuccessfully installed on this website.

  然后同样的试试https://server, 应该也有同样的字眼。

  · 获取一个证书

  下面描述的是怎样产生自己的一个测试性证书。这个证书不能公开使用,但是怎样获得一个可以公开使用的证书就不在本文所讲述的范围了。

  利用下列命令得到一个测试证书

  # cd /usr/ports/www/apache13-modssl

  # make certificate

  笔者已经改动了所能够看懂的所有名称,其他的都跟缺省的相同,然后会在下列目录产生一个证书文件

/usr/ports/www/apache13-modssl/work/apache_1.3.14/conf

  应该可以找到两个子目录,ssl.crt和ssl.key。这些目录需要拷贝到apache的根目录,但是之前笔者还是对原来的文件做了一个备份。

# cd /usr/local/etc/apache

# mv ssl.crt ssl.crt-default

# mv ssl.key ssl.key-default

  然后把刚才生成的文件拷贝过去

# cd /usr/ports/www/apache13-modssl/work/apache_1.3.14/conf

# cp -rp ssl.key /usr/local/etc/apache

# cp -rp ssl.crt /usr/local/etc/apache

  然后就需要重起apache,但是笔者刚开始用的是graceful restart,但是没能读取新的证书。于是就做了stop然后start.下面是屏幕输出

# /usr/local/sbin/apachectl startssl

Apache/1.3.14 mod_ssl/2.7.1 (Pass Phrase Dialog)

Some of your private key files are encrypted for security reasons.

In order to read them you have to provide us with the pass phrases.

Server new.host.name:443 (RSA)

Enter pass phrase:

Ok: Pass Phrase Dialog successful.

/usr/local/sbin/apachectl startssl: httpd started

  这样就好了。同时笔者还在生成证书的时候加上了一个passphrase。WWW服务器启动的时候需要它。

  · 取消passphrase

如果后来你有不想要passhprase了,那么也可以取消它。可以参阅http://www.modssl.org/docs/2.7/ssl_faq.html#ToC31 然而取消passphrase会有一些安全问题,上面的url里面也提到了,可以根据需要来斟酌。

  · 确认安全问题

  为了确认我的https连接确实是被经过加密的,于是在Netscape里面我查阅了View-->Page Info,里面有下列字样:

  

  所以https连接确实是加密的。

  · 笔者遇到的问题

  当我试图在Netscape 4.74里面访问https://server时,下面的对话框冒了出来:When I tried to browse to the website from Netscape 4.74, I was created with the following message box:

SSL has recieved an error from the server indicating an incorrect Message Authentication Code. This could indicate a network error, a bad server implementation, or a security violation.

然后在/var/log/apache_error_log发现了下面的错误:

[error] mod_ssl: SSL handshake failed (server new.host.name:443,

client 10.0.0.99) (OpenSSL library error follows)

[error] OpenSSL: error:0407106B::lib(4) :func(113) :reason(107)

[error] OpenSSL: error:04065072::lib(4) :func(101) :reason(114)

[error] OpenSSL: error:1408F071::lib(20) :SSL3_GET_RECORD:bad

mac decode [Hint: Browser still remembered details of a

re-created server certificate?]

  而在IE5.5里面却没有问题,实在很令人困扰。更好笑的是,第二天用Netscape再次访问的时候却好了!我想大概是因为我重起了Netscape的进程,当没有重起的时候Netscape认为这是个新的证书,对自己不适用,重起以后就认得了(Netscape确实雄风不再,唉)。

  · 在已有的安装上配置ssl

  笔者开始时就尝试在已有的apache上安装SSL,按照以上的步骤。但是不幸的是,竟然把以前的apache也搞崩溃了。然后采取的办法就是:

  · 为配置文件作一备份

  · 卸载所有apache,ssl和php(从pkg_info的输出查看一下,然后用pkg_delete卸载)

  · 按照上面的步骤重新来过

  · 安装mod_php4

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。