簡體   English   中英

AWS ubuntu服務器node.js安裝的設置bash腳本失敗

[英]Setup bash script for aws ubuntu server node.js installation fails

我試圖基於腳本編寫一個node.js / MEAN.JS安裝bash腳本,以在AWS Ubuntu服務器上創建自定義node.js開發環境。

到目前為止,一切都很好,但是當我嘗試啟動並推廣腳本(向其中添加MEAN安裝)時,測試開始失敗。 看起來它沒有安裝任何節點程序:

    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

我發布了鏈接,因為代碼很大。 另外,我將其全部開源,因此,如果您想為它貢獻力量,那對我來說很好。

PS管理員和主持人:如果我在“問題”帖子中做錯了任何事情,請不要殺了我,這是我的第一次,請通知我,我會立即進行編輯。


編輯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以下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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM