Ubuntu 14.04配置虛擬主機

虛擬主機經常使用於在一個單獨的IP地址上提供多個域名的網站服務。若是有人想在單個VPS的單個IP地址運行多個網站,這是很是有用的。在這個教程中,讓我告訴你如何設置在Ubuntu 14.04 LTS的Apache網頁服務器設置虛擬主機。請注意,這個教程只針對Ubuntu14.04的64位版本。html

我不保證它也能夠工做在其它更低的Ubuntu版本或者Ubuntu衍生版本(雖然可能過程是相似的)。web

方案apache

在這個教程中,我會使用Ubuntu 14.04 64位 LTS,並搭建2個測試網站分別命名爲「unixmen1.local」 和 「unixmen2.local」.個人測試機分別爲192.168.1.250/24和server.unixmen.local。你能夠根據你的須要更改虛擬域名。服務器

安裝Apache網站服務器網絡

安裝apache服務器以前,咱們來更新一下咱們的Ubuntu服務器:app

  • sudo apt-get update

而後,用下面命令來安裝apache網絡服務器:less

  • sudo apt-get install apache2

安裝apache服務器以後,讓咱們經過這個URL http://你的服務器的IP地址/ 來測試網站服務器是否正常工做 ide

如你所見,apache服務器已經工做了。測試

設置虛擬主機網站

1.建立虛擬目錄

如今,讓咱們繼續安裝虛擬主機。正如我先前所述,我要新建2臺虛擬主機分別命名爲「unixmen1.local」和「unixmen2.local」.

建立一個公用的文件夾來存放這兩臺虛擬主機的數據。

首先,讓咱們爲unixmen1.local這個站點建立一個目錄:

  • sudo mkdir -p /var/www/unixmen1.local/public_html

接着,爲for unixmen2.local站點建立一個目錄:

  • sudo mkdir -p /var/www/unixmen2.local/public_html

2. 設置全部者和權限

上面目錄如今只有root擁有權限。咱們須要修改這2個目錄的擁有權給普通用戶,而不單單是root用戶。

  • sudo chown -R $USER:$USER /var/www/unixmen1.local/public_html/
  • sudo chown -R $USER:$USER /var/www/unixmen2.local/public_html/

「$USER」變量指向了當前的登陸用戶。

設置讀寫權限給apache網頁根目錄(/var/www)及其子目錄,這樣每一個人均可以從目錄中讀取文件。

  • sudo chmod -R 755 /var/www/

這樣,咱們就建立好了一些文件夾來保存網絡相關數據並分配必要的權限和所屬用戶。

3. 爲虛擬主機建立示例頁

如今,咱們給網站增長示例頁。第一步,讓咱們給虛擬主機unixmen1.local建立一個示例頁。

給unixmen1.local虛擬主機建立一個示例頁,

  • sudo vi /var/www/unixmen1.local/public_html/index.html

添加如下內容:

  • <html>
  • <head>
  • <title>www.unixmen1.local</title>
  • </head>
  • <body>
  • <h1>Welcome To Unixmen1.local website</h1>
  • </body>
  • </html>

保存並關閉文件。

一樣的,添加示例頁到第二臺虛擬主機。

  • sudo vi /var/www/unixmen2.local/public_html/index.html

添加如下內容:

  • <html>
  • <head>
  • <title>www.unixmen2.local</title>
  • </head>
  • <body>
  • <h1>Welcome To Unixmen2.local website</h1>
  • </body>
  • </html>

保存並關閉文件。

4. 建立虛擬主機配置文件

默認狀況下,apache有一個默認的虛擬主機文件叫000-default.conf。咱們將會複製000-default.conf文件內容到咱們新的虛擬主機配置文件中。

  • sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/unixmen1.local.conf
  • sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/unixmen2.local.conf

確保虛擬主機配置文件末尾包含.conf擴展名。

如今,修改unximen1.local.conf文件以符合需求。

  • sudo vi /etc/apache2/sites-available/unixmen1.local.conf

使相關的變化直接呈如今unixmen1站點中(譯註:以「#」開頭的註釋行能夠忽略。)。

  • <VirtualHost *:80>
  • # The ServerName directive sets the request scheme, hostname and port that
  • # the server uses to identify itself. This is used when creating
  • # redirection URLs. In the context of virtual hosts, the ServerName
  • # specifies what hostname must appear in the request's Host: header to
  • # match this virtual host. For the default virtual host (this file) this
  • # value is not decisive as it is used as a last resort host regardless.
  • # However, you must set it for any further virtual host explicitly.
  • #ServerName www.example.com
  •  
  • ServerAdmin webmaster@unixmen1.local
  • ServerName unixmen1.local
  • ServerAlias www.unixmen1.local
  • DocumentRoot /var/www/unixmen1.local/public_html
  •  
  • # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
  • # error, crit, alert, emerg.
  • # It is also possible to configure the loglevel for particular
  • # modules, e.g.
  • #LogLevel info ssl:warn
  •  
  • ErrorLog ${APACHE_LOG_DIR}/error.log
  • CustomLog ${APACHE_LOG_DIR}/access.log combined
  •  
  • # For most configuration files from conf-available/, which are
  • # enabled or disabled at a global level, it is possible to
  • # include a line for only one particular virtual host. For example the
  • # following line enables the CGI configuration for this host only
  • # after it has been globally disabled with "a2disconf".
  • #Include conf-available/serve-cgi-bin.conf
  • </VirtualHost>

同理,修改第二臺主機文件。

  • sudo vi /etc/apache2/sites-available/unixmen2.local.conf

使相關的修改在unixmen2 站點呈現出來。

  • <VirtualHost *:80>
  • # The ServerName directive sets the request scheme, hostname and port that
  • # the server uses to identify itself. This is used when creating
  • # redirection URLs. In the context of virtual hosts, the ServerName
  • # specifies what hostname must appear in the request's Host: header to
  • # match this virtual host. For the default virtual host (this file) this
  • # value is not decisive as it is used as a last resort host regardless.
  • # However, you must set it for any further virtual host explicitly.
  • #ServerName www.example.com
  •  
  • ServerAdmin webmaster@unixmen2.local
  • ServerName unixmen2.local
  • ServerAlias www.unixmen2.local
  • DocumentRoot /var/www/unixmen2.local/public_html
  •  
  • # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
  • # error, crit, alert, emerg.
  • # It is also possible to configure the loglevel for particular
  • # modules, e.g.
  • #LogLevel info ssl:warn
  •  
  • ErrorLog ${APACHE_LOG_DIR}/error.log
  • CustomLog ${APACHE_LOG_DIR}/access.log combined
  •  
  • # For most configuration files from conf-available/, which are
  • # enabled or disabled at a global level, it is possible to
  • # include a line for only one particular virtual host. For example the
  • # following line enables the CGI configuration for this host only
  • # after it has been globally disabled with "a2disconf".
  • #Include conf-available/serve-cgi-bin.conf
  • </VirtualHost>

修改虛擬主機文件後,禁用默認的虛擬主機配置(000.default.conf),而後啓用新的虛擬主機配置,以下所示。

  • sudo a2dissite 000-default.conf
  • sudo a2ensite unixmen1.local.conf
  • sudo a2ensite unixmen2.local.conf

最後,重啓apache服務器。

  • sudo service apache2 restart

就是這樣。如今,咱們成功地配置了apach虛擬主機在咱們的Ubuntu服務器上

相關文章
相關標籤/搜索