1. 程式人生 > >Apache虛擬目錄、用戶認證、基於端口/IP/域名的虛擬主機、SSL

Apache虛擬目錄、用戶認證、基於端口/IP/域名的虛擬主機、SSL

mage nss pda noexec serve 啟動 enable clu adc

環境配置:

配置DNS以便域名解析

  1. 安裝Bind軟件包。

yum install -y bind

2. 修改Bind配置文件。

vim /etc/named.conf

listen-on port 53 { 192.168.200.101; };

allow-query { any; };

vim /etc/named.rfc1912.zones

zone "a.com" IN {

type master;

file "a.com.localhost";

allow-update { none; };

};

3. 修改Bind區域文件。

cd /var/named/

cp -p named.localhost a.com.loaclhost

vim a.com.loaclhost

$TTL 1D

@ IN SOA a.com. rname.invalid. (

0 ; serial

1D ; refresh

1H ; retry

1W ; expire

3H ) ; minimum

NS dns.a.com.

dns A 192.168.200.101

www A 192.168.200.102

m A 192.168.200.102

4. Bind服務器語法檢查,啟動Bind服務。

named-checkconf /etc/named.conf

named-checkconf /etc/named.rfc1912.zones

named-checkzone a.com /var/named/a.com.localhost

systemctl start named

systemctl enable named

WEB服務器安裝apache軟件包。

yum install -y httpd httpd-devel

虛擬目錄

  1. 虛擬目錄配置。

vim /etc/httpd/conf/httpd.conf

95 ServerName www.a.com:80

vim /etc/httpd/conf.d/vhost.conf

alias /vshare "/var/www/share"

<directory "/var/www/share">

allowoverride none

options includesnoexec

order allow,deny

allow from all

</directory>

2. 創建站點目錄,配置默認文檔

mkdir /var/www/share/

echo "share" > /var/www/share/index.html

chown apache:apache /var/www/share/

3. apache語法檢測,啟動apache服務

httpd -t

systemctl start httpd

4. 客戶端dns設置為192.168.200.101,然後訪問www.a.com/vshare

技術分享圖片

用戶認證

  1. 虛擬目錄配置。

vim /etc/httpd/conf.d/vhost.conf

alias /vhome "/var/www/home"

<directory "/var/www/home">

authtype basic

authname "enter user and password"

authuserfile /etc/httpd/users-password

require valid-user

options Includesnoexec

allowOverride none

order allow,deny

allow from all

</directory>

2. 創建站點目錄,配置默認文檔

mkdir /var/www/home/

echo "home" >/var/www/home/index.html

chown apache:apache /var/www/home

3. 創建用戶認證

htpasswd -c /etc/httpd/users-password user123

htpasswd /etc/httpd/users-password user456 #第二次創建不加-c

4. apache語法檢測,啟動apache服務

httpd -t

systemctl restart httpd

5. 客戶端dns設置為192.168.200.101,然後訪問www.a.com/vhome

技術分享圖片

技術分享圖片

基於端口的虛擬主機

  1. 虛擬目錄配置。

vim /etc/httpd/conf/httpd.conf

97 ServerName www.a.com:80

42 listen 80

listen 8888

listen 9999

vim /etc/httpd/conf.d/vhost.conf

<virtualhost www.a.com:8888>

servername www.a.com:8888

serveradmin [email protected]

documentroot /var/www/port8888

errorlog logs/port8888/error.log

customlog logs/port8888/access.log combined

</virtualhost>

<virtualhost www.a.com:9999>

servername www.a.com:9999

serveradmin [email protected]

documentroot /var/www/port9999

errorlog logs/port9999/error.log

customlog logs/port9999/access.log combined

</virtualhost>

mkdir /var/www/port8888

mkdir /var/www/port9999

mkdir /etc/httpd/logs/port8888

mkdir /etc/httpd/logs/port9999

chown -R apache:apache /var/www/port*

chown -R apache:apache /etc/httpd/logs/port*

echo "port8888" > /var/www/port8888/index.html

