Certainly! Here’s a step-by-step guide to deploying a Laravel + React application on a typical VPS (like Hetzner or Hostinger). This assumes you have root or sudo access to your server.
1. Prepare Your Server
Install Required Software:
- PHP (Laravel requirement)
- Composer
- Node.js & npm (React and Laravel Mix)
- Nginx or Apache
- MySQL/PostgreSQL (if needed)
- Git
Example for Ubuntu (adjust versions as needed):
sudo apt update
sudo apt install nginx php php-fpm php-mysql php-xml php-mbstring php-curl php-zip unzip git curl
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
2. Upload Your Code
You can use git clone or SFTP to upload your project:
cd /var/www
git clone https://github.com/yourusername/your-laravel-react-app.git
cd your-laravel-react-app
3. Install Dependencies
Backend (Laravel):
composer install --no-dev --optimize-autoloader
cp .env.example .env
php artisan key:generate
Edit .env for your production database and settings.
Frontend (React):
If your React app is inside resources/js (using Laravel Mix):
npm install
npm run build
If it's a separate React app (e.g., in /frontend), build it and copy the static files to Laravel's public directory.
4. Set Permissions
sudo chown -R www-data:www-data /var/www/your-laravel-react-app
sudo chmod -R 775 storage bootstrap/cache
5. Configure Web Server
Nginx Example:
server {
listen 80;
server_name yourdomain.com;
root /var/www/your-laravel-react-app/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:/var/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Reload Nginx:
sudo systemctl reload nginx
6. Set Up Environment
- Set up your database and update
.env. - Run migrations:
php artisan migrate --force - Set up queues, cache, etc. as needed.
7. SSL (Optional but recommended)
Use Let's Encrypt for free SSL:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx
8. Final Steps
- Test your app in the browser.
- Set up supervisor for queues if needed.
- Set up cron for Laravel scheduler:
* * * * * cd /var/www/your-laravel-react-app && php artisan schedule:run >> /dev/null 2>&1
References:
If you need a more specific guide for your setup (e.g., React as SPA in /frontend), let me know!