what are my options if i want to deploy nextjs apps on my vps, but i dont want to use docker or pm2. Additionally, i might use the bun runtime instead of nodejs for the nextjs apps, and i might also run some separate apps/scripts via bun runtime
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Of course! This is an excellent way to gain a deeper understanding of how your server works. Moving away from abstractions like Docker and even process managers like PM2 forces you to interact with the core components of a Linux system.
Your primary and most robust option is to use systemd, which is the standard init system and service manager on virtually all modern Linux distributions (Ubuntu, Debian, CentOS, etc.). It's powerful, reliable, and built-in.
Here’s a complete breakdown of your options, with a focus on the systemd approach.
The Recommended Path: systemd + a Reverse Proxy (Nginx or Caddy)
This combination is the industry-standard way to run "bare metal" applications.
systemd will act as your process manager. It will be responsible for starting your Next.js/Bun app, restarting it if it crashes, and ensuring it starts on server boot.
A Reverse Proxy (like Nginx or Caddy) will sit in front of your app. It will handle incoming public traffic on ports 80 (HTTP) and 443 (HTTPS), manage SSL certificates, and forward the requests to your application, which will be running on an internal port like 3000.
Step-by-Step Guide for systemd
Let's assume:
Your user is myuser.
Your app is located in /home/myuser/my-next-app.
You are using Bun.
1. Prepare Your Next.js App for Production
First, make sure your app is built and ready to be served.
bash
# Navigate to your app directory
cd /home/myuser/my-next-app
# Install dependencies
bun install
# Build the production version of your app
# This creates the .next directory
bun run build
# (Optional but Recommended) Use the Standalone Output
# Edit next.config.mjs to include output: 'standalone'.
# This bundles only the necessary files into a .next/standalone folder,
# making your deployment smaller and more isolated.
# Then run `bun run build` again.
The command to start your app will be bun run start. This will typically start a server on http://localhost:3000.
2. Create a systemd Service File
You'll create a unit file that tells systemd how to manage your application.
Create the file using nano or vim:
bash
sudo nano /etc/systemd/system/nextjs-app.service
Paste the following content into the file. Pay close attention to the paths and user.
ini
[Unit]
Description=My Next.js Application
After=network.target
[Service]
# User and Group to run the app as. NEVER use root.
User=myuser
Group=myuser
# Directory where your app is located
WorkingDirectory=/home/myuser/my-next-app
# Command to start the app. Use the full path to bun.
# Find it with `which bun` (e.g., /home/myuser/.bun/bin/bun)
ExecStart=/home/myuser/.bun/bin/bun run start
# Required for Bun/Node to find the correct path
Environment="PATH=/home/myuser/.bun/bin:/usr/bin"
# What to do if the process crashes
Restart=always
# How long to wait before restarting
RestartSec=10
# Standard output and error logging will be handled by journalctl
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Key Directives Explained:
Description: A human-readable name for your service.
After=network.target: Don't start this service until the network is ready.
User/Group: Crucial for security. Create a dedicated, non-root user for your app.
WorkingDirectory: The context from which your app will run.
ExecStart: The exact command to execute. Using the full path to the bun executable is essential.
Restart=always: This is your replacement for PM2's auto-restart feature.
StandardOutput=journal: This tells systemd to pipe all your app's logs (console.log, errors, etc.) into the system's centralized logging system, which you can access with journalctl.
3. Manage Your New Service
Now you can use the systemctl command to control your app.
bash
# Reload systemd to recognize the new service file
sudo systemctl daemon-reload
# Enable the service to start on boot
sudo systemctl enable nextjs-app.service
# Start the service immediately
sudo systemctl start nextjs-app.service
# Check the status of the service
sudo systemctl status nextjs-app.service
To view the logs for your app:
bash
# View live logs
sudo journalctl -u nextjs-app.service -f
# View the last 100 lines of logs
sudo journalctl -u nextjs-app.service -n 100
This setup works perfectly for any separate Bun scripts as well. Just create a new .service file for each script you want to run as a background process.
Step 4: Set Up the Reverse Proxy
You need a way for the outside world to reach your app on localhost:3000.
Option A: Nginx (Powerful and Popular)
Install Nginx:
bash
sudo apt update
sudo apt install nginx
Configure Nginx: Create a new configuration file for your site.
# Create a symbolic link to enable the config
sudo ln -s /etc/nginx/sites-available/your-domain.com /etc/nginx/sites-enabled/
# Test the Nginx configuration for errors
sudo nginx -t
# Restart Nginx to apply changes
sudo systemctl restart nginx
# Use Certbot to get a free SSL certificate from Let's Encrypt
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
Option B: Caddy (Simple and Automatic HTTPS)
Caddy is a fantastic, modern alternative known for its simplicity.
Install Caddy: (Follow the official instructions for your OS)
That's it! Caddy will automatically provision and renew an SSL certificate for you.
Other (Less Recommended) Alternatives
If for some reason you can't or don't want to use systemd, here are other options:
1. supervisord
A process control system written in Python. It's older than systemd but still very reliable and a solid choice if your distro doesn't have systemd (which is very rare today).
Pros: Very stable, good logging, web UI available.
Cons: Not built-in, requires installing and configuring a separate service (supervisor).
2. nohup & & (Not for Production)
You can run a command in the background and detach it from your terminal session.
bash
nohup bun run start > app.log 2>&1 &
nohup: Prevents the process from being killed when you log out.
> app.log 2>&1: Redirects both standard output and standard error to a file.
&: Puts the process in the background.
Why this is bad for production:
No automatic restart: If your app crashes, it stays dead.
No startup on boot: If the server reboots, you have to start it again manually.
Hard to manage: Finding the process ID to kill it is a manual task.
3. screen or tmux (Definitely Not for Production)
These are terminal multiplexers. You can start a session, run your app, and then "detach" from the session, leaving it running on the server.
Why this is even worse for production:
All the same problems as nohup.
Prone to user error (accidentally closing the session instead of detaching).
Not designed for running daemons/services.
Summary of Your Best Option
For deploying Next.js (with Node or Bun) and other scripts on a VPS without Docker or PM2, the definitive, most professional solution is:
Process Management: Use systemd to run, monitor, and restart your applications.
Web Serving: Use Nginx or Caddy as a reverse proxy to handle public traffic and SSL.
Workflow:
Push/pull your code with Git.
Run bun install and bun run build.
Use sudo systemctl restart my-app.service to apply the new build.