Deploy Laravel + Next JS in VPS
laravelnextjsvpsdeploymentnginx

Deploy Laravel + Next JS in VPS

A complete guide to deploying a Laravel API and Next.js frontend on a VPS with Nginx, PM2, PHP 8.3, MySQL, and SSL.

Author logo

Khaled Saifullah

12 min read

For a production setup, I recommend:

Domain
│
├── example.com        → Next.js Frontend
│
└── api.example.com    → Laravel Backend API

Server stack:

Ubuntu 24.04
Nginx
PHP 8.3
MySQL
Node.js
PM2
Certbot SSL

Step 1: Login to VPS

From Hostinger panel: VPS → Manage → SSH Access You will get: IP Address: xxx.xxx.xxx.xxx Username: root Password:

Connect using SSH:

ssh root@YOUR_SERVER_IP

Example:

ssh root@123.45.67.89

Step 2: Update Ubuntu

apt update
apt upgrade -y

Install basic tools:

apt install curl wget git unzip nano -y

Step 3: Point Domain to VPS

Go to your domain DNS management. Create these records:

Main Website (Next.js)
Type: A
Name: @
Value: YOUR_VPS_IP
TTL: 3600

Example:
example.com → 123.45.67.89

API Subdomain (Laravel)

Create:
Type: A
Name: api
Value: YOUR_VPS_IP
TTL: 3600

Result:
api.example.com → 123.45.67.89

Wait 5-30 minutes. Check:

ping example.com
ping api.example.com

Step 4: Install Nginx

apt install nginx -y

Check:

systemctl status nginx

Open firewall:

ufw allow 'Nginx Full'
ufw enable

Step 5: Install PHP for Laravel

Install PHP 8.3:

apt install software-properties-common -y
add-apt-repository ppa:ondrej/php
apt update

Install:

apt install php8.3 php8.3-fpm php8.3-mysql php8.3-cli php8.3-curl php8.3-mbstring php8.3-xml php8.3-zip php8.3-gd -y

Check:

php -v

Step 6: Install MySQL

apt install mysql-server -y

Secure:

mysql_secure_installation

Login:

mysql

Create database:

CREATE DATABASE laravel_db;

CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'StrongPassword';

GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';

FLUSH PRIVILEGES;

EXIT;

Step 7: Deploy Laravel Backend

Create folder:

mkdir -p /var/www/api

Go there:

cd /var/www/api

Clone your project:

git clone YOUR_GITHUB_REPO .

Example:

git clone https://github.com/user/project.git .

Install Composer:

apt install composer -y

Install packages:

composer install --no-dev --optimize-autoloader

Copy environment:

cp .env.example .env

Edit:

nano .env

Change:

APP_NAME=Laravel
APP_ENV=production
APP_DEBUG=false

APP_URL=https://api.example.com

DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=StrongPassword

Generate key:

php artisan key:generate

Run migration:

php artisan migrate --force

Storage:

php artisan storage:link

Permission:

chown -R www-data:www-data /var/www/api
chmod -R 755 /var/www/api/storage

Step 8: Configure Laravel Nginx

Create:

nano /etc/nginx/sites-available/api.example.com

Paste:

server {

listen 80;

server_name api.example.com;


root /var/www/api/public;


index index.php index.html;


location / {

try_files $uri $uri/ /index.php?$query_string;

}


location ~ \.php$ {

include snippets/fastcgi-php.conf;

fastcgi_pass unix:/run/php/php8.3-fpm.sock;

}


location ~ /\.ht {

 deny all;

}

}

Save.

Enable:

ln -s /etc/nginx/sites-available/api.example.com /etc/nginx/sites-enabled/

Test:

nginx -t

Restart:

systemctl restart nginx

Step 9: Install Node.js

Install Node 22:

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

Check:

node -v
npm -v

Step 10: Deploy Next.js Frontend

Create:

mkdir -p /var/www/frontend

Go:

cd /var/www/frontend

Clone:

git clone YOUR_FRONTEND_REPO .

Install:

npm install

Create env:

nano .env.production

Example:

NEXT_PUBLIC_API_URL=https://api.example.com

Build:

npm run build

Test:

npm start

Stop using CTRL+C.

Step 11: Install PM2

npm install pm2 -g

Start Next:

pm2 start npm --name frontend -- start

Save:

pm2 save

Auto start:

pm2 startup

Copy the generated command and run it.

Step 12: Configure Nginx for Next.js

Create:

nano /etc/nginx/sites-available/example.com

Add:

server {

listen 80;

server_name example.com www.example.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:

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Restart:

nginx -t
systemctl restart nginx

Step 13: Add SSL Certificate (HTTPS)

Install Certbot:

apt install certbot python3-certbot-nginx -y

For frontend:

certbot --nginx -d example.com -d www.example.com

For API:

certbot --nginx -d api.example.com

Test renewal:

certbot renew --dry-run

Step 14: Laravel Queue (If Needed)

If your project uses jobs:

Install supervisor:

apt install supervisor -y

Create:

nano /etc/supervisor/conf.d/laravel-worker.conf

Add:

[program:laravel-worker]

command=php /var/www/api/artisan queue:work

directory=/var/www/api

autostart=true

autorestart=true

user=www-data

Restart:

supervisorctl reread
supervisorctl update
supervisorctl start laravel-worker

Final Structure

/var/www
├── api
│   └── Laravel
│
└── frontend
    └── Next.js

Your URLs: Frontend: https://example.com Backend API: https://api.example.com

Important VPS Optimization

VPS usually has: 1 CPU 4GB RAM 50GB SSD Recommended:

apt install htop -y
monitor:
htop
enable swap:
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

This prevents Next.js builds from crashing.

Last updated on 04/08/2026