源码安装的httpd,如何部署http协议和https协议的虚拟主机,并实现https重定向

一、源码安装httpd

1、安装apr

[root@bogon ~]# tar xf apr-1.5.2.tar.gz 
[root@bogon ~]# cd apr-1.5.2/
[root@bogon apr-1.5.2]# ./configure --prefix=/usr/local/apr 	//apr和apr-utils是源码安装httpd的运行环境
[root@bogon apr-1.5.2]# make && make install 

2、安装apr-util

[root@bogon ~]# tar xf apr-util-1.5.4.tar.gz 
[root@bogon ~]# cd apr-util-1.5.4/
[root@bogon apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr 
[root@bogon apr-util-1.5.4]# make && make install 

3、安装httpd

  • 编译的时候可以自行添加想要的模块,但一定要指出apr、apr-util的位置
[root@bogon ~]# tar xf httpd-2.4.12.tar.gz 
[root@bogon ~]# cd httpd-2.4.12/
[root@bogon httpd-2.4.12]# ./configure --prefix=/usr/local/httpd24 --enable-so --enable-rewrite --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared-most --enable-mpm-shared=all --with-mpm=event --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
[root@bogon httpd-2.4.12]# make && make install 

二、将httpd加入到环境变量中

[root@bogon ~]# vim /etc/profile
export PATH=$PATH:/usr/local/httpd24/bin
[root@bogon ~]# source /etc/profile		//重新读取配置文件
[root@bogon ~]#httpd -k start

三、将源码安装的httpd设置为开机自启

  • /etc/rc.d/rc.local

    • 这个文件会在每次开机的时候加载,所以可以将想要开机自启的命令放到这里

    • 并且为该文件添加执行权限

[root@bogon ~]# vim /etc/rc.d/rc.local 
/usr/local/httpd24/bin/httpd -k start
[root@bogon ~]# chmod a+x /etc/rc.d/rc.local

四、配置http协议的虚拟主机

[root@localhost logs]# vim /game/index.html         //编写网页目录文件
<h1> Wisdasdasdelcome to game web</h1>

[root@localhost logs]# vim /usr/local/httpd24/conf/extra/game.conf        //编写子配置文件
<VirtualHost 192.168.152.12:80>
        ServerName game.linux.com
        DocumentRoot /game
        ErrorLog /usr/local/httpd24/logs/game_error.log
        CustomLog /usr/local/httpd24/logs/game_access.log combined
</VirtualHost>

<Directory "/game">
        Require all granted
</Directory>

[root@localhost logs]# vim /usr/local/httpd24/conf/httpd.conf        //写完记得要在主配置文件中加载对应的子配置文件
Include conf/extra/game.conf

//修改windows的hosts文件后测试

 五、配置https协议的虚拟主机

  • 因为https协议涉及到公钥的认证体系可参考博客

如何基于https协议部署虚拟主机_ljh451321的博客-CSDN博客

  • 在web服务器获得证书后,开始配置https的虚拟主机
[root@localhost logs]# mkdir /www
[root@localhost logs]# cd
[root@localhost ~]# vim /www/index.html
<h1> www.linux.com</h1>
[root@localhost ~]# vim /usr/local/httpd24/conf/extra/httpd-ssl.conf
<VirtualHost _default_:443>

#   General setup for the virtual host
DocumentRoot "/www"
ServerName www.linux.com:443
ErrorLog "/usr/local/httpd24/logs/www_error.log"
TransferLog "/usr/local/httpd24/logs/www_access.log"
SSLCertificateFile "/usr/local/httpd24/ssl/www.linux.com.crt"
.
.
.
.
SSLCertificateKeyFile "/usr/local/httpd24/ssl/www.linux.com.key"
</VirtualHost>
<Directory "/www">
        Require all granted
</Directory>
  • 记得在主配置文件中让httpd开启时加载mod_ssl.so和mod_socache_shmcb.so模块
[root@localhost ~]# vim /usr/local/httpd24/conf/httpd.conf
//去掉这两个模块的#号
LoadModule ssl_module modules/mod_ssl.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
.
.
.
//启动时加载httpd-ssl.conf文件
Include conf/extra/httpd-ssl.conf
[root@localhost modules]# httpd -t
Syntax OK
[root@localhost modules]# httpd -k restart
//在浏览器测试
  • 可以在浏览器输入https://域名

六、配置https协议虚拟主机的重定向

  • 先测试不写https的时候主机在哪个网站的配置文件中,也就是说https的虚拟主机要在这个网站跳到自己的网站配置中
  • 我的在http协议的主机中修改game.conf即可
[root@www modules]# vim /usr/local/httpd24/conf/extra/game.conf 
<VirtualHost 192.168.152.10:80>
        ServerName      game.linux.com
        DocumentRoot    /game
        ErrorLog        /usr/local/httpd24/logs/game_error.log
        CustomLog       /usr/local/httpd24/logs/game_access.log combined
        RewriteEngine On	//开启重定向
        RewriteCond %{HTTP_HOST} www.linux.com [NC]	
        RewriteRule ^/ https://www.linux.com [L]
</VirtualHost>

<Directory "/game">
        Require all granted
</Directory>
[root@www modules]# vim /usr/local/httpd24/conf/httpd.conf	//将对应模块启动
LoadModule rewrite_module modules/mod_rewrite.so
  • 检查语法,重新启动,测试,既能正常访问
    [root@www extra]# httpd -t
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using www.linux.com. Set the 'ServerName' directive globally to suppress this message
    Syntax OK
    [root@www extra]# httpd -k restart
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using www.linux.com. Set the 'ServerName' directive globally to suppress this message
    [root@www extra]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值