Apache 虚拟主机 VirtualHost 配置
虛擬主機 (Virtual Host) 是在同一臺機器搭建屬于不同域名或者基于不同 IP 的多個網站服務的技術. 可以為運行在同一物理機器上的各個網站指配不同的 IP 和端口, 也可讓多個網站擁有不同的域名.
Apache 是世界上使用最廣的 Web 服務器, 從 1.1 版開始支持虛擬主機. 本文將講解在不同服務器 (Redhat Enterprise Linux, Ubuntu Linux, Windows) 上使用 Apache 搭建虛擬主機來搭建多個網站.
主旨
本文旨在讓讀者知道如何在同一臺機器上搭建多個網站, 并附帶一些使用技巧. 以操作為主, 不會過多談及原理.
目標
本文是寫給擁有一定的服務器配置和管理技能, 工作中需要同時維護多個網站的網站主, 網站開發者和網絡管理員. 如果你是互聯網公司的配管工程師, 對計算機服務器原理和操作十分熟悉, 請忽視本文, 你不會在上面找到太多有價值的東西.
以下是各操作系統的配置方法.
- Redhat Enterprise Linux
- Ubuntu Linux
- Windows
- Mac OS
Redhat Enterprise Linux
Redhat Enterprise Linux (包括 CentOS Linux), 是使用最廣的 Linux 服務器, 大量的網站應用都部署在其上.
1. 打開文件 /etc/httpd/conf/httpd.conf, 搜索 VirtualHost example, 找到代碼如下:
| # # 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 /www/docs/dummy-host.example.com # ServerName dummy-host.example.com # ErrorLog logs/dummy-host.example.com-error_log # CustomLog logs/dummy-host.example.com-access_log common #</VirtualHost> |
2. 仿照例子, 添加一段代碼來指定某一域名的網站.
| # # DocumentRoot 是網站文件存放的根目錄 # ServerName 是網站域名, 需要跟 DNS 指向的域名一致 # <VirtualHost *:80>ServerAdmin webmaster@dummy-host.example.comDocumentRoot /var/www/httpdocs/demo_neoease_comServerName demo.neoease.comErrorLog logs/demo.neoease.com-error.logCustomLog logs/demo.neoease.com-access.log common </VirtualHost> |
3. 重啟 httpd 服務, 執行以下語句.
| service httpd restart |
Ubuntu Linux
Ubuntu 在 Linux 各發行版中, 個人用戶數量最多的. 很多人在本機和虛擬機中使用. 但 Ubuntu 和 Redhat 的 VirtualHost 設置方法不相同.
1. 打開目錄 /etc/apache2/sites-available/, 發現 default 和 default-ssl 兩個文件, 其中 default 是 http 虛擬主機服務的配置文件, default-ssl 是配置 https 服務使用的. 可以復制一份 default 文件. 并修改配置文件名, 文件名必須與域名一致 (如: demo.neoease.com)
2. 打開新建的配置文件, 修改 DocumentRoot, ServerName 和對應的配置目錄. 例子如下:
| # # DocumentRoot 是網站文件存放的根目錄 # ServerName 是網站域名, 需要跟 DNS 指向的域名一致 # <VirtualHost *:80>ServerAdmin webmaster@dummy-host.example.comDocumentRoot /var/www/httpdocs/demo_neoease_comServerName demo.neoease.comErrorLog ${APACHE_LOG_DIR}/demo.neoease.com-error.logCustomLog ${APACHE_LOG_DIR}/demo.neoease.com-access.log combined </VirtualHost> |
3. 通過 a2ensite 激活虛擬主機配置
| sudo a2ensite demo.neoease.com |
4. 打開目錄 /etc/apache2/sites-enabled/, 你會發現所有激活的虛擬主機, 可以通過 a2dissite 進行注銷
| sudo a2dissite demo.neoease.com |
5. 重啟 Apache 服務, 激活虛擬主機
| sudo /etc/init.d/apache2 restart |
Windows
Windows 是市場占有率最高的 PC 操作系統, 也是很多人的開發環境. 其 VirtualHost 配置方法與 Linux 上有些差異, 以下方式適合原生 Apache, XAMPP 和 WAMP 套件.
1. 打開目錄 {Apache2 安裝目錄}\conf\extra\, 找到 httpd-vhosts.conf 文件.
2. 仿照例子, 添加一段代碼來指定某一域名的網站.
| # # DocumentRoot 是網站文件存放的根目錄 # ServerName 是網站域名, 需要跟 DNS 指向的域名一致 # <VirtualHost *:80>ServerAdmin webmaster@dummy-host.example.comDocumentRoot "D:/workspace/php/demo_neoease_com"ServerName demo.neoease.comErrorLog "logs/demo.neoease.com-error.log"CustomLog "logs/demo.neoease.com-access.log" common </VirtualHost> |
3. 打開 httpd.conf 文件, 添加如下語句.
| # Virtual hosts Include conf/extra/httpd-vhosts.conf |
4. 重啟 Apache 服務.
Mac OS
近年蘋果的雄起, 讓 Mac 日催普及, 也成為很多開發人員的選擇. 因為與 Linux 同源, 配置方法也相似.
1. 打開文件 /private/etc/apache2/extra/httpd-vhosts.conf.
2. 仿照例子, 添加一段代碼來指定某一域名的網站.
| # # DocumentRoot 是網站文件存放的根目錄 # ServerName 是網站域名, 需要跟 DNS 指向的域名一致 # <VirtualHost *:80>ServerAdmin webmaster@dummy-host.example.comDocumentRoot "/usr/docs/httpdocs/demo_neoease_com"ServerName demo.neoease.comErrorLog "/private/var/log/apache2/demo.neoease.com-error_log"CustomLog "/private/var/log/apache2/demo.neoease.com-access_log" common </VirtualHost> |
3. 打開文件 /private/etc/apache2/httpd.conf, 搜索 Virtual hosts, 找到代碼如下:
| # Virtual hosts #Include /private/etc/apache2/extra/httpd-vhosts.conf |
去掉前面的注釋符號 #, 保存文件.
4. 重啟 apache 服務, 執行以下語句.
| sudo apachectl restart |
?Nginx 是一個輕量級高性能的 Web 服務器, 并發處理能力強, 對資源消耗小, 無論是靜態服務器還是小網站, Nginx 表現更加出色, 作為 Apache 的補充和替代使用率越來越高.
我在《Apache 虛擬主機 VirtualHost 配置》介紹了在不同操作系統上使用 Apahce 虛擬主機的方法, 還有那么些朋友想知道 Nginx 虛擬主機配置方法, 本文作為補充也介紹如何 Nginx 上添加虛擬主機.
絕大多數的 Nginx 運行在 Linux 機器上, 雖然有 Windows 移植版, 但我也沒搭建過. 所以本文將以 Linux 為例講解, 而 Mac OS 或其他 Unix like 機器上的操作應該是一樣的.
增加 Nginx 虛擬主機
這里假設大家的 Nginx 服務器已經安裝好, 不懂的請閱讀各 Linux 發行版的官方文檔或者 LNMP 的安裝說明. 配置 Virtual host 步驟如下:
1. 進入 /usr/local/nginx/conf/vhost 目錄, 創建虛擬主機配置文件 demo.neoease.com.conf ({域名}.conf).
2. 打開配置文件, 添加服務如下:
| server {listen 80;server_name demo.neoease.com;index index.html index.htm index.php;root /var/www/demo_neoease_com;log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request''$status $body_bytes_sent $http_referer ''$http_user_agent $http_x_forwarded_for';access_log /var/log/demo.neoease.com.log demo.neoease.com; } |
3. 打開 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在 http 范圍引入虛擬主機配置文件如下:
| include vhost/*.conf; |
4. 重啟 Nginx 服務, 執行以下語句.
| service nginx restart |
讓 Nginx 虛擬主機支持 PHP
在前面第 2 步的虛擬主機服務對應的目錄加入對 PHP 的支持, 這里使用的是 FastCGI, 修改如下.
| server {listen 80;server_name demo.neoease.com;index index.html index.htm index.php;root /var/www/demo_neoease_com;location ~ .*\.(php|php5)?$ {fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_index index.php;include fcgi.conf;}log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request''$status $body_bytes_sent $http_referer ''$http_user_agent $http_x_forwarded_for';access_log /var/log/demo.neoease.com.log demo.neoease.com; } |
圖片防盜鏈
圖片作為重要的耗流量大的靜態資源, 可能網站主并不希望其他網站直接引用, Nginx 可以通過 referer 來防止外站盜鏈圖片.
| server {listen 80;server_name demo.neoease.com;index index.html index.htm index.php;root /var/www/demo_neoease_com;# 這里為圖片添加為期 1 年的過期時間, 并且禁止 Google, 百度和本站之外的網站引用圖片location ~ .*\.(ico|jpg|jpeg|png|gif)$ {expires 1y;valid_referers none blocked demo.neoease.com *.google.com *.baidu.com;if ($invalid_referer) {return 404;}}log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request''$status $body_bytes_sent $http_referer ''$http_user_agent $http_x_forwarded_for';access_log /var/log/demo.neoease.com.log demo.neoease.com; } |
WordPress 偽靜態配置
如果將 WordPress 的鏈接結構設定為 /%postname%/, /%postname%.html 等格式時, 需要 rewrite URL, WordPress 提供 Apache 的 .htaccess 修改建議, 但沒告知 Nginx 該如何修改. 我們可以將 WordPress 的虛擬主機配置修改如下:
| server {listen 80;server_name demo.neoease.com;index index.html index.htm index.php;root /var/www/demo_neoease_com;location / {if (-f $request_filename/index.html){rewrite (.*) $1/index.html break;}if (-f $request_filename/index.php){rewrite (.*) $1/index.php;}if (!-f $request_filename){rewrite (.*) /index.php;}}rewrite /wp-admin$ $scheme://$host$uri/ permanent;location ~ .*\.(php|php5)?$ {fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_index index.php;include fcgi.conf;}log_format demo.neoease.com '$remote_addr - $remote_user [$time_local] $request''$status $body_bytes_sent $http_referer ''$http_user_agent $http_x_forwarded_for';access_log /var/log/demo.neoease.com.log demo.neoease.com; } |
LNMP 套件在提供了 WordPress 為靜態配置文件 /usr/local/nginx/conf/wordpress.conf, 在虛擬主機配置的 server 范圍引用如下即可.
| include wordpress.conf; |
如果你使用 LNMP 套件, 進入 WordPress 后臺發現會出現 404 頁面, wp-admin 后面缺少了斜桿 /, 請在 wordpress.conf 最后添加以下語句:
| rewrite /wp-admin$ $scheme://$host$uri/ permanent; |
后話
一直以來, 我主要在用 Apache, 自從去年從 MT 搬家到 Linode VPS 之后, 發現服務器壓力很大, 每隔幾天就要宕機一次, 在胡戈戈的協助下轉成了 Nginx, 大半年了一直很穩定.
相對 Apache, Nignx 有更加強大的并發能力, 而因為他對進程管理耗用資源也比較少. 而 Apache 比 Nginx 有更多更成熟的可用模塊, bug 也比較少. 賣主機的 IDC 選擇 Nignx, 因為高并發允許他們創建更多虛擬主機空間更來錢; 淘寶也因此改造 Nignx (Tengine) 作為 CDN 服務器, 可承受更大壓力.?
總結
最近我在不同的幾臺服務器上搭建了一些網站服務, 這篇文章也算是我的個人筆記, 望日后可自用, 也希望對讀者有用. 文中介紹了幾個主流開發和部署環境上配置虛擬主機的方法, 其他 OS 大同小異.?
原文鏈接:
http://www.neoease.com/apache-virtual-host/
http://www.neoease.com/nginx-virtual-host/
總結
以上是生活随笔為你收集整理的Apache 虚拟主机 VirtualHost 配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu 12.10下配置Web服务
- 下一篇: Nginx 0.8.x + PHP 5.