How to Install Laravel With Nginx on CentOS 8

Install Laravel With Nginx on CentOS 8

In this tutorial we will show you how to install Laravel with nginx on CentOS 8.  For those of you who didn’t know, Laravel is a free, open-source PHP web application framework, created by Taylor Otwell and intended for the development of web applications following the model view controller (MVC) architectural pattern. It is a pretty new framework, but with a big potential to become one of the most popular PHP frameworks.

This article assumes you have at least basic knowledge of linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo’ to the commands to get root privileges. I will show you through the step by step installation Laravel PHP Framework with nginx on a CentOS 8 server.

Install Laravel With Nginx on CentOS 8

Step 1. First let’s start by ensuring your system is up-to-date.

sudo dnf update

Step 2. Install LEMP stack server.

A CentOS 8 LEMP stack server is required. If you do not have LEMP installed, you can follow our guide here.

Step 3. Installing Composer.

The composer is required for installing Laravel dependencies. So use below commands to download and use as a command in our system:

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

Confirm the installation of Composer using following command:

composer

Step 3. Installing Laravel on CentOS 8.

Install Laravel using following command:

cd /var/www/
composer create-project --prefer-dist laravel/laravel laravel

We will need to change some folders permissions:

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

Step 4. Configuring MariaDB for Laravel.

By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation script. You should read and below each steps carefully which will set root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB.

mysql_secure_installation

Configure it like this:

- 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

Next we will need to log in to the MariaDB console and create a database for the Laravel. Run the following command:

mysql -u root -p

This will prompt you for a password, so enter your MariaDB root password and hit Enter. Once you are logged in to your database server you need to create a database for Laravel installation:

mysql> CREATE DATABASE laravel;
mysql> GRANT ALL ON laravel.* to 'laravel'@'localhost' IDENTIFIED BY 'your_strong_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=your_strong_password

Step 5. Configure Nginx For Laravel.

First, create a root directory for your Laravel project by typing following command:

mkdir -p /var/www/laravel

Now go to Nginx configuration directory and create a file your-domain.com.conf for your project configuration:

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

Add the following lines:

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;
         }
 }

Save and close the file. Restart the Nginx service for the changes to take effects:

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

Step 6. Configuring Firewall for Laravel.

Create a firewall rule to allow access from external machines to the Laravel:

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

Step 7. Accessing Laravel.

Laravel PHP Framework will be available on HTTP port 80 by default. Open your favorite browser and navigate to http://your_domain.com or http://server-ip-addressand complete the required the steps to finish the installation.

Install Laravel With Nginx on CentOS 8

Congratulation’s! You have successfully installed Laravel. Thanks for using this tutorial for installing Laravel PHP Framework on your CentOS 8 system. For additional help or useful information, we recommend you to 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 a best deal!