When you create an instance, you will be asked to download ssh keys and assign ipv4 address.
ssh -i ./<private_ssh_key> <username>@<ipaddress>
Example:ssh -i ./ssh-private.key ubuntu@192.168.0.1
sudo apt update sudo apt upgrade
you can verify if you already have node.js installed in your system by running the below commandnode -v
If you want to install the other version than 18.x, follow this link.
Run the below command to install node.jscurl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - &&\ sudo apt-get install -y nodejs
Node.js and NPM should be installed by now. You can verify by running the command
node -v; npm -v
Create a project directory and run the below commandnpm init -y npm install express
Create a file named **app.js** at the same location, and paste below contentconst express = require('express');
const app = express();
app.use(express.json());
app.get('/', (req,res) => {
return res.json({message: 'Welcome to Express Project');
});
app.listen(3000, console.log('APP running on PORT 3000'));
I have hard-coded PORT number for the shake of simplicity, you can use
dotenvpackage to get those details from environment file.
Modify your 'package.json' according to sample shown below:{
"name": "express-project",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"keywords": [],
"author": "",
"license":"ISC",
"dependencies": {
"express": "^4.18.2"
}
}
Test your project by running below commandnpm start
You should see the console log; Terminate the process by pressing
CTRL+C
Install PM2 to run your node application in backgroundsudo npm i pm2 -g
Run your app.js with pm2pm2 start app.js
You can test your application is up on not by running curl commandcurl localhost:3000
Go to Compute-> Instance -> Instance Details and click on subnet name on right panel
Click on Add Security List
Click on Add Ingres List
Save Ingres Rules
nginx to reverse proxy your project port to HTTP.npm i nginx
Once installed, you can check if it's running or not by running below commandsudo systemctl status nginx
firewalld and enable HTTP and HTTPS service in linux firewall.sudo apt install firewalld
Run below command to enable http and https service and reload the firewallsudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
Once done, you can go to your public IP address in your browser, you can see the
nginxhomepage.
nginx to proxy to your applicationsudo vi /etc/nginx/sites-available/default
Change the file configuration as shown belowserver_name _; location / { proxy_pass http://localhost:3000; }
Save the file and restart nginxsudo systemctl restart nginx
Once done, you can go to your public IP address in your browser, you can see your express js project running! Congratulations for coming this far.
Go to your domain name provider, and in your domain name DNS configuration, add your details as sample shown in below image
It will take few minutes or hours to update. After that, you can now try going to domain name instead of your ip address, and you can still open the same express server!
letsencrypt using certbotFollow the steps from official site to install certbot on your instance. or you can follow along with me.
sudo snap install core;sudo snap refresh core sudo apt-get remove certbot sudo snap install --classic certbot sudo ln -s /snap/bin/certbot /usr/bin/certbot
Once done, change your server name in /etc/nginx/sites-available/default as shown belowserver_name your_domain_name www.your_domain_name;
Run below command to add SSH to your nginx configuration file automaticallysudo certbot --nginx
Enter the details, and congratulations, you have the SSL certificate installed on your server for your domain.
Happy Coding ❤️