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

如何在Apache服务器上配置单域名部署三个独立应用?

Apache多应用部署配置方案(www.example.com分路径指向独立目录)

完全可行,以下提供两种可靠实现方式:

一、虚拟主机配置(推荐,性能更优)

直接修改Apache虚拟主机配置文件(例如/etc/apache2/sites-available/www.example.com.conf),内容如下:

<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /www_test_folder/web

    # 映射/admin到admin应用目录
    Alias /admin /www_test_folder/admin
    <Directory /www_test_folder/admin>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    # 映射/api到api应用目录
    Alias /api /www_test_folder/api
    <Directory /www_test_folder/api>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    # 主web应用目录配置
    <Directory /www_test_folder/web>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/www.example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/www.example.com-access.log combined
</VirtualHost>

配置完成后执行以下命令生效:

a2ensite www.example.com.conf
systemctl restart apache2

二、.htaccess配置方式

若必须使用.htaccess,需先确保主web目录(/www_test_folder/web)的Apache配置中开启AllowOverride All,否则.htaccess规则不会生效。

在/www_test_folder/web/.htaccess中添加:

RewriteEngine On

# 转发/admin路径请求到admin应用目录
RewriteRule ^admin/(.*)$ /www_test_folder/admin/$1 [L]

# 转发/api路径请求到api应用目录
RewriteRule ^api/(.*)$ /www_test_folder/api/$1 [L]

注意事项:

  1. 确保Apache已启用mod_rewrite模块,启用命令:
    a2enmod rewrite
    systemctl restart apache2
    
  2. 若admin或api应用自身带有.htaccess,需在对应子目录的.htaccess开头添加RewriteBase /admin(admin目录)或RewriteBase /api(api目录),避免路径解析冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 05:22:48