Install Laravel with LAMP on Ubuntu 22.04 and you will have a Laravel 13 application running behind Apache, PHP-FPM, and MariaDB in about 20 minutes. This setup uses PHP 8.3, a dedicated database account, UFW, and a trusted HTTPS certificate instead of exposing the development server.
Prerequisites
- A fresh 64-bit Ubuntu 22.04 LTS (Jammy Jellyfish) VPS with at least 2 GB of RAM and 15 GB of free disk space.
- SSH access as
root, or a normal account with workingsudoprivileges and outbound HTTPS access. - A DNS record such as
YOUR_DOMAINpointing to the VPS public IP. You need the domain before requesting a Let’s Encrypt certificate. - A long database password and an email address such as
YOUR_EMAILfor certificate renewal notices. - A VPSLake Linux VPS with root access if you need an Ubuntu server for Laravel.
These commands assume a sudo-enabled account. If you are logged in as root, remove sudo. Replace every uppercase placeholder with your value before running the related command.
Step 1: Update Ubuntu and install Apache, MariaDB, and PHP 8.3
Laravel 13 needs PHP 8.3 or newer, while Ubuntu 22.04’s native PHP series is 8.1, so add the maintained PHP PPA before installing the versioned runtime and extensions.
grep -E '^(PRETTY_NAME|VERSION_ID|VERSION_CODENAME)=' /etc/os-release
uname -m
sudo apt update
sudo apt full-upgrade -y
sudo apt install -y software-properties-common ca-certificates curl unzip git apache2 mariadb-server ufw
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.3-cli php8.3-fpm php8.3-mysql php8.3-curl php8.3-mbstring php8.3-xml php8.3-zip php8.3-bcmath php8.3-intl php8.3-gd php8.3-opcache
sudo a2enmod proxy_fcgi setenvif rewrite headers
sudo a2enconf php8.3-fpm
sudo systemctl enable --now apache2 php8.3-fpm mariadb
php -v
sudo systemctl is-active apache2 php8.3-fpm mariadb
The release check should show 22.04 and jammy; PHP should begin with 8.3, followed by three active lines.
PRETTY_NAME="Ubuntu 22.04.x LTS"
VERSION_ID="22.04"
VERSION_CODENAME=jammy
PHP 8.3.x (cli) ...
active
active
active
The Laravel deployment requirements list PHP 8.3 and the extensions used here. The PPA is necessary on Jammy because the default repository does not provide that PHP series.
Step 2: Create a private MariaDB database
Laravel should use its own database and login so an application problem cannot turn into access to every database on the server.
sudo mariadb-secure-installation
In the security wizard, remove anonymous users and the test database, disallow remote root login, and reload the privilege tables. Keeping local Unix-socket authentication for the database administrator is appropriate for this setup.
Create the application database and two local-only user entries. Replace YOUR_DB_PASSWORD with a strong password before running the block; keep the placeholder free of single quotes.
sudo mariadb <<'SQL'
CREATE DATABASE laravel_app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'laravel_app'@'localhost' IDENTIFIED BY 'YOUR_DB_PASSWORD';
CREATE USER 'laravel_app'@'127.0.0.1' IDENTIFIED BY 'YOUR_DB_PASSWORD';
GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_app'@'localhost';
GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_app'@'127.0.0.1';
FLUSH PRIVILEGES;
SQL
sudo mariadb -e "SHOW DATABASES LIKE 'laravel_app';"
The last command should print laravel_app. Do not open port 3306 in UFW; the web application and MariaDB communicate locally.
Database (laravel_app)
laravel_app
Step 3: Install Composer with checksum verification
Composer resolves Laravel’s PHP dependencies, and checking the installer hash ensures that an incomplete or altered download is not executed.
cd /tmp
curl -fsSLo composer-setup.php https://getcomposer.org/installer
EXPECTED_CHECKSUM="$(curl -fsSL https://composer.github.io/installer.sig)"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then echo 'ERROR: Invalid Composer installer checksum'; rm -f composer-setup.php; exit 1; fi
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer --quiet
rm -f composer-setup.php
composer --version
The final line should report Composer 2.x. This follows Composer’s official programmatic installation procedure, including its published SHA-384 check.
Composer version 2.x.x 20xx-xx-xx
Step 4: Create the Laravel project and set its environment
Run Composer as a separate application owner rather than as root; this prevents package scripts and the project cache from receiving unnecessary system privileges.
sudo adduser --disabled-password --gecos "" laravel
sudo install -d -o laravel -g laravel /var/www/laravelapp
sudo -u laravel -H composer create-project --no-interaction --prefer-dist laravel/laravel:^13.0 /var/www/laravelapp
sudo -u laravel -H cp /var/www/laravelapp/.env.example /var/www/laravelapp/.env
sudoedit /var/www/laravelapp/.env
In .env, keep the generated APP_KEY line for the next command, then set these values. Use the same database password from Step 2.
APP_ENV=production
APP_DEBUG=false
APP_URL=http://YOUR_DOMAIN
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=laravel_app
DB_PASSWORD=YOUR_DB_PASSWORD
Generate the encryption key and apply Laravel’s initial migrations:
sudo -u laravel -H php /var/www/laravelapp/artisan key:generate --force
sudo -u laravel -H php /var/www/laravelapp/artisan config:clear
sudo -u laravel -H php /var/www/laravelapp/artisan migrate --force
You should see Application key set successfully and completed migration lines. A new project does not need db:seed; only run it when your own application includes seed data you intend to load.
Step 5: Configure Apache to serve Laravel’s public directory
Apache must expose only public, because the project root contains .env, Composer metadata, and source files that should never be downloadable.
Create the virtual host and replace YOUR_DOMAIN before saving it:
sudo tee /etc/apache2/sites-available/laravel.conf > /dev/null <<'APACHE'
<VirtualHost *:80>
ServerName YOUR_DOMAIN
DocumentRoot /var/www/laravelapp/public
DirectoryIndex index.php
<Directory /var/www/laravelapp/public>
AllowOverride All
Options -Indexes +FollowSymLinks
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/laravel-error.log
CustomLog ${APACHE_LOG_DIR}/laravel-access.log combined
</VirtualHost>
APACHE
sudo a2dissite 000-default.conf
sudo a2ensite laravel.conf
sudo apachectl configtest
sudo systemctl reload apache2
Syntax OK confirms that Apache accepted the configuration. The PHP-FPM Unix socket keeps PHP execution local to the VPS.
Step 6: Apply narrow permissions and open the firewall
The web process needs write access only to Laravel’s logs, cache, sessions, and uploaded-file paths; the rest of the source can stay read-only.
sudo chown -R laravel:www-data /var/www/laravelapp
sudo find /var/www/laravelapp -type d -exec chmod 0750 {} +
sudo find /var/www/laravelapp -type f -exec chmod 0640 {} +
sudo find /var/www/laravelapp/storage /var/www/laravelapp/bootstrap/cache -type d -exec chmod 0770 {} +
sudo find /var/www/laravelapp/storage /var/www/laravelapp/bootstrap/cache -type f -exec chmod 0660 {} +
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable
sudo ufw status verbose
The Apache www-data group can read the application and write only to the two required directories. UFW should list SSH, HTTP, and HTTPS; leave MariaDB’s port closed.
Step 7: Enable HTTPS with Certbot
Request TLS only after YOUR_DOMAIN resolves to this server, so the certificate authority can reach Apache on port 80.
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache --redirect -d YOUR_DOMAIN -m YOUR_EMAIL --agree-tos --no-eff-email
sudo -u laravel -H sed -i 's#^APP_URL=.*#APP_URL=https://YOUR_DOMAIN#' /var/www/laravelapp/.env
sudo -u laravel -H php /var/www/laravelapp/artisan config:clear
sudo -u laravel -H php /var/www/laravelapp/artisan config:cache
Certbot adds the Apache TLS configuration and redirects HTTP to HTTPS. If DNS is not ready, postpone this step and do not use the temporary HTTP site for passwords or other sensitive data.
Verify the Laravel installation
Run these checks before opening the domain in a browser:
sudo systemctl is-active apache2 php8.3-fpm mariadb
sudo apachectl configtest
sudo -u laravel -H php /var/www/laravelapp/artisan --version
sudo -u laravel -H php /var/www/laravelapp/artisan migrate:status
curl -fsSI https://YOUR_DOMAIN/
Expect three active lines, Syntax OK, a Laravel 13 version, migration statuses, and an HTTP success response.
active
active
active
Syntax OK
Laravel Framework 13.x
HTTP/2 200
Open https://YOUR_DOMAIN in a browser. The default Laravel page confirms that Apache reaches public, PHP-FPM executes PHP, and the HTTPS virtual host is serving the application.
Troubleshooting
Unable to locate package php8.3-fpm
The PPA was not added successfully, or the server is not using Ubuntu 22.04 repositories. Confirm the release and refresh APT before trying to install versioned PHP packages again.
grep -E '^(PRETTY_NAME|VERSION_ID|VERSION_CODENAME)=' /etc/os-release
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.3-cli php8.3-fpm php8.3-mysql
The codename must be jammy. Do not mix packages for another Ubuntu release.
SQLSTATE[HY000] [1045] Access denied
The password in .env does not match MariaDB, or Laravel is still using cached configuration. Test the account over TCP and clear the cache after correcting the value.
mariadb -h 127.0.0.1 -u laravel_app -p laravel_app -e 'SELECT 1;'
sudo -u laravel -H php /var/www/laravelapp/artisan config:clear
sudo -u laravel -H php /var/www/laravelapp/artisan migrate --force
Enter the real password at the prompt. If the direct query fails, reset both local account entries rather than granting the application administrative privileges.
403 Forbidden or Permission denied
Apache may not be able to traverse the project, or the writable directories may have lost their group permissions. Reapply the ownership and modes without using a broad permission such as chmod 777.
sudo chown -R laravel:www-data /var/www/laravelapp
sudo find /var/www/laravelapp -type d -exec chmod 0750 {} +
sudo find /var/www/laravelapp -type f -exec chmod 0640 {} +
sudo find /var/www/laravelapp/storage /var/www/laravelapp/bootstrap/cache -type d -exec chmod 0770 {} +
sudo find /var/www/laravelapp/storage /var/www/laravelapp/bootstrap/cache -type f -exec chmod 0660 {} +
sudo systemctl reload apache2
503 Service Unavailable or PHP source appears
PHP-FPM may be stopped, or Apache may be using a socket for a different PHP series. Check the service and the exact socket used in laravel.conf.
sudo systemctl status php8.3-fpm --no-pager
sudo test -S /run/php/php8.3-fpm.sock && echo 'PHP-FPM socket exists'
sudo apachectl configtest
sudo systemctl restart php8.3-fpm apache2
The socket test should print PHP-FPM socket exists, and Apache should return Syntax OK.
Hardening
- Keep
APP_DEBUG=false, back up/var/www/laravelappand MariaDB, and runsudo apt update && sudo apt upgradeduring maintenance windows. - Keep port 3306 closed and restrict SSH to trusted source addresses in your provider firewall where possible.
- Test certificate renewal with
sudo certbot renew --dry-run; for additional UFW rules, use VPSLake’s UFW firewall generator.
FAQ
Why does Ubuntu 22.04 need a PHP PPA for Laravel?
Jammy’s standard repositories provide PHP 8.1, while Laravel 13 requires PHP 8.3 or newer. The PPA supplies versioned PHP 8.3 packages and extensions without replacing the operating system.
Can I install Laravel without a domain?
You can test Apache with an IP address or a temporary hosts-file entry, but a public DNS name is required for a normal Let’s Encrypt certificate. Use the domain before accepting logins, payments, or other sensitive data.
Why does Apache point to the public folder?
Laravel keeps .env, vendor, and application source outside public. Serving only that directory lets Apache route requests through index.php without making configuration files part of the web root.
Should I use php artisan serve on a VPS?
No. artisan serve is a development server, not a production replacement for Apache, PHP-FPM, UFW, and HTTPS. Use it only for short local tests.