萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> CMS 教學

Discuz! X Apache 虛擬主機多域名綁定配置教程

前置條件

Apache 已安裝,需啟用虛擬主機配置文件。

步驟 1:啟用 vhosts 配置

編輯 Apache/conf/httpd.conf,搜尋:

Include conf/extra/httpd-vhosts.conf

確保行首無 # 註釋。同時取消 NameVirtualHost *:80 註釋。

步驟 2:編輯虛擬主機規則

編輯 Apache/conf/extra/httpd-vhosts.conf,在文件末尾添加:

<VirtualHost 127.0.0.1:80>
  DocumentRoot "D:/ComsenzEXP/wwwroot"
  ServerName www.moke8.com
  ServerAlias *
  DirectoryIndex index.htm index.php index.html
  ErrorLog "logs/www_moke8_error.log"
  CustomLog "logs/www_moke8_access.log" common
</VirtualHost>

<VirtualHost 127.0.0.2:80>
  DocumentRoot "D:/ComsenzEXP/www"
  ServerName moke8.com
  ServerAlias *
  DirectoryIndex index.htm index.php index.html
  ErrorLog "logs/moke8_error.log"
  CustomLog "logs/moke8_access.log" common
</VirtualHost>

關鍵點:

  • DocumentRoot:網站程式物理路徑(使用正斜線或雙反斜線)
  • ServerName:主域名
  • ServerAlias *:接受所有子域名/別名
  • 不同虛擬主機建議使用不同 IP(如 127.0.0.1 / 127.0.0.2)或同 IP 不同端口,避免衝突
  • 日誌文件分離便於排查

步驟 3:本地 Hosts 映射(測試環境)

C:\Windows\System32\drivers\etc\hosts 添加:

127.0.0.1  www.moke8.com
127.0.0.2  moke8.com

步驟 4:重啟 Apache 驗證

分別訪問 https://www.moke8.com、https://moke8.com,應正確指向對應目錄並加載 Discuz! 前台。

生產環境補充

  • 正式域名需在 DNS 服務商解析到服務器公網 IP。
  • 建議啟用 HTTPS(Let's Encrypt 或付費證書),在 443 端口複製虛擬主機配置並加入 SSL 指令。
  • 生產環境請移除 ServerAlias *,明確列出允許的域名,防止惡意解析綁定。
copyright © 萬盛學電腦網 all rights reserved