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

问题描述 投票:0回答:5

我尝试在 AWS EC2 linux 中使用

NodeJS
安装
nvm
,如下所示在用户数据中:

#!/bin/bash

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

实例成功创建后,我进入并检查了我的 ec2 实例,当我输入

nodejs
nvm
时,没有安装
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
node.js amazon-web-services amazon-ec2 user-data
5个回答
11
投票

正如日志所解释的,

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 是 /) 我在 Amazon Linux 上的交互式会话中测试了上述内容。

$ ssh [email protected]
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

请注意,上面将为 root 用户安装

nvm
node
npm
。它不会在
ec2-user
的环境中添加正确的 ENV VAR。为此,请以
ec2-user
身份登录,然后输入

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

或将其添加到

ec2-user
.bashrc

它有效的证明(登录为

ec2-user
:

[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

1
投票

如果我们使用amazon linux,请尝试安装

nvm
版本16

#!/bin/bash
sudo su
cd ~
amazon-linux-extras install nginx1 -y 
systemctl enable nginx
systemctl start nginx
touch ~/.bashrc
cat > /tmp/startup.sh << EOF
echo "Setting up NodeJS Environment"
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.0/install.sh | bash
echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-user/.bashrc
echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-user/.bash_profile
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm' >> /home/ec2-user/.bashrc
. /home/ec2-user/.nvm/nvm.sh
. /home/ec2-user/.bash_profile
. /home/ec2-user/.bashrc
# Install NVM, NPM, Node.JS & Grunt
nvm install 16
nvm ls
EOF
chown ec2-user:ec2-user /tmp/startup.sh && chmod a+x /tmp/startup.sh
sleep 1; su - ec2-user -c "/tmp/startup.sh"

1
投票

花了几个小时进行锻炼后,这对我很有效。

#!/bin/bash
touch ~/.bashrc
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
source ~/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install --lts

0
投票

我已经尝试了本文中提到的其他解决方案,但唯一对我有帮助的是使用以下命令安装 Node.js

#!/bin/bash
curl -sL https://rpm.nodesource.com/setup_16.x | bash -
yum install -y nodejs

0
投票

对我有用的是将 $HOME 设置为“~”,因为运行脚本时它是空的,但 nvm 安装需要它。

export HOME=~

然后继续正常安装nvm

#!/bin/bash
export HOME=~
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 18

结果是nvm以root权限安装在/root目录下

[ec2-user@ip-172-31-39-39 ~]$ sudo su
[root@ip-172-31-39-39 ec2-user]# ls -l /root/.nvm
total 152
drwxr-xr-x. 3 root root     32 Dec  8 18:16 alias
-rw-r--r--. 1 root root   2299 Dec  8 18:16 bash_completion
-rwxr-xr-x. 1 root root    371 Dec  8 18:16 nvm-exec
-rw-r--r--. 1 root root 143991 Dec  8 18:16 nvm.sh
drwxr-xr-x. 3 root root     18 Dec  8 18:16 versions
© www.soinside.com 2019 - 2024. All rights reserved.