CentOSLinuxTutorials

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 the step-by-step installation of the Laravel PHP Framework with Nginx on a CentOS 8 server.

Prerequisites

  • A server running one of the following operating systems: CentOS 8.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Laravel With Nginx on CentOS 8

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

sudo dnf clean all
sudo dnf update

Step 2. Install the 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 the 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 the following command:

composer

Step 3. Installing Laravel on CentOS 8.

Install Laravel using the 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 step carefully which will set a 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 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 the following command:

mkdir -p /var/www/laravel

Now go to the 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 effect:

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-address and complete the required steps to finish the installation.

Install Laravel With Nginx on CentOS 8

Congratulations! 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 the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button