Prerequisites :
Things needed to start
- VPS with Ubuntu 18.04 installed and root access
- Putty or Other SSH Clients
- Nodejs + Expressjs Server
- 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
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:
- Enable and set up firewall
- Configure Nginx to serve static files
- Set up HTTPS with Let’s Encrypt and Certbot
- Configure database
Hello
ReplyDelete