將指定的域名綁定到指定的服務(wù)器目錄上面,這就是web服務(wù)器的虛擬主機(jī)功能。綁定之后,打開域名就會(huì)訪問服務(wù)器上指定的目錄。此處用Nginx做例子說明。
1、進(jìn)入nginx的conf.d目錄
cd /etc/nginx/conf.d
用ls命令查看目錄下內(nèi)容,可以看到default.conf這個(gè)文件
2、通過復(fù)制default.conf創(chuàng)建新的 .conf文件。
cp default.conf test.xiangzhige.cn.conf
3、修改test.xiangzhige.cn.conf配置文件的內(nèi)容
vim test.xiangzhige.cn.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server標(biāo)志定義虛擬主機(jī)開始,listen用于指定虛擬主機(jī)的服務(wù)端口(80是http默認(rèn)的端口號(hào)),server_name用來指定IP地址或者域名,多個(gè)域名之間用空格分 開。index用于設(shè)定訪問的默認(rèn)首頁地址,root指令用于指定虛擬主機(jī)的網(wǎng)頁根目錄,這個(gè)目錄可以是相對(duì)路徑,也可以是絕對(duì)路徑。
修改server_name、root、index項(xiàng)的內(nèi)容如下:
server_name test.xiangzhige.cn;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
root /alidata1/www/test.xiangzhige.cn;
location / {
index index.php index.html index.htm;
}
要使修改的nginx配置文件生效,需要重新加載或啟動(dòng)nginx
測(cè)試nginx配置文件是否有錯(cuò)
nginx -t
如果有錯(cuò)就改正,沒有就執(zhí)行下面命令重新加載nginx
service nginx reload
4、創(chuàng)建虛擬主機(jī)需要的目錄
[root@liboxiangECS conf.d]# mkdir /alidata1
[root@liboxiangECS conf.d]# cd /alidata1
[root@liboxiangECS alidata1]# mkdir www
[root@liboxiangECS alidata1]# cd www
[root@liboxiangECS www]# mkdir test.xiangzhige.cn
創(chuàng)建index.html文件
[root@liboxiangECS www]# cd test.xiangzhige.cn/
[root@liboxiangECS test.xiangzhige.cn]# vim index.html
在index.html文件中輸入hello,也可以輸入其它內(nèi)容
5、驗(yàn)證虛擬主機(jī)是否創(chuàng)建成功
在瀏覽器中輸入域名,以為我用的是test.xiangzhige.cn,所以我輸入test.xiangzhige.cn

如果瀏覽器打開顯示的是4中輸入的內(nèi)容,則證明虛擬主機(jī)創(chuàng)建成功