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

ASP.NET Core应用在AWS Linux EC2实例无法运行,显示Apache测试页

问题:ASP.NET Core应用部署到EC2后显示Apache测试页,而非应用内容

我通过AWS CodePipeline从CodeCommit拉取代码、构建并将默认ASP.NET Core Web应用发布到Linux EC2实例,整个流程执行成功,已确认应用文件已发布至/var/www/html/目录。但访问负载均衡器(EC2实例位于其后方)的URL时,仅显示Apache测试页,而非我的ASP.NET Core应用。

用到的配置文件

buildspec.yaml

version: 0.2
env:
    variables:
        DOTNET_CORE_RUNTIME: 6.0
phases:
    install:
        on-failure: ABORT
        runtime-versions:
            dotnet: ${DOTNET_CORE_RUNTIME}
        commands:
            - echo install stage - started `date`
    pre_build:
        commands:
            - echo pre build stage - stared `date`
            - echo restore dependencies started `date`
            - dotnet restore ./WebApplication1/WebApplication1.csproj
    build:
        commands:
            - echo build stage - started `date`
            - dotnet publish --configuration Release --runtime linux-x64  ./WebApplication1/WebApplication1.csproj --self-contained
            - cp ./WebApplication1/appspec.yml ./WebApplication1/bin/Release/net6.0/linux-x64/publish/
artifacts:
    files:
        - '**/*'
        - appspec.yml
    name: artifact-test-cham
    discard-paths: no
    base-directory: ./WebApplication1/bin/Release/net6.0/linux-x64/publish/

appspec.yaml

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html/

排查与解决步骤

  • 修改Apache反向代理配置
    Apache默认加载/var/www/html/index.html测试页,需要配置反向代理指向ASP.NET Core应用的运行端口(默认5000):

    1. 新建或编辑Apache站点配置文件,比如/etc/httpd/conf.d/webapp.conf:
    <VirtualHost *:80>
        ProxyPreserveHost On
        ProxyPass / http://localhost:5000/
        ProxyPassReverse / http://localhost:5000/
        ErrorLog /var/log/httpd/webapp-error.log
        CustomLog /var/log/httpd/webapp-access.log common
    </VirtualHost>
    
    1. 禁用默认欢迎页配置(CentOS/RHEL):sudo mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak
    2. 重启Apache:sudo systemctl restart httpd
  • 确保ASP.NET Core应用持续运行
    部署的文件只是静态可执行文件,需要配置服务让它开机自启并保持运行:

    1. 新建systemd服务文件/etc/systemd/system/webapp.service:
    [Unit]
    Description=ASP.NET Core Web Application
    
    [Service]
    WorkingDirectory=/var/www/html
    ExecStart=/var/www/html/WebApplication1
    Restart=always
    RestartSec=10
    SyslogIdentifier=webapp
    User=apache
    Environment=ASPNETCORE_ENVIRONMENT=Production
    
    [Install]
    WantedBy=multi-user.target
    
    1. 加载配置并启动服务:
    sudo systemctl daemon-reload
    sudo systemctl start webapp
    sudo systemctl enable webapp
    
    1. 检查服务状态:sudo systemctl status webapp
  • 验证端口与权限

    1. 确认应用监听5000端口:sudo netstat -tulpn | grep 5000
    2. 给EC2安全组开放80端口(负载均衡转发端口),以及5000端口(本地测试用)
    3. 调整文件权限,确保Apache用户能访问应用文件:
    sudo chown -R apache:apache /var/www/html
    sudo chmod -R 755 /var/www/html
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 04:10:19