Ask AI
English

Installing a Laravel application on a VPS, step by step

15 min read

A Laravel application starts up in a matter of minutes on a developer machine, but on a production server, you’ll need to set up the web server, PHP-FPM, the database, permissions, HTTPS, background tasks and the scheduler yourself. In this guide, we’ll walk you through the entire process on a fresh Ubuntu 24.04 LTS server, from logging in to the first production update. If you don’t have a server yet, our AMD Epyc-based KVM VPS plans start from €4.62 per month plus VAT, with root access, an NVMe SSD and daily backups.

If you’d rather not deal with server management, our managed VPS service takes care of it for you: Linux maintenance, web server configuration, security hardening and, if required, the CI/CD process too.

What will you need?

  • A VPS running Ubuntu 24.04 LTS. A small Laravel application, together with its database, fits comfortably on 2 GB of RAM (AMD VPS 2). If you’re building the frontend on the server as well, or running Redis and multiple queue workers, the 4 GB plan provides a more comfortable margin. The plan can be upgraded later; our article on choosing the right VPS plan will help you choose.
  • A domain whose A record (and, if you have IPv6, AAAA record) points to the VPS’s IP address. This is required for the HTTPS certificate, so it’s worth setting it up right from the start so that the DNS has been updated by the time you get there.
  • The application’s source code is stored in a Git repository (GitLab, GitHub or your own Git server).

In the examples, the domain is pelda.hu, the user is deploy, and the application directory is /var/www/pelda.hu. Replace these with your own details.

Which version of Laravel and PHP? Laravel 13 (March 2026) requires at least PHP 8.3 and supports versions 8.3–8.5; Laravel 12 supports PHP 8.2–8.5. The required extensions are ctype, cURL, DOM, Fileinfo, Filter, Hash, Mbstring, OpenSSL, PCRE, PDO, Session, Tokenizer and XML. In this guide, we’ll be installing PHP 8.4, which will receive security updates until the end of 2028. We’ve covered what’s new in the latest 8.5 branch in our article on the new features of PHP 8.5.

1. Logging in via SSH and updating the system

You’ll receive your VPS login details by email after placing your order. Log in as root (you’ll find the details for Windows and macOS in our article on connecting to a VPS via SSH), and update the system:

ssh root@203.0.113.10
apt update && apt upgrade -y

2. Your own user account and firewall

Don’t work as root every day. Create a user with sudo privileges and copy your SSH key from your own machine to that user:

adduser deploy
usermod -aG sudo deploy
# on your own machine:
ssh-copy-id deploy@203.0.113.10

Next, enable the firewall and only allow SSH, HTTP and HTTPS. Be sure to add the OpenSSH rule before enabling the firewall, otherwise you’ll lock yourself out:

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

From this point onwards, work as the deploy user, using sudo. For more details on user management, see the article on creating a user with sudo privileges; for more on the firewall, see the article on configuring the VPS firewall. If key-based authentication is working, it is advisable to disable password-based and root login as well; the steps are described in the article on securing your SSH server.

3. Installing Nginx

sudo apt install -y nginx

After installation, when you enter the server’s IP address into your browser, you should see the Nginx default page.

4. PHP 8.4-FPM and the required extensions

Ubuntu 24.04’s own repository contains PHP 8.3. This is sufficient for Laravel 13, and if you choose this option, simply replace 8.4 with 8.3 in the commands below and omit the PPA. For a newer version, use Ondřej Surý’s PPA, which comes from the maintainer of the Debian PHP packages and is available for both Ubuntu 22.04 and 24.04. (The maintainer is gradually moving the packages to the packages.sury.org repository, so please check the PPA page for the latest instructions before installing.) On Debian 13, PHP 8.4, and on Ubuntu 26.04 LTS, PHP 8.5 are already available in the distribution’s own repository; no external repository is required there.

sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

