CentOSRHEL Based

How To Install Laravel on CentOS Stream 10

Install Laravel on CentOS Stream 10

In this tutorial, we will show you how to install Laravel on CentOS Stream 10. Laravel has become the go-to PHP framework for developers seeking to build robust and scalable web applications. Its elegant syntax, powerful features, and extensive ecosystem make it a top choice for projects of all sizes. When it comes to deploying Laravel applications, CentOS Stream 10 offers a stable and secure environment that’s perfect for hosting your PHP projects. In this comprehensive guide, we’ll walk you through the process of installing Laravel on CentOS Stream 10, covering everything from system prerequisites to post-installation verification.

Prerequisites for Installation

Before diving into the Laravel installation process, it’s crucial to ensure your CentOS Stream 10 system meets the necessary requirements. Let’s start by examining the essential components and preparing your server environment.

System Requirements

To run Laravel efficiently on CentOS Stream 10, your server should meet or exceed the following specifications:

  • Minimum 2GB RAM (4GB recommended for optimal performance)
  • At least 1 CPU core (2 cores recommended for better responsiveness)
  • Minimum 20GB of free disk space

Laravel 11.x, the latest version as of this writing, requires PHP 8.2 or higher. It’s essential to keep this in mind as we proceed with the installation process.

Initial Server Setup

Begin by updating your CentOS Stream 10 system to ensure you have the latest packages and security patches. Open a terminal and run the following command:

sudo dnf update -y

Next, install the EPEL (Extra Packages for Enterprise Linux) repository, which provides additional packages that we’ll need later:

sudo dnf install epel-release

Essential Component Overview

Laravel requires several key components to function properly:

  • Web server: Apache or Nginx
  • Database server: MySQL, MariaDB, PostgreSQL, or SQLite
  • PHP with specific extensions enabled

For this guide, we’ll focus on using Apache as the web server and MariaDB as the database server, as they are commonly used in CentOS environments.

Installing Core Dependencies

With our system updated and the EPEL repository added, let’s proceed to install the core dependencies required for Laravel.

PHP 8.2 Installation

CentOS Stream 10 doesn’t include PHP 8.2 in its default repositories. We’ll use the Remi repository to install the required PHP version. First, add the Remi repository:

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

Now, install PHP 8.2 and its core modules:

sudo dnf module install php:remi-8.2

PHP Extensions Configuration

Laravel requires several PHP extensions to function correctly. Install these essential packages:

sudo dnf install php-gd php-mbstring php-xml php-zip php-curl php-tokenizer php-json

These extensions provide crucial functionality for Laravel, such as image processing, multibyte string support, and JSON handling.

Composer Installation

Composer is a dependency management tool for PHP that Laravel relies on. Install it using the official method:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Alternatively, you can install Composer from the EPEL repository:

sudo dnf --enablerepo=epel install composer

Verify the installation by running:

composer --version

Laravel Installation Process

With all the dependencies in place, we can now proceed to install Laravel itself.

Project Creation via Composer

Use Composer to create a new Laravel project. Navigate to your desired directory (e.g., /var/www/) and run:

composer create-project --prefer-dist laravel/laravel project-name

Replace “project-name” with your desired application name. This command downloads the latest Laravel version and sets up a new project structure.

Directory Permissions Setup

Laravel requires specific permissions to function correctly. Set the appropriate ownership and permissions:

sudo chown -R apache:apache /var/www/project-name
sudo chmod -R 755 /var/www/project-name/storage

These commands ensure that the web server can read and write to the necessary directories.

Environment Configuration

Laravel uses an .env file for environment-specific configuration. Generate an application key by running:

cd /var/www/project-name
php artisan key:generate

This command sets up a unique encryption key for your application. Next, open the .env file and configure your database settings:

nano .env

Update the following lines with your database information:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password

Web Server Configuration

Now that Laravel is installed, we need to configure the web server to serve our application.

Apache Setup

