一。基於域名的虛擬主機
1,
- [root@localhost ~]# uname -r
- 2.6.32-71.el6.i686
- [root@localhost ~]# cat /etc/redhat-release
- Red Hat Enterprise Linux Server release 6.0 (Santiago)
2,安裝httpd
- yum install httpd -y
3,編輯配置文件
- vim /etc/httpd/conf/httpd.conf
- NameVirtualHost *:80將這一行的#去掉
- <VirtualHost *:80> 複製這一行下面的7行 粘貼至最後(Y7Y P)
- 修改成爲
- <VirtualHost *:80>
- ServerAdmin [email protected] 管理員的郵箱
- DocumentRoot /var/www/html 發佈目錄
- ServerName www.test1.com 一個虛擬主機的域名
- (其餘均爲日誌,可以不要)
- </VirtualHost>
- <VirtualHost *:80>
- ServerAdmin [email protected]
- DocumentRoot /www/virtual 另一個虛擬主機的發佈目錄
- ServerName www.test2.com 另一個虛擬主機的域名
- </VirtualHost>
- [root@localhost virtual]# mkdir /www/virtual -p
- [root@localhost virtual]# vi /www/virtual/index.html
- www.test2.com
- [root@localhost virtual]# vi /var/www/html/index.html
- www.test1.com
- [root@localhost virtual]# ll -Z /var/www/html/ 查看這個目錄的安全上下文
- -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 \
- -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
- [root@localhost virtual]# chcon index.html -R -t httpd_sys_content_t /www 修改安全上下文
- [root@localhost virtual]# /etc/init.d/httpd restart 重啓服務
WINDOW下
C:\Windows\System32\drivers\etc\hosts用記事本打開,添加
192.168.1.12 www.test1.com
192.168.1.12 www.test2.com
LINUX下 vi /etc/hosts
192.168.1.12 www.test1.com
192.168.1.12 www.test2.com
測試:在瀏覽器重輸入www.test1.com會顯示虛擬機1的發佈目錄裏的東西
在瀏覽器重輸入www.test2.com會顯示虛擬機2的發佈目錄裏的東西
二,用戶認證
在/www/virtual目錄下建secret目錄,在secret目錄下建index.html
- [root@localhost ~]# vi /www/virtual/index.html
- This is a secret!
在發佈目錄爲、www/virtal的塊內的後面加上
- <Directory /www/virtual/secret>
- authuserfile /etc/httpd/conf/passwd
- authname "secret"
- authtype basic
- require valid-user
- require user 123
- </Directory>
這樣這個塊就變成了
- <VirtualHost *:80>
- ServerAdmin [email protected]
- DocumentRoot /www/virtual
- ServerName www.test2.com
- <Directory /www/virtual/secret>
- authuserfile /etc/httpd/conf/passwd
- authname "secret"
- authtype basic
- require valid-user
- require user 123 用戶名
- </Directory>
- </VirtualHost>
- [root@localhost ~]# htpasswd -cm /etc/httpd/conf/passwd 123
- 給用戶設置密碼
- [root@localhost ~]# /etc/init.d/httpd restart
在瀏覽器輸入http://www.test2.com/secret/
輸入用戶,密碼 訪問,若沒有權限,修改安全上下文
三。基於端口號的虛擬主機
因爲在/etc/httpd/conf/httpd.conf 裏包含 Include conf.d/*.conf,故在 /etc/httpd/conf.d目錄下
vim port.conf
- [root@vm1 www]# vim /etc/httpd/conf.d/port.conf
- Listen 81 開啓端口81
- Listen 82 開啓82
- <VirtualHost 192.168.122.11:81>
- ServerName 81.qq.com 可以不要
- ServerAdmin [email protected] 省略日誌文件
- DocumentRoot /www/virtual
- </VirtualHost>
- <VirtualHost 192.168.122.11:82 >
- ServerName 81.qq.com
- ServerAdmin [email protected]
- DocumentRoot /var/www/html
- </VirtualHost>
- [root@vm1 ~]# /etc/init.d/httpd restart
- Stopping httpd: [FAILED]
- Starting httpd: Warning: DocumentRoot [/www/virtual] does not exist
- (13)Permission denied: make_sock: could not bind to address [::]:81
- (13)Permission denied: make_sock: could not bind to address 0.0.0.0:81
- no listening sockets available, shutting down
- Unable to open logs
- [FAILED]
- [root@vm1 www]# ll -Z
- drwxr-xr-x. root root system_u:object_r:httpd_sys_script_exec_t:s0 cgi-bin
- drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 error
- drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 html
- drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 icons
- [root@vm1 www]# chcon -R -t httpd_sys_content_t /www
- [root@vm1 www]# /etc/init.d/httpd restart
- Stopping httpd: [FAILED]
- Starting httpd: (13)Permission denied: make_sock: could not bind to address [::]:81
- (13)Permission denied: make_sock: could not bind to address 0.0.0.0:81
- no listening sockets available, shutting down
- Unable to open logs
- [FAILED]
- [root@vm1 www]# semanage port -a -t http_port_t -p tcp 81
- -bash: semanage: command not found 找不到這個命令
- [root@vm1 www]# yum install policycoreutils-python -y 安裝命令
- [root@vm1 www]# semanage port -l|grep http
- http_cache_port_t tcp 3128, 8080, 8118, 10001-10010
- http_cache_port_t udp 3130
- http_port_t tcp 80, 443, 488, 8008, 8009, 8443
- pegasus_http_port_t tcp 5988
- pegasus_https_port_t tcp 5989
- [root@vm1 www]# semanage port -a -t http_port_t -p tcp 81
- [root@vm1 www]# semanage port -a -t http_port_t -p tcp 82
- [root@vm1 www]# /etc/init.d/httpd restart
- Stopping httpd: [FAILED]
- Starting httpd: [ OK ] 好了
- 在兩個主機的目錄裏寫上東西,瀏覽器訪問
四。基於IP地址的虛擬主機
- [root@vm1 www]# ifconfig eth0:1 192.168.122.111 netmask 255.255.255.0
- [root@vm1 www]# ifconfig eth0:1 up 臨時添加子接口IP
- [root@vm1 www]# vim /etc/httpd/conf/httpd.conf
- <VirtualHost 192.168.122.11:80>
- ServerAdmin [email protected]
- DocumentRoot /www/virtual
- ServerName 11.example.com
- ErrorLog logs/dummy-host.example.com-error_log
- CustomLog logs/dummy-host.example.com-access_log common
- </VirtualHost>
- <VirtualHost 192.168.122.111:80>
- ServerAdmin [email protected]
- DocumentRoot /var/www/html
- ServerName 111.example.com
- ErrorLog logs/dummy-host.example.com-error_log
- CustomLog logs/dummy-host.example.com-access_log common
- </VirtualHost>
- 重啓HTTP OK
cp ifconfig-eth0 ifconfig-eth0:1
vi ifconfig-eth0:1
DEVICE=eth0:1
IPADDR=192.168.122.111
重啓網卡即可 這是永久添加網卡