虚拟主机介绍:所谓的虚拟主机,在web服务器里就是一个独立的网络站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外服务供用户访问。
一、基于域名的虚拟主机
1、创建域名对应的站点目录文件
mkdir -p /data/xingyun/basic
mkdir -p /data/anttech/basic
chown -R nginx:nginx /data/xingyun/basic/
chown -R nginx:nginx /data/anttech/basic/
mkdir -p /data/xingyun/log
mkdir -p /data/anttech/log
chown -R nginx:nginx /data/xingyun/log/
chown -R nginx:nginx /data/anttech/log/
2、配置防火墙(先开启防火墙)
systemctl stop firewalld
sed -ri '/^SELINUX/c\SELINUX=disabled' /etc/selinux/config
setenforce 0
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-servcie=https
firewall-cmd --reload
firewall-cmd --permanent --list-all
3、修改配置
cd /opt/data/nginx/conf/conf.d/
vi xingyun.conf
server {
listen 192.168.65.153:80;
server_name www.xingyun.com;
access_log /data/xingyun/log/access.log combined;
location / {
root /data/xingyun/basic;
index index.html index.htm;
}
}
vi anttech.conf
server {
listen 192.168.655.153:80;
server_name www.anttech.com;
access_log /data/anttech/log/access.log combined;
location / {
root /data/xingyun/basic;
index index.html index.htm;
}
}
4、域名解析
在本地hosts文件中添加域名信息
C:\Windows\System32\drivers\etc
5、结果测试
在/data/xingyun/basic 和/data/anttech/basic 目录下分别创建index.html 文件并添加内容
echo "this is a test from xingyun" >> /data/xingyun/basic/index.html
echo "this is a test from anttech" >> /data/anttech/basic/index.html
二、基于端口的虚拟主机
1、创建域名对应的站点目录文件
[root@localhost ~]# mkdir -p /data/xingyun1/basic
[root@localhost ~]# mkdir -p /data/xingyun2/basic
[root@localhost ~]# chown -R nginx:nginx /data/xingyun1/basic/
[root@localhost ~]# chown -R nginx:nginx /data/xingyun2/basic/
[root@localhost ~]# mkdir /data/xingyun1/log
[root@localhost ~]# mkdir /data/xingyun2/log
[root@localhost ~]# chown -R nginx:nginx /data/xingyun1/log/
[root@localhost ~]# chown -R nginx:nginx /data/xingyun2/log/
2、配置防火墙
[root@localhost ~]# systemctl start firewalld
You have new mail in /var/spool/mail/root
[root@localhost ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --query-port=8080/tcp --permanent
yes
[root@localhost ~]# firewall-cmd --zone=public --add-port=6969/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --query-port=6969/tcp --permanent
yes
[root@localhost ~]# firewall-cmd --reload
success
3、修改配置文件
cd /opt/data/nginx/conf/conf.d/
[root@localhost conf.d]# cat xingyun1.com
server {
listen 192.168.65.153:8080;
server_name www.xingnyun.com;
access_log /data/xingyun1/log/aeecss.log combined;
location / {
root /data/xingyun1/basic;
index index.html index.htm;
}
}
[root@localhost conf.d]# cat xingyun2.conf
server {
listen 192.168.65.153:6969;
server_name www.xingnyun.com;
access_log /data/xingyun2/log/access.log combined;
location / {
root /data/xingyun2/basic;
index index.html index.htm;
}
}
4、域名解析
在本地hosts文件中添加域名信息
C:\Windows\System32\drivers\etc
192.168.65.153 www.xingyun.com
5、结果测试
echo "This is a test from 8080..." >> /data/xingyun1/basic/index.html
echo "This is a test from 6969..." >> /data/xingyun2/basic/index.html
三、基于IP的虚拟主机
1、创建域名对应的站点目录文件
2、配置防火墙
3、在网卡上增加多个IP
4、修改配置
5、结果测试