Centos6上编译安装httpd2.4并实现HTTPS浏览

实验目的:

模拟Centos6上安装httpd2.4,并实现https加密访问主页

实验器材:

Centos6.5虚拟机

实验步骤:

 

1 生成自签名证书

2 下载并编译安装httpd2.4及其相关组件

3 安装ssl模块并修改httpd配置文件

实验过程:

 

1 生成自签名证书

 

https协议是基于SSL协议的,在使用https协议之前首先要获得SSL的CA证书,在生产情况下此证书需要向CA证书颁发机构购买,而在实验环境下可以使用openssl工具生成自签名证书。

1)  创建根CA证书

 

根CA证书是CA认证机构必须有的,在本实验需要先创建此证书才能为自建网站颁发证书。

 

cd /etc/pki/CA/ #进入CA目录
touch index.txt #创建index.txt文件,此文件记录了根CA颁发的所有证书
echo 01 > serial #添加起始计数
(umask 077;openssl genrsa -out private/cakey.pem 4096)#生成私钥
openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650 #生成根证书

 

2)  创建网站CA申请

 

(umask 066;openssl genrsa -out private/client.key 1024) #生成私钥
openssl req -new -key private/client.key -days 356 -out client.csr #通过私钥生成证书申请文件

 

3)  申请网站CA证书

 

openssl ca -in client.csr -out certs/client.cer

 

至此,网站的CA证书创建完成,证书文件client.cer与私钥client.key备用

2 下载并编译安装httpd2.4及其相关组件

 

         安装httpd2.4需要的组件包括apr-1.5.0、apr-util-1.5.3、httpd-2.4.25、pcre及openssl-devel

1)  下载编译安装apr-1.5.0

 

wget http://archive.apache.org/dist/apr/apr-1.5.0.tar.gz
tar xf apr-1.5.0.tar.gz 
cd apr-1.5.0
./configure --prefix=/usr/local/apr
make && make install

 

 

2)下载编译安装apr-util-1.5.3

 

wget http://archive.apache.org/dist/apr/apr-util-1.5.3.tar.gz
tar xf apr-util-1.5.3.tar.gz
cd apr-util-1.5.3
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
make && make install

 

4)  安装pcre及openssl-devel

 

 

yum install -y pcre-devel
yum install –y openssl-devel

 

5)  下载编译安装httpd-2.4.10

 

wget http://archive.apache.org/dist/httpd/httpd-2.4.25.tar.gz
tar xf httpd-2.4.25.tar.gz
cd httpd-2.4.25
./configure --prefix=/usr/local/apache24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpm-shared=all --with-mpm=prefork
make && make install

 

 

3 安装ssl模块并修改httpd配置文件

 

1) 安装ssl模块

yum install mod_ssl -y
httpd -M | grep ssl #查看模块安装情况

 

2)修改配置文件/usr/local/apache24/conf/httpd.conf

LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
Include conf/extra/httpd-ssl.conf

 

3)修改配置文件/usr/local/apach24/conf/extra/httpd-ssl.conf

DocumentRoot "/var/www/html"
ServerName www.ch.com
SSLCertificateFile /usr/local/apach24/ssl/client.csr
SSLCertificateKeyFile /usr/local/apach24/ssl/client.key

 

 

重启服务

 

/usr/local/apache24/bin/apachectl restart


把httpd的可执行程序加入系统环境变量

#加入系统环境变量以后使用httpd就不再需要每次都输入其绝对路径,方便快捷。
[root@pxe68 httpd-2.4.25]# vi /etc/profile.d/httpd.sh
#在/etc/profile.d/这个目录新建一个httpd.sh的文件,加入以下内容:
PATH=$PATH:/usr/local/httpd24/bin/
export PATH
#然后保存退出,执行以下命令,使这个文件生效。
[root@pxe68 httpd-2.4.25]# source /etc/profile.d/httpd.sh

启动httpd

[root@pxe68 bin]# apachectl start
[root@pxe68 bin]# ss -tuan
Netid State      Recv-Q Send-Q        Local Address:Port                       Peer Address:Port              
tcp   LISTEN     0      128                       *:22                                    *:*                  
tcp   LISTEN     0      100               127.0.0.1:25                                    *:*                  
tcp   ESTAB      0      52            192.168.81.88:22                         192.168.81.1:60469              
tcp   LISTEN     0      128                      :::80                                   :::*                  
tcp   LISTEN     0      128                      :::22                                   :::*                  
tcp   LISTEN     0      100                     ::1:25                                   :::*


把httpd的库文件加入到系统的库中,这样别的软件包就也使用这些库文件了。
[root@pxe68 htdocs]# vi /etc/ld.so.conf.d/httpd.conf
/usr/local/httpd24/modules
[root@pxe68 htdocs]# ldconfig
#让系统生成库文件的缓存

把httpd的man文档加入到系统的man库中,方便随时调用

[root@pxe68 htdocs]# vi /etc/man_db.conf
 20 MANDATORY_MANPATH                       /usr/man
 21 MANDATORY_MANPATH                       /usr/share/man
 22 MANDATORY_MANPATH                       /usr/local/share/man
 23 MANDATORY_MANPATH                       /usr/local/httpd24/man
 #在第22行下边加入httpd的man文件的地址

把编译安装的httpd 实现服务脚本,通过service和chkconfig 进行管理

1 编译安装httpd

把httpd编译安装在/usr/local/apache24/目录下。

2 在/etc/rc.d/init.d/目录下新建一个文件httpd

这个文件的目的在于让service 命令可以管理编译安装的httpd服务。

文件内容如下:

 

[root@CentOS68 ~]# cat /etc/rc.d/init.d/httpd
#!/bin/bash
#
# httpd        Start up the httpd server daemon
#
# chkconfig: 2345 99 01
# description: httpd is a protocol for web server.
# This service starts up the httpd server daemon.
#
# processname: httpd
case $1 in
start)
/usr/local/apache24/bin/apachectl start ;;
stop)
/usr/local/apache24/bin/apachectl stop ;;
status)
/usr/local/apache24/bin/apachectl status ;;
*)
echo err
esac

或者修改已有的路径就行了。。。。

  添加为开机启动

 

[root@CentOS68 /app/httpd/bin]# chkconfig --add httpd
[root@CentOS68 /app/httpd/bin]# chkconfig --list |grep httpd
httpd     0:off    1:off    2:on    3:on    4:on    5:on    6:off
[root@CentOS68 /app/httpd/bin]#

 

可以看到已经添加成功

通过service 命令启动服务

 

[root@CentOS68 ~]# service httpd start
httpd: Could not reliably determine the server's fully qualified domain name, using CentOS68.localhost for ServerName

 

可以看到会报错,但是服务已经启动成功了,修改/app/httpd/conf/httpd.conf这个文件,把98行前面的#去掉即可

98 #ServerName www.example.com:80

 

现在可以通过service命令管理手动安装的httpd 服务了

posted @ 2017-07-02 12:30  chenghuan  阅读(229)  评论(0)    收藏  举报