在一个Apache服务器上可以配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录。Apache虚拟主机配置有3中方法:基于IP配置、基于域名配置和基于端口配置。下面将演示基于域名的虚拟主机。
1、编辑httpd.conf
[[email protected] ~]# vi /usr/local/apache2.4/conf/httpd.conf Include conf/extra/httpd-vhosts.conf ##启用httpd-vhosts.conf
2、编辑httpd-vhosts.conf
[[email protected] ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> ##默认虚拟主机 DocumentRoot "/data/www/abc.com" ##网站根目录 ServerName abc.com ##主域名 ServerAlias abc.net ##域名别名 ErrorLog "logs/abc.com-error_log" ##错误日志 CustomLog "logs/abc.com-access_log" common ##访问日志 </VirtualHost> <VirtualHost *:80> DocumentRoot "/data/www/123.com" ServerName 123.com ErrorLog "logs/123.com-error_log" CustomLog "logs/123.com-access_log" common </VirtualHost>
3、创建网站目录与文件
[[email protected] ~]# mkdir /data/www [[email protected] ~]# mkdir /data/www/abc.com [[email protected] ~]# mkdir /data/www/123.com [[email protected] ~]# cat /data/www/abc.com/index.php <?php echo "abc.com"; ?> [[email protected] ~]# cat /data/www/123.com/index.php <?php echo "123.com"; ?> [[email protected] ~]# chmod 755 /data/www/123.com/index.php [[email protected] ~]# chmod 755 /data/www/abc.com/index.php
4、重新加载配置
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful
5、测试虚拟主机效果
[[email protected] ~]# curl -x192.168.137.100:80 123.com <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> <p>You don‘t have permission to access / on this server.<br /> </p> </body></html>
测试发现没有权限,由于前面给index.php文件授权过,因此定位问题在httpd.conf。
[[email protected] ~]# vi /usr/local/apache2.4/conf/httpd.conf <Directory /> AllowOverride none # Require all denied ##将该行注释掉 </Directory> [[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful ##重新加载配置 [[email protected] ~]# curl -x192.168.137.100:80 123.com 123.com [[email protected] ~]# curl -x192.168.137.100:80 abc.com abc.com [[email protected] ~]# curl -x192.168.137.100:80 ##匹配别名主机 abc.com [[email protected] ~]# curl -x192.168.137.100:80 abcd.com ##采用默认虚拟主机 abc.com
时间: 2024-10-08 19:35:08