<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


File Structure

installation

Setup

APIs Setup

Website Basics

VPS Setup

Contact us


</aside>

VPS Hosting Guide for Laravel API, Next.js, and MySQL

1. Setting Up Your VPS

Choose a VPS Provider

Initial Server Setup

  1. SSH into the VPS:

    ssh root@your-vps-ip
    
    
  2. Update packages:

    apt update && apt upgrade -y
    
    
  3. Create a new user:

    adduser deploy
    usermod -aG sudo deploy
    
    
  4. Secure SSH (edit /etc/ssh/sshd_config):

  5. Install basic utilities:

    apt install unzip curl git ufw -y
    
    
  6. Enable Firewall:

    ufw allow OpenSSH
    ufw allow 80/tcp
    ufw allow 443/tcp
    ufw enable
    
    

2. Install Dependencies

Install Nginx

apt install nginx -y
systemctl enable --now nginx

Install MySQL

apt install mysql-server -y
mysql_secure_installation

Install PHP for Laravel

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

Install Node.js for Next.js

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

3. Setting Up Laravel API

Clone and Configure Laravel

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

Configure MySQL

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

Configure Nginx for Laravel

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

4. Setting Up Next.js Frontend

Clone and Build

cd /var/www/
git clone <https://github.com/your-repo/nextjs-frontend.git>
cd nextjs-frontend
npm install
npm run build

Configure Nginx for Next.js

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

Run Next.js with PM2

npm install -g pm2
pm2 start npm --name "nextjs" -- start
pm2 startup
pm2 save

5. SSL Setup with Let's Encrypt

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

6. Process Management

Laravel Queue (Supervisor)

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:*

7. Security & Performance


Now your Laravel API and Next.js frontend are live on your VPS! 🚀