一。基于域名的虛拟主機
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
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>
ServerAdmin [email protected]
DocumentRoot /www/virtual 另一個虛拟主機的釋出目錄
ServerName www.test2.com 另一個虛拟主機的域名
[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
測試:在浏覽器重輸入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>
這樣這個塊就變成了
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>
[root@localhost ~]# htpasswd -cm /etc/httpd/conf/passwd 123
給使用者設定密碼
[root@localhost ~]# /etc/init.d/httpd restart
輸入使用者,密碼 通路,若沒有權限,修改安全上下文
三。基于端口号的虛拟主機
因為在/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 192.168.122.11:82 >
ServerName 81.qq.com
ServerAdmin [email protected]
DocumentRoot /var/www/html
[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
Starting httpd: (13)Permission denied: make_sock: could not bind to address [::]:81
[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 82
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]
ServerName 11.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
<VirtualHost 192.168.122.111:80>
DocumentRoot /var/www/html
ServerName 111.example.com
重新開機HTTP OK
cp ifconfig-eth0 ifconfig-eth0:1
vi ifconfig-eth0:1
DEVICE=eth0:1
IPADDR=192.168.122.111
重新開機網卡即可 這是永久添加網卡
本文轉自 369藍寶 51CTO部落格,原文連結:http://blog.51cto.com/3739387/1154476,如需轉載請自行聯系原作者