這篇文章給大家分享的是有關(guān)單臺(tái)服務(wù)器中怎么利用Apache的VirtualHost搭建多個(gè)Web站點(diǎn)的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)公司為客戶提供專業(yè)的成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項(xiàng)目涵蓋了網(wǎng)頁設(shè)計(jì)、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、手機(jī)網(wǎng)站開發(fā)等網(wǎng)站方面業(yè)務(wù)。
開發(fā)環(huán)境
先說下我各項(xiàng)開發(fā)環(huán)境參數(shù):
操作系統(tǒng): RedHat6.7(CentOS)
WEB服務(wù)器:apache2.2
php5.6.30
修改Apache配置
apache2.2 的配置文件路徑在 /etc/httpd/conf/httpd.conf
我們用下面的命令修改apache的配置文件:
$ vim /etc/httpd/conf/httpd.conf
添加監(jiān)聽端口
找到如下的部分,
# # Listen: Allows you to bind Apache to specific IP addresses and/or # ports, in addition to the default. See also the <VirtualHost> # directive. # # Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses (0.0.0.0) # #Listen 12.34.56.78:80 Listen 80
默認(rèn)的話,應(yīng)該只會(huì)監(jiān)聽80端口,這里我們在后面加上用于另外站點(diǎn)的端口號(hào)。例如我們A站點(diǎn)是默認(rèn)的80端口,B站點(diǎn)計(jì)劃搭建在8080端口上,最終的配置文件修改成
... #Listen 12.34.56.78:80 Listen 80 Listen 8080
啟動(dòng)并添加VirtualHost
接著在配置文件中找到下面的章節(jié):
### Section 3: Virtual Hosts # # VirtualHost: If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd.apache.org/docs/2.2/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # NameVirtualHost *:80 NameVirtualHost *:8080
上面的代碼是我已經(jīng)修改好的,默認(rèn)的話,最后兩行NameVirtualHost應(yīng)該也是被注釋掉了。 因?yàn)槲覀円獑⒂锰摂M主機(jī),所以這里就把我們之前監(jiān)聽的兩個(gè)端口都設(shè)置好。
同時(shí),將之后的配置文件修改成如下的樣子,我們先來設(shè)置默認(rèn)的80端口的站點(diǎn)A
# # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for requests without a known # server name. # <VirtualHost *:80> # ServerAdmin webmaster@dummy-host.example.com DocumentRoot /var/www/webA ServerName webA # ErrorLog logs/dummy-host.example.com-error_log # CustomLog logs/dummy-host.example.com-access_log common </VirtualHost>
默認(rèn)的Apache是沒有開啟VirtualHost的,所以這些代碼都是被注釋掉了的,我們這里只需要把DocumentRoot和ServerName所在的行去掉注釋并且編輯下就好了。
DocumentRoot指的的是我們A站點(diǎn)的網(wǎng)站根目錄位置
接下來再補(bǔ)充上8080端口的B站點(diǎn)信息就好了。
<VirtualHost *:8080> DocumentRoot /var/www/webB ServerName webB </VirtualHost>
到這里,重啟一下Apache服務(wù)(service httpd restart),就可以訪問兩個(gè)不同的站點(diǎn)了。
獨(dú)立Session
如果我們的A,B兩個(gè)站點(diǎn)的登錄邏輯是用的一套代碼,那我們使用后會(huì)發(fā)現(xiàn),A站點(diǎn)和B站點(diǎn)的Session是共享的,也就是說,如果用戶在A站點(diǎn)登錄了之后,B站點(diǎn)是無需登錄,自動(dòng)也處于登錄狀態(tài); 用戶在A站點(diǎn)退出后,也會(huì)自動(dòng)從B站點(diǎn)退出。
這顯然不是我們想要的結(jié)果,原因就是A,B兩個(gè)站點(diǎn)公用了一套Session體系,所以才會(huì)造成這樣的問題。
解決的辦法就是我們指定站點(diǎn)中session的存放位置。
同樣是修改配置文件中指定虛擬主機(jī),我們以站點(diǎn)B做為示例,修改配置文件如下:
<VirtualHost *:8080> DocumentRoot /var/www/webB ServerName webB <Directory "/var/www/webB"> AllowOverride All php_value session.save_path "/var/lib/php/session_B" </Directory> </VirtualHost>
php_value中 session.save_path 其實(shí)就是php.ini文件中的session.save_path字段,這里我們其他值都使用默認(rèn)的php.ini配置文件,唯獨(dú)指定了session的文件存放路徑,默認(rèn)的php session文件是存儲(chǔ)在 /var/lib/php/session/文件夾中。
再試著去訪問以下站點(diǎn)B,并且進(jìn)行一些session的存取操作,回頭到/var/lib/php/session_B文件夾中,就會(huì)發(fā)現(xiàn)新的session文件了。
感謝各位的閱讀!關(guān)于“單臺(tái)服務(wù)器中怎么利用Apache的VirtualHost搭建多個(gè)Web站點(diǎn)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)頁標(biāo)題:單臺(tái)服務(wù)器中怎么利用Apache的VirtualHost搭建多個(gè)Web站點(diǎn)
本文路徑:http://chinadenli.net/article26/ihhojg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、做網(wǎng)站、網(wǎng)站建設(shè)、小程序開發(fā)、建站公司、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)