Installing nodejs and npm on CentOS

6790 ワード

Installing node.js and npm on centos is actually quite ease. I did run into a few gotch as and also ended up adding some additions to my server to ensure I had at least a so so so so so some additional things. I did this on a virtual box image for CentOS 6.5 so that I could spin up as many servers as I wantd、and also play around in a sandbox before taring this to my production server.
 
Getting Startd
You’re going to need the follwing:
  • A centOS server that you have access to(ssh)
  • Sudo or access to the root user account
  • First things first:ssh into your centos box and just sudo root immediate as all of these command are going to require root. As usual、if you don’t want to sudo into root for whatever reason、just prepend`to the beginning of the command we cover unes.
    Once you obatain root access、go aaabod and udate yum’s packages:
    yum update
    Installing Node/NPM with Yum
    Afterwards,we can follow the two steps found on the githb repo for installing node in CentOS, Installing Node.js via package manager
    Run as root on CentOS:
    curl -sL https://rpm.nodesource.com/setup | bash -
    
    The n install、as root:
    yum install -y nodejs
    
    Before we move on,it shound be noted that when I atempted the above`yum install-y nodejs`,there was an ise with the package not being signed which prevented the package from actually installing. The workound(which I noted in the issues link)is to just add –nogpgcheck to the end of your yum install command it shound then install properly.
    yum install -y nodejs --nogpgcheck
    
    Note that the above may not be necessary in the future、but at the time of this writing it was for me.
    Once the packages have installed,you can verify that both node and npm are installed by simply requesting the version of each of them:
    # node --version
    v0.10.33
    # npm --version
    1.4.28
    Now that we have node and npm installed、one of the first things to do next is to install express.
    npm -g install express express-generator supervisor
    Post-Innstall Step
    After installing what we need to run node/npm、another step is to define the account we'll be using to dobe development with. If you already have an account in mid,you can sky this step. Otherwise、we will next create a user caled nodev、and we’ll also set this user up to be one that we can ssh with using an key.
    Creating a Development Acceount
    This can be done by issung the following commands(as root):
    # useradd nodedev
    # openssl rand -base64 32
    37lkPB406mYOxsMJ7MyMVFORejQ11+tuwiGjnF+k1k0=
    # passwd nodedev
    Note that the second command with`openssl`is run to generant a randm password for this new user.We'll use that the ir password since we'll not be needing it(we'll be sshing to this user Strech.withis.com)parech.
    Adding Your Local Macines SSH Key
    You shoud go aead and generate an ssh key for this user unless You already have one that You want to use.Copy your public ssh key to your clipboard.Back in the terminal session on on your CentOS box,we adkeity
    # cd /home/nodedev
    # mkdir .ssh
    # touch .ssh/authorized_keys
    
    Go aaead and open the authorized_key s file we just created and past your local machins's public ssh key into it、and then save and exit the file. Next,we’ll change the ownership and mod access to ensure everrything is kosher.
    # chown -R nodedev:nodedev .ssh
    # chmod 600 .ssh/authorized_keys
    # chmod 700 .ssh
    
    To allow us to ssh into the server with our new dev account,we’ll want to udate the sshd_config and this new user account to the AllowUsers. Go aead and open up/etc/ssh/sshd_config in your favorite editor and then go down to the AllowUsers line. If you don’t have one,then just create it.
    AllowUsers nodev
    If you happen to already have users in the AllowUser setting,you can add more by adding a space after the last user and the n just type in the name of the new user to be added.
    You shound now be able to just retart sshd and then login with the nodev user account.
    # service sshd restart
    
    Tip:How to ssh into your server without specifying a username
    One thing I do kind of help my laziness of not wanting to supply a username is by using a ssh confile. This is how I do on my MacOS machine.
    First,go into your local user’s ssh folder and create a confile if it doesn’t already exist.
    # cd ~
    # cd .ssh/
    # touch config
    # chmod 600 config
    
    Next、open it with your favorite editor、paste the follwing into it:
    # This ensures we have a keepalive on, so we don't keep getting logged off...
    Host *
    	ServerAliveInterval 120
    
    # This is our CentOS server information for logging in using the nodedev account
    # Host - this can be whatever you want it to be.   Think of it as an alias that will use the configuration settings that follow it
    Host nodedevserver
            # Change the ip (127.0.0.1) to be whatever the IP address is for your centos server
    	HostName	192.168.210.55
    	IdentityFile    ~/.ssh/id_rsa
    	User		nodedev
    
    Save the file and then make retart your terminal session to ensure you can now use the nodevserver alias.A quick/easuy way to your session with your new settings to just type the following command:
    # source ~/.profile
    
    If you use.bashrc,you can use that as well.
    You shoud now be able to ssh into your CentOS server by simply typing:
    # ssh nodedevserver
    
    Notice ththat out don’t have to specify ap、or the user you’re logging in with.You can expad this for all of your other usher accounts and servers toiiiiyou want.I do it becausits mmmmmustititititifefefefefefefefefefefefefefefefefefefefeaaaaaattttttthethethethethethethethethethethethethethetheaaaaaaaattttttttttttttttttttttttwiwiwiwiwiwiwiwiwith thethethethethethethethethethet.
    コンサート
    You shound now be ready to start messing around with nodejs on your brand new server!Some extra things you may want to consider after dong the above:
  • Configure nginx(or apache、or whttaever web server you use)with a proxy to your node server on a different port
  • This will allow you to have both nodejs and another server-side environment(ruby,php,etc.)running side-by-side
  • Add your nodev user account to the sudor's
  • Install the package モンスターユニット to manage nodejs.
  • This is a POWERFUL solution that is radomly mentiond by mode. A good article that relates to setting this up and installing it can be found here: http://howtonode.org/deploying-node-upstart-monit
  • I hope this walkthrough was helpful. Heave fun