Apache虛擬主機配置詳解
1、配置環境說明
系統環境:CentOS7
Apache環境:編譯安裝的httpd-2.4.7
系統限制:關閉了防火墻和selinux
hosts文件中配置以下域名解析
192.168.115.150 www.web1.com
192.168.115.150 www.web2.com
2、Apache虛擬主機簡介
虛擬主機是指在一個機器上運行多個網站(例如:www.web1.com和www.web2.com)。每個站點希望用不同的域名和站點目錄,或者是不同端口、不同的IP就需要虛擬主機。
3、Apache虛擬主機的分類
(1)基於IP地址的虛擬主機:一臺服務器上使用不同IP地址對應不同站點
(2)基於域名的虛擬主機:一臺服務器上使用不同的域名對應不同站點
(3)基於端口的虛擬主機:一臺服務器上使用不同的端口對應不同站點
4、Apache基於IP的虛擬主機配置方法
(1)創建web1和web2站點的目錄和網頁文件
[root@localhost ~]# mkdir -p /www/web{1..2}/logs/
[root@localhost ~]#echo 'www.web1.com'>/www/web1/index.html
[root@localhost ~]# echo 'www.web2.com'>/www/web2/index.html
[root@localhost ~]# tree /www
/www
├── web1
│ ├── index.html
│ └── logs
└── web2
├── index.html
└── logs
(2)修改Apache相關配置文件
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf 刪除這一行的註釋
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost 192.168.115.150:80>
DocumentRoot "/www/web1"
ServerName www.web1.com
ServerAlias web1.com
ErrorLog "/www/web1/logs/web1_error.log"
CustomLog "/www/web1/logs/web1_access.log" common
<Directory "/www/web1">
Options Indexes
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.115.160:80>
DocumentRoot "/www/web2"
ServerName www.web2.com
ErrorLog "/www/web2/logs/web2_error.log"
CustomLog "/www/web2/logs/web2_access.log" common
<Directory "/www/web2">
Options Indexes
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# /usr/local/apache2/bin/apachectl -t 檢查語法
Syntax OK
[root@localhost ~]# systemctl restart httpd 重啟服務
(3)瀏覽器或Linux中進行訪問測試
為了方便我這裏直接在Linux中測試了
[root@localhost ~]# curl 192.168.115.150
www.web1.com
[root@localhost ~]# curl 192.168.115.160
www.web2.com
5、Apache虛擬主機基於域名配置方法
(1)創建web1和web2站點的目錄和網頁文件
[root@localhost ~]# mkdir -p /www/web{1..2}/logs/
[root@localhost ~]#echo 'www.web1.com'>/www/web1/index.html
[root@localhost ~]# echo 'www.web2.com'>/www/web2/index.html
[root@localhost ~]# tree /www
/www
├── web1
│ ├── index.html
│ └── logs
└── web2
├── index.html
└── logs
(2)修改Apache相關配置文件
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf 刪除這一行的註釋
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost www.web1.com:80>
DocumentRoot "/www/web1"
ServerName www.web1.com
ServerAlias web1.com
ErrorLog "/www/web1/logs/web1_error.log"
CustomLog "/www/web1/logs/web1_access.log" common
<Directory "/www/web1">
Options Indexes
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost www.web2.com:80>
DocumentRoot "/www/web2"
ServerName www.web2.com
ErrorLog "/www/web2/logs/web2_error.log"
CustomLog "/www/web2/logs/web2_access.log" common
<Directory "/www/web2">
Options Indexes
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# systemctl restart httpd
(3)瀏覽器或Linux中進行訪問測試
為了方便我這裏直接在Linux中測試了
[root@localhost ~]# curl www.web1.com
www.web1.com
[root@localhost ~]# curl www.web2.com
www.web2.com
6、Apache虛擬主機基於端口配置方法
(1)創建web1和web2站點的目錄和網頁文件
[root@localhost ~]# mkdir -p /www/web{1..2}/logs/
[root@localhost ~]#echo 'www.web1.com'>/www/web1/index.html
[root@localhost ~]# echo 'www.web2.com'>/www/web2/index.html
[root@localhost ~]# tree /www
/www
├── web1
│ ├── index.html
│ └── logs
└── web2
├── index.html
└── logs
(2)修改Apache相關配置文件
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf 刪除這一行的註釋
Listen 80 添加監聽的端口
Listen 8080
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/www/web1"
ServerName www.web1.com
ServerAlias web1.com
ErrorLog "/www/web1/logs/web1_error.log"
CustomLog "/www/web1/logs/web1_access.log" common
<Directory "/www/web1">
Options Indexes
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot "/www/web2"
ServerName www.web2.com
ErrorLog "/www/web2/logs/web2_error.log"
CustomLog "/www/web2/logs/web2_access.log" common
<Directory "/www/web2">
Options Indexes
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
[root@localhost ~]# systemctl restart httpd
(3)瀏覽器或Linux中進行訪問測試
為了方便我這裏直接在Linux中測試了
[root@localhost ~]# curl 192.168.115.150:80
www.web1.com
[root@localhost ~]# curl 192.168.115.150:8080
www.web2.com
以上就是Apache虛擬主機配置的三種不同方法,可以根據實際需要進行配置。
Apache虛擬主機配置詳解