linux - 为 aws ubuntu 服务器 node.js 安装设置 bash 脚本失败

标签 linux node.js bash amazon-web-services mean-stack

我正在尝试编写基于脚本的 node.js/MEAN.JS 设置 bash 脚本,以在 AWS Ubuntu 服务器上创建自定义 node.js 开发环境。

到目前为止一切顺利,但是当我尝试启动并概括脚本(向其添加 MEAN 安装)时,测试开始失败。看起来它没有安装任何 Node 程序:

    Testing Basic dependencies:
    git                           OK
    gcc                           OK
    g++                           OK
    autoconf                      OK
    automake                      OK
    libtool                       OK
    python                        OK
    make                          OK

    Testing Basic command line editor installations:
    rlwrap                        OK
    emacs                         OK
    nano                          OK

    Testing Node.JS installations:
    node                          missing
    nvm                           missing
    npm                           missing
    heroku                        OK
    foreman                       OK

    Testing MEAN JS Stack installations:
    mongo                         OK
    mongod                        OK
    mean                          missing
    bower                         missing
    grunt                         missing

另外,当我尝试执行例如 node 时,我收到以下错误:

    $ node
    The program 'node' can be found in the following packages:
     * node
     * nodejs
    Try: sudo apt-get install <selected package>

奇怪的是,当执行 bash 脚本时,它似乎安装一切正常。我一直在环顾四周,找不到为什么会这样,解决方案应该是什么?它与 $PATH 有什么关系吗?

