80埠(通過多域名的方式)配置多站點的方法
阿新 • • 發佈:2019-01-22
在部署一些專案時,為了滿足某些第三方服務的要求(微信)或者其他因素,需要使用80埠;為了節約伺服器的空間,提高使用率,可能會在同一臺伺服器,部署多個專案。
SO,問題來了:如果部署同一臺伺服器的多個專案都要求使用80埠該怎麼辦?
使用ip訪問的話,肯定不行了,因為一臺伺服器僅有一個對應的IP,但是域名不同,多個域名可以都解析為一個ip。SO, 解決問題的思路來了,就是通過域名進行多站點部署。
下面介紹一種部署的方法,找到Apache/conf/httpd.conf檔案,開啟Include conf/vhosts.conf,然後通過vhosts.conf檔案進行多站點的部署。
<VirtualHost _default_:80>
DocumentRoot "F:\website_one"
ServerName example1.com.cn
ServerAlias example1.com.cn
<Directory "F:\website_one">
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "F:\website_two"
ServerName example2.com.cn
ServerAlias example2.com.cn
<Directory "F:\website_two">
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "E:\website_three"
ServerName example3.com.cn
ServerAlias example3.com.cn
<Directory "E:\website_three">
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
通過如上方式即可到達多站點(example1.com.cn, example2.com.cn , example3.com.cn)同時使用80埠的目的。[有個前提是:這些域名必須已經成功解析到該臺伺服器的ip]
2、另外一個問題:
如果通過多域名配置多站點,總是進入第一個站點,那麼需要在配置前新增一個設定:
NameVirtualHost *:80
如下中的兩行(為了記錄日誌):
ErrorLog "logs/xiangmu4-error.log"
CustomLog "logs/xiangmu4-access.log" common
完整配置參考:
<VirtualHost *:80>
DocumentRoot "E:/xiangmu4"
ServerName example4.com.cn
DirectoryIndex index.html index.php
<Directory "E:/xiangmu4">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog "logs/xiangmu4-error.log"
CustomLog "logs/xiangmu4-access.log" common
</VirtualHost>