配置環境: linux版本:Centos6.4 httpd版本: [root@centos64Study init.d]# pwd/etc/init.d[root@centos64Study init.d]# httpd -vServer version: Apache/2.2.15 (Unix)S ...
配置環境:
linux版本:Centos6.4
httpd版本:
[root@centos64Study init.d]# pwd
/etc/init.d
[root@centos64Study init.d]# httpd -v
Server version: Apache/2.2.15 (Unix)
Server built: Oct 19 2017 16:43:38
1,安裝httpd服務
yum install httpd -y
2,關閉selinux和防火牆
臨時設置selinux為permissive( disabled )狀態
[root@centos64Study ~]# getenforce
Enforcing
[root@centos64Study ~]# setenforce 0
[root@centos64Study ~]# getenforce
Permissive
永久修改方法:
[root@centos64Study ~]# vim /etc/sysconfig/selinux
SELINUX=permissive
3,關閉防火牆
service iptables stop
第一種,基於多ip訪問的配置:
1,先把httpd.conf備份一下,以防出錯,可以恢復
httpd.conf的文件路徑( /etc/httpd/conf )
[root@centos64Study conf]# ls
httpd.conf httpd.conf.bak magic
2,如果需要啟用虛擬主機配置,在文件httpd.conf中,先把中心主機的配置註釋
#DocumentRoot "/var/www/html"
3,在httpd.conf中,會預設包conf.d目錄中的所有 以.conf結尾的配置文件
Include conf.d/*.conf
所以,把虛擬主機的配置文件獨立出來放在conf.d目錄下
[root@centos64Study httpd]# ls
conf conf.d logs modules run
[root@centos64Study httpd]# pwd
/etc/httpd[root@centos64Study httpd]# cd conf.d
[root@centos64Study conf.d]# pwd
/etc/httpd/conf.d
[root@centos64Study conf.d]# ls
README VirtualHost.conf welcome.conf
VirtualHost.conf 配置文件內容:
<VirtualHost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
</VirtualHost>
<VirtualHost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
</VirtualHost>
我的主機ip是192.168.1.8,添加另一個ip:
ip addr add 192.168.1.2/24 dev eth0
在對應的目錄建立文件:
[root@centos64Study www]# tree
.
├── 7mxt.com
│ └── index.html
└── 7mxt.net
└── index.html
重啟服務: service httpd restart,在瀏覽器中分別用這兩個ip訪問,就能看到對應的頁面
第二種,基於多埠訪問的配置:
1,在VirtualHost.conf中增加一項配置:
<VirtualHost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
</VirtualHost>
<VirtualHost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
</VirtualHost>
<VirtualHost 192.168.1.2:8080>
ServerName www.abc.net
DocumentRoot "/www/abc.net"
</VirtualHost>
2,在httpd.conf監聽8080埠
Listen 80
Listen 8080
3,/www建立對應的目錄和文件
重啟服務: service httpd restart,在瀏覽器中用http://192.168.1.2:8080/ 就能訪問到abc.net目錄下麵的文件index.html內容
[root@centos64Study www]# tree
.
├── 7mxt.com
│ └── index.html
├── 7mxt.net
│ └── index.html
└── abc.net
└── index.html
第三種:基於功能變數名稱的主機配置
1,NameVirtualHost:指定192.168.1.8:80這個ip地址使用功能變數名稱解釋
NameVirtualHost 192.168.1.8:80
<VirtualHost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
</VirtualHost><VirtualHost 192.168.1.8:80>
ServerName www.7mxt.org
DocumentRoot "/www/7mxt.org"
</VirtualHost><VirtualHost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
</VirtualHost><VirtualHost 192.168.1.2:8080>
ServerName www.abc.net
DocumentRoot "/www/abc.net"
</VirtualHost>
2,在對應的目錄下建立文件
[root@centos64Study www]# tree
.
├── 7mxt.com
│ └── index.html
├── 7mxt.net
│ └── index.html
├── 7mxt.org
│ └── index.html
└── abc.net
└── index.html
3,在windows host文件中增加主機映射配置
【C:\Windows\System32\drivers\etc】
192.168.1.8 www.7mxt.com
192.168.1.8 www.7mxt.org
4,重啟服務: service httpd restart,分別用功能變數名稱www.7mxt.com, www.7mxt.org就能訪問到對應的文件內容
使用ip: 192.168.1.8返回的是第一個虛擬主機的配置,即:www.7mxt.com對應目錄下麵的內容
----------------------------------------------------------------------------------------------------------------------------------------------------
更詳細的配置信息:
NameVirtualHost 192.168.1.8:80
<VirtualHost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
CustomLog /var/log/httpd/7mxt.com/access_log combined
<Directory "/www/7mxt.com">
AllowOverride AuthConfig
AuthType Basic
AuthName "Restricted Site..."
AuthUserFile "/etc/httpd/conf/htpasswd"
AuthGroupFile "/etc/httpd/conf/htgroup"
#Require user tom
Require group myusers
#Require valid-user
</Directory>
</VirtualHost><VirtualHost 192.168.1.8:80>
ServerName www.7mxt.org
DocumentRoot "/www/7mxt.org"
CustomLog /var/log/httpd/7mxt.org/access_log combined
</VirtualHost><VirtualHost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
<VirtualHost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
CustomLog /var/log/httpd/7mxt.net/access_log combined
<Directory "/www/7mxt.net">
Options none
AllowOverride none
Order deny,allow
Deny from 192.168.1.100
</Directory>
</VirtualHost><VirtualHost 192.168.1.2:8080>
ServerName www.abc.net
DocumentRoot "/www/abc.net"
</VirtualHost>
1,www.7mxt.com這個功能變數名稱下麵的Directory配置為 需要用戶驗證,支持3種情況的配置:
。只允許某個用戶訪問
。只允許文件中的用戶
。只允許組文件中定義的用戶
AllowOverride AuthConfig: 需要授權
AuthType Basic:基本授權
AuthName "Restricted Site..." :授權說明
AuthUserFile "/etc/httpd/conf/htpasswd" : 需要授權的用戶 保存在這個文件中,這個文件中的內容用
htpasswd這個命令創建,第一次帶參數c表示創建,第二次不需要帶c
htpasswd -c -m /etc/httpd/conf/htpasswd ghost
htpasswd -m /etc/httpd/conf/htpasswd ghostwu
AuthGroupFile "/etc/httpd/conf/htgroup": 需要授權的用戶保存在這個文件中
#Require user ghost:只允許ghost這個用戶登錄
Require group myusers: 只允許myusers這個組中的用戶登錄
#Require valid-user:只允許htpasswd這個文件中定義的用戶登錄
[root@centos64Study conf]# ls
htgroup htpasswd httpd.conf httpd.conf.bak magic
[root@centos64Study conf]# cat htgroup
myusers: ghost ghostwu
[root@centos64Study conf]# cat htpasswd
ghost:$apr1$IeKI2YLO$RX8EjYBoHbMzCkHocFK0l/
ghostwu:$apr1$LoA/O0i0$0/EJu3xTXo2yCMimMUAcQ.
2,CustomLog /var/log/httpd/7mxt.org/access_log combined
自定義訪問日誌的文件路徑,【註意:需要在/var/log/httpd下麵存在7mxt.org這個目錄】
3,
<Directory "/www/7mxt.net">
Options none
AllowOverride none
Order deny,allow
Deny from 192.168.1.100
</Directory>
這個配置針對的是7mxt.net,禁止192.168.1.100訪問7mxt.net,其他的ip都允許