前言
本文將詳細記錄一下如何在單臺服務器上,利用apache的virtualhost(虛擬主機)來搭建多個不同的web站點,并且每個站點獨立管理自己的session,下面話不多說了,來一起看看詳細的介紹吧。
開發環境
先說下我各項開發環境參數:
操作系統: RedHat6.7(CentOS) WEB服務器:apache2.2 php5.6.30修改Apache配置
apache2.2 的配置文件路徑在 /etc/httpd/conf/httpd.conf
我們用下面的命令修改apache的配置文件:
$ vim /etc/httpd/conf/httpd.conf
添加監聽端口
找到如下的部分,
## 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:80Listen 80
默認的話,應該只會監聽80端口,這里我們在后面加上用于另外站點的端口號。例如我們A站點是默認的80端口,B站點計劃搭建在8080端口上,最終的配置文件修改成
...#Listen 12.34.56.78:80Listen 80Listen 8080
啟動并添加VirtualHost
接著在配置文件中找到下面的章節:
### 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
上面的代碼是我已經修改好的,默認的話,最后兩行NameVirtualHost應該也是被注釋掉了。 因為我們要啟用虛擬主機,所以這里就把我們之前監聽的兩個端口都設置好。
同時,將之后的配置文件修改成如下的樣子,我們先來設置默認的80端口的站點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>
默認的Apache是沒有開啟VirtualHost的,所以這些代碼都是被注釋掉了的,我們這里只需要把DocumentRoot和ServerName所在的行去掉注釋并且編輯下就好了。
DocumentRoot指的的是我們A站點的網站根目錄位置
接下來再補充上8080端口的B站點信息就好了。
新聞熱點
疑難解答