linux - centos下配置虚拟主机

本文详细介绍了如何在Linux环境下使用root账号通过终端操作,为eth0网卡配置多个IP地址,创建并配置多虚拟主机。并通过Nginx进行web服务,包括设置监听端口、访问日志、根目录及索引文件等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.root账号进入终端,ifconfig查看当前ip地址信息和网卡

2.重新配置eth0网卡

ifconfig eth0 192.168.1.9 netmask 255.255.255.0

3.开始配置第一个虚拟主机(broadcast必须与eth0的一致)

ifconfig eth0:1 192.168.1.7 broadcast 192.168.1.255 netmask 255.255.255.0

4.配置第二个虚拟主机

ifconfig eth0:1 192.168.1.8 broadcast 192.168.1.255 netmask 255.255.255.0

5.其他虚拟主机配置参考以上

 

6.开始配置nginx文件

cd /usr/local/nginx/conf

touch virtual-host.conf

vim virtual-host.conf
user nobody;
worker_processes 4;
events {
        worker_connections 1024;
}
http {
        server {
                listen 192.168.1.7:80;
                server_name 192.168.1.7;
                access_log logs/server1.access.log combined;
                location / {
                        index index.html index.htm;
                        root html/server1;
                }
        }
        server {
                listen 192.168.1.8:80;
                server_name 192.168.1.8;
                access_log logs/server2.access.log combined;
                location / {
                        index index.html index.htm;
                        root html/server2;
                }
        }
}

7.在/usr/local/nginx/html下新建server1和server2目录,并分别在目录里新建index.html文件

8.加载virtual-host.conf配置文件

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/virtual-host.conf

9.在浏览器里访问相应地址 - 192.168.1.7 or 192.168.1.8

 

10.如有需要,结束nginx服务

pkill -9 nginx

 

转载于:https://www.cnblogs.com/sunbey/p/8528876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值