系統環境:
[[email protected] ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
一、配置基于IP的虛拟主機
1、配置網卡IP,先檢視本地虛拟機ip位址,實體機IP是:192.168.80.1
[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE="Ethernet"
BOOTPROTO="static"
NAME="eno16777736"
DEVICE="eno16777736"
ONBOOT="yes"
IPADDR=192.168.80.10
NETMASK=255.255.255.0
GATEWAY=192.168.80.2
DNS1=192.168.1.1
2、在我的虛機網卡eno16777736(centos6的網卡名字是eth0)裝置上添加另外兩個IP别名,192.168.80.11和192.168.80.12
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777736:0
TYPE=Ethernet
BOOTPROTO=static
NAME=eno16777736:0
DEVICE=eno16777736:0
ONBOOT=yes
IPADDR=192.168.80.11
NETMASK=255.255.255.0
GATEWAY=192.168.80.2
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777736:1
TYPE=Ethernet
BOOTPROTO=static
NAME=eno16777736:1
DEVICE=eno16777736:1
ONBOOT=yes
IPADDR=192.168.80.12
NETMASK=255.255.255.0
GATEWAY=192.168.80.2
修改完,重新開機網絡服務
[[email protected] ~]# systemctl restart network
3、檢視确認:
[[email protected] ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:38:cf:ef brd ff:ff:ff:ff:ff:ff
inet 192.168.80.10/24 brd 192.168.80.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.80.11/24 brd 192.168.80.255 scope global secondary eno16777736:0
valid_lft forever preferred_lft forever
inet 192.168.80.12/24 brd 192.168.80.255 scope global secondary eno16777736:1
valid_lft forever preferred_lft forever
4、ping測試正常
[[email protected] ~]# ping 192.168.80.11
PING 192.168.80.11 (192.168.80.11) 56(84) bytes of data.
64 bytes from 192.168.80.11: icmp_seq=1 ttl=64 time=0.032 ms
64 bytes from 192.168.80.11: icmp_seq=2 ttl=64 time=0.042 ms
^C
--- 192.168.80.11 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1014ms
rtt min/avg/max/mdev = 0.032/0.037/0.042/0.005 ms
[[email protected] ~]# ping 192.168.80.12
PING 192.168.80.12 (192.168.80.12) 56(84) bytes of data.
64 bytes from 192.168.80.12: icmp_seq=1 ttl=64 time=0.058 ms
64 bytes from 192.168.80.12: icmp_seq=2 ttl=64 time=0.046 ms
^C
--- 192.168.80.12 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
二、yum 安裝nginx
1、如果預設yum install -y nginx安裝不成功就先配置yum源,否則跳過。
[[email protected] ~]# vim /etc/yum.repos.d/epel.repo
[epel]
name=aliyun epel
baseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
gpgcheck=0
2、修改nginx配置檔案,預設路徑是 /etc/nginx/nginx.conf(主要是黑色粗體的配置,其他不寫都可以)
[[email protected] ~]# vim /etc/nginx/nginx.conf
預設IP 192.168.80.10的nginx配置
server {
listen 80;
server_name _;
root /usr/share/nginx/html; #網頁檔案存放的目錄
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
添加server1 192.168.80.11的nginx配置
server {
listen 80;
server_name 192.168.80.11;
root /usr/share/nginx/server1; #網頁檔案存放的目錄
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
添加server2 192.168.80.12的nginx配置
server {
listen 80;
server_name 192.168.80.12;
root /usr/share/nginx/server2; #網頁檔案存放的目錄
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
啟動服務
[[email protected] ~]# systemctl start nginx
3、添加另外兩個新增IP的nginx頁面。建立server*目錄,然後拷貝預設目錄html裡面的檔案到新目錄并修改網頁頭部内容以便區分。
server1
[[email protected] nginx]# mkdir server1
[[email protected] nginx]# cp -R html/* server1/
[[email protected] server1]# vim index.html
修改各自目錄下的index.html
77 <h1>Welcome to <strong>server1</strong> on Fedora!</h1>
server2
[[email protected] nginx]# mkdir server2
[[email protected] nginx]# cp -R html/* server2/
[[email protected] server2]# vim index.html
修改各自目錄下的index.html
77 <h1>Welcome to <strong>server2</strong> on Fedora!</h1>
三、重新開機服務并檢視
[[email protected] ~]# systemctl restart network
[[email protected] ~]# systemctl restart nginx
網頁通路成功!
