When I first started using Next.js, I loved how smooth it felt to build websites with it. But when it came time to put my project online, I hit a wall. I wasn’t sure how to deploy it, especially on a Linux server. If you’re like me, you might feel stuck too. But don’t worry—this guide will walk you through the process in a simple, clear way.
We’ll go step-by-step, and I’ll explain new words as we go. I’ll also share some tips that helped me. By the end, you’ll be able to run your Next.js app on your own server.
What Is Next.js?
Let’s start with the basics.
Next.js is a free and open-source web framework built on top of React. React is a popular tool that helps you build user interfaces—what people see and use on a website.
Next.js makes React even better. It lets you do things like:
- Build pages ahead of time, so they load faster
- Create server-side code, like APIs
- Optimize images and performance easily
If you’ve built a Next.js project on your own computer, the next step is moving it online so others can see it. That’s where deployment comes in. Deployment means putting your app on a server so anyone can open it in their browser.
What Is a Linux Server?
A Linux server is a computer that runs the Linux operating system. It’s often used to host websites because it’s free, fast, and secure. You don’t usually plug in a screen or keyboard to it—it runs in the cloud or at a data center.
When you rent one from a company (like Hetzner or DigitalOcean), they give you a way to connect using a tool called SSH. SSH (short for Secure Shell) is like a remote control for your server. It lets you send commands as if you were sitting in front of it.
What You Need Before Starting
Here’s what you should have ready:
- A completed Next.js app on your computer
- Access to a Linux server (with Ubuntu or Debian works best)
- A domain name (optional but helpful)
- Some basic knowledge of using the terminal
If you don’t have these yet, you can still read along. It helps to see how everything fits together.
Step-by-Step: How I Deploy My Next.js App
Let me walk you through exactly what I do when I deploy a Next.js app on a Linux server. It’s not too hard once you know the steps.
1. Connect to Your Server
I usually use this command from my terminal:
ssh username@your-server-ip
Replace username
with something like root
or whatever your host gave you. Replace your-server-ip
with the actual IP address of your server.
If this is your first time, it will ask if you want to trust the server. Type “yes”, then enter your password.
Now you’re in.
2. Install Node.js and npm
Node.js is the runtime that runs your Next.js app. npm helps manage packages or tools your app uses.
I use this to install Node.js:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
You can check the version with:
node -v
npm -v
3. Upload Your Project
There are a few ways to move your project to the server. I like using scp (Secure Copy).
From your local machine, run:
scp -r your-nextjs-folder username@your-server-ip:/home/username/
This copies your project folder to the server.
Once it’s done, you can cd
into your project folder:
cd your-nextjs-folder
4. Install Project Dependencies
Inside your project folder, run:
npm install
This installs everything your app needs to run.
5. Build the App
Next.js apps need to be built before they can run in production.
Use this:
npm run build
This creates an optimized version of your app inside a folder called .next
.
6. Start the App
To start the app, use:
npm start
But this is only temporary. If you close the terminal, your app stops running. So, I use a process manager like PM2.
Install PM2 with:
npm install -g pm2
Then run:
pm2 start npm --name "next-app" -- start
This keeps your app running in the background—even if you log out.
Optional: Serve Behind Nginx
I like using Nginx to make my app easier to access and more secure. Nginx is a web server that can forward traffic to your app.
To install it:
sudo apt install nginx
Here’s a simple config for Nginx:
server {
listen 80;
server_name 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;
}
}
Replace yourdomain.com
with your actual domain.
Save the file in /etc/nginx/sites-available/nextjs
and link it to sites-enabled
:
sudo ln -s /etc/nginx/sites-available/nextjs /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Now your app is available at your domain.
Why Use a Linux Server?
You might wonder, “Why not just use a platform like Vercel?” That’s a good question.
I use Linux servers when:
- I want full control over the app
- I have more than one app to host
- I don’t want to depend on a third-party service
There’s nothing wrong with Vercel or Netlify. They’re great for beginners. But sometimes, going the manual route teaches you more and gives you flexibility.
Common Mistakes and How to Avoid Them
Here are a few things I’ve learned the hard way:
- Forget to install Node.js
Your app won’t run if Node.js isn’t installed. - Start the app without PM2
It stops when you close the terminal. PM2 keeps it alive. - Skip the build step
Next.js needs to be built before it works in production.
Knowing these helped me fix problems faster.
Tips to Make Deployment Easier
- Keep a copy of your
.env
file with secret keys in a safe place - Use Git to keep track of changes in your project
- Set up a firewall (like
ufw
) to protect your server
Here are three things I do after every deployment:
- Check the logs
Usepm2 logs
to see if the app is working or crashing. - Restart services
If something feels off, restart Nginx or PM2. - Test on another device
Make sure your site works on mobile and desktop.
Final Thoughts
Deploying Next.js on a Linux server takes some time at first. But once you’ve done it, it becomes second nature. You’ll understand more about how the web works and feel more in control.
I hope this guide helped break things down clearly. If you’re still unsure about anything, take it slow. Try one step at a time. And don’t be afraid to search for answers or ask for help.
Have you tried deploying a site before? What challenges did you face?
Let me know—I’d love to hear your story.