How to Install Laravel PHP Framework on Rocky Linux 8
Laravel is a modern PHP framework that follows the Model-View-Controller (MVC) architecture, providing a clean and elegant syntax for building web applications.
Prerequisites
-
Rocky Linux 8 server.
- SELinux set to permissive mode.
Install Required Packages
Installs essential tools and utilities required to set up Laravel.
dnf update -y
dnf install -y epel-release dnf-utils unzip curl git
Enable Remi Repository for PHP
Enables the Remi repository to install the latest PHP version and required extensions.
dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf module reset php -y
dnf module enable php:remi-8.2 -y
dnf install -y php php-cli php-common php-mysqlnd php-zip php-gd php-mbstring php-curl php-xml php-bcmath
Install and Configure MariaDB (MySQL)
Installs MariaDB, starts the service, and ensures it runs at boot.
dnf install -y mariadb-server mariadb
systemctl start mariadb
systemctl enable mariadb
Secure MariaDB Installation
Secures MariaDB by prompting for root password, removing anonymous users, disabling remote root login, and dropping the test database.
mysql_secure_installation
Create Laravel Database and User
Creates a new database and user for the Laravel application.
mysql -u root -p
Create database, users for laravel configuration,
CREATE DATABASE laravelapp;
CREATE USER 'laraveluser'@'localhost' IDENTIFIED BY 'Strong_Password_Here';
GRANT ALL PRIVILEGES ON laravelapp.* TO 'laraveluser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Install Composer
Installs Composer, a dependency manager required for Laravel.
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
Install Laravel
Downloads and sets up the latest Laravel project in /var/www/laravelapp
.
cd /var/www/
composer create-project --prefer-dist laravel/laravel laravelapp
*Configure Database in Laravel
Updates .env
to connect Laravel with the configured MySQL database.
vi /var/www/laravelapp/.env
UncommUpdate the following:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelapp
DB_USERNAME=laraveluser
DB_PASSWORD=Strong_Password_Here
Note: Replace Strong_Password_Here with actual strong password
Run the command below to clear, migrate the database and populate tables with sample data.
cd /var/www/laravelapp
php artisan config:clear
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan view:clear
sudo -u apache php artisan migrate
sudo -u apache php artisan db:seed
Set Correct Permissions
Assigns ownership to Apache and sets appropriate permissions for storage and cache.
chown -R apache:apache /var/www/laravelapp
chmod -R 775 /var/www/laravelapp/storage
chmod -R 775 /var/www/laravelapp/bootstrap/cache
touch /var/www/laravelapp/storage/logs/laravel.log
chown apache:apache /var/www/laravelapp/storage/logs/laravel.log
chmod 666 /var/www/laravelapp/storage/logs/laravel.log
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
Creates an Apache virtual host file to serve Laravel from the public directory.
<VirtualHost *:80>
ServerName laravel.domainhere.info
DocumentRoot /var/www/laravelapp/public
<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
Enable Rewrite Module and Restart Apache
Enables .htaccess
overrides required for Laravel routing.
sed -i 's/None/All/g' /etc/httpd/conf/httpd.conf
systemctl restart httpd
Configure Firewall
Opens HTTP and HTTPS ports to allow web traffic.
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
Optional: Allow Custom SSH Port
If you’re using a custom SSH port, this allows incoming traffic.
firewall-cmd --permanent --zone=public --add-port=<your-port>/tcp
firewall-cmd --reload
Note: Replace
with actual port
Verify and Fix SELinux
Allows Apache to write to Laravel directories when SELinux is enabled.
sestatus
If SELinux is enforcing:
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
Clear and Cache Laravel Configuration
Clears and caches Laravel’s configuration and routes to optimize performance.
cd /var/www/laravelapp
php artisan config:clear
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan view:clear
Restart Apache and MySQL
To ensures that all changes take effect properly.
systemctl restart httpd
systemctl restart mariadb
Accessing Laravel Dashboard
Open your browser and navigate to:
http://laravel.domainhere.info
Note: Replace laravel.domainhere.info with actual domain name
You should see the default Laravel welcome page if everything is confYou have successfully installed and configured Laravel on Rocky Linux 8. Your application is now ready for development. Access your Laravel project in the browser and start building powerful web applications with ease. 🚀igured correctly.
Done, You have successfully installed and configured Laravel on Rocky Linux 8. 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