Centos 7 服务器部署多网站

一、准备工作

  1. 安装 Apache

    bash

    复制代码
    sudo yum install httpd -y
    sudo systemctl start httpd
    sudo systemctl enable httpd
  2. 创建网站目录

    假设部署 2 个网站,目录结构如下:

    bash

    复制代码
    sudo mkdir -p /var/www/site1/html
    sudo mkdir -p /var/www/site2/html
  3. 添加测试内容

    bash

    复制代码
    echo "This is Site 1" > /var/www/site1/html/index.html
    echo "This is Site 2" > /var/www/site2/html/index.html

二、配置虚拟主机(基于 IP 或域名)

方案 1:仅用 IP 访问(无域名,适合内网测试)

修改 Apache 虚拟主机配置文件(若不存在需新建):

bash

复制代码
sudo vi /etc/httpd/conf.d/vhosts.conf

添加以下内容(通过 不同端口 区分网站):

apache

复制代码
# 网站 1:通过 IP:端口 访问(如 http://10.10.10.129:8080)
<VirtualHost *:8080>
    ServerAdmin webmaster@site1
    DocumentRoot "/var/www/site1/html"
    ServerName 10.10.10.129:8080
    ErrorLog "/var/log/httpd/site1-error_log"
    CustomLog "/var/log/httpd/site1-access_log" combined
</VirtualHost>

# 网站 2:通过 IP:另一端口 访问(如 http://10.10.10.129:8081)
<VirtualHost *:8081>
    ServerAdmin webmaster@site2
    DocumentRoot "/var/www/site2/html"
    ServerName 10.10.10.129:8081
    ErrorLog "/var/log/httpd/site2-error_log"
    CustomLog "/var/log/httpd/site2-access_log" combined
</VirtualHost>
方案 2:搭配域名访问(推荐,需提前配置 DNS 解析)

假设域名为 site1.localsite2.local(内网可通过本地 hosts 文件绑定:10.10.10.129 site1.local site2.local),配置如下:

apache

复制代码
# 网站 1:通过域名访问(如 http://site1.local)
<VirtualHost *:80>
    ServerAdmin webmaster@site1
    DocumentRoot "/var/www/site1/html"
    ServerName site1.local
    ServerAlias www.site1.local  # 可选:支持带 www 的域名
    ErrorLog "/var/log/httpd/site1-error_log"
    CustomLog "/var/log/httpd/site1-access_log" combined
</VirtualHost>

# 网站 2:通过另一域名访问(如 http://site2.local)
<VirtualHost *:80>
    ServerAdmin webmaster@site2
    DocumentRoot "/var/www/site2/html"
    ServerName site2.local
    ServerAlias www.site2.local
    ErrorLog "/var/log/httpd/site2-error_log"
    CustomLog "/var/log/httpd/site2-access_log" combined
</VirtualHost>

三、配置防火墙(CentOS 7 默认使用 firewalld)

  1. 开放端口(根据方案选择端口,示例为 80、8080、8081):

    bash

    复制代码
    sudo firewall-cmd --permanent --add-port=80/tcp
    sudo firewall-cmd --permanent --add-port=8080/tcp
    sudo firewall-cmd --permanent --add-port=8081/tcp
    sudo firewall-cmd --reload
  2. 若为公网服务器:需在云服务商控制台放行对应端口(如阿里云安全组、腾讯云防火墙)。

四、验证访问

  1. 内网测试

    • 方案 1:访问 http://10.10.10.129:8080http://10.10.10.129:8081,查看是否显示对应内容。
    • 方案 2:在内网电脑的 hosts 文件中添加 10.10.10.129 site1.local site2.local,然后访问 http://site1.localhttp://site2.local
  2. 公网测试(若 IP 为公网 IP):

    • 直接通过公网 IP: 端口 或域名访问(需确保 DNS 解析已生效)。

五、扩展:使用 HTTPS(需域名)

若后续需要为网站添加 HTTPS(需先绑定域名):

  1. 安装证书工具(以 Let's Encrypt 为例):

    bash

    复制代码
    sudo yum install epel-release -y
    sudo yum install certbot python2-certbot-apache -y
  2. 申请证书并配置:

    bash

    复制代码
    sudo certbot --apache -d site1.local -d www.site1.local  # 替换为实际域名

    证书会自动配置到 Apache 虚拟主机中,访问时会强制跳转至 https://

常见问题处理

  1. 端口冲突 :确保端口未被其他服务占用(如 netstat -tunlp | grep 8080)。
  2. 权限问题 :若无法访问文件,检查目录权限(建议设置为 chown -R apache:apache /var/www/site*)。
  3. 域名解析延迟 :公网域名解析可能需要几分钟到几小时生效,可通过 nslookup site1.local 检查解析状态。

通过以上步骤,可在同一服务器上基于 IP 或域名部署多个独立网站服务。如果需要更复杂的负载均衡或动态网站(如 PHP、Node.js),可进一步配置 FastCGI 或反向代理。

测试第一个网站

测试第二个网站

网站内容自己添加,网站页面是开发做的跟运维没关系

相关推荐
BlueBirdssh6 分钟前
linux 内核通过 dts 设备树 配置pcie 控制器 各种参数和中断等, 那freeRTOS 是通过直接设置PCIe寄存器吗
linux
小目标一个亿23 分钟前
Windows平台Nginx配置web账号密码验证
linux·前端·nginx
Aotman_37 分钟前
Element-UI Message Box弹窗 使用$confirm方法自定义模版内容,修改默认样式
linux·运维·前端
heartbeat..2 小时前
零基础学 SQL:DQL/DML/DDL/DCL 核心知识点汇总(附带连接云服务器数据库教程)
java·服务器·数据库·sql
那些年的笔记2 小时前
Linux屏幕旋转方法
linux·运维·服务器
XiaoHu02072 小时前
Linux网络编程套接字
linux·服务器·网络·git
竹之却2 小时前
CentOS 系列,防火墙相关指令
linux·运维·centos
gaize12133 小时前
科普篇“机架、塔式、刀片”三类服务器对比
运维·服务器
咕噜企业分发小米3 小时前
如何利用云服务器搭建游戏服务器并实现跨平台游戏?
运维·服务器·游戏
一颗青果3 小时前
进程组 | 会话 |终端 | 前台后台 | 守护进程
linux·运维·jvm