2

I am new to node js and have written a REST api that works locally on my windows m/c. I want to deploy this to my dev linux box, Can you please let me know how to efficiently do this?

Thanks

Developer
  • 239
  • 1
  • 3
  • 17
  • 1
    Following this tutorial will hep you http://www.codingtricks.biz/run-nodejs-application-apache/ – Ahmed Ghoniem Dec 17 '18 at 03:59
  • This tutorial may help you https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04 – Gopal Joshi Dec 17 '18 at 04:54

4 Answers4

4

You need to follow the following steps:

1. Push code in git

One of the most efficient way to transfer code into remote server is by using git. Push your code into git repository.

git add .
git commit -m "Commit changes in remote"
git push 

2. Login to your server

In windows you can use SSH client application like putty to access to the server. This link will guide you to install and use putty to access your remote server.

3. Install Git and pull your project in server

sudo apt-get install -y git
mkdir projects
chown user_name:group_name -R projects(give user permission)
cd projects
git clone --branch=master https://github.com/myProject.git myProject (here myProject is project name)

The above commands will fetch your source code inside home/projects/myProject folder.

4. Install npm packages

If you do not have npm installed in server. You at first need to install nodejs on the server.

sudo apt-get update
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install nodejs
sudo apt-get install npm

You can see version of nodejs using: 
nodejs --version

You can refer to this link for further details.

Once nodejs is installed, install the node packages of your project.

cd projects/myProject
npm install

Note: If your application depends on other libraries (like mySql, sqlServer or others), you need to install them too.

5. Installing nginx

You can use nodejs own server as well without using nginx but there are advantages of using nginx which is explained in this stackoverflow answer . I will recommend using nginx in production as a reverse proxy. For that, you need to install following dependencies.

sudo apt-get install -y nginx build-essential g++ node-gyp

6. Setting up nginx

After nginx is installed, we need to create another nginx conf file inside

touch /etc/nginx/conf.d/myProject.conf 

Paste the following content in the file myProject.conf

server {
  listen 80;
  server_name your_domain.com;
  location / {
    proxy_pass http://localhost:5555; (this is the port on which your node app runs)
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
   }
}

6. Restarting nginx

sudo nginx -t (to verify if all configuration are correct and error free)
sudo service nginx restart

7. Setting up PM2

If we want to run Nodejs for a long run and not breaking the server in case of errors, we need a process manager like PM2. We need to install it globally.

npm install -g pm2

cd projects/myProject
pm2 start app.js --name="myProject" (note: you can give any name to the process)

In case to save pm2 process when server is restarted, we need to do:

 pm2 save
 pm2 startup

Refer to the link in order to get more details on setting up nginx and pm2.

nirazlatu
  • 983
  • 8
  • 18
  • I know the author hasn't asked this explicitly but: What if the target system has no connection to the internet? – Naxos84 Aug 03 '20 at 12:01
1

Create a Github repo, push your code there. Then clone your repo to your dev linux box. Install dependencies, configure your connections. Voila.

westdabestdb
  • 4,353
  • 20
  • 28
0

You can do it easily using GIT.

In short you have to follow below steps:

  • Install git on your local windows m/c.
  • Create new repository
  • Push the code to the repo
  • Go to ubuntu m/c and install git
  • Take the pull from the repo.
  • Install dependencies
  • Run the code.

Here is detailed tutorial to help you out. https://www.phusionpassenger.com/library/walkthroughs/deploy/nodejs/ownserver/nginx/oss/trusty/deploy_app.html

Harshal Yeole
  • 4,812
  • 1
  • 21
  • 43
0

Normally what i do is :

1. Create a git repo (github.bitbucket/gitlab etc)
2. Push my code to git repo
3. Ssh into my server
4. Clone the code from repo and npm install --production (dont want to install dev dependencies which are actually not needed in production)
5. open up ports on linux machine that the app requires
6. Install additional packages like pm2 or forever that keeps my nodejs app running and run the npm start through those process manager
Rubin bhandari
  • 1,873
  • 15
  • 20