sudo apt install -y php8.4-fpm php8.4-cli php8.4-common php8.4-opcache \
  php8.4-mysql php8.4-mbstring php8.4-xml php8.4-curl php8.4-zip \
  php8.4-bcmath php8.4-intl php8.4-gd unzip git acl

The php8.4-common package contains the ctype, fileinfo, tokenizer and PDO extensions, while php8.4-xml contains DOM and XML. zip and unzip are required by Composer, while acl is needed for permissions (see below). For PostgreSQL, install the php8.4-pgsql package instead of php8.4-mysql. For PHP 8.5, omit the php8.5-opcache package, as OPcache is already part of PHP in that version.

Check the version and extensions:

php -v
php -m | grep -iE 'ctype|curl|dom|fileinfo|mbstring|openssl|pdo|tokenizer|xml'
systemctl status php8.4-fpm

5. Composer

Install Composer using the official installer with a checksum:

cd ~
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
    echo 'ERROR: the checksum does not match'
else
    php composer-setup.php --quiet
fi
rm composer-setup.php

If the checksum does not match, the installer will not run; in this case, please try again. After successful installation, add Composer to your system path:

sudo mv composer.phar /usr/local/bin/composer
composer --version

6. Database: MariaDB

sudo apt install -y mariadb-server
sudo mariadb-secure-installation

In the security script, delete the anonymous users and the test database, and disable remote root access. Then create a separate database and user for the application:

sudo mariadb
CREATE DATABASE laravel CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'a-long-strong-password-here';
GRANT ALL PRIVILEGES ON laravel.* TO 'laravel'@'localhost';
FLUSH PRIVILEGES;
EXIT;

The application should never connect as the root user. If you are using PostgreSQL, use the postgresql package and the createuser and createdb commands to create a separate user and database in the same way. Our MariaDB and PostgreSQL articles discuss the differences between the two systems.

7. Installing the application

Create the directory and clone the repository into it:

sudo mkdir -p /var/www/pelda.hu
sudo chown deploy:deploy /var/www/pelda.hu
git clone git@gitlab.com:ceg/alkalmazas.git /var/www/pelda.hu
cd /var/www/pelda.hu

For private repositories, first generate an SSH key on the server (ssh-keygen -t ed25519), and add the public key to the repository as a ‘deploy’ key with read-only access.

Install the dependencies without the development packages, using the optimised autoloader, and create the .env file:

composer install --no-dev --optimize-autoloader
cp .env.example .env
php artisan key:generate

The key:generate command writes the value of APP_KEY to the .env file. Do not change this key later, as encrypted data and sessions are linked to it. Next, edit the .env file (nano .env):

APP_NAME="Example"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://pelda.hu

LOG_CHANNEL=daily

DB_CONNECTION=mariadb
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=a-long-strong-password-here

SESSION_DRIVER=database
CACHE_STORE=database
QUEUE_CONNECTION=database

APP_DEBUG=false is mandatory in production: if enabled, the error page may display configuration values, and even passwords, to visitors. New Laravel applications use the database by default for sessions, caching and queues; this is sufficient for getting started, but for higher workloads, Redis is the faster option.

Run the migrations and create the public/storage link to the uploaded files:

php artisan migrate --force
php artisan storage:link

In production, migrate requires confirmation, while --force skips this. If the application uses Vite to build CSS and JS files, you’ll need Node.js on the server to run the npm ci && npm run build command, or you can carry out the build on your own machine or via CI and upload the public/build directory.

8. Permissions

Laravel needs to write to the storage and bootstrap/cache directories, and two users do this: PHP-FPM as www-data, and Artisan commands as deploy. If only one of them is granted write permissions, sooner or later a 500 error will occur due to a log file created by the other. With ACL, you can permanently grant both users the necessary permissions, including for newly created files:

