Install Laravel PHP framework on AlmaLinux 9 in about 20 minutes and finish with a Laravel 13 application behind Apache and PHP-FPM. You will create a separate MariaDB account, keep SELinux enforcing, and expose only the web ports the application needs.
AlmaLinux 9.6 and later provide PHP 8.3 through AppStream, which meets Laravel 13’s minimum requirement. The procedure below uses that native module stream, Composer’s verified installer, and a real virtual host instead of Laravel’s development server.
Prerequisites
- A 64-bit AlmaLinux 9.6 or later VPS with at least 2 GB of RAM and 15 GB of free disk space.
- SSH access as
root, or a non-root account with workingsudoprivileges and outbound HTTPS access. - A public IPv4 address. A DNS name is optional for the first local HTTP check but required for a trusted certificate.
- A VPSLake Linux VPS if you need an AlmaLinux server with root access.
The commands use sudo for a regular account. If you are already root, omit sudo. Replace uppercase placeholders before running a command; the database password must be long and unique.
Step 1: Confirm AlmaLinux 9 and install the server stack
Confirming the release prevents an EL8 or EL10 PHP instruction from being mixed into this installation.
grep -E '^(PRETTY_NAME|VERSION_ID|PLATFORM_ID)=' /etc/os-release
uname -m
sudo dnf upgrade -y
sudo dnf install -y epel-release firewalld httpd mariadb-server curl git unzip policycoreutils-python-utils
sudo dnf module reset php -y
sudo dnf module install php:8.3/common -y
sudo dnf install -y php-mysqlnd php-mbstring php-xml php-bcmath php-curl php-gd php-opcache php-pecl-zip
sudo systemctl enable --now firewalld httpd php-fpm mariadb
php -v
sudo systemctl is-active firewalld httpd php-fpm mariadb
The release should show AlmaLinux 9, the architecture should normally be x86_64, and PHP should begin with 8.3. Each service check should print active.
PRETTY_NAME="AlmaLinux 9.x (Seafoam Ocelot)"
VERSION_ID="9.x"
x86_64
PHP 8.3.x (cli) ...
active
active
active
active
The common PHP profile includes PHP-FPM for Apache. The extra packages add the database driver and Laravel’s common extensions without relying on a third-party PHP repository.
Step 2: Install Composer and verify its installer
Composer resolves Laravel’s dependency tree, and checking the published SHA-384 value avoids running a damaged installer.
cd /tmp
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: Invalid Composer installer checksum'; rm -f composer-setup.php; exit 1; fi
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
rm -f composer-setup.php
composer --version
The final line should show Composer 2.x. The Composer installation documentation describes the global PHAR location and the checksum workflow; the exact patch number will change over time.
Composer version 2.x.x 20xx-xx-xx
Step 3: Create a MariaDB database for Laravel
Using a dedicated account limits the damage if the application credentials are ever exposed.
sudo mariadb-secure-installation
When prompted, remove anonymous users, reject remote root login, remove the test database, and reload the privilege tables. Keep the local administrative socket authentication unless you have a specific reason to change it.
Create the application database and two local account entries. The second host matches Laravel’s explicit 127.0.0.1 TCP connection rather than relying on socket behavior.
sudo mariadb <<'SQL'
CREATE DATABASE laravel_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'YOUR_DB_PASSWORD';
CREATE USER 'laravel_user'@'127.0.0.1' IDENTIFIED BY 'YOUR_DB_PASSWORD';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'127.0.0.1';
FLUSH PRIVILEGES;
SQL
sudo mariadb -e "SHOW DATABASES LIKE 'laravel_db';"
The check should return laravel_db. Do not add MariaDB to firewalld; the web process connects locally, so port 3306 has no reason to be public.
Database (laravel_db)
laravel_db
Step 4: Create the Laravel application
Installing the project as apache keeps the Composer cache and application files usable by PHP-FPM without a later ownership sweep.
sudo install -d -o apache -g apache /var/www/laravelapp /usr/share/httpd/.cache /usr/share/httpd/.config
sudo -u apache -H composer create-project --no-interaction --prefer-dist laravel/laravel:^13.0 /var/www/laravelapp
This pins the new project to Laravel 13, whose minimum PHP version is 8.3. Laravel’s deployment documentation also requires the web server to point at public and identifies storage and bootstrap/cache as writable directories.
Open the generated environment file:
sudoedit /var/www/laravelapp/.env
Keep the generated APP_KEY line for the next command, then set the following values. Replace YOUR_DOMAIN and YOUR_DB_PASSWORD with real values.
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_db
DB_USERNAME=laravel_user
DB_PASSWORD=YOUR_DB_PASSWORD
Generate the encryption key and apply Laravel’s initial migrations:
sudo -u apache -H php /var/www/laravelapp/artisan key:generate --force
sudo -u apache -H php /var/www/laravelapp/artisan config:clear
sudo -u apache -H php /var/www/laravelapp/artisan migrate --force
Migration output should end with completed migration entries. Do not run db:seed unless your own application contains seed data you actually want to load.
Step 5: Configure Apache to serve Laravel
Apache must expose only the public directory; the project root contains .env, Composer metadata, and other files that should never be downloadable.
Create the virtual host and replace both YOUR_DOMAIN occurrences before saving it:
sudo tee /etc/httpd/conf.d/zz-laravel.conf > /dev/null <<'APACHE'
<VirtualHost *:80>
ServerName YOUR_DOMAIN
ServerAlias www.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-fpm/www.sock|fcgi://localhost"
</FilesMatch>
ErrorLog /var/log/httpd/laravel-error.log
CustomLog /var/log/httpd/laravel-access.log combined
</VirtualHost>
APACHE
sudo apachectl configtest
sudo systemctl reload httpd
Syntax OK means Apache accepted the file. The zz- prefix makes this site configuration load late, while the PHP-FPM socket sends PHP requests to the managed application worker.
Step 6: Set SELinux-safe permissions
Laravel needs to write logs, sessions, compiled views, and caches, but the rest of the code can remain owned by root and readable by Apache’s group.
sudo semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/laravelapp/(storage|bootstrap/cache)(/.*)?'
sudo restorecon -Rv /var/www/laravelapp
sudo chown -R root:apache /var/www/laravelapp
sudo find /var/www/laravelapp -type d -exec chmod 0750 {} +
sudo find /var/www/laravelapp -type f -exec chmod 0640 {} +
sudo chown -R apache:apache /var/www/laravelapp/storage /var/www/laravelapp/bootstrap/cache
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 setsebool -P httpd_can_network_connect_db on
The persistent file-context rule survives relabeling, and the boolean permits the web process to reach MariaDB. Leave SELinux enforcing; making the entire project writable or disabling protection hides the actual configuration problem.
Step 7: Open web ports and enable HTTPS
Allow SSH before applying the web rules so a new firewalld activation does not strand your session, then publish only HTTP and HTTPS.
sudo firewall-cmd --zone=public --permanent --add-service=ssh
sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --zone=public --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --list-services
When YOUR_DOMAIN resolves to the VPS, install Certbot and let it update Apache. Replace YOUR_EMAIL with an address that can receive renewal notices.
sudo dnf install -y mod_ssl certbot python3-certbot-apache
sudo certbot --apache --redirect -d YOUR_DOMAIN -m YOUR_EMAIL --agree-tos --no-eff-email
sudo sed -i 's#^APP_URL=.*#APP_URL=https://YOUR_DOMAIN#' /var/www/laravelapp/.env
sudo -u apache -H php /var/www/laravelapp/artisan config:clear
sudo -u apache -H php /var/www/laravelapp/artisan config:cache
Certbot creates the HTTPS virtual-host changes and redirects HTTP. If DNS is not ready, skip the certificate commands, test on HTTP, and do not use that unencrypted endpoint for real credentials.
Verify the Laravel installation
These checks cover service health, Apache syntax, the Laravel release, database migrations, and the virtual host response before you open a browser.
sudo systemctl is-active firewalld httpd php-fpm mariadb
sudo apachectl configtest
sudo -u apache -H php /var/www/laravelapp/artisan --version
sudo -u apache -H php /var/www/laravelapp/artisan migrate:status
curl -fsSI -H 'Host: YOUR_DOMAIN' http://127.0.0.1/
Expect four active lines, Syntax OK, a Laravel 13 version, migration statuses, and an HTTP response. A 200 OK means the page was served directly; a 301 is normal after Certbot enables the HTTPS redirect.
active
active
active
active
Syntax OK
Laravel Framework 13.x
HTTP/1.1 200 OK
Once DNS is live, browse to https://YOUR_DOMAIN. The default Laravel page confirms that Apache is using the intended document root and PHP-FPM is executing the framework.
Troubleshooting
No matching Modules to list or PHP is older than 8.3
The server may be on an older AlmaLinux 9 minor release, or a different PHP stream is still enabled. Check the release and module inventory, then reset the stream before installing the native 8.3 profile.
cat /etc/almalinux-release
sudo dnf module list php
sudo dnf module reset php -y
sudo dnf module install php:8.3/common -y
php -v
If the 8.3 stream is absent, update the AlmaLinux 9 packages and repository metadata before continuing; do not mix an EL8 repository into this server.
SQLSTATE[HY000] [1045] Access denied
The password in .env may not match the account, or Laravel may still have old configuration cached. Test the TCP account separately and clear the cached settings after correcting the file.
mariadb -h 127.0.0.1 -u laravel_user -p laravel_db -e 'SELECT 1;'
sudo -u apache -H php /var/www/laravelapp/artisan config:clear
sudo -u apache -H php /var/www/laravelapp/artisan migrate --force
The query should print 1. If it fails, reset the password for both local account hosts in MariaDB rather than granting the application administrative access.
403 Forbidden or Permission denied
Apache can read the application only when its group permissions and SELinux labels agree with the paths in the virtual host. Restore the labels and reapply the narrow writable-directory permissions.
sudo restorecon -Rv /var/www/laravelapp
sudo chown -R root:apache /var/www/laravelapp
sudo find /var/www/laravelapp -type d -exec chmod 0750 {} +
sudo find /var/www/laravelapp -type f -exec chmod 0640 {} +
sudo chown -R apache:apache /var/www/laravelapp/storage /var/www/laravelapp/bootstrap/cache
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 httpd
If the denial continues, inspect sudo ausearch -m AVC -ts recent and fix the path named by SELinux instead of applying a broad mode such as 777.
503 Service Unavailable or PHP code appears as text
PHP-FPM may be stopped, or Apache may not be reaching the socket named in the virtual host. Inspect the service, socket, and Apache configuration together.
sudo systemctl status php-fpm --no-pager
sudo test -S /run/php-fpm/www.sock && echo 'PHP-FPM socket exists'
sudo journalctl -u php-fpm -n 40 --no-pager
sudo apachectl configtest
sudo systemctl restart php-fpm httpd
The socket test should print PHP-FPM socket exists, and the configuration test should return Syntax OK.
Hardening
- Keep
APP_DEBUG=falseand runconfig:cacheonly after.envcontains its final production values. - Leave MariaDB port 3306 closed and restrict SSH at the provider firewall to trusted source addresses where possible.
- Back up
/var/www/laravelappand the MariaDB database before upgrades. For a conservative SSH baseline, use the VPSLake SSH hardening generator. - Apply
sudo dnf upgrade -yregularly and check certificate renewal withsudo certbot renew --dry-run.
FAQ
Does AlmaLinux 9 include PHP 8.3?
Current AlmaLinux 9 updates provide PHP 8.3 as an AppStream module. This guide selects php:8.3/common, which avoids adding a separate PHP repository; an older minor release may need its package metadata updated first.
Why are both localhost and 127.0.0.1 database users created?
MariaDB can treat socket connections and TCP connections as different user-host combinations. The two entries make the explicit DB_HOST=127.0.0.1 setting predictable while keeping both accounts local to the VPS.
Can I test the site before my domain is ready?
Yes. The verification command sends a Host header to Apache on loopback, so you can test the virtual host before public DNS changes. Certbot still requires a real domain that resolves to the server.
Why does Apache use /var/www/laravelapp/public instead of the project directory?
Laravel’s public directory is the intended web boundary. Keeping the document root there prevents visitors from requesting .env, vendor, and application source files directly.