代码之家  ›  专栏  ›  技术社区  ›  PPShein

用户数据内部的AWS EC2中未成功安装NodeJS

  •  0
  • PPShein  · 技术社区  · 7 年前

    NodeJS 具有 nvm 在AWS EC2 linux中,用户数据如下:

    #!/bin/bash
    
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
    source ~/.bashrc
    nvm install 7
    

    在成功创建实例并在我的ec2实例中输入并检查之后,没有 nodejs 当我喜欢打字时安装 node --version nvm --version .

    [ec2-user@ip-0-0-0-0 ~]$ node --version
    -bash: node: command not found
    [ec2-user@ip-0-0-0-0 ~]$ nvm --version
    -bash: nvm: command not found
    

    当我检查实例的日志时,发现以下错误消息。

    [   16.310115] cloud-init[3300]: => Downloading nvm as script to '/.nvm'
    [   17.053885] cloud-init[3300]: => Profile not found. Tried  (as defined in $PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
    [   17.076402] cloud-init[3300]: => Create one of them and run this script again
    [   17.087459] cloud-init[3300]: => Create it (touch ) and run this script again
    [   17.092307] cloud-init[3300]: OR
    [   17.100669] cloud-init[3300]: => Append the following lines to the correct file yourself:
    [   17.117606] cloud-init[3300]: export NVM_DIR="$HOME/.nvm"
    [   17.124904] cloud-init[3300]: [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [   17.161419] cloud-init[3300]: => Close and reopen your terminal to start using nvm or run the following to use it now:
    [   17.177964] cloud-init[3300]: export NVM_DIR="$HOME/.nvm"
    [   17.185400] cloud-init[3300]: [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    
    1 回复  |  直到 7 年前
        1
  •  9
  •   Sébastien Stormacq    7 年前

    正如日志所解释的 install.sh 脚本正在尝试查找一个配置文件,但找不到该配置文件。(请记住,用户数据中提供的脚本是以root用户身份运行的,因此$HOME是 /root .

    解决方案1 (未经测试)

    #!/bin/bash
    
    touch ~/.bashrc # this ensure the bashrc file is created
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
    source ~/.bashrc
    nvm install 7
    

    解决方案2 (测试)

    #!/bin/bash
    
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    nvm install 7
    

    (从用户数据运行时,$HOME为/)

    $ ssh ec2-user@ec2-18-202-174-164.eu-west-1.compute.amazonaws.com
    Warning: Permanently added 'ec2-18-202-174-164.eu-west-1.compute.amazonaws.com,18.202.174.164' (ECDSA) to the list of known hosts.
    
           __|  __|_  )
           _|  (     /   Amazon Linux 2 AMI
          ___|\___|___|
    
    https://aws.amazon.com/amazon-linux-2/
    3 package(s) needed for security, out of 3 available
    Run "sudo yum update" to apply all updates.
    [ec2-user@ip-172-31-30-44 ~]$ sudo bash
    [root@ip-172-31-30-44 ec2-user]# curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 10250  100 10250    0     0  10250      0  0:00:01 --:--:--  0:00:01 54521
    => Downloading nvm as script to '/root/.nvm'
    
    
    => Appending source string to /root/.bashrc
    => Close and reopen your terminal to start using nvm or run the following to use it now:
    
    export NVM_DIR="/root/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
    [root@ip-172-31-30-44 ec2-user]#
    [root@ip-172-31-30-44 ec2-user]# export NVM_DIR="$HOME/.nvm"
    [root@ip-172-31-30-44 ec2-user]# [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
    [root@ip-172-31-30-44 ec2-user]# nvm install 7
    ######################################################################## 100.0%
    Computing checksum with sha256sum
    Checksums matched!
    Now using node v7.10.1 (npm v4.2.0)
    Creating default alias: default -> 7 (-> v7.10.1)
    [root@ip-172-31-30-44 ec2-user]# node --version
    v7.10.1
    

    请注意,将安装上述组件 nvm , node npm 对于根用户。它不会在中添加正确的环境变量 ec2-user 中国的环境。为此,请登录为 那么是哪种类型

    export NVM_DIR="/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    

    或者将此添加到 .bashrc

    它的工作证明(登录为 ec2用户 :

    [ec2-user@ip-172-31-20-26 ~]$ export NVM_DIR="/.nvm"
    [ec2-user@ip-172-31-20-26 ~]$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    [ec2-user@ip-172-31-20-26 ~]$ node --version && npm --version
    v7.10.1
    4.2.0
    

    你可以在你的电脑里自动完成 user-data

    cat <<EOF >> /home/ec2-user/.bashrc
    export NVM_DIR="/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
    EOF
    
    推荐文章