httpd配置虚拟主机实验
配置前
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf # If your host doesn't have a registered DNS name, enter its IP address here. ServerName 0.0.0.0:80 //修改主配置文件 ······ [root@localhost ~]# vim /usr/share/doc/httpd/httpd-vhosts.conf //打开虚拟主机的示例文件 [root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf //在con.f下创建虚拟主机配置文件,并将示例文件粘贴进去,再进行进一步的修改 <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/var/www/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/var/log/httpd/dummy-host.example.com-error_log" //错误日志 CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common //访问日志 </VirtualHost>
同IP不同端口
[root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf <VirtualHost 192.168.133.152:80> //80端口 ServerName 192.168.133.152 //域名或ip地址 DocumentRoot /www/xiaopang //网站根目录 </VirtualHost> <VirtualHost 192.168.133.152:8888> //8888端口 ServerName 192.168.133.152 DocumentRoot /www/xiaopang1 </VirtualHost> LISTEN 8888 //全局定义监听8888端口 <Directory /www> //定义/www的目录标签 AllowOverride none //.htaccess中权限不生效 Require all granted //访问控制列表允许所有主机访问 </Directory> [root@localhost ~]# mkdir -p /www/{xiaopang,xiaopang1} [root@localhost ~]# echo "xiaopang" > /www/xiaopang/index.html [root@localhost ~]# echo "this is xiaopang1" > /www/xiaopang/index.html [root@localhost xiaopang]# curl 192.168.133.152 xiaopang [root@localhost xiaopang]# curl 192.168.133.152:8888 this is xioapang1
不同IP同端口
[root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf <VirtualHost 192.168.133.152:80> ServerName 192.168.133.152 DocumentRoot /www/xiaopang </VirtualHost> <VirtualHost 192.168.133.153:80> ServerName 192.168.133.153 DocumentRoot /www/zhongpang </VirtualHost> <Directory /www> AllowOverride none Require all granted </Directory> [root@localhost ~]# mkdir -p /www/zhongpang [root@localhost ~]# echo "zhongpang" > /www/zhongpang/index.html [root@localhost xiaopang]# curl 192.168.133.152 xiaopang [root@localhost xiaopang]# curl 192.168.133.153 zhongpang
同IP同端口不同域名
[root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf <VirtualHost 192.168.133.150:80> ServerName www.dapang.com DocumentRoot /www/dapang </VirtualHost> <VirtualHost 192.168.133.150:80> ServerName www.dapang.com DocumentRoot /www/dapang </VirtualHost> <Directory /www> AllowOverride none Require all granted </Directory> [root@localhost ~]# mkdir /www/dapang [root@localhost ~]# echo "dapang" > /www/dapang/index.html [root@localhost ~]# vim /etc/hosts 192.168.133.150 www.dapang.com www.dapang1.com [root@localhost ~]# systemctl restart httpd.service [root@localhost xiaopang]# curl http://www.dapang.com dapang [root@localhost xiaopang]# curl http://www.dapang1.com dapang