Nginx虛擬主機
- 基於域名的虛擬主機
- 基於IP地址的虛擬主機
- 基於端口的虛擬主機
一,安裝DNS域名解析服務器
1,安裝bind服務器
[root@localhost ~]# yum install bind -y
2,修改主配置文件(named.conf)
[root@localhost ~]# vim /etc/named.conf
options {
listen-on port 53 { any; }; ##監聽所有
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; }; ##允許所有
3,修改區域配置文件(named.rfc1912.zones)
[root@localhost ~]# vim /etc/named.rfc1912.zones ##配置區域配置文件
zone "kgc.com" IN {
type master;
file "kgc.com.zone"; ##kgc區域數據配置文件
allow-update { none; };
};
zone "accp.com" IN {
type master;
file "accp.com.zone"; ##accp區域數據配置文件
allow-update { none; };
};
4,修改區域數據配置文件(kgc.com.zone accp.com.zone)
[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost kgc.com.zone ##複製模板
[root@localhost named]# vim kgc.com.zone ##修改區域配置文件
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.13.128 ##本機地址
[root@localhost named]# cp -p kgc.com.zone accp.com.zone ##複製文件爲accp區域數據配置文件
[root@localhost named]# systemctl start named ##開啓dns服務
[root@localhost named]# systemctl stop firewalld.service ##關閉防火牆
[root@localhost named]# setenforce 0
5,創建兩個網站的站點目錄並添加首頁內容
[root@localhost ~]# mkdir -p /var/www/html/accp ##創建accp站點
[root@localhost ~]# mkdir -p /var/www/html/kgc ##創建kgc站點
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
accp kgc
[root@localhost html]# echo "this a accp web" > accp/index.html ##創建首頁內容
[root@localhost html]# echo "this a kgc web" > kgc/index.html ##創建首頁內容
二,在Windows上將LAMP所需壓縮軟件包共享出來(此處如有問題請看之前的博客相關文章)
三,在Linux上使用遠程共享獲取文件並掛載到mnt目錄下
[root@localhost ~]# smbclient -L //192.168.100.3/ ##遠程共享訪問
Enter SAMBA\root's password:
Sharename Type Comment
--------- ---- -------
LNMP-C7 Disk
[root@localhost ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt ##掛載到/mnt目錄下
四,編譯安裝Nginx
1,解壓源碼包到/opt下,並查看
[root@localhost ~]# cd /mnt ##切換到掛載點目錄
[root@localhost mnt]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz
mysql-boost-5.7.20.tar.gz php-7.1.20.tar.gz
[root@localhost mnt]# tar zxvf nginx-1.12.2.tar.gz -C /opt ##解壓Nginx源碼包到/opt下
[root@localhost mnt]# cd /opt/ ##切換到解壓的目錄下
[root@localhost opt]# ls
nginx-1.12.2 rh
2,安裝編譯需要的環境組件包
[root@localhost opt]# yum -y install \
gcc \ //c語言
gcc-c++ \ //c++語言
pcre-devel \ //pcre語言工具
zlib-devel //數據壓縮用的函式庫
3,創建程序用戶nginx並編譯Nginx
[root@localhost opt]# useradd -M -s /sbin/nologin nginx ##創建程序用戶,安全不可登陸狀態
[root@localhost opt]# id nginx
uid=1001(nginx) gid=1001(nginx) 組=1001(nginx)
[root@localhost opt]# cd nginx-1.12.0/ ##切換到nginx目錄下
[root@localhost nginx-1.12.0]# ./configure \ ##配置nginx
> --prefix=/usr/local/nginx \ ##安裝路徑
> --user=nginx \ ##用戶名
> --group=nginx \ ##用戶組
> --with-http_stub_status_module ##狀態統計模塊
4,編譯和安裝
[root@localhost nginx-1.12.0]# make ##編譯
...
[root@localhost nginx-1.12.0]# make install ##安裝
...
5,優化nginx啓動腳本,以便於系統識別
[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
##創建軟連接讓系統識別nginx啓動腳本
[root@localhost nginx]# nginx -t ##檢查配置文件的語法問題
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx]# nginx ##開啓ngnix
[root@localhost nginx]# netstat -ntap | grep 80 ##查看端口,nginx已經開啓
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 39620/nginx: master
[root@localhost nginx]# systemctl stop firewalld.service ##關閉防火牆
[root@localhost nginx]# setenforce 0
[root@localhost nginx]# nginx ##開啓
6,製作管理腳本,便於使用service管理使用
[root@localhost nginx]# cd /etc/init.d/ ##切換到啓動配置文件目錄
[root@localhost init.d]# ls
functions netconsole network README
[root@localhost init.d]# vim nginx ##編輯啓動腳本文件
#!/bin/bash
# chkconfig: - 99 20 ##註釋信息
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx" ##設置變量爲nginx命令文件
PIDF="/usr/local/nginx/logs/nginx.pid" ##設置變量PID文件 進程號爲5346
case "$1" in
start)
$PROG ##開啓服務
;;
stop)
kill -s QUIT $(cat $PIDF) ##關閉服務
;;
restart) ##重啓服務
$0 stop
$0 start
;;
reload) ##重載服務
kill -s HUP $(cat $PIDF)
;;
*) ##錯誤輸入提示
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost init.d]# chmod +x /etc/init.d/nginx ##給啓動腳本執行權限
[root@localhost init.d]# chkconfig --add nginx ##添加到service管理器中
[root@localhost init.d]# service nginx stop ##就可以使用service控制nginx
[root@localhost init.d]# service nginx start
7,或者方便systemctl管理,配置文件(爲方便寫一種即可)
[root@localhost ~]# vim /lib/systemd/system/nginx.service ##創建配置文件
[Unit]
Description=nginx ##描述
After=network.target ##描述服務類型
[Service]
Type=forking ##後臺運行形式
PIDFile=/usr/local/nginx/logs/nginx.pid ##PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx ##啓動服務
ExecReload=/usr/bin/kill -s HUP $MAINPID ##根據PID重載配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ##根據PID終止進程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service ##設置執行權限
[root@localhost ~]# systemctl stop nginx.service ##關閉
[root@localhost ~]# systemctl start nginx.service ##開啓
五,基於不同域名的虛擬主機
1,修改nginx配置文件信息
[root@localhost ~]# cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf ##修改Nginx配置文件
server {
listen 80;
server_name www.kgc.com; ##kgc網站
charset utf-8;
access_log logs/www.kgc.com.access.log; ##日誌文件
location / {
root /var/www/html/kgc; ##站點目錄
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.accp.com; ##accp網站
charset utf-8;
access_log logs/www.accp.com.access.log; ##日誌文件
location / {
root /var/www/html/accp; ##站點目錄
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost conf]# service nginx restart ##重啓nginx服務
2,用測試機訪問不同域名的網站
六,基於不同端口的虛擬主機
1,修改nginx配置文件信息
[root@localhost ~]# cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf ##修改Nginx配置文件
server {
listen 80;
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.com.access.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.13.138:8080; ##修改監聽端口爲8080
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp8080.com.access.log; ##日誌文件修改爲8080
location / {
root /var/www/html/accp8080; ##8080端口的站點目錄
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
2,創建accp8080站點目錄,並創建首頁內容
[root@localhost conf]# cd /var/www/html/ ##切換到站點目錄中
[root@localhost html]# mkdir accp8080 ##創建站點目錄
[root@localhost html]# cd accp8080/
[root@localhost accp8080]# echo "this is accp8080 web" > index.html ##創建首頁內容
[root@localhost accp8080]# service nginx restart ##重啓Nginx服務
3,用測試機訪問不同端口的網站
七,基於不同IP的虛擬主機
1,在虛擬機上添加一塊網卡
192.168.13.138
192.168.13.133
2,修改dns區域數據配置文件
[root@localhost ~]# cd var/named/
[root@localhost named]# vim kgc.com.zone ##修改kgc的區域數據配置文件
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.13.133 ##地址爲133
[root@localhost named]# vim accp.com.zone ##修改accp的區域數據配置文件
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.13.138 ##地址爲138
[root@localhost named]# systemctl restart named ##重啓dns服務
3,修改nginx配置文件信息
[root@localhost ~]# cd /usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf ##修改Nginx配置文件
server {
listen 192.168.13.133:80; ##指定IP地址
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.com.access.log;
location / {
root /var/www/html/kgc;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 192.168.13.138:80; ##指定IP地址
server_name www.accp.com;
charset utf-8;
access_log logs/www.accp.com.access.log;
location / {
root /var/www/html/accp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost conf]# service nginx restart ##重啓Nginx服務