Laravel is a web application framework based on PHP that is used to build enterprise and robust full-stack web applications. It follows model-view-controller (MVC) architecture and is based on Symfony. Laravel supports multiple databases, including MySQL, PostgreSQL, SQLite, and SQL Server. Laravel also provides scaffolding for secure authentication.

This tutorial will show you how to install Laravel on the Alma Linux 9 server. We’ll walk you through the Laravel installation with LAMP Stack (Linux, Apache/Httpd, MariaDB, and PHP) and Composer.

Prerequisites

Before moving forward, make sure you have the following:

  • An Alma Linux 9 server.
  • A non-root user with administrator privileges.
  • A local domain name.
  • A SELinux with permissive mode.

Installing LAMP Stack and Composer

Laravel is one of the most popular PHP web frameworks for building web applications. To install Laravel, you need to install dependencies, including the LAMP Stack (Apache/Httpd, MariaDb, and PHP) and Composer. Currently, Laravel requires PHP 8.3. You must install PHP 8.3 via a third-party repository.

First, add the third-party repository EPEL and Remi to your Alma Linux server using the command below. The EPEL repository will be used for installing packages like Composer, while the Remi repository provides multiple PHP versions for Laravel.

sudo dnf install epel-release dnf-utils http://rpms.remirepo.net/enterprise/remi-release-9.rpm

<img alt="add repo" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/7-install-repo.png66509b7c05f85.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="304" loading="lazy" src="data:image/svg xml,” width=”750″>

Now enable the PHP 8.3 repository via Remi using the command below.

sudo dnf reset php -y

sudo dnf module enable php:remi-8.3 -y

Next, install the LAMP Stack (Apache/Httpd, MariaDB, and PHP) and Composer packages with the following command. Inpu y to confirm with the installation.

sudo dnf install httpd mariadb-server composer php php-curl php-bcmath php-json php-mbstring php-xml php-tokenizer php-zip

<img alt="install deps" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/2-install-deps.png66509b7c22881.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="380" loading="lazy" src="data:image/svg xml,” width=”750″>

Once installation is complete, start and enable the httpd service with the command below.

sudo systemctl start httpd

sudo systemctl enable httpd

Then start and enable the MariaDB server with the following command.

sudo systemctl start mariadb

sudo systemctl enable mariadb

Now, verify the PHP and Composer versions using the command below. You should see that PHP 8.3 and Composer 2.7.1 is installed.

php -v

sudo -u apache composer -v

<img alt="check php and composer" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/3-check-php-composer.png66509b7c43834.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="372" loading="lazy" src="data:image/svg xml,” width=”692″>

Lastly, run the command below to verify your PHP modules. Make sure the fileinfo, mbstring, and openssl are enabled.

php -m

Configuring MariaDB

With PHP configured, you will secure the MariaDB server and create a new database and user for Laravel. The MariaDB server provides a MariaDB-secure-installation utility for securing the MariaDB server and the MariaDB client for connecting to the MariaDB server via the command line.

To secure the MariaDB server, run the command below and you will be asked for some of the MariaDB server configurations. Input Y or N to agree or disagree on applying new MariaDB changes.

sudo mariadb-secure-installation

Below are some of the MariaDB server configurations you will be asked for:

  • Switch to unix_socket authentication?. Input n and press ENTER. The default MariaDB root user is already protected. optionally, you can also enable it by typing y for yes.
  • Change the root password?. Input y to confirm and set up your new MariaDB root password.
  • Remove anonymous user?. Input y to confirm.
  • Disallow root login remotely? Input y to confirm. Only local connection will be allowed if you are using the MariaDB root user.
  • Remove the test database and access to it?. Input y to confirm and remove the default database ‘test’.
  • Lastly, input y again to reload all tables privileges on your MariaDB server and apply new changes.

After MariaDB is secured, you will create a new database and user for Laravel via the mariadb client.

Log in to the MariaDB server with the mariadb client command below. Input your MariaDB root password when prompted.

sudo mariadb -u root -p

Now run the following queries to create a new database and user for Laravel. In this example, you will create a new database laravelapp, a user laravel, with the password p4ssw0rd. Adjust the details database and user below with your information.

CREATE DATABASE laravelapp;

CREATE USER laravel@localhost IDENTIFIED BY 'p4ssw0rd';

GRANT ALL PRIVILEGES ON laravelapp.* TO laravel@localhost;

FLUSH PRIVILEGES;

<img alt="create database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/4-create-database.png66509b7c61715.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="257" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, run the query below to verify privileges for user laravel. Make sure that the user laravel can access the database laravelapp.

SHOW PRIVILEGES FOR laravel@localhost;

Lastly, type quit to exit from the MariaDB server.

<img alt="show privileges" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/5-show-user.png66509b7c88813.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="231" loading="lazy" src="data:image/svg xml,” width=”750″>

Creating Laravel Project

So now you have configured both PHP and MariaDB, let’s download and install Laravel to your system. You will create and set up the project directory, then download and install the Laravel project via Composer.

Create new directories for Laravel project /var/www/laravelapp and additional directories .cache and .config for storing Composer cache and configuration. Then, change the ownership of those directories to the user apache.

mkdir -p /var/www/laravelapp /usr/share/httpd/.composer/{.cache,.config}

sudo chown -R apache:apache /var/www/laravelapp /usr/share/httpd/.composer

Go to the /var/www/laravelapp directory and run the composer command below to download and install Laravel.

cd /var/www/laravelapp/

sudo -u apache composer create-project laravel/laravel .

The download process should be like this:

<img alt="install Laravel" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/6-install-laravel.png66509b7ca4758.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="527" loading="lazy" src="data:image/svg xml,” width=”750″>

Once Laravel is downloaded, open the .env file using the following nano editor command.

nano .env

Change the APP_URL with your local domain name, then change the database details with your MariaDB database information.

APP_URL=http://dev.hwdomain.local

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=laravelapp

DB_USERNAME=laravel

DB_PASSWORD=password

When finished, save the file and exit.

Now run the command below to migrate the database and fill your tables with new sample data.

sudo -u apache php artisan migrate

sudo -u apache php artisan db:seed

<img alt="migrate database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/7-migrate-seed-database-mariadb.png66509b7ce605e.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="204" loading="lazy" src="data:image/svg xml,” width=”750″>

Setting up Httpd Virtual Host

After installing Laravel, you will create a new Httpd virtual host configuration that Laravel will use. For this, make sure to have a local domain name or any/random domain as you want. You can configure this domain later.

Create a new httpd virtual host configuration /etc/httpd/conf.d/laravel.conf using the following nano editor command.

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

Insert the following configuration and make sure to change the ServerName option with your Laravel domain name.

ServerAdmin [email protected]

ServerName dev.hwdomain.local

DocumentRoot /var/www/laravelapp/public



Options FollowSymLinks

AllowOverride None





AllowOverride All

ErrorLog /var/log/httpd/laravel-error.log

CustomLog /var/log/httpd/laravel-access.log combined

Save the file and exit the editor.

Now run the command below to verify your httpd configuration. If you have the correct httpd syntax, you should get an output Syntax OK.

sudo apachectl configtest

Lastly, restart the httpd service to apply your new virtual host configuration for Laravel.

sudo systemctl restart httpd

<img alt="setup vhost" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/8-setup-vhost.png66509b7d1c0ff.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="202" loading="lazy" src="data:image/svg xml,” width=”640″>

Setting up Firewalld

before accessing your Laravel installation, you must open both HTTP and HTTPS ports on your system, which can be done via Firewalld.

Open both HTTP and HTTPS traffic to your Alma Linux server with the command below.

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

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

Now reload firewalld to apply the changes.

sudo firewall-cmd --reload

Lastly, verify the firewalld list rules using the following command. You will see both HTTP and HTTPS services added to firewalld.

sudo firewall-cmd --list-all

<img alt="setup firewalld" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/11-firewalld.png66509b7d38dad.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="278" loading="lazy" src="data:image/svg xml,” width=”750″>

Accesing Laravel from Local Computer

To set up a local domain name for your Laravel installation, use the following:

  • For Windows users, modify the file C:WindowsSystem32driversetchosts as administrator.
  • For Linux and Mac users, open the /etc/hosts file as root privileges.

Insert the configuration below and make sure to change the IP address and domain name with your information.

192.168.5.60 dev.hwdomain.local

Save and exit the file.

Now open your web browser and visit your Laravel installation http://dev.hwdomain.local/. If your installation is successful, you should get the following Laravel index page.

<img alt="laravel" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/1-default-home.png66509b7d469ab.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="471" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

Congratulations! You have completed the installation of Laravel on Alma Linux 9 with the LAMP Stack (Linux, Apache/Httpd, MariaDB, and PHP) and Composer. You also configured firewalld to open both HTTP and HTTPS ports, then also configured the local domain name for your Laravel development via /etc/hosts file or C:WindowsSystem32driversetchosts file.