1

AWS CodeDeploy 用于简单的 WordPress 应用程序。在以下脚本的帮助下,在 ubuntu 20.04 上安装了 AWS codedeploy-agent

#!/bin/bash
apt update
apt install ruby -y
gem install bundler
git clone https://github.com/aws/aws-codedeploy-agent.git /opt/codedeploy-agent
sudo chown -R root.root /opt/codedeploy-agent
sudo chmod 644 /opt/codedeploy-agent/conf/codedeployagent.yml
sudo chmod 755 /opt/codedeploy-agent/init.d/codedeploy-agent
sudo chmod 644 /opt/codedeploy-agent/init.d/codedeploy-agent.service
cd /opt/codedeploy-agent
bundle install --system
rake clean && rake
cp /opt/codedeploy-agent/init.d/codedeploy-agent /etc/init.d/
systemctl daemon-reload
systemctl start codedeploy-agent
systemctl enable codedeploy-agent

使用下面的 appspec.yml 进行代码部署。它的工作正常runas root

问题 :

  1. 如何以ubuntu用户身份运行它,?
  2. root以用户身份运行时有任何问题吗?……

appspec.yaml 文件

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html/
    overwrite: true
hooks:
  BeforeInstall:
    - location: scripts/before_install.sh
      timeout: 300
      runas: root
  AfterInstall:
    - location: scripts/setup_environment.sh
      timeout: 300
      runas: root 
    - location: scripts/after_install.sh
      timeout: 900
      runas: root
  ApplicationStart:
    - location: scripts/start_server.sh
      timeout: 300
  ApplicationStop:
    - location: scripts/stop_server.sh
      timeout: 300
  ValidateService:
    - location: scripts/validate_service.sh
      timeout: 300

虽然 runas ubuntu 收到以下错误。

Error code
ScriptFailed
Script name
scripts/setup_environment.sh
Message
Script at specified location: scripts/setup_environment.sh run as user ubuntu failed with exit code 4



LifecycleEvent - AfterInstall
Script - scripts/setup_environment.sh
[stderr]shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
[stderr]shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
[stderr]/opt/codedeploy-agent/deployment-root/44d6390b-485e-87ef-b50855bbf251/d-D0RTN7AR5/deployment-archive/scripts/setup_environment.sh: line 4: /var/www/html/.env: Permission denied
[stderr]sed: couldn't open temporary file /var/www/html/scripts/seTwGZAv: Permission denied
4

2 回答 2

1

如果您以用户身份运行它,由于您遇到的权限不足,ubuntu它将无法工作:

couldn't open temporary file /var/www/html/scripts/seTwGZAv: Permission denied

原因是用户/var/www/html/无法访问ubuntu。要使其正常工作,您必须更改其默认权限,这是一种不好的做法

有些事情必须执行为root,除非您想开始更改ubuntu操作系统的默认配置和权限模型。

于 2020-10-07T22:43:43.690 回答
0

由于 appspec.yml 文件和脚本由您管理,因此在以 root 身份运行我们的脚本时没有任何安全问题。你会写什么,你就会得到什么。

在使用任何非 root 用户时,向该用户提供所有必需的权限非常重要。在大多数情况下,您必须sudo在每个命令之前使用并确保将您的用户添加到 sudoers

你需要确保

  1. 您的 git 不会受到任何未经授权的更改。
  2. CodeDeploy 只能由受信任的资源访问。

如果这两件事都被检查了,那么任何异常命令都无法在您的系统上运行

于 2020-10-07T12:41:22.007 回答