setup.sh源码:

    #!/bin/bash

    #Added this usefull function from a Stack Overflow post:
    #Link: http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
    function coloredEcho(){
        local exp=$1;
        local color=$2;
        if ! [[ $color =~ '^[0-9]$' ]] ; then
           case $(echo $color | tr '[:upper:]' '[:lower:]') in
            black) color=0 ;;
            red) color=1 ;;
            green) color=2 ;;
            yellow) color=3 ;;
            blue) color=4 ;;
            magenta) color=5 ;;
            cyan) color=6 ;;
            white|*) color=7 ;; # white or invalid color
           esac
        fi
        tput setaf $color;
        echo $exp;
        tput sgr0;
    }

    #Base installation function:
    #Is from:
    #Link: https://github.com/startup-class/setup/blob/master/setup.sh

    function baseInstall() {
        # Simple setup.sh for ISMAT School Project
        cd
        apt-get install -y curl

        coloredEcho "Installing project structure:" blue

        # Installing dependencies:
        sudo apt-get update
        sudo apt-get install -y git
        sudo apt-get install -y gcc g++ autoconf automake libtool
        sudo apt-get install -y python-software-properties python make
        sudo apt-get install -y build-essential libssl-dev

        # Install jshint to allow checking of JS code within emacs
        # http://jshint.com/
        npm install -g jshint

        # Install rlwrap to provide libreadline features with node
        # See: http://nodejs.org/api/repl.html#repl_repl
        sudo apt-get install -y rlwrap

        # Install emacs24
        # https://launchpad.net/~cassou/+archive/emacs
        sudo add-apt-repository -y ppa:cassou/emacs
        sudo apt-get -qq update
        sudo apt-get install -y emacs24-nox emacs24-el emacs24-common-non-dfsg

        # Install nvm: node-version manager
        # https://github.com/creationix/nvm
        curl https://raw.github.com/creationix/nvm/master/install.sh | sh

        # Load nvm and install latest production node
        source $HOME/.nvm/nvm.sh
        source ~/.profile
        nvm ls-remote
        nvm install v0.10.26
        nvm use v0.10.26

        # git pull and install dotfiles as well
        cd $HOME
        if [ -d ./dotfiles/ ]; then
            mv dotfiles dotfiles.old
        fi
        if [ -d .emacs.d/ ]; then
                mv .emacs.d .emacs.d~
        fi
        git clone https://github.com/cmpsoares91/dotfiles.git
        ln -sb dotfiles/.screenrc .
        ln -sb dotfiles/.bash_profile .
        ln -sb dotfiles/.bashrc .
        ln -sb dotfiles/.bashrc_custom .
        ln -sf dotfiles/.emacs.d .

        sudo apt-get update
    }

    install=1

    if [ $1 == server ] ; then
        coloredEcho "Starting Server Instalation..." red

        # Instaling Base:
        baseInstall

        # Install Heroku toolbelt
        # https://toolbelt.heroku.com/debian
        wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

        # All extra stuff for server add here

        coloredEcho "Server Setup Ready" red

    elif [ $1 == dev ] ; then
        coloedEcho "Starting Developer Instalation..." green

        # Instaling Base:
        baseInstall

        coloredEcho "Developer Setup Ready" green

    else
        coloredEcho "No selection made." blue
        install=0
    fi

    if [ $install == 1 ] ; then 
        coloredEcho "Install MEAN JavaScript Stack as well? (y/n)" magenta
        a=1
        while [ $a == 1 ]
        do
            a=0
            read meanOption
            if [ $meanOption == y ] ; then
                coloredEcho "Starting MEAN JavaScript Stack Instalation..." red

                #Add MEAN.io installation commands...

                #Installing MongoDB:
                sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
                echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
                sudo apt-get update
                sudo apt-get install -y mongodb-org

                #Installing Bower:
                npm install bower

                #Installing MEAN.io:
                sudo npm install -g meanio@latest

                #Start App Right away?
                coloredEcho "--> Want to initiate mean App now? (y/n)" red
                c=1
                while [ $c == 1 ]
                do
                    c=0
                read appOption
                    if [ $appOption == y ] ; then
                        coloredEcho "----> Enter App name:" red
                        read myApp
                            mean init $myApp
                            cd $myApp && npm install
                            grunt
                    elif [ $appOption == n ] ; then
                        #Nothing Happens...
                        coloredEcho "--> Not initiation App..." red
                    else
                        coloredEcho "--> Please enter y or n:" red
                        c=1
                    fi
                done
            elif [ $meanOption == n ] ; then
                #Nothing Happens...
                coloredEcho "Not installing MEAN..." red
            else
                coloredEcho "Please enter y or n:" magenta
                a=1
            fi
        done

        g=1
        while [ $g == 1 ]
        do
            g=0
            coloredEcho "Do you want to install Grunt.JS? (y/n)" green
            read gruntOption
            if [ $gruntOption == y ] ; then
            # Install Grunt for automated node builds
            # http://gruntjs.com/getting-started for details
            npm install -g grunt-cli
        elif [ $gruntOption == n ] ; then
                #Nothing Happens...
                    coloredEcho "--> Not Installing Grunt.js..." green
            else
                    coloredEcho "--> Please enter y or n:" green
                    g=1
            fi
        done
    fi

test.sh源码:

    #!/bin/bash

    #Bash Script for installation testing
    #Link: http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script

    echo "Testing Basic dependencies:"
    for cmd in "git" "gcc" "g++" "autoconf" "automake" "libtool" "python" "make" ; do
      printf "%-30s" "$cmd"
      if hash "$cmd" 2>/dev/null; then printf "OK\n"; else printf "missing\n"; fi
    done

    echo -e "\nTesting Basic command line editor installations:"
    for cmd in "rlwrap" "emacs" "nano"; do
      printf "%-30s" "$cmd"
      if hash "$cmd" 2>/dev/null; then printf "OK\n"; else printf "missing\n"; fi
    done

    echo -e "\nTesting Node.JS installations:"
    for cmd in "node" "nvm" "npm" "heroku" "foreman"; do
      printf "%-30s" "$cmd"
      if hash "$cmd" 2>/dev/null; then printf "OK\n"; else printf "missing\n"; fi
    done

    echo -e "\nTesting MEAN JS Stack installations:"
    for cmd in "mongo" "mongod" "mean" "bower" "grunt"; do
      printf "%-30s" "$cmd"
      if hash "$cmd" 2>/dev/null; then printf "OK\n"; else printf "missing\n"; fi
    done

