Skip to main content

Deploying Express.js Web Application to VPS with Nginx, PM2 and Ubuntu 16.04/18 or Debian



Prerequisites : 

Things needed to start 

  1. VPS with Ubuntu 18.04 installed and root access
  2. Putty or Other SSH Clients
  3. Nodejs + Expressjs Server
  4. Domain with DNS records pointed to your VPS

    1. CREATING USER

     We want to create a new user with “sudo” rights because it’s bad practice to user default    
    root user in production.

    I will create a user with the name algo.
# creating new user
adduser algo
# providing superuser rights
usermod -aG sudo algo
  Log out and log in as your new user. 
sudo apt-get update && sudo apt-get upgrade

   2. Node.js
  We will install Node.js version 10
    
   # this command will download install script with curl
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
# or use wget
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash


Then log out and log back in.
# check if installed
nvm --version
==> 0.33.11
# list available Node.js versions
nvm ls-remote
# choose one version and install it
# example of v10.4.1 installation
nvm install v10.4.1
# check if installed properly
node --version
==> v10.4.1


NVM is a powerful tool, that allows you to have multiple Node versions installed and to switch between them as you like.


Creating a basic server with Express.js
# go to your user's directory
cd /home/algo
# create folder simpleServer
mkdir simpleServer
# go inside
cd simpleServer
# create package.json
npm init -y
# install Express.js
npm install express

Create a file with this text inside it and save it. You can use nano or any other tool using Nano or VIM


# open nano
nano server.js
# to save, press Ctrl + X ==> Y ==> Enter 
# start server
node server.js
==> Listening on http://localhost:3000

If you go to <YourVPSIpAddress>:3000, you should see “Hello World!” inside your browser widow.


3. PM2


If you now close your SSH client widow, your website will stop working. We need some tool that will keep our server “alive”. Say hello to PM2!
# globally install PM2
npm install pm2 -g
# check if installed properly
pm2 -V
==> 2.10.4

PM2 will make our server run in the background and if it crashes, PM2 will restart it automatically.


# start our server with PM2
pm2 start server.js

# check <YourVPSIpAddress>:3000 if your server is running



Make PM2 to start at boot

If we now reboot VPS, our server won’t start up.
pm2 startup
# this will generate another command that you need to run
# We also need to save what processes 
# should get started with pm2
pm2 save
# reboot VPS and check if your website is up
sudo reboot



4. Nginx


Install Nginx:
sudo apt-get install nginx
# check if installed
sudo nginx -v
==> nginx version: nginx/1.14.0 (Ubuntu)



Create a configuration file for your server.


cd /etc/nginx/sites-available

sudo nano simpleServer

Inside this file write this piece of code:


server {
    listen 80;
    server_name <YourVPSIpAddress>;

    location / {
        proxy_pass http://localhost:3000/;
    }
}

 If you have your domain set up, you can do this.


# ...
server_name www.xyz.com xyz.com;
# ...


Final steps
 # check if your configuration is ok
sudo nginx -t
# enable your configuration
sudo ln -s /etc/nginx/sites-available/simpleServer /etc/nginx/sites-enabled
# restart nginx
sudo systemctl restart nginx


Your server should be now available on <Your VPS IP-Address>

 Done?


This is just a basic setup to get you running as soon as possible. For production it’s probably a good idea to do some other things:

  1. Enable and set up firewall
  2. Configure Nginx to serve static files
  3. Set up HTTPS with Let’s Encrypt and Certbot
  4. Configure database

 THANKS & ENJOY !



Comments

Post a Comment

Popular posts from this blog

How to setup eth server for PEATIO EXCHANGE

What do you need? An instance with 4G of ram at least 100 G of hard disk Ubuntu 16.04   Install geth sudo apt-get install software-properties-common sudo add-apt-repository -y ppa:ethereum/ethereum sudo apt-get update sudo apt-get install ethereum Run geth copy the geth service file to /etc/systemd/system/geth.service sudo systemctl start geth sudo systemctl enable geth Install Nginx + fcgi sudo apt install nginx -y fcgiwrap copy default file to /etc/nginx/sites-enabled/default sudo systemctl restart nginx cgi files copy the cgi files to /var/www/html/cgi-bin and update total.cgi with your username sudo chown www-data:www-data -R /var/www/html/cgi-bin sudo chmod +x /var/www/html/cgi-bin/* Install filter service copy total.js to /var/www sudo chown www-data:www-data /var/www/total.js don't forget to edit service.rb with your url sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libr...

Tether(USDT) Installation | Command-line Tutorial

What is USDT Tether’s Omni layer constructed by bitcoin block network. On this overlay network, all parties can issue token, tether company. Token, codenamed USDT, was issued on Omni layer, anchoring in US$1:1. Tether’s idea is very simple. You give me the dollar and give me one dollar. I issue 1 USDT. The dollar is deposited in the designated asset account. Everyone can inquire about it. The total amount of USDT issued can also be inquired on the block. When converting, give me 1 USDT and I return 1 dollar. Omni(USDT) Wallet installation 1.Download Omni Layer Wallet    wget https://bintray.com/artifact/download/omni/OmniBinaries/omnicore-0.3.0-x86_64-linux-gnu.tar.gz ux-gn 2.Decompress & Run    tar - xzvf omnicore - 0.3 . 0 - x86_64 - linux - gnu . tar . gz Now, copy Dependent library to Local sudo CP omnicore-0.3.0/lib/*/lib Open the wallet directory cd omnicore-0.3.0/bin ./omnicored Initial star...

Market Research, Financial Insights, Technology Trends, and Consumer Analytics

This list covers market research, financial insights, technology trends, and consumer analytics . 🔹 General Market Research & Industry Reports   1. Market Research Future - https://www.marketresearchfuture.com/   2. Allied Market Research - https://www.alliedmarketresearch.com/   3. Mordor Intelligence - https://www.mordorintelligence.com/   4. Grand Vie w Research - https://www.grandviewresearch.com/   5. Research and Markets - https://www.researchandmarkets.com/   6. IBISWorld - https://www.ibisworld.com/   7. Statista - https://www.statista.com/   8. Fact.MR - https://www.factmr.com/   9. Transparency Market Research -https://www.transparencymarketresearch.com/   10. Global Market Insights - https://www.gminsights.com/   🔹 Technology & Innovation Research    11. Technavio - https://www.technavio.com/   12. Forrester Research - https://www.forrest...