cd /var/www/pelda.hu
sudo setfacl -R -m u:www-data:rwX -m u:deploy:rwX storage bootstrap/cache
sudo setfacl -dR -m u:www-data:rwX -m u:deploy:rwX storage bootstrap/cache
chmod 640 .env
sudo chgrp www-data .env

The rest of the code should remain under the deploy user’s ownership; the web server should only read it. Never use chmod -R 777: this does not solve anything that ACLs cannot solve, but it does grant write access to anyone. The basics of permissions are covered in the article explaining how to use chmod and chown.

9. Nginx server block

Create the file /etc/nginx/sites-available/pelda.hu. This is based on the example given in the Laravel documentation:

server {
    listen 80;
    listen [::]:80;
    server_name pelda.hu www.pelda.hu;
    root /var/www/pelda.hu/public;

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

    index index.php;
    charset utf-8;
    client_max_body_size 20M;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/run/php/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffer_size 32k;
        fastcgi_buffers 8 32k;
        fastcgi_busy_buffers_size 64k;
        fastcgi_hide_header X-Powered-By;
    }

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

The most important line is the root: the web server serves the public directory, not the project root. This means that the .env, vendor and source code are not accessible via the web. Enable the site, disable the default one, and reload Nginx:

sudo ln -s /etc/nginx/sites-available/pelda.hu /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx

10. HTTPS with Certbot

If the domain is already pointing to the server, request a free Let’s Encrypt certificate. Certbot’s Nginx plugin also adds the certificate and the HTTP→HTTPS redirect to the server block:

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d pelda.hu -d www.pelda.hu
sudo certbot renew --dry-run

The renewal is carried out automatically by the package’s systemd timer; the --dry-run option simply checks that it will work. Details and common errors are covered in our article on Certbot.

11. Queue worker with systemd

If your application uses queued tasks (such as sending emails, image processing or exporting), you’ll need a continuously running queue:work process, which the system restarts if it stops. Systemd is well suited for this, even without a separate package. Create the file /etc/systemd/system/laravel-queue.service:

[Unit]
Description=Laravel queue worker (pelda.hu)
After=network.target mariadb.service

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/pelda.hu
ExecStart=/usr/bin/php artisan queue:work --sleep=3 --tries=3 --max-time=3600
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now laravel-queue
systemctl status laravel-queue

--max-time=3600 restarts the worker every hour, so any memory leaks do not accumulate. If you’d prefer to use Supervisor (as described in the Laravel documentation), after running sudo apt install supervisor, the /etc/supervisor/conf.d/laravel-worker.conf file might look like this:

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

Then: sudo supervisorctl reread, sudo supervisorctl update, sudo supervisorctl start "laravel-worker:*". Use one of these two solutions, not both.

12. The scheduler

Laravel’s scheduled tasks (routes/console.php) are triggered by a single cron job that runs every minute. Add the following to the www-data user’s crontab:

sudo crontab -u www-data -e
* * * * * cd /var/www/pelda.hu && php artisan schedule:run >> /dev/null 2>&1

The command php artisan schedule:list shows you what is due to run next and when. You can read about cron syntax in the article explaining crontab syntax.

13. Caching in production

In a production environment, it is also worth pre-caching the configuration, events, routes and Blade views. This can be done with a single command:

php artisan optimize

This runs the config:cache, event:cache, route:cache and view:cache commands. One important consequence is that, with cached configuration, Laravel does not read the .env file, so the env() function returns null for files outside the config/ directory. After any changes to the .env, run the optimize command again; if you wish to clear the caches completely, use the optimize:clear command.

14. Installing updates

Deploying a new version always involves the same steps, so it’s worth putting them into a small script (for example, named deploy.sh and placed alongside the project):

#!/usr/bin/env bash
set -e
cd /var/www/pelda.hu

php artisan down
git pull origin main
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan optimize
php artisan queue:restart
php artisan up

