1. 程式人生 > >httpd-2.4實現虛擬主機、訪問控制及https功能

httpd-2.4實現虛擬主機、訪問控制及https功能

httpd-2.4 安全訪問 https 虛擬主機

準備工作:在Centos7中安裝httpd,使用yum安裝或自己編譯安裝,建議使用yum安裝,快捷又方便。

關閉防火墻及selinux。


  1. 提供兩個基於名稱的虛擬主機www1, www2;有單獨的錯誤日誌和訪問日誌;

    先建立虛擬主機www1

    a.在httpd的輔助配置文件目錄/etc/httpd/conf.d/中創建屬於虛擬主機自己的配置文件

~]# vim /etc/httpd/conf.d/vhosts-www1.conf

<VirtualHost 192.168.127.128:80>
        DocumentRoot "/myweb/vhosts/www1"
        ServerName www.link1.com
        ErrorLog "/myweb/vhosts/www1/logs/error_log"
        CustomLog "/myweb/vhosts/www1/logs/access_log" combined
</VirtualHost>
<Directory "/myweb/vhosts/www1">
    AllowOverride None
    Options None
    Require all granted
</Directory>

b.創建好配置文件後,再創建文檔根目錄及日誌目錄

~]# mkdir /myweb/vhosts/www1/logs -pv

c.創建並向文檔根目錄下的index.html寫點東西,並在本機的C:\Windows\System32\drivers\etc目錄下的HOST文件中添加192.168.127.128 www.link1.com。

重新載入配置文件

systemctl reload httpd.service

然後用本地瀏覽器打開,結果如下:

技術分享

查看訪問日誌/myweb/vhosts/www1/logs/access_log,內容如下:

192.168.127.1 - - [29/Aug/2017:15:40:00 +0800] "GET /sky/ HTTP/1.1" 200 1319 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0"

狀態碼為200,請求成功。


虛擬主機www2的建立過程與www1的沒有差別,只不過把相關名稱改了就行,最後用瀏覽器測試,結果如下:技術分享



2.訪問控制

a.通過www1的/server-status提供狀態信息,且僅允許link用戶訪問;

a-1.修改www1的配置文件如下:

<VirtualHost 192.168.127.128:80>
        DocumentRoot "/myweb/vhosts/www1"
        ServerName www.link1.com
        ErrorLog "/myweb/vhosts/www1/logs/error_log"
        CustomLog "/myweb/vhosts/www1/logs/access_log" combined
</VirtualHost>
<Directory "/myweb/vhosts/www1">
    AllowOverride None
    Options None
    AuthType basic
    AuthName "Please input user and password to login,only link has permission to access!!"
     AuthUserFile /etc/httpd/users/.htpasswd
     Require user link
</Directory>

a-2.使用htpasswd命令創建虛擬用戶

~]# mkdir /etc/httpd/users
~]# htpasswd -c -m /etc/httpd/users/.htpasswd link
~]# htpasswd -m /etc/httpd/users/.htpasswd link1

a-3.重新載入配置文件,打開瀏覽器輸入就會出現以下情況:

技術分享

當輸入link用戶及密碼後:


技術分享


技術分享

當輸入link1用戶及密碼時:

技術分享

技術分享

因為只允許link用戶登錄:

技術分享

至此,要求實現。


b.www2不允許192.168.127.0/24 網絡中任意主機訪問;

從之前查看訪問日誌中看到本主機的ip地址為192.168.127.1。

那我們就將www2的配置文件修改如下:

<VirtualHost 192.168.127.128:80>
        DocumentRoot "/myweb/vhosts/www2"
        ServerName www.link2.com
        ErrorLog "/myweb/vhosts/www2/logs/error_log"
        CustomLog "/myweb/vhosts/www2/logs/access_log" combined
<Directory "/myweb/vhosts/www2">
    AllowOverride None
    Options None
    <RequireAll>
      Require all granted
      Require not ip 192.168.127.0/24
    </RequireAll>
</Directory>
</VirtualHost>

修改之前訪問如下:

技術分享

修改之後訪問如下:

技術分享

至此,所要求的功能實現。


3.為上面的www2虛擬主機提供https服務

創建私有CA,然後再為本服務器頒發自簽證書。

a.創建私有CA

a-1.創建私有CA私鑰文件

~]# (umask 077 ; openssl genrsa -out /etc/pki/CA/private/cakey.pem 4096)

a-2.生成自簽證書

~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem  -out /etc/pki/CA/cacert.pem -days 3653

a-3.滿足CA所必須的目錄級文件和文本文件的布局

~]# touch /etc/pki/CA/index.txt
~]# echo 01 > /etc/pki/CA/serial

b.為服務器提供證書

b-1.創建服務器的私鑰文件

~]# mkdir /etc/httpd/conf/ssl
~]# cd /etc/httpd/conf/ssl
ssl]# (umask 077 ; openssl genrsa -out httpd.key 4096)

b-2.生成證書請求文件

ssl]# openssl req -new -key httpd.key -out httpd.csr -days 3653

b-3.由CA簽發證書:在CA所在的服務器上完成

ssl]# openssl ca -in httpd.csr -out httpd.crt -days 365

至此證書頒發完成。


c.安裝mod_ssl模塊

yum -y install mod_ssl

修改ssl的配置文件的部分內容如下:

SSLCertificateFile /etc/httpd/conf/ssl/httpd.crt
<directory "/myweb/vhosts/ssl">
    AllowOverride None
    Options None
    Require all granted
</Directory>
DocumentRoot "/myweb/vhosts/ssl"
ServerName www.link2.com
SSLCertificateKeyFile /etc/httpd/conf/ssl/httpd.key

然後再創建/myweb/vhosts/ssl目錄

~]# mkdir /myweb/vhosts/ssl
~]# echo "welcome to https://www.link2.com" >> /myweb/vhosts/ssl/index.html

然後重啟服務。

不加密的訪問如下:

技術分享

https訪問如下:

技術分享

因為該證書是我們自己頒發的,所以剛開始訪問時會說證書不受信任或有風險,添加例外就行了。

httpd-2.4實現虛擬主機、訪問控制及https功能