First, install Apache if it’s not already on your system:

sudo dnf install httpd

Create a new virtual host configuration file:

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

Add the following configuration, adjusting the ServerName and DocumentRoot as needed:

<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/project-name/public
    <Directory /var/www/project-name/public>
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog /var/log/httpd/laravel_error.log
    CustomLog /var/log/httpd/laravel_access.log combined
</VirtualHost>

Save the file and restart Apache:

sudo systemctl restart httpd

Nginx Alternative Setup

If you prefer Nginx, install it and configure a server block:

sudo dnf install nginx
sudo nano /etc/nginx/conf.d/laravel.conf

Add the following configuration:

server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/project-name/public;

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Save the file and restart Nginx:

sudo systemctl restart nginx

Firewall Adjustments

Allow HTTP and HTTPS traffic through the firewall:

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

Database Configuration

Laravel supports various database systems. We’ll set up MariaDB, a popular choice for CentOS environments.

MariaDB Installation

Install MariaDB server and client:

sudo dnf install mariadb-server mariadb

Start and enable MariaDB:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Secure your MariaDB installation:

sudo mysql_secure_installation

Database Creation

Log into MariaDB and create a database for your Laravel project:

mysql -u root -p
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Laravel Database Setup

Update your Laravel .env file with the database credentials:

nano /var/www/project-name/.env

Modify the following lines:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=your_password

Security Hardening

Securing your Laravel installation is crucial for maintaining a robust and reliable application.

SELinux Configuration

SELinux can sometimes interfere with Laravel’s functionality. If you encounter issues, you may need to set it to permissive mode:

sudo setenforce 0
sudo sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config

However, it’s recommended to configure SELinux properly for production environments rather than disabling it entirely.

File Ownership Best Practices

Ensure proper file ownership for your Laravel application:

sudo chown -R apache:apache /var/www/project-name
sudo chmod -R 755 /var/www/project-name
sudo chmod -R 775 /var/www/project-name/storage
sudo chmod -R 775 /var/www/project-name/bootstrap/cache

SSL Implementation

For production environments, it’s crucial to secure your site with SSL. Let’s Encrypt provides free SSL certificates. Install Certbot:

sudo dnf install certbot python3-certbot-apache

Obtain and install a certificate:

sudo certbot --apache -d yourdomain.com

Post-Installation Verification

After completing the installation process, it’s important to verify that everything is working correctly.

Development Server Test

Laravel includes a built-in development server for testing. Run it with:

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

Access your application at http://your_server_ip:8000 to ensure it’s working.

Install Laravel on CentOS Stream 10

Basic Route Testing

Create a test route to verify Laravel’s routing system. Edit routes/web.php:

Route::get('/test', function () {
    return 'Laravel is working!';
});

Access http://your_server_ip/test to see the message.

Troubleshooting Common Issues

If you encounter a “Permission denied” error, double-check your directory permissions:

sudo chown -R apache:apache /var/www/project-name
sudo chmod -R 755 /var/www/project-name/storage

For Composer memory limit issues, increase the PHP memory limit in php.ini:

sudo nano /etc/php.ini

Find and modify the memory_limit line:

memory_limit = 256M

Maintenance & Updates

Keeping your Laravel installation up-to-date is crucial for security and performance.

Composer Update Strategy

Regularly update your Laravel dependencies:

cd /var/www/project-name
composer update --prefer-dist --no-dev

Laravel Version Upgrades

For major Laravel upgrades, always refer to the official upgrade guide in the Laravel documentation. Each version may have specific upgrade steps.

Backup Procedures

Implement a regular backup strategy for your Laravel application. Key areas to backup include:

  • Application code (/var/www/project-name)
  • Database (use mysqldump for MariaDB)
  • .env file (contains sensitive configuration data)

Congratulations! You have successfully installed Laravel. Thanks for using this tutorial to install the Laravel PHP framework on CentOS 10 Stream. 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