Apache SSL安装与配置教程(Linux系统)

转自http://www.bootf.com/563.html

 

互联网的线路侦听无处不在,明文传输的数据一不留神就可能被窃取。而Apache的SSL加密连接可以帮助浏览者更加安全可靠的传输数据。通常来说,普通的HTTP协议URL是以 http:// 开头,而SSL加密协议则是以 https:// 开头。本文将介绍CentOS系统下通过仓库(yum、apt-get)配置apache中SSL加密模块的方法。

 

 

实验环境:

CentOS release 5.7 (Final)
Apache/2.2.3

 

首先安装Apache

1 [root@www ~]# yum install httpd

大多数情况下,安装Apache需要附带安装好php、Mysql等模块,可以参考VPS管理百科以前的文章《Apache+PHP+MySQL环境配置教程》,本文将不再赘述。

部署好Apache环境后,开始

 

安装SSL模块

1 [root@www ~]# yum install mod_ssl

安装完成,直接重启Apache服务:

1 [root@www ~]# /etc/init.d/httpd restart

由于安装mod_ssl会创建一个默认的SSL证书,路径位于 /etc/pki/tls ,此时可以立即通过https访问服务器了:

https://IP/

如果不使用默认的证书,也可以使用OPENSSL手动创建证书。

 

使用OPENSSL手动创建证书

先执行以下命令安装OPENSSL:

1 [root@www ~]# yum install openssl

完成后即可按照以下步骤生成证书文件:

1、创建私钥

1 [root@www ~]# openssl genrsa -out server.key 1024

2、用私钥 server.key 文件生成证书签署请求 CSR

1 [root@www ~]# openssl req -new -key server.key -out server.csr

此步骤需要输入一些证书信息,解释如下:

Country Name (2 letter code) [GB]:【在此输入两个字符的国家名。中国的为 CN 】
State or Province Name (full name) [Berkshire]:【省份名称,如北京为 beijing 】
Locality Name (eg, city) [Newbury]:【城市名称,如 beijing】
Organization Name (eg, company) [My Company Ltd]:【公司名称】
Organizational Unit Name (eg, section) []:【部门名称】
Common Name (eg, your name or your server’s hostname) []:【姓名,通常即证书名】
Email Address []:【电子邮箱地址】

随后会要求输入一个challenge password(密码),无需输入,后面一律直接回车即可。

3、生成证书CRT文件

1 [root@www ~]# openssl x509 -days 3650 -req -in server.csr -signkey server.key -out server.crt

上面生成的证书有效期为10年(呵呵太长了点,一般三年就行了)

这时证书的相关文件就都已经生成好了。当前文件夹下应该有server.crt、server.csr、server.key这三个文件。

如果你是个完美论者,理所当然应该把这三个文件丢到证书的“官方目录”中去,省的把文件胡乱放置以后找不着:

1 [root@www ~]# mkdir /etc/pki/tls/mycert
2 [root@www ~]# mv server.* /etc/pki/tls/mycert

最后仅仅需要修改配置文件来指定证书路径:

4、指定证书路径

打开Apache的SSL配置文件 /etc/httpd/conf.d/ssl.conf 

1 [root@www ~]# vi /etc/httpd/conf.d/ssl.conf

找到如下一节:

# Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate. If
# the certificate is encrypted, then you will be prompted for a
# pass phrase. Note that a kill -HUP will prompt again. A new
# certificate can be generated using the genkey(1) command.
SSLCertificateFile /etc/pki/tls/mycert/server.crt

# Server Private Key:
# If the key is not combined with the certificate, use this
# directive to point at the key file. Keep in mind that if
# you’ve both a RSA and a DSA private key you can configure
# both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateKeyFile /etc/pki/tls/mycert/server.key

注意红色的文字修改的部分。保证其路径分别指向刚刚创建的server.crt与server.key即可。

5、重启Apache2

1 [root@www ~]# /etc/init.d/httpd restart

这时新的证书便已经生效了。刷新浏览器,点击浏览器中的证书标志,便可以看到刚刚制作的证书信息:

猜你喜欢

转载自yuxianhua.iteye.com/blog/1857079