提示:以下是本篇文章正文内容,下面案例可供参考
●作用
控制对网站资源的访问
为特定的网站目录添加访问授权
●常用访问控制方式
客户机地址限制
用户授权限制
vim /usr/local/httpd/conf/extra/httpd-default.conf
① 为每个虚拟主机使用不同的域名,但是其对应的IP地址是相同的
② 域名不同–IP相同,端口相同
③ 基于域名是最为普遍的虚拟web主机类型
① 为每个虚拟主机使用不同的域名,且各自对应的IP地址也不相同
② 这种方式需要为服务器配备多个网络接口(加网卡,虚拟网卡),因此应用不是很广泛
③ IP不同,端口相同
① 这种方式并不使用域名,IP地址来区分不同的站点内容,而是使用不同的TCP端口号
② 因此用户在浏览不同的虚拟站点时需要同时制定端口号才能访问
③ IP相同,端口不同
[root@httpd ~]# cd /opt
[root@httpd opt]# rz -E
[root@httpd opt]# ls
apr-1.6.2.tar.gz apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2 rh
[root@httpd opt]# tar xf apr-1.6.2.tar.gz
[root@httpd opt]# tar xf apr-util-1.6.0.tar.gz
[root@httpd opt]# tar xf httpd-2.4.29.tar.bz2
[root@httpd opt]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@httpd opt]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
[root@httpd opt]# yum -y install \
> gcc \ //C语言的编译器
> gcc-c++ \ //C++编译器
> make \ //源代码编译器
> pcre-devel \ //perl接口开发数据包
> expat-devel \ //用于支持网站解析HTML、XML文件
> perl //perl语言编译器
[root@httpd opt]# cd httpd-2.4.29
[root@httpd httpd-2.4.29]# ./configure \
> --prefix=/usr/local/httpd \ //指定路径
> --enable-so \ //开启核心功能模块
> --enable-rewrite \ //开启重写功能,防盗链保护
> --enable-charset-lite \ //字符集设定
> --enable-cgi //开启通用网关接口,它定义了web服务器运行时外部程序的规范
make && make install
ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/
[root@httpd httpd-2.4.29]# cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
vim /etc/init.d/httpd
2 # chkconfig: 35 85 21 //35级别自动运行,第85个启动,第21个关闭
3 # description: Apache is a World Wide Web server //设置开机自启
[root@httpd httpd-2.4.29]# chkconfig --add httpd
[root@httpd httpd-2.4.29]# vim /usr/local/httpd/conf/httpd.conf
52 Listen 192.168.27.136:80
197 ServerName www.kgc.com:80
①部署DNS域名解析服务器,提供域名解析
②在/etc/hosts文件中临时配置域名IP地址的映射关系
[root@httpd httpd-2.4.29]# vim /etc/hosts
echo "192.168.27.136 www.benet.com" >> /etc/hosts
echo "192.168.27.136 www.accp.com" >> /etc/hosts
[root@httpd httpd-2.4.29]# cd /var/www/html
[root@httpd html]# mkdir -p /var/www/html/benet
[root@httpd html]# mkdir -p /var/www/html/accp
[root@httpd html]# ls
accp benet
[root@httpd html]# echo "www.benet.com
" > /var/www/html/benet/index.html
[root@httpd html]# echo "www.accp.com
" > /var/www/html/accp/index.html
[root@httpd httpd-2.4.29]# cd /opt
[root@httpd opt]# cd /usr/local/httpd
[root@httpd httpd]# cd conf
[root@httpd conf]# ls
extra httpd.conf magic mime.types original
[root@httpd conf]# cd extra
[root@httpd extra]# ls
httpd-autoindex.conf httpd-mpm.conf
httpd-dav.conf httpd-multilang-errordoc.conf
httpd-default.conf httpd-ssl.conf
httpd-info.conf httpd-userdir.conf
httpd-languages.conf httpd-vhosts.conf //虚拟主机模板文件
httpd-manual.conf proxy-html.conf
<VirtualHost 192.168.27.136:80>
DocumentRoot "/var/www/html/benet"
ServerName www.benet.com
ErrorLog "logs/benet.com-error_log"
CustomLog "logs/benet.com-access_log" common
</VirtualHost>
<VirtualHost 192.168.27.136:80>
DocumentRoot "/var/www/html/accp"
ServerName www.accp.com
ErrorLog "logs/accp.com-error_log"
CustomLog "logs/accp.com-access_log" common
</VirtualHost>
<Directory "/var/www/html"> #设置目录访问权限
Options None #不启用任何的服务器特性
AllowOverride None #不允许重写Apache默认配置
Require all granted #允许所有主机访问
</Directory>
[root@httpd conf]# vim /usr/local/httpd/conf/httpd.conf
479 # Virtual hosts
480 Include conf/extra/httpd-vhosts.conf //取消注释
[root@httpd conf]# systemctl restart httpd
[root@httpd conf]# ifconfig ens33:0 192.168.27.100 netmask 255.255.255.0
[root@httpd conf]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 192.168.27.136:80>
DocumentRoot "/var/www/html/benet"
ServerName www.benet.com
ErrorLog "logs/benet.com-error_log"
CustomLog "logs/benet.com-access_log" common
</VirtualHost>
<VirtualHost 192.168.27.100:80> //将IP地址改为虚拟网卡的IP地址
DocumentRoot "/var/www/html/accp"
ServerName www.accp.com
ErrorLog "logs/accp.com-error_log"
CustomLog "logs/accp.com-access_log" common
</VirtualHost>
<Directory "/var/www/html">
Options None
AllowOverride None
Require all granted
</Directory>
[root@httpd conf]# vim /usr/local/httpd/conf/httpd.conf
52 Listen 192.168.27.136:80
53 Listen 192.168.27.100:80 //添加监听地址
systemctl restart httpd
root@httpd conf]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost 192.168.27.136:80>
DocumentRoot "/var/www/html/benet"
ServerName www.benet.com
ErrorLog "logs/benet.com-error_log"
CustomLog "logs/benet.com-access_log" common
</VirtualHost>
<VirtualHost 192.168.27.136:8080> //将端口号由80改为8080
DocumentRoot "/var/www/html/accp"
ServerName www.accp.com
ErrorLog "logs/accp.com-error_log"
CustomLog "logs/accp.com-access_log" common
</VirtualHost>
<Directory "/var/www/html">
Options None
AllowOverride None
Require all granted
</Directory>
[root@httpd conf]# vim /usr/local/httpd/conf/httpd.conf
Listen 192.168.27.136:80
Listen 192.168.27.136:8080
[root@httpd conf]# systemctl restart httpd
[root@httpd conf]# netstat -antp | grep httpd