You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ubuntu服务器同一主机上运行两个监听不同端口的Apache服务

同设备多Apache服务运行方案(Ubuntu Server环境)

完全可以实现,共有两种成熟的实现路径,可根据实际需求选择:优先推荐单实例多端口虚拟主机方案,配置简单、资源占用低,可满足绝大多数场景;如果需要完全隔离的配置规则、独立启停控制、不同模块加载策略,可选择双独立实例方案。

方案一:单Apache实例监听多端口(推荐)

操作步骤如下:

  • 安装Apache服务(已安装可跳过)
    apt update && apt install apache2 -y
    
  • 修改端口监听配置,编辑/etc/apache2/ports.conf,在原有Listen 80配置行下方新增端口监听规则:
    Listen 81
    
  • 复制默认虚拟主机配置作为81端口的配置文件:
    cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/001-port81.conf
    
  • 编辑81端口虚拟主机配置/etc/apache2/sites-available/001-port81.conf,修改如下内容:
    • 将开头的<VirtualHost *:80>改为<VirtualHost *:81>
    • 修改DocumentRoot参数为81端口对应站点的根目录,例如/var/www/port81,可按需配置日志路径、访问规则等其他参数
  • 创建对应站点根目录并配置权限:
    mkdir -p /var/www/port81
    chown www-data:www-data /var/www/port81
    chmod 755 /var/www/port81
    
  • 启用新的虚拟主机配置:
    a2ensite 001-port81.conf
    
  • 校验配置语法合法性,返回Syntax OK即为正常:
    apache2ctl configtest
    
  • 重启Apache服务生效:
    systemctl restart apache2
    
  • 若开启了ufw防火墙,需要放行81端口:
    ufw allow 81/tcp
    

验证:分别访问http://192.168.0.1和http://192.168.0.1:81,确认返回对应站点内容即可。

方案二:运行两个完全独立的Apache实例

操作步骤如下:

  • 先停止并禁用默认Apache服务,避免端口、进程冲突:
    systemctl stop apache2
    systemctl disable apache2
    
  • 复制默认Apache配置目录作为第二个实例的独立配置路径:
    cp -r /etc/apache2 /etc/apache2-second
    
  • 修改第二个实例的端口监听规则,编辑/etc/apache2-second/ports.conf,将所有Listen配置的80端口改为81,避免与第一个实例冲突。
  • 修改第二个实例的运行环境变量配置,编辑/etc/apache2-second/envvars,修改如下参数:
    export APACHE_PID_FILE=/var/run/apache2-second/apache2.pid
    export APACHE_RUN_DIR=/var/run/apache2-second
    export APACHE_LOCK_DIR=/var/lock/apache2-second
    export APACHE_LOG_DIR=/var/log/apache2-second
    
  • 创建第二个实例所需的运行目录并配置权限:
    mkdir -p /var/run/apache2-second /var/lock/apache2-second /var/log/apache2-second
    chown www-data:www-data /var/run/apache2-second /var/lock/apache2-second /var/log/apache2-second
    
  • 调整第二个实例的虚拟主机配置,编辑/etc/apache2-second/sites-available/000-default.conf,将<VirtualHost *:80>改为<VirtualHost *:81>,配置对应站点的DocumentRoot路径。
  • 为两个实例分别创建systemd服务配置文件:
    1. 第一个实例配置文件/etc/systemd/system/apache2-first.service,核心配置参考:
      [Unit]
      Description=The Apache HTTP Server (first instance)
      After=network.target remote-fs.target nss-lookup.target
      
      [Service]
      Type=forking
      ExecStart=/usr/sbin/apache2ctl -f /etc/apache2/apache2.conf start
      ExecReload=/usr/sbin/apache2ctl -f /etc/apache2/apache2.conf graceful
      ExecStop=/usr/sbin/apache2ctl -f /etc/apache2/apache2.conf stop
      PIDFile=/var/run/apache2/apache2.pid
      Restart=on-abnormal
      
      [Install]
      WantedBy=multi-user.target
      
    2. 第二个实例配置文件/etc/systemd/system/apache2-second.service,核心配置参考:
      [Unit]
      Description=The Apache HTTP Server (second instance)
      After=network.target remote-fs.target nss-lookup.target
      
      [Service]
      Type=forking
      ExecStart=/usr/sbin/apache2ctl -f /etc/apache2-second/apache2.conf start
      ExecReload=/usr/sbin/apache2ctl -f /etc/apache2-second/apache2.conf graceful
      ExecStop=/usr/sbin/apache2ctl -f /etc/apache2-second/apache2.conf stop
      PIDFile=/var/run/apache2-second/apache2.pid
      Restart=on-abnormal
      
      [Install]
      WantedBy=multi-user.target
      
  • 重载systemd配置使服务生效:
    systemctl daemon-reload
    
  • 启动两个实例并设置开机自启:
    systemctl start apache2-first apache2-second
    systemctl enable apache2-first apache2-second
    

验证:执行systemctl status apache2-first apache2-second查看两个服务均为运行状态后,分别访问两个端口验证即可。

内容的提问来源于stack exchange,提问作者h_dml

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 09:06:04