Thursday, March 13, 2025

Hosting Your Next.js Website on a Linux Server with Nginx

Hi there! Today, we're going to dive deep into hosting a Next.js website on a Linux server using the ever-popular web server—Nginx. Whether you're just getting started with Next.js or already have your site ready to go live, this step-by-step guide will make sure you're all set up and running smoothly.

What You'll Learn:

  • How to set up your Linux server
  • Deploying a Next.js application
  • Configuring Nginx as a reverse proxy
  • Securing your Next.js website with SSL (HTTPS)
  • Troubleshooting common issues

Let’s get started!


Step 1: Setting Up Your Linux Server

We'll assume you already have a Linux server, either a VPS from providers like DigitalOcean, Linode, or AWS, or your own dedicated server. Log in using SSH:

ssh username@your_server_ip

Once logged in, ensure everything is up-to-date:

sudo apt update
sudo apt upgrade -y

Install some necessary packages:

sudo apt install curl git ufw

Configure Firewall (Optional but Recommended):

Allow SSH, HTTP, and HTTPS traffic through the firewall:

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

Confirm firewall status:

sudo ufw status

Step 2: Install Node.js and npm

Next.js relies on Node.js, so install Node.js using NodeSource:

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

Confirm installation:

node -v
npm -v

Step 3: Deploy Your Next.js Application

Clone your app

Let's clone your app from GitHub or another repository:

git clone https://github.com/yourusername/your-nextjs-app.git
cd your-nextjs-app

Install dependencies and build

npm install
npm run build

Test your app locally

Run your app to verify it's working correctly:

npm start

Visit your server IP on port 3000:

http://your_server_ip:3000

You should see your Next.js site running perfectly.


Step 4: Keep Your Next.js App Running with PM2

Your app needs to keep running after closing your SSH connection. PM2 is excellent for this:

Install PM2 globally:

sudo npm install pm2@latest -g

Start your app using PM2:

pm2 start npm --name "nextjs" -- start

Save the PM2 process list and configure it to run at system boot:

pm2 save
pm2 startup systemd

Now your Next.js application will run continuously!


Step 5: Install and Configure Nginx

Install Nginx:

sudo apt install nginx

Configure Nginx as a Reverse Proxy:

Create a new configuration file for your site:

sudo nano /etc/nginx/sites-available/nextjs_site

Add the following configuration:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    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;
    }
}

Enable your Nginx configuration:

sudo ln -s /etc/nginx/sites-available/nextjs_site /etc/nginx/sites-enabled/

Test your configuration:

sudo nginx -t

Reload Nginx to apply changes:

sudo systemctl reload nginx

Now your site is available via http://yourdomain.com.


Step 6: Secure Your Site with SSL (HTTPS) using Certbot

SSL certificates encrypt data and secure your site, essential for trust and SEO.

Install Certbot:

sudo apt install certbot python3-certbot-nginx

Obtain and Install an SSL certificate:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Follow the interactive prompts. Certbot automatically configures Nginx to redirect HTTP to HTTPS.

Test automatic renewal:

sudo certbot renew --dry-run

Step 7: Troubleshooting Common Issues

App Not Running?

Check PM2 status:

pm2 status

Restart your app if needed:

pm2 restart nextjs

Nginx Errors?

Check Nginx logs:

sudo tail -f /var/log/nginx/error.log

Correct any reported errors.


Congratulations!

Your Next.js website is now up, secure, and accessible from anywhere. Hosting your website yourself provides flexibility and deep control, helping your project shine exactly as you envisioned.

No comments:

Post a Comment

Featured Posts

Hosting Your Next.js Website on a Linux Server with Nginx

Hi there! Today, we're going to dive deep into hosting a Next.js website on a Linux server using the ever-popular web server—Nginx. Whet...

Trending Posts