Deploying a Node.js project to Linode
To deploy a Node.js app to Linode, follow the instructions below:
-
Sign up for a Linode account
-
Click on Create Linode
-
Choose Ubuntu LTS for the distribution
-
Choose a Region closest to you
-
In the Linode Plan section, choose Shared CPU and then choose Nanode 1GB.
-
Create a Root Password
-
Add an SSH key or choose one from your existing keys
-
Click Create Linode
-
Wait for the Linode to show Running
-
Copy the Linode SSH Access, paste it in the terminal and run it
-
When it prompts you asking -> "Are you sure you want to continue connecting (yes/no/fingerprint)?", type "yes"
-
Now, run the following commands ->
apt-get update
apt-get upgrade
apt-get install npm
npm i -g n
n install lts
exit -
Restart the server by entering your SSH Access. The server will now be using the latest node version
-
Make sure your latest project code is pushed to a GitHub repository.
-
Create a SSH key on your server and copy and upload to GitHub.
-
Git clone the repository using SSH.
-
cd into your project and run
npm install
-
(optional) If you want to copy the .env file from your local machine to the remote server, run the following command:
scp <location_of_file_in_local_machine>/.env root@<server_ip_address>:~/<location_of_project>
You can add a -r flag to the command to copy an entire folder. -
Run the node project to ensure it's running correctly.
-
Then run the following commands:
npm i -g pm2 // install pm2
pm2 start <entry_file_name>.js -n <project_name> // start the project
pm2 startup ubuntu // to restart API by itself, in case of crash
apt-get install nginx // install nginx for reverse-proxy
cd ~/etc/nginx/sites-available
nano default
-
In this file make the following changes:
-
a. At "server*name *;" replace the underscore with your domain name(with and without www prefix) or server IP address.
-
b. In the "location /" block, remove the existing code and add the following:
location / {
proxy_pass http://localhost:5000;
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;
} -
c. [Ctrl] + S to save the file. [Ctrl] + X to exit the file.
-
-
Run
nginx -t
to check if the changes are correct. -
Restart nginx by running
systemctl restart nginx
. -
cd ~
and thenufw enable
to enable the firewall. -
Run
ufw allow ssh
,ufw allow http
andufw allow https
to allow using these methods through the firewall. The firewall restricts access trough other ports like 5000. -
Run
snap install --classic certbot
to install the Let's Encrypt certificate manager. -
Run
ln -s /snap/bin/certbot /usr/bin/certbot
to link the certificate manager to the /usr/bin/certbot folder. -
Setup Domain on Linode by ->
- Go to Domains tab in sidebar.
- Create a new domain.
- In the "Domain" section, type the domain name.
- Provide an email address.
- In Insert Default Records, select Insert Default Records from one of my Linodes
- Select the Linode you want to use.
- Click Create Domain
- Copy the NS(name servers) and add them to your domain name provider (example: namecheap) as Custom DNS
- Click Save
- Check if your domain (without https) now shows your project.
-
Run
certbot --nginx
-
(optional) Run
certbot renew --dry-run
to check if the SSL certificate gets renewed successfully. -
Reload your domain to check if it is now secure.
You have now successfully deployed your project to a Linode with a SSL certificate.