Friday, June 14, 2024
HomeHow toHow to Install Laravel PHP Framework on Alma Linux 9

How to Install Laravel PHP Framework on Alma Linux 9

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

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

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

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;

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.

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:

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

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

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

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:\Windows\System32\drivers\etc\hosts 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.

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:\Windows\System32\drivers\etc\hosts file.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular