RHEL BasedRocky Linux

How To Install Laravel on Rocky Linux 10

Install Laravel on Rocky Linux 10

Laravel stands as one of the most popular PHP frameworks for modern web development, offering elegant syntax and powerful features that streamline application development. When combined with Rocky Linux 10, an enterprise-grade RHEL-compatible distribution, developers gain access to a robust, secure platform perfect for hosting Laravel applications.

This comprehensive guide will walk you through every step of installing Laravel on Rocky Linux 10, from initial system preparation to final testing and security hardening. Whether you’re a system administrator, developer, or DevOps professional, you’ll learn how to create a production-ready Laravel environment that leverages the stability and performance of Rocky Linux 10. By following these detailed instructions, you’ll have a fully functional Laravel installation ready for development or production deployment.

Prerequisites and System Requirements

Before beginning the Laravel installation process on Rocky Linux 10, ensure your system meets the necessary hardware and software requirements. Rocky Linux 10 requires a minimum of a 1 GHz 64-bit (x86_64-v3) processor with AVX support, though a multi-core processor is recommended for optimal performance. The system needs at least 2 GB of RAM, but 4-8 GB is strongly recommended for production environments where Laravel applications will handle concurrent users and database operations.

Storage requirements include a minimum of 10 GB free disk space, though allocating 20-30 GB provides better flexibility for application growth, logs, and temporary files. Ensure you have network connectivity for downloading packages and dependencies during the installation process.

Your user account must have sudo privileges to install packages and modify system configurations. Basic familiarity with Linux command-line operations is essential, as this guide involves extensive terminal usage. If you’re planning to use a domain name, have your DNS records configured to point to your server’s IP address.

Consider backing up any existing data on your system before proceeding, especially if you’re installing Laravel on a server with existing applications or configurations. SELinux will be active by default on Rocky Linux 10, so understanding basic SELinux concepts will help troubleshoot any permission-related issues that may arise during the installation process.

Updating Rocky Linux 10 System

Start by ensuring your Rocky Linux 10 system has the latest packages and security updates. This step is crucial for maintaining system security and compatibility with the packages we’ll install later.

Execute the system update command:

sudo dnf update -y

This command updates all installed packages to their latest versions. The process may take several minutes depending on your internet connection and the number of packages requiring updates.

Install the EPEL (Extra Packages for Enterprise Linux) repository, which provides additional packages not included in the standard Rocky Linux repositories:

sudo dnf install epel-release -y

The EPEL repository contains many packages essential for web development, including some PHP extensions and tools we’ll need for Laravel.

Verify your system architecture to ensure compatibility:

uname -m

This should return x86_64 for a 64-bit system. After major updates, consider rebooting your system to ensure all kernel and system-level changes take effect:

sudo reboot

Check available disk space to confirm adequate storage for the installation:

df -h

Ensure you have at least 5-10 GB of free space in your root partition for the Laravel installation and its dependencies.

Installing PHP 8.4 and Required Extensions

Laravel requires PHP 8.1 or higher, so we’ll install PHP 8.4, which provides excellent performance and the latest features. Rocky Linux 10’s default repositories may not include the latest PHP versions, so we’ll use the Remi repository.

First, install the Remi repository:

sudo dnf install -y https://rpms.remirepo.net/enterprise/remi-release-10.rpm

Reset any existing PHP modules to avoid conflicts:

sudo dnf module reset php -y

Enable PHP 8.4 from the Remi repository:

sudo dnf module enable php:remi-8.4 -y

Install PHP 8.4 along with essential extensions required by Laravel:

sudo dnf install -y php php-cli php-fpm php-mysqlnd php-opcache php-xml php-mbstring php-curl php-gd php-json php-common php-intl php-zip php-bcmath php-tokenizer

Each extension serves a specific purpose:

  • php-cli: Command-line interface for PHP
  • php-fpm: FastCGI Process Manager for improved performance
  • php-mysqlnd: Native MySQL database driver
  • php-opcache: Opcode caching for enhanced performance
  • php-xml: XML processing capabilities
  • php-mbstring: Multi-byte string handling for internationalization
  • php-curl: HTTP client functionality for external API calls
  • php-gd: Image processing library
  • php-json: JSON data handling
  • php-intl: Internationalization support
  • php-zip: Archive manipulation
  • php-bcmath: Arbitrary precision mathematics
  • php-tokenizer: Token parsing for PHP code analysis

Verify the PHP installation:

php -v

You should see output indicating PHP 8.4.x is installed. Check which extensions are loaded:

php -m

Start and enable the PHP-FPM service:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Installing and Configuring Web Server (Apache)

Apache HTTP Server provides a reliable, secure foundation for serving Laravel applications. Install Apache using the DNF package manager:

sudo dnf install httpd -y

Start the Apache service and enable it to start automatically on system boot:

sudo systemctl start httpd
sudo systemctl enable httpd

Verify Apache is running:

sudo systemctl status httpd

Create a virtual host configuration for your Laravel application. This configuration tells Apache how to serve your Laravel project:

sudo nano /etc/httpd/conf.d/laravel.conf

Add the following virtual host configuration:

<VirtualHost *:80>
    ServerName your-domain.com
    ServerAlias www.your-domain.com
    DocumentRoot /var/www/laravelapp/public

    <Directory /var/www/laravelapp/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/laravel_error.log
    CustomLog /var/log/httpd/laravel_access.log combined
</VirtualHost>

Replace your-domain.com with your actual domain name, or use localhost for local development. The DocumentRoot points to Laravel’s public directory, which contains the application’s entry point.

The AllowOverride All directive is crucial for Laravel’s routing system, as it allows the .htaccess file to override server configuration for clean URLs.

Test the Apache configuration:

sudo httpd -t

If the syntax is correct, restart Apache to apply the changes:

sudo systemctl restart httpd

Installing and Configuring MariaDB

Laravel supports multiple database systems, but MariaDB provides excellent performance and compatibility. Install MariaDB server and client packages:

sudo dnf install mariadb-server mariadb -y

Start and enable the MariaDB service:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Secure your MariaDB installation by running the security script:

sudo mysql_secure_installation

Follow the prompts to:

  • Set a root password (choose a strong password)
  • Remove anonymous users
  • Disable remote root login
  • Remove the test database
  • Reload privilege tables

Connect to MariaDB as the root user:

mysql -u root -p

Create a dedicated database for your Laravel application:

CREATE DATABASE laravel_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Create a database user with appropriate privileges:

CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'strong_password_here';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Replace strong_password_here with a secure password. The UTF8MB4 character set ensures full Unicode support, including emoji and special characters.

Test the database connection:

mysql -u laravel_user -p laravel_db

Verify you can access the database, then exit with the EXIT; command.

Installing Composer Package Manager

Composer is PHP’s dependency manager and is essential for installing Laravel and managing its packages. Install Composer from the EPEL repository:

sudo dnf --enablerepo=epel install composer -y

Verify Composer installation:

composer --version

You should see Composer version information. Configure Composer for optimal performance by increasing memory limits and enabling parallel downloads:

composer config --global process-timeout 2000
composer config --global repos.packagist composer https://packagist.org

Create a composer cache directory to speed up future installations:

mkdir -p ~/.composer/cache

For security, ensure Composer runs with appropriate user permissions. Never run Composer as root unless absolutely necessary, as this can create security vulnerabilities.

Creating and Installing Laravel Project

Navigate to the web server’s document root and create a directory for your Laravel application:

sudo mkdir -p /var/www/laravelapp
cd /var/www

Set proper ownership for the directory:

sudo chown -R $USER:apache laravelapp

Create the Laravel project using Composer:

composer create-project laravel/laravel laravelapp

This command downloads Laravel and all its dependencies, creating a complete project structure. The process may take several minutes depending on your internet connection.

Navigate to the Laravel project directory:

cd laravelapp

Copy the environment configuration file:

cp .env.example .env

Edit the .env file to configure your database connection:

nano .env

Update the database configuration section:

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
APP_URL=http://your-domain.com

Generate an application encryption key:

php artisan key:generate

This command creates a unique application key used for encrypting session data, cookies, and other sensitive information.

Run the database migrations to create Laravel’s default tables:

php artisan migrate

You should see output indicating successful migration of several tables including users, password resets, and failed jobs.

Clear any cached configuration:

php artisan config:clear
php artisan cache:clear

Setting File Permissions and SELinux Configuration

Proper file permissions are crucial for Laravel’s functionality and security. Set the correct ownership for all Laravel files:

sudo chown -R apache:apache /var/www/laravelapp

Set specific permissions for Laravel’s storage and cache directories:

sudo chmod -R 775 /var/www/laravelapp/storage
sudo chmod -R 775 /var/www/laravelapp/bootstrap/cache

These directories need write permissions for Laravel to store logs, cache files, and uploaded content. The 775 permissions allow the web server to write while maintaining security.

Configure SELinux contexts for web directories:

sudo setsebool -P httpd_can_network_connect_db 1
sudo setsebool -P httpd_execmem 1

The first command allows Apache to connect to database servers, while the second permits memory execution required by some PHP operations.

Set the proper SELinux context for web content:

sudo semanage fcontext -a -t httpd_exec_t "/var/www/laravelapp(/.*)?"
sudo restorecon -R /var/www/laravelapp

Verify SELinux contexts are correctly applied:

