How to install Laravel PHP Framework using Nginx on CentOS 8

How to install Laravel PHP Framework using Nginx on CentOS 8

Install Laravel PHP framework on CentOS

Laravel is the most popular PHP framework and is free and open source. In a short time, Laravel has gained much popularity as it has become the most popular PHP framework today. Laravel is basically used for developing web apps based on the MVC architecture. With Laravel, you can build scalable and flexible web applications, e-commerce solutions, and Restful APIs. Laravel also comes with some tools you can use to create clean maintainable PHP code. In this tutorial, you will learn how to install Latinvel PHP Framework with Nginx with CentOS 8 (With LEMP stack).

Prerequisites

Before you start installing Laravel on CentOS 8, you need a non-root user account on a server with sudo privileges.

Install Nginx

Next, you need to install Nginx. To install Nginx with the latest stable version, enter the following command:

sudo yum install nginx

Check the Nginx version and installation by entering the following command:

systemctl status nginx

Install PHP

Install the required extensions for PHP and Laravel using the following command:

sudo yum install php7.2 php7.2-curl php7.2-common php7.2-cli php7.2-mysql php7.2-mbstring php7.2-fpm php7.2-xml php7.2-zip

Install MariaDB

Next, install MariaDB on CentOS. Run the following command in Terminal to install MariaDB on CentOS:

sudo yum install mariadb-server

Confirm MariaDB installation by entering:

mysql -V

Enter the following command to secure the MariaDB installation:

sudo mysql_secure_installation

The following question appears. We recommend that you answer “Y” to all questions.

Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

Install Composer

Composer is a PHP dependency manager. Install Composer by entering the following command in a terminal:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Verify the Composer installation by entering the following command:

composer

Install Laravel

Run the following command to install Laravel and the latest version of Laravel will be installed.

sudo yum install unzip
cd /var/www/
composer create-project --prefer-dist laravel/laravel laravel

The above command may take a few minutes to install all required packages for Laravel.

Set the permissions required to enter the project root directory.

sudo chown -R www-data:www-data /var/www/laravel/
sudo chmod -R 755 /var/www/laravel/

Create a database for your Laravel project using some of the following commands:

$ mysql -u root -p
mysql> CREATE DATABASE laravel;
mysql> GRANT ALL ON laravel.* to 'laravel'@'localhost' IDENTIFIED BY 'new_password';
mysql> FLUSH PRIVILEGES;
mysql> quit

In session .env File and make changes as follows:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=new_password

Note: If you have an existing Git project, you can also clone the git project in the laravel directory and use composer to install the required packages.

Nginx configuration for Laravel

Create the root directory of your Laravel project by entering the following command: In the following command: '/var/www/laravel' As the project root directory.

mkdir -p /var/www/laravel

Go to the Nginx configuration directory and create a file example.com.conf for the project configuration. Replace example.com with the domain name in the configuration file name.

cd /etc/nginx/
nano sites-available/laravel.conf

Copy the following code into the above file.

server {
         listen 80;
         listen [::]:80 ipv6only=on;
 
         # Log files for Debugging
         access_log /var/log/nginx/laravel-access.log;
         error_log /var/log/nginx/laravel-error.log;
 
         # Webroot Directory for Laravel project
         root /var/www/example.com/public;
         index index.php index.html index.htm;
 
         # Your Domain Name
         server_name example.com;
 
         location / {
                 try_files $uri $uri/ /index.php?$query_string;
         }
 
         # PHP-FPM Configuration Nginx
         location ~ .php$ {
                 try_files $uri =404;
                 fastcgi_split_path_info ^(.+.php)(/.+)$;
                 fastcgi_pass unix:/run/php/php7.2-fpm.sock;
                 fastcgi_index index.php;
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
         }
 }

Next, enable the virtual host and enter the following command in a terminal:

ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/

Check the syntax in the configuration file.

nginx -t

If all goes well, restart Nginx by entering the following command:

systemctl restart nginx

Testing

Go to http://example.com and replace example.com with your domain name. You will see output similar to the following:

How to install laravel on Ubuntu 18.04 How to install laravel on Debian 18.04

Conclusion

You learned how to install Laravel PHP Framework on CentOS 8 using Nginx. If you have any questions, don’t forget to comment below.

Sidebar