React.js & Express.js ES6 Build and Deployment
I have been using DigitalOcean for me and setup for my customers, so recommend use it for your project just pick vps depend on how big of your project start at 5$ or 10$, 20$, price very flexibility. DO providing SSD cloud hosting but good price i dont think you can get same price on other providers with same quality. beside their support very fast and well documentation , friendly UI for end-user. so let get start register an account and deploy your app at DigitalOcean.com
Login to your vps with following commandssh root@YOUR-IP-ADDREESS
This is very important step we need to do. so we need to reconfigure our firewall software to allow access to the service
I recommend open port only for 80,443, ssh (port 22), but it is depend on your project may need more port open for other service. in this project we need open port 80 for http access, 443 https (ssl) , and port 22 (for ssh login) that is enough.
apt-get update
sudo apt-get install nginx
By default Firewall is inactive, you can check it by run command sudo ufw status
sudo ufw app list
So let config FW allow those ports by
sudo ufw allow 'Nginx Full'
sudo ufw allow 'OpenSSH'
sudo ufw enable
We are using Node.js for Backend and will serve static files of react application build. So Node.js is required
visit https://nodejs.org/en/download/package-manager/ to see the documentation
We use package management to install, here is command to install Node.js v9
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
sudo apt-get install -y nodejs
After successful Node.js installed we can check the version by typing in command line : node -v
and you see see "v9.3.0"
We are using MongoDB for database in backend restful service so let install Mongodb by follow the documentation https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
Import the public key used by the package management system
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
Create a list file for MongoDB (Ubuntu 16.04)
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
Reload local package database
sudo apt-get update
Install the latest stable version of MongoDB
sudo apt-get install -y mongodb-org
Start MongoDB (Port running default: 27017)
sudo service mongod start
If You Want to Stop MongoDB
sudo service mongod stop
Or Restart MongoDB
sudo service mongod restart
Let me explain to you very simple why we do use Nginx for this Nodejs Web application. Example: Our Chat app when we start it running on port 3000 that is default of Nodejs application running. we can change the port to 3001 or 3002, or 8080 … so if you pointed your domain tabvn.com to Degital Ocean cloud vps so this case we can visit the chat app in http://tabvn.com:3000 so we need just see nodejs web app in default port 80 such at http://tabvn.com , that is why we use Nginx.
To install Nginx visit the documentation from official http://nginx.org/en/linux_packages.html
So we will run following command on Ubuntu Cloud Hosting 16.04
apt-get update
sudo apt-get install nginx
Start Nginx: open your IP address ex: http://123.456.789 you should see “Welcome to nginx!”, all Nginx config in our cloud is in /etc/nginx/nginx.conf
sudo nginx
Stop Nginx
sudo nginx -s stop
Reload Nginx
nginx -s reload
sudo apt-get install build-essential
Nginx Websocket document: http://nginx.org/en/docs/http/websocket.html
server { listen 80; root /var/www/html; location / { proxy_pass http://127.0.0.1:3001; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }}