首页 > 解决方案 > AWS EC2 CodeDeploy 不会从 Angular dist 文件夹加载 javascript

问题描述

我正在使用 Bitbucket Pipelines + AWS CodeDeploy + AWS EC2 实例来运行我的 Angular 应用程序。管道运行良好,代码是即时构建的,然后对于 EC2,它只上传 dist/ 文件夹。然后当我尝试访问我的页面时,没有加载 css 文件和 js 文件。

我尝试使用 stackoverflow 的另一种解决方案,但没有任何效果。EC2 在 Ubuntu 18.04 上的 apache2 上运行。

我试图授予执行 js 文件的权限,但它也不起作用。

所有文件都在同一个目录中。

/deployment-id/deploment-archive/
-index.html
-runtime.js
-main.js
-polyfills.js
-runtime.js
-styles.css

索引.html

<script type="text/javascript" src="runtime26209474bfa8dc87a77c.js"></script>
<script type="text/javascript" src="es2015-polyfillsbda95d5896422d031328.js" nomodule></script>
<script type="text/javascript" src="polyfills8bbb231b43165d65d357.js"></script>
<script type="text/javascript" src="maindcabcbb8258040c4d925.js"></script>

现在它只呈现空白页面,并且我在控制台中收到警告“使用源 www.myawslink.com/main.js 加载失败”

当我尝试直接访问文件时,apache 说“找不到 404 个文件”,但是当我通过 putty 连接到 ec2 时,我看到部署目录中的文件带有 index.html。我希望页面能够正确呈现并且 js 文件能够正常工作。

标签: javascriptangularamazon-web-servicesamazon-ec2aws-code-deploy

解决方案


好的,问题出在apache2上。

因为文件是从 /var/www/html/ 提供的,并且在我的 appspec.yml 中,我在这里只包含了 index.html

所以我改变了这个

  - source: /index.html
    destination: /var/www/html/

对此

  - source: /dist
    destination: /var/www/html/

现在一切正常。

我的 bitbucket-pipeline.yml 看起来像这样

pipelines:
  default:
    - step:
        name: build angular
        image: node:10
        caches:
          - node
        script:
          - npm install
          - npm install -g @angular/cli
          - ng build --prod
        artifacts:
          - dist/**
    - step:
        name: deploy
        image: python:3.5.1
        script:
          - apt-get update # required to install zip
          - apt-get install -y zip # required for packaging up the application
          - pip install boto3==1.3.0 # required for codedeploy_deploy.py
          - zip -r /tmp/artifact.zip dist/* scripts/* appspec.yml package.json # package up the application for deployment
          - python codedeploy_deploy.py # run the deployment script

推荐阅读