How to Install Laravel PHP Framework With Nginx on CentOS 7

How to Install Laravel PHP Framework With Nginx on CentOS 7
How to Install Laravel PHP Framework With Nginx on CentOS 7

Install Laravel PHP Framework on CentOS

Laravel is a most popular PHP framework which is free and opensource. In a short time span Laravel has got a lot of popularity because of that today Laravel has become the most popular PHP framework. Laravel is basically used for web app development based on MVC architecture. By using Laravel you can build scalable and flexible web applications, eCommerce solutions, restful API’s. Laravel also comes with several tools which you can use to write maintainable, clean PHP code. Here in this tutorial you will learn to install Laravel PHP Framework With Nginx on CentOS 7 (With LEMP stack ).

Prerequisites

Before you start to install Laravel on CentOS 7. You must have the non-root user account on your server with sudo privileges.

Install Nginx

Now you will need to install Nginx. To install Nginx with the latest stable version type following command.

sudo yum install nginx

Confirm Nginx version and installation by typing following command.

systemctl status nginx

Install PHP

Before starting to install PHP on Ubuntu update the package manager index & upgrade old packages by typing following in terminal.

sudo apt update && sudo apt upgrade

Now install PHP and some required extensions for Laravel by 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

Now its time to install MariaDB on CentOS. Run the following command on in terminal to install MariaDB on CentOS.

sudo yum install mariadb-server

Confirm the MariaDB installation by typing following.

mysql -V

Now secure MariaDB installation by typing following command.

sudo mysql_secure_installation

Now you will be asked following questions, it is recommended to answer ‘Y’ for all the 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 dependency manager for PHP. Install composer by typing following command in terminal.

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

Confirm the installation of Composer by typing following command.

composer

Install Laravel

Install Laravel by running the following command it will install the latest version of Laravel.

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

Above command will install all the required packages for Laravel so it can take few minutes.

Set the required permissions for the project root directory typing.

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

Use some of following commands and create database for your Laravel project.

$ 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

Now open .env file and make changes as given below.

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 are having an existing Git project then you can also clone your git project inside laravel directory then install required packages by using composer.

Configure Nginx For Laravel

Create a root directory for your Laravel project by typing following command. In following command we are assuming '/var/www/laravel' as our project root directory.

mkdir -p /var/www/laravel

Now go to Nginx configuration directory and create a file example.com.conf for your project configuration. Replace example.com with your domain name for the configuration file name.

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

Now copy following code inside 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;
         }
 }

Now you should enable the virtual host to do so type the following command in terminal.

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

Check the syntax inside configuration file.

nginx -t

If all is well the restart Nginx by typing following command.

systemctl restart nginx

Testing

Now visit http://example.com replacing example.com with your domain name. You should see output like the following.

How to install laravel on ubuntu 18.04
How to install laravel on Debian 18.04

Conclusion

You have successfully learned how to install Laravel PHP Framework With Nginx on CentOS 7. If you have any queries please don’t forget to comment below.