VPS Tutorials
Deploy Laravel 13 on a VPS: Nginx, PHP-FPM, MySQL and Backups (2026)
Deploy Laravel 13 on Ubuntu 24.04 with PHP 8.3-FPM, Nginx, MySQL and HTTPS, then configure permissions, migrations, queues, scheduling, off-site backups and rollback.

Moving a working Laravel application to a VPS often exposes four deployment mistakes: Nginx serves the project root, .env points to the wrong database, storage is not writable, or no persistent process consumes queued jobs. Reinstalling PHP repeatedly rarely fixes these causes of an HTTP 500. This guide uses Ubuntu 24.04, Laravel 13, PHP 8.3-FPM, Nginx and MySQL for a single-server deployment, including HTTPS, migrations, queues, scheduled tasks, backups and rollback. The original guide checked Laravel 13 documentation on October 3, 2026; the official deployment documentation also confirms PHP 8.3 or later. Your project's composer.lock and extension dependencies remain authoritative.
| Workload | Starting point | Watch closely |
|---|---|---|
| Low-traffic website with its database on the same VPS | 2 vCPU / 4 GB RAM / 40 GB SSD | PHP-FPM workers, MySQL buffer pool, logs and backup space |
| Imports, image processing or several queue workers | 4 vCPU / 8 GB RAM / 80 GB SSD | Peak memory and execution time of concurrent jobs |
These are planning examples, not performance guarantees. Test capacity against real requests, database size and peak job concurrency. Expose only the SSH management port and ports 80/443; keep MySQL 3306 local. If the database is not ready, follow the MySQL 8.4 deployment and backup guide first. This guide does not require a public database port.
Run these commands on an Ubuntu 24.04 VPS without another web service already occupying the intended ports. If a hosting panel or existing Nginx site manages the machine, inspect ports 80/443 and its configuration before changing the entry point.
sudo apt update
sudo apt install -y nginx git unzip composer supervisor certbot python3-certbot-nginx mysql-client \
php8.3-fpm php8.3-cli php8.3-mysql php8.3-mbstring php8.3-xml \
php8.3-curl php8.3-zip php8.3-bcmath php8.3-intl php8.3-gd
php -v
composer --version
php -m | grep -E 'mbstring|pdo_mysql|xml|curl'
systemctl status php8.3-fpm --no-pager
Laravel 13 requires at least PHP 8.3. Add extensions for images, PDFs, Redis or other project features according to composer.json. Do not hide missing dependencies with composer install --ignore-platform-reqs.
Create a dedicated deployment user and place source code in /srv/laravel-app. Replace YOUR_REPO_URL with your existing Git repository. For a private repository, prefer a read-only deployment key; do not put a personal access token in shell history.
sudo adduser --disabled-password --gecos '' deploy
sudo install -d -o deploy -g www-data -m 2750 /srv/laravel-app
sudo -u deploy git clone YOUR_REPO_URL /srv/laravel-app
cd /srv/laravel-app
sudo -u deploy composer install --no-dev --prefer-dist --no-interaction --optimize-autoloader
Install composer install from the committed composer.lock. For Vite projects, use a Node.js version satisfying package.json, run npm ci && npm run build, and confirm public/build exists in this release. Alternatively, build assets in CI and deploy them with the source. Avoid ad hoc npm install on the server, which can resolve different dependency versions.
Create a dedicated database and account on the database host with a long randomly generated password. The example grants access only to laravel_app; established deployments can separate runtime credentials from migration credentials.
CREATE DATABASE laravel_app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'laravel_app'@'127.0.0.1' IDENTIFIED BY 'REPLACE_WITH_LONG_RANDOM_PASSWORD';
GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_app'@'127.0.0.1';
Copy the project's .env.example and edit /srv/laravel-app/.env. These are essential settings; configure email, object storage and external APIs according to the application:
APP_NAME=MyLaravelApp
APP_ENV=production
APP_DEBUG=false
APP_URL=https://app.example.com
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=laravel_app
DB_PASSWORD=REPLACE_WITH_LONG_RANDOM_PASSWORD
QUEUE_CONNECTION=database
CACHE_STORE=database
SESSION_DRIVER=database
cd /srv/laravel-app
sudo -u deploy cp .env.example .env
# 用编辑器填入上面的生产配置后,再执行以下命令
sudo chown -R deploy:www-data /srv/laravel-app
sudo find storage bootstrap/cache -type d -exec chmod 2775 {} \;
sudo find storage bootstrap/cache -type f -exec chmod 664 {} \;
sudo chmod 640 .env
sudo -u deploy php artisan key:generate
Generate APP_KEY only on the first deployment. Regenerating it during an update can make encrypted cookies, sessions and application data unreadable. Keep a secure off-server backup of .env and exclude it from Git. Laravel needs write access to storage and bootstrap/cache, not unrestricted permissions throughout the project: avoid chmod -R 777. For locally stored uploads, run sudo -u deploy php artisan storage:link and include storage/app/public in backups.
Check that migrations exist for the chosen queue, cache and session drivers before migrating. New Laravel projects commonly include these tables; older applications may need version-specific migration generation. Inspect the files first:
cd /srv/laravel-app
ls database/migrations
sudo -u deploy php artisan migrate --force
sudo -u deploy php artisan optimize
optimize caches configuration, routes and views. Run it again after changing .env. Application code should call env() only from configuration files; values may be unavailable elsewhere after configuration caching. Back up the database before production migrations, which may not be safely reversible.
Point the A record for app.example.com to the VPS. Save this virtual host in /etc/nginx/sites-available/laravel-app:
server {
listen 80;
server_name app.example.com;
root /srv/laravel-app/public;
index index.php;
charset utf-8;
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options SAMEORIGIN;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ { return 404; }
location ~ /\.(?!well-known).* { deny all; }
}
sudo ln -s /etc/nginx/sites-available/laravel-app /etc/nginx/sites-enabled/laravel-app
sudo nginx -t
sudo systemctl reload nginx
sudo certbot --nginx -d app.example.com
curl -I https://app.example.com/up
sudo certbot renew --dry-run
root must point to the project's public, not /srv/laravel-app. Otherwise .env, source files and dependencies may become publicly accessible. Before certificate issuance, verify DNS and both cloud and host firewalls permit 80/443. Laravel's default /up health route establishes that the framework started; add independent database/cache checks when needed. A 200 from /up does not prove every business dependency works.
Setting QUEUE_CONNECTION=database does not start a consumer. For email, uploads or asynchronous jobs, configure a persistent worker in /etc/supervisor/conf.d/laravel-worker.conf:
[program:laravel-worker]
command=/usr/bin/php /srv/laravel-app/artisan queue:work database --sleep=3 --tries=3 --timeout=60 --max-time=3600
directory=/srv/laravel-app
user=deploy
numprocs=1
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stopwaitsecs=120
redirect_stderr=true
stdout_logfile=/srv/laravel-app/storage/logs/worker.log
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status laravel-worker
cd /srv/laravel-app
sudo -u deploy php artisan queue:failed
Keep --timeout=60 below the queue connection's retry_after to reduce the risk of a job being processed twice. Long-running work requires coordinated adjustments to both settings and Supervisor's stopwaitsecs. Worker code stays in memory, so invoke php artisan reload or queue:restart after every release and let Supervisor restart the process.
The Laravel scheduler needs one trigger per minute. Run crontab -e as the deployment user and add:
* * * * * cd /srv/laravel-app && /usr/bin/php artisan schedule:run >> /srv/laravel-app/storage/logs/scheduler.log 2>&1
Inspect sudo -u deploy crontab -l and sudo -u deploy php artisan schedule:list. Skip scheduler setup if the application has no scheduled tasks. Rotate growing logs; the VPS logrotate guide provides configuration details.
Record the previous commit and database backup location before each release. This single-server sequence assumes a maintenance window. Do not run key:generate again when updating:
cd /srv/laravel-app
git rev-parse HEAD # 记下旧版本
sudo -u deploy php artisan down
sudo -u deploy git fetch --tags
sudo -u deploy git switch --detach YOUR_TESTED_COMMIT_SHA
sudo -u deploy composer install --no-dev --prefer-dist --no-interaction --optimize-autoloader
# 若前端资源在服务器构建:以项目要求的 Node.js 版本执行 npm ci && npm run build
sudo -u deploy php artisan migrate --force
sudo -u deploy php artisan optimize
sudo -u deploy php artisan reload
sudo systemctl reload php8.3-fpm
sudo -u deploy php artisan up
curl -fsS https://app.example.com/up
Back up at least three components: a consistent database dump, encrypted copies of .env/APP_KEY, and uploaded files. Check the client with mysqldump --version. For a separate MySQL 8.4 container, prefer its matching client. Put backup-only MySQL [client] connection settings in /home/deploy/.my.cnf with mode chmod 600; keep passwords out of command arguments. The example assumes this file can connect to laravel_app:
sudo install -d -o deploy -g deploy -m 700 /var/backups/laravel
sudo -u deploy bash -c 'set -o pipefail; mysqldump --defaults-extra-file=/home/deploy/.my.cnf --single-transaction --routines --triggers --events laravel_app | gzip > /var/backups/laravel/db-$(date +%F-%H%M).sql.gz'
sudo -u deploy tar -C /srv/laravel-app -czf /var/backups/laravel/uploads-$(date +%F-%H%M).tgz storage/app/public
Omit the last command if there are no local uploads. Copy archives and the securely encrypted .env to another machine or object storage: a second file on the VPS disk is not an off-site backup. Regularly restore with gunzip -c 数据库备份.sql.gz | mysql --defaults-extra-file=测试库凭据.cnf 测试库名 into an isolated test database, restore uploads and verify key pages. Never use the production database for a restore drill. See the automatic VPS backup guide for retention planning.
For rollback, return to the previous tested commit, install its locked dependencies and assets, rebuild cached configuration, and reload workers. Code rollback does not reverse database changes. Destructive migrations require a compatibility plan or restoration of consistent database and file backups. Do not immediately run migrate:rollback after a failure: it may revert unrelated migrations or lose data.
| Symptom | Inspect first | Common causes |
|---|---|---|
| HTTP 500 | storage/logs/laravel.log, journalctl -u php8.3-fpm | .env, APP_KEY, database connection, permissions or extensions |
| 502 Bad Gateway | nginx -t, PHP-FPM status and Nginx error log | Stopped FPM, incorrect socket or exhausted workers |
| Missing styles | public/build and browser network requests | Vite assets were not built or deployed |
| Email/jobs never run | supervisorctl status, queue:failed | No worker, mismatched queue connection or failed jobs |
| Scheduled tasks do not run | User crontab, schedule:list, scheduler.log | Wrong user/time zone or task failure |
Changing .env has no effect | php artisan about and configuration cache | optimize was not rerun, or env() is called outside configuration files |
References: Laravel 13 deployment, queues and Supervisor, and task scheduling. Before changing production DNS, exercise migrations, HTTPS, queue consumption, backup restoration and code rollback using a test domain.