Laravel On Nginx Centos 7


Laravel On Nginx Centos 7

Introduction

Laravel is an open-source MVC (model-view-controller) web framework for PHP. It is free and has been used by many developers to create great websites and applications. With powerful features such as routing, authentication, and more, it has proven to be useful for web development projects. In this tutorial, we will show you how to setup a development environment on an Nginx server running CentOS 7.

Prerequisites

For this tutorial, you need a system running CentOS 7, an SSH client such as PuTTY, and a non-root user with sudo privileges. Note that the non-root user needs to have sudo privileges in order to perform some of the steps.

Install Nginx

Nginx is an open-source HTTP and reverse proxy server. It is commonly used for web hosting and is highly optimized for throughput and low memory usage. To install Nginx, run the following command:

sudo yum install nginx

This will install the latest version of Nginx. After the installation is complete, start the service by running the following command:

sudo systemctl start nginx

You can also enable it to start automatically after a reboot by running the following command:

sudo systemctl enable nginx

Verify that the service is running by running the following command:

sudo systemctl status nginx

Install PHP-FPM

The FastCGI Process Manager (PHP-FPM) is an alternative PHP FastCGI implementation. It is a FastCGI process manager that allows you to configure different environments for different applications. To install PHP-FPM, run the following command:

sudo yum install php-fpm

After the installation is complete, start the service by running the following command:

sudo systemctl start php-fpm

You can also enable it to start automatically after a reboot by running the following command:

sudo systemctl enable php-fpm

Verify that the service is running by running the following command:

sudo systemctl status php-fpm

Install MariaDB

MariaDB is an open-source relational database management system (RDBMS). It is a fork of the popular MySQL RDBMS and is designed to be a drop-in replacement for MySQL. To install MariaDB, run the following command:

sudo yum install mariadb-server

After the installation is complete, start the MariaDB service by running the following command:

sudo systemctl start mariadb

You can also enable it to start automatically after a reboot by running the following command:

sudo systemctl enable mariadb

Verify that the service is running by running the following command:

sudo systemctl status mariadb

Install Composer

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will automatically download and install those libraries. To install Composer, run the following command:

curl -sS https://getcomposer.org/installer | php

This will download the latest version of Composer and install it in the current directory. You can move it to a directory that is in your PATH, such as /usr/bin, by running the following command:

mv composer.phar /usr/bin/composer

Install Laravel

Now that we have all the prerequisites installed, we can install Laravel. To do this, change to the directory where you want to install Laravel and run the following command:

composer create-project laravel/laravel --prefer-dist

This will download and install the latest version of Laravel in the current directory. Once the installation is complete, you can start the development server by running the following command:

php artisan serve

This will start the Laravel development server on port 8000. You can access it from your browser by navigating to http://localhost:8000.

Configure Nginx

Now that we have Laravel installed, we need to configure Nginx to use it. To do this, create a new configuration file in the Nginx configuration directory with the following command:

sudo nano /etc/nginx/conf.d/laravel.conf

This will open the Nano text editor. Paste the following into the file and save it:


server {
listen 80;
root /path/to/laravel/project/public;
index index.php index.html;
server_name example.com;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ .php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

This file configures Nginx to serve files from the public directory of the Laravel project and handle requests for PHP files with the PHP-FPM service. Replace the example.com with your domain name. After the configuration is complete, test the syntax by running the following command:

sudo nginx –t

If the syntax is correct, restart Nginx by running the following command:

sudo systemctl restart nginx

Conclusion

In this tutorial, we have shown you how to setup a development environment on an Nginx server running CentOS 7. We have installed and configured Nginx, PHP-FPM, MariaDB, Composer, and Laravel. Now you can start developing your websites and applications with Laravel.

FAQs

Q1: What is Laravel?

A: Laravel is an open-source MVC (model-view-controller) web framework for PHP.

Q2: What is PHP-FPM?

A: The FastCGI Process Manager (PHP-FPM) is an alternative PHP FastCGI implementation. It is a FastCGI process manager that allows you to configure different environments for different applications.

Q3: How do I install Laravel?

A: To install Laravel, run the following command: composer create-project laravel/laravel --prefer-dist. This will download and install the latest version of Laravel in the current directory.

Q4: How do I configure Nginx to serve Laravel?

A: To configure Nginx to serve Laravel, create a new configuration file in the Nginx configuration directory and paste the following code:

server {
listen 80;
root /path/to/laravel/project/public;
index index.php index.html;
server_name example.com;

location / {
try_files $uri

Leave a Reply

Your email address will not be published. Required fields are marked *