完整的源代码都可以在这里获得:https://github.com/cmpsoares91/Node.js-Project-Setup

我发布链接是因为代码很大。另外,我将它全部开源,所以如果你想为它做贡献,我没问题。

附言管理员和版主:如果我在问题帖中做错了什么,请不要杀了我,这是我的第一次,请通知我,我会立即编辑。


编辑 1:

已安装的 nvm 显示:

    [ubuntu@domU-12-31-39-0A-28-86:~]$nvm

    Node Version Manager

    Usage:

    (...)

但是执行测试的时候还是显示缺失。 node npm mean bower and grunt 继续保持缺失和不可执行状态。

编辑 2:在 Louis 的评论后添加了源代码,感谢您的见解!

最佳答案

我找到了导致问题的原因,在一两种情况下,它与我忘记添加 sudo 这一事实有关,但不是大部分。

问题是 nvm 实际上并没有完全安装 node.js、npm 等。所以我必须像这样通过 apt-get install node 进行这些安装:

# Installing last node and npm version
sudo apt-add-repository -y ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install -y nodejs

在此之后应使用 `sudo npm install [package] 命令完成其他安装。

代码最终看起来像这样:

    #!/bin/bash

    #Added this usefull function from a Stack Overflow post:
    #Link: http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
    function coloredEcho(){
       (...)
    }

    #Base installation function:
    #Is from:
    #Link: https://github.com/startup-class/setup/blob/master/setup.sh

    function baseInstall() {
        # Simple setup.sh for ISMAT School Project
        cd
        sudo apt-get install -y curl

        coloredEcho "Installing project structure:" blue

        # Installing dependencies:
        sudo apt-get update
        sudo apt-get install -y git
        sudo apt-get install -y gcc g++ autoconf automake libtool
        sudo apt-get install -y python-software-properties python make
        sudo apt-get install -y build-essential libssl-dev
        sudo apt-get install -y software-properties-common

        # Install emacs24
        # https://launchpad.net/~cassou/+archive/emacs
        sudo add-apt-repository -y ppa:cassou/emacs
        sudo apt-get -qq update
        sudo apt-get install -y emacs24-nox emacs24-el emacs24-common-non-dfsg

        # Install rlwrap to provide libreadline features with node
        # See: http://nodejs.org/api/repl.html#repl_repl
        sudo apt-get install -y rlwrap

        # Installing last node and npm version
        sudo apt-add-repository -y ppa:chris-lea/node.js
        sudo apt-get update
        sudo apt-get install -y nodejs
        # Still Testing if this is necessary:
        # ln -s /usr/bin/nodejs /usr/bin/node

        # Install nvm: node-version manager
        # Link: https://www.npmjs.org/package/nvm
        sudo npm install -g nvm
        sudo export PATH=./node_modules/.bin:$PATH

        # Install jshint to allow checking of JS code within emacs
        # http://jshint.com/
        sudo npm install -g jshint

        # git pull and install dotfiles as well
        cd $HOME
        if [ -d ./dotfiles/ ]; then
            mv dotfiles dotfiles.old
        fi
        if [ -d .emacs.d/ ]; then
                mv .emacs.d .emacs.d~
        fi
        git clone https://github.com/cmpsoares91/dotfiles.git
        ln -sb dotfiles/.screenrc .
        ln -sb dotfiles/.bash_profile .
        ln -sb dotfiles/.bashrc .
        ln -sb dotfiles/.bashrc_custom .
        ln -sf dotfiles/.emacs.d .

        sudo apt-get update
    }

    install=1

    if [ $1 == server ] ; then
        coloredEcho "Starting Server Instalation..." red

        # Instaling Base:
        baseInstall

        # Install Heroku toolbelt
        # https://toolbelt.heroku.com/debian
        wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

        # All extra stuff for server add here

        coloredEcho "Server Setup Ready" red

    elif [ $1 == dev ] ; then
        coloedEcho "Starting Developer Instalation..." green

        # Instaling Base:
        baseInstall

        coloredEcho "Developer Setup Ready" green

    else
        coloredEcho "No selection made." blue
        install=0
    fi

    if [ $install == 1 ] ; then 
        coloredEcho "Install MEAN JavaScript Stack as well? (y/n)" magenta
        a=1
        while [ $a == 1 ]
        do
            a=0
            read meanOption
            if [ $meanOption == y ] ; then
                coloredEcho "Starting MEAN JavaScript Stack Instalation..." red

                #Installing MongoDB:
                sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
                echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
                sudo apt-get update
                sudo apt-get install -y mongodb-org

                #Installing Bower:
                sudo npm install -g bower

                #Installing MEAN.io:
                sudo npm install -g meanio@latest

                #Start App Right away?
                coloredEcho "--> Want to initiate mean App now? (y/n)" red
                c=1
                while [ $c == 1 ]
                do
                    c=0
                read appOption
                    if [ $appOption == y ] ; then
                        coloredEcho "----> Enter App name:" red
                        read myApp
                        mean init $myApp
                        cd $myApp && npm install
                    elif [ $appOption == n ] ; then
                        #Nothing Happens...
                        coloredEcho "--> Not initiation App..." red
                    else
                        coloredEcho "--> Please enter y or n:" red
                        c=1
                    fi
                done
            elif [ $meanOption == n ] ; then
                #Nothing Happens...
                coloredEcho "Not installing MEAN..." red
            else
                coloredEcho "Please enter y or n:" magenta
                a=1
            fi
        done

        g=1
        while [ $g == 1 ]
        do
            g=0
            coloredEcho "Do you want to install Grunt.JS? (y/n)" green
            read gruntOption
            if [ $gruntOption == y ] ; then
            # Install Grunt for automated node builds
            # http://gruntjs.com/getting-started for details
            sudo npm install -g grunt-cli
        elif [ $gruntOption == n ] ; then
                #Nothing Happens...
                    coloredEcho "--> Not Installing Grunt.js..." green
            else
                    coloredEcho "--> Please enter y or n:" green
                    g=1
            fi
        done
    fi

现在测试已经完成,所有程序都按预期运行。 测试输出:

    $ bash ./test.sh
    Testing Basic dependencies:
    git                           OK
    gcc                           OK
    g++                           OK
    autoconf                      OK
    automake                      OK
    libtool                       OK
    python                        OK
    make                          OK

    Testing Basic command line editor installations:
    rlwrap                        OK
    emacs                         OK
    nano                          OK

    Testing Node.JS installations:
    node                          OK
    nvm                           OK
    npm                           OK
    heroku                        OK
    foreman                       OK

    Testing MEAN JS Stack installations:
    mongo                         OK
    mongod                        OK
    mean                          OK
    bower                         OK
    grunt                         OK

关于linux - 为 aws ubuntu 服务器 node.js 安装设置 bash 脚本失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23189968/

相关文章:

linux - csh 内联数学

linux - ELF入口域和实际程序入口

python - 如何移动或调整 X11 窗口的大小(即使它们已最大化)?

javascript - Socket.io broadcast.to 不工作但 broadcast.emit 不工作

bash - 如何使用 GNU 并行在 BASH 上运行带有多个参数的 python 脚本?

linux - Firefox 崩溃后,Web 内容进程仍在内存中

node.js - 未充分利用的 CPU 是否可以进行 CPU 阻塞?

javascript - hapi.js Cors Pre-flight 不返回 Access-Control-Allow-Origin header

bash - 获取第一个命令行参数并传递其余参数

python - 使用 shell=True w/list 时忽略 subprocess.call() 参数