down displays a maintenance page while the update is in progress, while queue:restart signals to the workers to stop after their current task; systemd or Supervisor will then restart them using the new code. If you’re using Vite, the frontend build can follow after composer install. Before carrying out a major update, a major version change or a database migration, take a snapshot of your VPS: if something goes wrong, you can revert to the previous state in a matter of minutes.

If even a few seconds of downtime is too much, Deployer or Laravel Envoy will install the new release into a separate directory and only switch over to it at the end using a symbolic link.

Troubleshooting

  • Error 500, blank page: first check the Laravel log in the storage/logs/ directory (tail -n 50 storage/logs/laravel-*.log), then the Nginx error log (/var/log/nginx/error.log) and the PHP-FPM log (journalctl -u php8.4-fpm). For help on reading the logs, see the article on using journalctl and /var/log.
  • ‘Permission denied’ in the log: the permissions for the storage or bootstrap/cache directory are incorrect. Run the setfacl commands from step 8 again.
  • 502 Bad Gateway: Nginx cannot reach PHP-FPM. Check that it is running (systemctl status php8.4-fpm) and that the socket specified in the fastcgi_pass line of the server block matches the one in the /run/php/ directory.
  • Only the homepage is working; everything else returns a 404 error: either the try_files line is missing or incorrect, or the root directory does not point to the public directory.
  • ‘No application encryption key has been specified’: the APP_KEY is empty; run the php artisan key:generate command, then optimize.
  • Editing the .env file has no effect: the configuration is cached; please run the php artisan optimize command again.
  • Missing CSS and JS files, or HTTP links on HTTPS: the frontend build hasn’t been run, or the APP_URL is incorrect.
  • Uploaded images are not displaying: the public/storage link is missing (php artisan storage:link).
  • Queued tasks are not running: check the worker’s status (systemctl status laravel-queue), and php artisan queue:failed will list any failed tasks.

If the error page doesn’t provide any information, don’t enable APP_DEBUG in production, not even temporarily: the log contains the same information, hidden from visitors.

Security and backups in a nutshell

  • Keep your system up to date; you can also install security updates automatically (configuring automatic updates for DNF and APT).
  • Fail2ban helps protect against brute-force attacks (configuration).
  • We carry out daily backups of the VPS, but it is also worth making your own regular backups (using mariadb-dump) of the database and storing the copy off-site.
  • Do not open the database port (3306) on the firewall: the application accesses it locally at the address 127.0.0.1.

Frequently Asked Questions

How much VPS capacity do you need for a Laravel application?

A small business application or API, together with its database, will run well on 1 vCPU and 2 GB of memory. For multiple concurrent users, multiple queue workers, Redis or a frontend build running on the server, 2 vCPUs and 4 GB are a comfortable starting point. With our KVM VPS plans, you can choose from 1–16 vCPUs and 1–32 GB of memory, and upgrade later.

Can Laravel run on web hosting too?

A simple Laravel site can be set up even on shared hosting, provided the plan includes SSH and Composer, and the public directory can serve as the document root. However, a continuously running queue worker, Redis, a WebSocket server (Laravel Reverb) or custom PHP configurations require your own server; therefore, a VPS is the reliable choice for a production Laravel application.

Apache or Nginx?

It works with both. Nginx uses less memory with PHP-FPM, and the Laravel documentation also provides an example for this. With Apache, the public/.htaccess file handles the rewriting rules; for this, the mod_rewrite module must be enabled.

Can I run it with Docker?

Yes, Docker also runs on a KVM VPS, and when working in a team with multiple environments, it simplifies many things. For a single application, however, the native installation described above involves fewer moving parts. We have a separate article on using Docker on a VPS.

How do I update PHP later?

Install the packages for the new version (such as php8.5-fpm and the extensions) alongside the old ones, update the fastcgi_pass line in the server block to point to the new socket, and make sure the worker and the cron job use the new version of the php command (sudo update-alternatives --config php). Before doing so, take a snapshot and check whether your version of Laravel supports the new PHP.

Back to the blog