CentOS 6下Apache的https虚拟主机实践

===============================================================================


1.建立httpd服务器

创建所需文件夹:
# mkdir -p /web/vhosts/www{1,2}


(a)、(b)

因为服务器自带httpd,无需安装
所以直接编辑httpd配置文件:httpd.conf
# vim /etc/httpd/conf/httpd.conf

注释掉:
DocumentRoot

取消注释:
#NameVirtualHost *:80

修改:
ServerName localhost:80

<Directory /web/vhosts/www1>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

<Directory /web/vhosts/www2>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

配置好后发现
Apache 403 error, (13)Permission denied: access to / denied问题
检查了一圈httpd.conf和目录权限,均没有发现问题。
发现是因为系统启动了SELINUX导致的。

临时关闭SELINUX
setenforce 0

永久关闭
vim /etc/selinux/config
修改
SELINUX=enforcing
改成
SELINUX=disabled

(c)


(d)

创建一个访问账户,按提示操作
# htpasswd -c /etc/httpd/conf.d/.htpasswd webadmin

修改httpd.conf,加入
<Location /server-status>
AuthType Basic
AuthName "Administrator privateeee"
AuthUserFile "/etc/httpd/conf.d/.htpasswd"
Require user "webadmin"
SetHandler server-status
Order deny,allow
Deny from all
Allow from 192.168.3.3
</Location>

需要使用OpenSSL生成自签名证书,确保OpenSSL已安装.
# httpd -M | grep ssl
如果没有则安装
# yum install mod_ssl openssl

在CentOS A服务器上配置CA服务,再给当前服务器(CentOS B)的https颁发证书.

CentOS A:

初始化CA服务,创建所需要的文件(/etc/pki/CA/)
# touch index.txt 创建索引文件
# echo 01 > serial 创建序列号文件

CA自签证书
生成私钥
# (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
使用私钥生成签名证书
# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem

CentOS B:

# mkdir /etc/httpd/ssl
# cd /etc/httpd/ssl
生成秘钥
# (umask 007;openssl genrsa -out httpd.key 1024)
生成请求文件
# openssl req -new -key httpd.key -out httpd.csr

把生成的文件发送到CA服务器 CentOS A:
# scp httpd.csr [email protected]:/tmp/


回到CentOS A:


回到CentOS B:

配置httpd的ssl配置(ssl.conf):

# cd /etc/httpd/conf.d/
备份
# cp ssl.conf{,.bak}

编辑ssl.conf

修改
<VirtualHost _default_:443>

<VirtualHost *:443>

私钥位置
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
=>
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key


配置完毕检查配置文件语法错误:
# httpd -t

重启httpd:
# service httpd restart

查看443端口是否已开启:
ss -tnl

使用s_client在CentOS A上做测试:
# openssl s_client -connect 192.168.3.60:443 -CAfile /etc/pki/CA/cacert.pem


HTTP/1.1 200 OK
Date: Wed, 05 Oct 2016 11:20:16 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Fri, 30 Sep 2016 13:33:02 GMT
ETag: "bf4e8-21-53db9a230598a"
Accept-Ranges: bytes
Content-Length: 33
Connection: close
Content-Type: text/html; charset=UTF-8

测试成功!

Apache 的详细介绍请点这里
Apache 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2016-11/137210.htm