echo "port9999" > /var/www/port9999/index.html

3. apache語法檢測,啟動apache服務

httpd -t

systemctl restart httpd

技術分享圖片

技術分享圖片

基於IP的虛擬主機

多配置一個IP地址用於測試

ifconfig ens32:0 192.168.200.202/24

ip addr

2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

inet 192.168.200.102/24 brd 192.168.200.255 scope global ens32

inet 192.168.200.202/24 brd 192.168.200.255 scope global secondary ens32:0

vim /etc/httpd/conf.d/vhost.conf

<virtualhost 192.168.200.102:80>

servername 192.168.200.102:80

serveradmin [email protected]

documentroot /var/www/102

errorlog logs/102/error.log

customlog logs/102/access.log combined

</virtualhost>

<virtualhost 192.168.200.202:80>

servername 192.168.200.202:80

serveradmin [email protected]

documentroot /var/www/202

errorlog logs/202/error.log

customlog logs/202/access.log combined

</virtualhost>

mkdir /var/www/102

mkdir /var/www/202

mkdir /etc/httpd/logs/102

mkdir /etc/httpd/logs/202

chown -R apache:apache /var/www/*02

chown -R apache:apache /etc/httpd/logs/*02

echo "102" > /var/www/102/index.html

echo "202" > /var/www/202/index.html

apache語法檢測,啟動apache服務

httpd -t

systemctl restart httpd

技術分享圖片

技術分享圖片

基於域名的虛擬主機

  1. 虛擬目錄配置。

vim /etc/httpd/conf/httpd.conf

97 ServerName a.com:80

vim /etc/httpd/conf.d/vhost.conf

<virtualhost www.a.com:80>

servername www.a.com:80

serveradmin [email protected]

documentroot /var/www/www

errorlog logs/www/error.log

customlog logs/www/access.log combined

</virtualhost>

<virtualhost m.a.com:80>

servername m.a.com:80

serveradmin [email protected]

documentroot /var/www/m

errorlog logs/m/error.log

customlog logs/m/access.log combined

</virtualhost>

mkdir /var/www/www

mkdir /var/www/m

mkdir /etc/httpd/logs/www

mkdir /etc/httpd/logs/m

chown -R apache:apache /var/www/www

chown -R apache:apache /var/www/m

chown -R apache:apache /etc/httpd/logs/www

chown -R apache:apache /etc/httpd/logs/m

echo "www" > /var/www/www/index.html

echo "m" > /var/www/m/index.html

apache語法檢測,啟動apache服務

httpd -t

systemctl restart httpd

技術分享圖片

技術分享圖片

基於SSL的web站點配置。

yum install -y mod_ssl openssl openssl-devel

cd /etc/pki/tls/private

openssl genrsa 1024 > www.a.com.key

cd ../certs/

openssl req -new -x509 -days 365 -key ../private/www.a.com.key -out www.a.com.crt

Country Name (2 letter code) [GB]: 輸入國家地區代碼,如中國的 CN

State or Province Name (full name) [Berkshire]: 地區省份

Locality Name (eg, city) [Newbury]: 城市名稱

Organization Name (eg, company) [My Company Ltd]: 公司名稱

Organizational Unit Name (eg, section) []: 部門名稱

Common Name (eg, your name or your server’s hostname) []: 申請證書域名

Email Address []: 電子郵箱

vim /etc/httpd/conf.d/ssl.conf

59 DocumentRoot "/var/www/ssl"
60 ServerName www.a.com:443

100 SSLCertificateFile /etc/pki/tls/certs/www.a.com.crt

107 SSLCertificateKeyFile /etc/pki/tls/private/www.a.com.key

mkdir /var/www/ssl

chown -R apache:apache /var/www/ssl/

echo "1 2 3 4 5" > /var/www/ssl/index.html

apache語法檢測,啟動apache服務

httpd -t

systemctl restart httpd

技術分享圖片

技術分享圖片

Apache虛擬目錄、用戶認證、基於端口/IP/域名的虛擬主機、SSL