在這個教程中,我會使用Ubuntu 14.04 LTS,並搭建1個測試網站命名目錄爲“tianmao .我的虛擬機IP192.168.1.128。你可以根據你的需要更改虛擬域名。前提是有了虛擬機的靜態ip,
sudo mkdir -p /var/www/tianmao
創建一個靜態html訪問文件,寫些東西並訪問
sudo touch index.html
創建虛擬主機配置文件
默認情況下,apache有一個默認的虛擬主機文件叫000-default.conf。我們將會複製000-default.conf文件內容到我們新的虛擬主機配置文件中。
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/tianmao.conf
確保虛擬主機配置文件末尾包含.conf擴展名。修改tianmao.conf文件以符合需求。
sudo vi /etc/apache2/sites-available/tianmao.conf
<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 [email protected]
ServerName www.tianmao.com
#ServerAlias www.unixmen1.local
DocumentRoot /var/www/tianmao
# 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 tianmao.conf
重啓apache服務器
sudo service apache2 restart
編輯/etc/hosts文件,
sudo vi /etc/hosts
192.168.1.250 www.tianmao.com