# Laravel on VPS: Installation, Nginx and Deployment

> Complete Laravel VPS setup guide: PHP-FPM, Nginx vhost, .env config, php artisan migrate, Supervisor for queues, cron for scheduler.

## Laravel Requirements on a VPS

Laravel 11.x requires PHP 8.2+ and the extensions BCMath, Ctype, cURL, DOM, Fileinfo, JSON, Mbstring, OpenSSL, PCRE, PDO, Tokenizer, XML. You also need Composer, Node.js (for npm) and a database (MySQL/MariaDB or PostgreSQL).

## Installing Dependencies

```
sudo apt update && sudo apt install -y php8.2-fpm php8.2-cli php8.2-mysql \
    php8.2-mbstring php8.2-xml php8.2-curl php8.2-zip php8.2-bcmath \
    php8.2-intl php8.2-redis nginx mysql-server git unzip

# Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
```

## Installing Laravel

```
cd /var/www
composer create-project laravel/laravel myapp
cd myapp
chmod -R 775 storage bootstrap/cache
chown -R www-data:www-data .
```

## Configuring .env

```
cp .env.example .env
php artisan key:generate

nano .env
```

```
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myapp_db
DB_USERNAME=myapp_user
DB_PASSWORD=StrongPassword123
```

## Nginx vhost for Laravel

```
sudo nano /etc/nginx/sites-available/myapp
```

```
server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/myapp/public;
    index index.php;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
```

```
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
```

## Migrations and Build

```
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
npm install && npm run build
```

## Supervisor for Laravel Queues

```
sudo apt install supervisor
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
```

```
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/myapp/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/log/laravel-worker.log
```

```
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
```

## Cron for the Laravel Scheduler

```
sudo crontab -u www-data -e
# Add:
* * * * * cd /var/www/myapp && php artisan schedule:run >> /dev/null 2>&1
```

**Production optimization:** Run `php artisan optimize` after every deploy — it caches the config, routes and views, which speeds up responses by 30–50%.

## Real Errors After Deploying Laravel

### 419 Page Expired

Happens when the CSRF token falls out of sync with the session: most often because of a wrong `SESSION_DOMAIN` in `.env` behind a reverse proxy, or because `APP_KEY` was never generated after copying the project to a new server.

### Cannot open storage/logs/laravel.log

```
The stream or file "/var/www/myapp/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied
```

PHP-FPM runs as `www-data`, while the `storage` folder is often left after a deploy with the permissions of whichever user ran `git pull` or `composer install`. Permissions need to be set after every code update, not just once during the initial install.

### A blank white screen instead of a page

With `APP_DEBUG=false`, Laravel shows visitors a generic "500 \| Server Error" page with zero technical detail — correct behavior for production, but the real cause needs to be found not on screen but in `storage/logs/laravel.log`.

## When This Setup Does Not Fit

- The default file drivers for sessions and cache store data on a single server's disk — scaling to several instances behind a load balancer requires Redis or a database as shared storage.
- `numprocs=2` in Supervisor is sized for a light queue — for heavy jobs (mass mailings, report generation), two workers quickly become too few, and the queue starts backing up with no warning in the logs.
- Running `php artisan queue:work` directly, without Supervisor, kills every job currently being processed on each deploy — Supervisor with `stopwaitsecs` gives the worker time to finish its current job cleanly.

## Verifying the Result

```
# The application's configuration and environment read correctly
php artisan about

# The queue workers are actually running
sudo supervisorctl status

# A live tail of the application log during a test request
tail -f storage/logs/laravel.log
```

`php artisan about` shows the PHP version, the environment, and the cache status for config and routes all in one table — faster than gathering the same information by hand from a dozen files. For queues on top of [Redis](https://www.zevshost.net/en/kb/php-sessii-v-redis/), see the dedicated article on session and cache driver setup.

---

Source: https://www.zevshost.net/en/kb/laravel-nastroika-vps-nginx/
Updated: 2026-09-30