ls -Z /var/www/laravelapp

The output should show httpd_exec_t context for your Laravel files.

Firewall Configuration

Configure the firewall to allow web traffic to reach your Laravel application. Rocky Linux 10 uses FirewallD by default.

Open HTTP port (port 80):

sudo firewall-cmd --add-service=http --permanent

Open HTTPS port (port 443) for future SSL configuration:

sudo firewall-cmd --add-service=https --permanent

If you plan to access the database remotely (not recommended for security), you can open the MySQL service:

sudo firewall-cmd --permanent --add-service=mysql

Reload the firewall rules:

sudo firewall-cmd --reload

Verify the firewall rules are active:

sudo firewall-cmd --list-all

You should see http and https services listed in the allowed services section.

Testing Laravel Installation

Test your Laravel installation using multiple methods to ensure everything works correctly.

First, test using Laravel’s built-in development server:

cd /var/www/laravelapp
php artisan serve --host 0.0.0.0 --port=8000

Open a web browser and navigate to http://your-server-ip:8000. You should see the Laravel welcome page with version information and links to documentation.

Install Laravel on Rocky Linux 10

For production testing, access your Laravel application through Apache by navigating to http://your-domain.com or http://your-server-ip. The same Laravel welcome page should appear.

Test database connectivity by creating a simple route. Edit the routes file:

nano routes/web.php

Add a test route:

Route::get('/test-db', function () {
    try {
        DB::connection()->getPdo();
        return 'Database connection successful!';
    } catch (\Exception $e) {
        return 'Database connection failed: ' . $e->getMessage();
    }
});

Visit http://your-domain.com/test-db to verify database connectivity.

Troubleshooting Common Issues

Several issues may arise during Laravel installation on Rocky Linux 10. Here are solutions to the most common problems:

Permission Denied Errors: If you encounter permission errors, verify file ownership and SELinux contexts. Run:

sudo chown -R apache:apache /var/www/laravelapp
sudo setsebool -P httpd_can_network_connect_db 1

500 Internal Server Error: Check Apache error logs:

sudo tail -f /var/log/httpd/laravel_error.log

Common causes include missing PHP extensions, incorrect file permissions, or misconfigured virtual hosts.

Database Connection Failures: Verify your database credentials in the .env file match those created in MariaDB. Test the connection manually:

mysql -u laravel_user -p laravel_db

Composer Memory Errors: Increase PHP memory limit by editing /etc/php.ini:

sudo nano /etc/php.ini

Find and modify:

memory_limit = 512M

Restart Apache after making changes:

sudo systemctl restart httpd

SELinux Denials: Check SELinux audit logs:

sudo ausearch -m avc -ts recent

Use sealert to analyze denials and get specific solutions.

Security Best Practices and Hardening

Securing your Laravel installation on Rocky Linux 10 involves multiple layers of protection. Start by protecting the .env file, which contains sensitive configuration data:

sudo chmod 600 /var/www/laravelapp/.env
sudo chown apache:apache /var/www/laravelapp/.env

For production environments, disable debug mode in the .env file:

APP_DEBUG=false
APP_ENV=production

Regularly update both system packages and Composer dependencies:

sudo dnf update -y
cd /var/www/laravelapp && composer update

Configure log rotation to prevent log files from consuming excessive disk space:

sudo nano /etc/logrotate.d/laravel

Add the following configuration:

/var/www/laravelapp/storage/logs/*.log {
    daily
    missingok
    rotate 52
    compress
    notifempty
    create 644 apache apache
}

Implement database security by using strong passwords and limiting database user privileges. Consider using environment-specific database credentials and enabling SSL connections for production deployments.

Next Steps and Production Considerations

With Laravel successfully installed on Rocky Linux 10, consider these enhancements for a robust development or production environment.

Install additional PHP extensions based on your application requirements:

sudo dnf install php-redis php-imagick php-ldap

Set up SSL certificates using Let’s Encrypt for production deployments:

sudo dnf install certbot python3-certbot-apache
sudo certbot --apache -d your-domain.com

Configure a process monitor like Supervisor for queue workers:

sudo dnf install supervisor

Implement caching solutions such as Redis or Memcached for improved performance:

sudo dnf install redis
sudo systemctl start redis
sudo systemctl enable redis

Consider setting up automated backups for your database and application files. Create monitoring solutions to track application performance and server health.

For development teams, integrate version control with Git and set up continuous integration pipelines. Configure development-specific tools like Laravel Telescope for debugging and Laravel Horizon for queue management.

Congratulations! You have successfully installed Laravel. Thanks for using this tutorial for installing Laravel PHP Framework on your Rocky Linux 10 system. For additional help or useful information, we recommend you check the official Laravel website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button