1. 程式人生 > >LAMP-Apache虛擬主機

LAMP-Apache虛擬主機

虛擬主機

在一個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


本文出自 “A man & A computer” 博客,請務必保留此出處http://juispan.blog.51cto.com/943137/1951872

LAMP-Apache虛擬主機