六 27
pache2主配置文件: /etc/apache2/apache2.conf。其最后兩行為:
# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/
顯然/etc/apache2/sites-enabled下存放著有關(guān)虛擬站點(diǎn)(VirtualHost)的配置。經(jīng)查看,初始情況下,該目錄下包含一個(gè)符號連接:000-default -> ../sites-available/default
這里又引出另外一個(gè)配置目錄:/etcc/apache2/sites-available。這個(gè)目錄下放置了所有可用站點(diǎn)的真正配置文件,對于Enabled的站點(diǎn),Apache2在sites-enabled目錄建立一個(gè)到sites-available目錄下文件的符號鏈接。
/etc/apache2/sites-available下有兩個(gè)文件:default和default-ssl。000-default鏈接的文件為default,我們就以default為例,看看一個(gè)VirtualHost的配置是啥樣的:
ServerAdmin webmaster@localhost
DocumentRoot /var/www
Options FollowSymLinks
AllowOverride None
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
… …
DocumentRoot是這個(gè)站點(diǎn)的根目錄,這樣Apache2啟動(dòng)時(shí)會掃描/etc/apache2/sites-enabled中可用的website配置并加載。當(dāng)用戶訪問localhost:80時(shí),Apache2就將default站點(diǎn)根目錄/var/www下的index.html作為請求的回應(yīng)返回給瀏覽器,你就會欣賞到的就是/var/www/index.html這個(gè)文件中的內(nèi)容了。
Apache2的默認(rèn)站點(diǎn)我們不要去動(dòng)它。我們新增站點(diǎn)配置來滿足我們的要求。到這里我猜測一下你可能有兩類需求:
一是如何配置根據(jù)訪問的域名區(qū)分配置不通的站點(diǎn)?
二是在相同域名地址的情況下,如何通過訪問不同的端口獲得不同的站點(diǎn)?
我們先來看看第一種需求。第一種需求講的是我要在一個(gè)Apache2服務(wù)器上配置兩個(gè)站點(diǎn):site1.com和site2.com。好,我們可以按照下面步驟來做:
* 建立配置文件
在sites-available中建立兩個(gè)站點(diǎn)的配置文件site1_com和site2_com:
sudo cp default site1_com
sudo cp default site2_com
編輯這兩個(gè)配置文件,以site1_com為例:
ServerAdmin webmaster@localhost
ServerName site1.com
DocumentRoot /var/www/site1_com
Options FollowSymLinks
AllowOverride None
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
… …
注意上面配置中:ServerName、DocumentRoot和Directory是我們重點(diǎn)關(guān)注的配置點(diǎn)。site1的ServerName為site1.com,根目錄為/var/www/site1_com,Directory同DocumentRoot。site2_com也做同樣的改動(dòng)。
* 在sites-enabled目錄下建立符號鏈接:
sudo ln -s /etc/apache2/sites-available/site1_com /etc/apache2/sites-enabled/site1_com
sudo ln -s /etc/apache2/sites-available/site2_com /etc/apache2/sites-enabled/site2_com
* 在/var/www下建立site1_com和site2_com兩個(gè)目錄,然后修改目錄所有者:
sudo chown -R www-data site1_com site2_com/
* 在site1_com和site2_com中各自創(chuàng)建一個(gè)index.html文件,用于測試使用。
以site1_com下index.html為例,其內(nèi)容為:Welcome To Site1。
* 重啟Apache2(sudo /init.d/apache2 restart)使配置生效。
* 修改/etc/hosts文件,便于測試。
添加如下兩行:
127.0.0.1 site1.com
127.0.0.1 site2.com
* 打開瀏覽器,輸入http://site1.com,之后不出意外你就會看到”Welcome to Site1“字樣。
第二類需求是希望通過端口號來區(qū)分虛擬站點(diǎn)。這個(gè)也不難,一些配置方法與上面內(nèi)容雷同,這里就不詳說了。
比如以site2為例:我通過80端口訪問site2,可看到"Welcome to Site2”,從8080端口訪問site2,則會看到"Welcome to Site2 through 8080"。我們?nèi)绾闻渲媚兀?/p>
* 首先我們得讓apache2監(jiān)聽端口8080
修改/etc/apache2/ports.conf,增加兩行:
NameVirtualHost *:8080
Listen 8080
* 在/etc/apache2/sites-available/下增加site2_com_8080,并在sites-enabled下建立符號連接。
site2_com_8080的主要配置如下:
ServerAdmin webmaster@localhost
ServerName site2.com
DocumentRoot /var/www/site2_com_8080
Options FollowSymLinks
AllowOverride None
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
… …
在/var/www下建立site2_com_8080目錄,方法同上。
重啟Apache2,訪問http://site2.com:8080,我們將看到“Welcome to Site2 through 8080”。
新聞熱點(diǎn)
疑難解答
圖片精選