# Effortless Deployment: Launching a Node.js  App on Ubuntu Server

First of all to deploy an application on a VPS (virtual private server) you will need a server. To get one for free you may tried out [Digital Ocean](https://m.do.co/c/7561cd0f40ba) droplets by redeeming benifits of [Github Student Pack](https://www.digitalocean.com/github-students).

So let's begin the process!

# Connect to your remote server

To connect to your vps from your local machine follow below steps.

```bash
ssh root@<VPS_IP>
## Update your server
sudo apt update && sudo apt upgrade -y
```

Install dependencies to run your application (Node.js in our case) and nginx to expose your application to the internet.

```bash
sudo apt install -y nodejs npm nginx
```

# Clone your application from Github

Assuming you have your application code published on github, clone the repository in your vps. If you want to use FTP to import code in your server, consider using tools like Filezilla.

```bash
git clone https://github.com/alpha951/Awasar-Job-Tracker
```

# Run your application

Install all the basic dependencies for your application and update environment variables.

```bash
vi .env  ## creating a .env file to update env credential
npm install 
npm start  # App starts at port 3000
```

At this point our application is running at port 3000 but if we try to access the app from our local machine at `<your-vps-ip>:3000` you won't be able to access this. Since by default only port 80 is exposed to the internet.

We need to expose our port 3000 to the internet in order to make our application live on internet. To this we will use nginx as a proxy server.

# Setting up nginx

```bash
sudo nano /etc/nginx/sites-available/our-app
## paste this config in our-app file
server {
  listen 80;
  server_name YOUR_IP_ADDRESS;
  location / {
    proxy_pass http://localhost:3000;
    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;
  }
}
```

Create a symbolic link to enable the configuration:

```bash
sudo ln -s /etc/nginx/sites-available/our-app /etc/nginx/sites-enabled/
```

Chect the Nginx configuration for any syntax errors:

```bash
sudo nginx -t
```

You must see something like this :

![image](https://doimages.nyc3.cdn.digitaloceanspaces.com/dev-center-tutorials/deploy-next-js-to-digitalocean-droplet/check-nginx-config.png align="left")

Now run below command to restart nginx:

```bash
sudo service nginx restart
```

Now finally our application is available on internet. But wait, we are running our application using a terminal blocking command like `npm start` so if some reason the process gets terminated then our app will stop running. We need to use a process manager like `pm2` to make sure our app always up and running.

# Setting Up PM2 Process Manager

```bash
sudo npm install -g pm2
cd <our-app-location>
pm2 start npm --name "awasar-app" -- start
```

This command will start the Node.js application with the name “awasar-app” using the npm start command. PM2 will automatically restart the application if it crashes or if the server reboots.

To ensure PM2 starts on boot, run:

```bash
pm2 startup
pm2 save
```

You may check status of all process running by `pm2` as below:

```bash
pm2 status 
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717952129195/42b1969d-3502-4371-88e2-5eef18fa64ee.png align="center")

You may use commands like these to control your application

```bash
pm2 stop our-app # stop running the app
pm2 restart our-app # restart the app
pm2 logs our-app # shows the terminallog of running app 
```

So that was it for today!
