Member-only story

How to deploy Next.js with Nginx and PM2

Travis Luong
1 min readNov 29, 2021

--

In this tutorial, I will show you how to deploy a Next.js app with Nginx and PM2.

First, launch an Ubuntu 20.04 compute instance with a cloud platform of your choice and ssh into it.

Update packages.

sudo apt-get update

Install nodejs and nginx.

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs nginx

Install pm2.

sudo npm install -g pm2

Create next app. Run this command and follow the prompts.

npx create-next-app@latest

Run the build.

npm run build

Start the process with pm2.

pm2 start "npm start"

Verify the server is running.

curl localhost:3000

Create the Nginx configuration.

sudo nano /etc/nginx/conf.d/default.conf

Paste the following into default.conf. Make sure to replace the IP address.

server {
listen 80;
server_name 123.456.789.10 example.com;
location / {
proxy_pass http://localhost:3000;
}
}

Restart Nginx.

sudo service nginx restart

Go to the ip address in your browser. You should see the Next.js welcome page.

Originally published at https://www.travisluong.com on November 29, 2021.

--

--

No responses yet