How to Install Laravel PHP Framework on Rocky Linux 9
Laravel is a PHP framework used for building modern web applications. It follows the Model-View-Controller (MVC) architecture, providing a clean and elegant syntax that simplifies common tasks like routing, authentication, and database management.
Prerequisites
Before proceeding, ensure you have the following:
-
An Rocky Linux 9 server.
- SELinux set to permissive mode.
Setting Up LAMP and Composer
Installs EPEL, DNF utilities, and the Remi repository to enable PHP packages.
dnf install epel-release dnf-utils http://rpms.remirepo.net/enterprise/remi-release-9.rpm
Activate the PHP 8.3 repository from Remi using the command below.
dnf reset php -y
dnf module enable php:remi-8.3 -y
Install Apache, MariaDB, PHP, and Composer, then confirm with y when prompted.
dnf install httpd mariadb-server composer php php-curl php-bcmath php-json php-mbstring php-xml php-tokenizer php-zip php-mysqlnd
Start the httpd service to run on boot and enable it using the following command.
systemctl start httpd
systemctl enable httpd
Start the MariaDB service and enable it to run on boot using the command below.
systemctl start mariadb
systemctl enable mariadb
Check the installed versions of PHP and Composer to confirm PHP 8.3 and Composer 2.7.1 are installed.
php -v
sudo -u apache composer -v
Output:
[root@vps ~]# php -v
PHP 8.3.19 (cli) (built: Mar 12 2025 13:10:27) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.3.19, Copyright (c) Zend Technologies
with Zend OPcache v8.3.19, Copyright (c), by Zend Technologies
[root@vps ~]# sudo -u apache composer -v
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 2.8.6 2025-02-25 13:03:50
Run the command below to confirm that fileinfo
, mbstring
, and openssl
modules are enabled.
php -m
Output:
[root@vps ~]# php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gettext
hash
...
MariaDB Configuration
Secures MariaDB by prompting for root password, removing anonymous users, disabling remote root login, and dropping the test database.
mariadb-secure-installation
Log in to the MariaDB
server using the mariadb command below. Enter your MariaDB root password when prompted.
mariadb -u root -p
Run the following queries to create a database and user for Laravel. Replace laravelapp, laravel, and p4ssw0rd with your details.
CREATE DATABASE laravel_db;
CREATE USER laravel_user@localhost IDENTIFIED BY 'Strong_Password_Here';
GRANT ALL PRIVILEGES ON laravel_db.* TO laravel_user@localhost;
FLUSH PRIVILEGES;
EXIT;
Note: Replace Strong_Password_Here with actual Strong Password
Create a Laravel Project
Now that PHP and MariaDB are configured, proceed to download and install Laravel.
Create the project directory /var/www/laravelapp
along with .cache
and .config
for Composer, and assign ownership to apache.
mkdir -p /var/www/laravelapp /usr/share/httpd/.composer/{.cache,.config}
chown -R apache:apache /var/www/laravelapp /usr/share/httpd/.composer
Navigate to /var/www/laravelapp
and run the command below to download and install Laravel using Composer.
cd /var/www/laravelapp/
sudo -u apache composer create-project laravel/laravel .
Check MySQL Socket Path,
mysqladmin variables | grep socket
Ensure the socket path matches the value in /etc/my.cnf:
[mysqld]
socket=/var/lib/mysql/mysql.sock
After downloading Laravel, open the .env
file using the nano editor with the command below.
nano .env
Update APP_URL
with your local domain name and modify database details with your MariaDB credentials.
APP_URL=http://laravel.domainhere.info
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=Strong_Password_Here
After making the changes, save the file and exit.
Run the command below to clear, migrate the database and populate tables with sample data.
php artisan config:clear
sudo -u apache php artisan migrate
sudo -u apache php artisan db:seed
Configure Apache Virtual Host
After installing Laravel, create a new Apache virtual host configuration for Laravel. Use any local or random domain, which can be configured later.
Create the configuration file /etc/httpd/conf.d/laravel.conf
using the nano editor.
nano /etc/httpd/conf.d/laravel.conf
Add the following configuration and update the ServerName with your Laravel domain name.
<VirtualHost *:80>
ServerAdmin admin@laravel.domainhere.info
ServerName laravel.domainhere.info
DocumentRoot /var/www/laravelapp/public
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/laravelapp>
AllowOverride All
</Directory>
ErrorLog /var/log/httpd/laravel-error.log
CustomLog /var/log/httpd/laravel-access.log combined
</VirtualHost>
Note: Replace
laravel.domainhere.info
with actual domain name
Save the changes and exit the editor.
Run the command below to check your Apache configuration. A valid syntax will return Syntax OK.
apachectl configtest
Output:
Syntax OK
Restart the httpd
service to apply the new virtual host configuration for Laravel.
systemctl restart httpd
Setting Correct Permissions and Ownership for Laravel
To ensure Laravel works properly, configure ownership, permissions, and SELinux context as follows: Set Correct Ownership,
chown -R apache:apache /var/www/laravelapp
Grant appropriate permissions to the storage
and bootstrap/cache
directories:
chmod -R 775 /var/www/laravelapp/storage
chmod -R 775 /var/www/laravelapp/bootstrap/cache
chcon -R -t httpd_sys_rw_content_t /var/www/laravelapp/storage
chcon -R -t httpd_sys_rw_content_t /var/www/laravelapp/bootstrap/cache
If SELinux is enabled, update the SELinux context for the storage
and bootstrap/cache
directories:
chmod -R 775 /var/www/laravelapp/storage
chmod -R 775 /var/www/laravelapp/bootstrap/cache
These steps ensure that Apache can read and write to the necessary directories, preventing permission errors during Laravel execution.
Configure FirewallD
Before accessing Laravel, open HTTP and HTTPS ports on your Rocky Linux server using the command below.
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --permanent --add-service=mysql
Reload firewalld to apply the changes.
firewall-cmd --reload
Output:
[root@vps laravelapp]# sudo firewall-cmd --reload
success
If SELinux is enforcing, allow Apache to connect to the database:
setsebool -P httpd_can_network_connect_db 1
Accessing Laravel Dashboard
Open your web browser and go to http://laravel.domainhere.info/
to verify the Laravel installation.
Note: Replace laravel.domainhere.info with actual domain name
A successful setup will display the Laravel index page.
Done, You have successfully installed and configured Laravel on Rocky Linux 9. Your application is now ready for development. Access your Laravel project in the browser and start building powerful web applications with ease. 🚀
CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME
for 10% off!
1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month
Available Locations: LAX | MIA | ATL | FRA | AMS