<aside> <img src="https://prod-files-secure.s3.us-west-2.amazonaws.com/c3942e98-5f9b-47e8-8676-2f507eb3b25c/344d0d49-169c-456a-aad9-0015ece6b9ff/Group_2865.png" alt="https://prod-files-secure.s3.us-west-2.amazonaws.com/c3942e98-5f9b-47e8-8676-2f507eb3b25c/344d0d49-169c-456a-aad9-0015ece6b9ff/Group_2865.png" width="40px" /> Home
</aside>
SSH into the VPS:
ssh root@your-vps-ip
Update packages:
apt update && apt upgrade -y
Create a new user:
adduser deploy
usermod -aG sudo deploy
Secure SSH (edit /etc/ssh/sshd_config
):
Port 2222
)PermitRootLogin no
)systemctl restart ssh
Install basic utilities:
apt install unzip curl git ufw -y
Enable Firewall:
ufw allow OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
apt install nginx -y
systemctl enable --now nginx
apt install mysql-server -y
mysql_secure_installation
apt install php8.2-cli php8.2-fpm php8.2-mbstring php8.2-xml php8.2-bcmath php8.2-curl php8.2-zip php8.2-mysql unzip -y
curl -fsSL <https://deb.nodesource.com/setup_18.x> | bash -
apt install -y nodejs
cd /var/www/
git clone <https://github.com/your-repo/laravel-api.git>
cd laravel-api
composer install
cp .env.example .env
php artisan key:generate
mysql -u root -p
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Update .env
:
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=your_password
Run migrations:
php artisan migrate --seed
Create config file:
nano /etc/nginx/sites-available/laravel
Add:
server {
listen 80;
server_name api.yourdomain.com;
root /var/www/laravel-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.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\\\\.ht {
deny all;
}
}
Enable site:
ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
systemctl restart nginx
cd /var/www/
git clone <https://github.com/your-repo/nextjs-frontend.git>
cd nextjs-frontend
npm install
npm run build
Create config file:
nano /etc/nginx/sites-available/nextjs
Add:
server {
listen 80;
server_name yourdomain.com;
root /var/www/nextjs-frontend/.next;
index index.html;
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 site:
ln -s /etc/nginx/sites-available/nextjs /etc/nginx/sites-enabled/
systemctl restart nginx
npm install -g pm2
pm2 start npm --name "nextjs" -- start
pm2 startup
pm2 save
apt install certbot python3-certbot-nginx -y
certbot --nginx -d yourdomain.com -d api.yourdomain.com
Auto-renew:
echo "0 3 * * * root certbot renew --quiet" | tee -a /etc/crontab > /dev/null
apt install supervisor -y
nano /etc/supervisor/conf.d/laravel-worker.conf
Add:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/laravel-api/artisan queue:work --tries=3
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/laravel-worker.log
Enable:
supervisorctl reread
supervisorctl update
supervisorctl start laravel-worker:*
apt install fail2ban -y
/etc/mysql/my.cnf
adjustments)worker_processes auto;
in /etc/nginx/nginx.conf
)Now your Laravel API and Next.js frontend are live on your VPS! 🚀