Home VPS Knowledge How to Install Laravel with Nginx on CentOS 7 (LEMP)

How to Install Laravel with Nginx on CentOS 7 (LEMP)

by Lê Quyền
Published: Last Updated on
A+A-
Reset

Introduction

Laravel is a powerful and popular web application framework, while Nginx is an open-source web server with high performance. On CentOS 7, a popular Linux operating system, we can combine Laravel and Nginx to deploy the best possible web applications. In this article, we will learn how to install and configure Laravel with Nginx on CentOS 7 (LEMP).

Steps to Install Laravel with Nginx on CentOS 7

Step 1: Install LEMP Stack

First, you need to install LEMP on the server. You can proceed with the installation:

Step 2: Install PHP Extensions

After installing LEMP Stack in Step 1, we will proceed to install and configure Laravel on CentOS 7. After installing LEMP Stack in Step 1, we will proceed to install and configure Laravel on CentOS 7.

In this guide, I use additional sudo due to the specific nature of some VPS services that require sudo privileges.

sudo yum install php php-fpm php-mbstring php-xml php-zip
sudo systemctl restart php-fpm
CleanShot 2023 12 05 at 03.33.47

Step 3: Install Composer

Before installing Laravel, let’s install Composer first. Use the following command to install Composer:

sudo yum install composer -y

Step 4: Install Git

Next, install Git, an essential tool for developers:

sudo yum install git -y

Step 5: Download and Configure Laravel

If you are installing Laravel version 8.x or later, it requires PHP 8.1 or later. Use the following command to clone Laravel:

sudo git clone -b 8.x https://github.com/laravel/laravel.git
CleanShot 2023 12 05 at 16.35.59@2x

After downloading Laravel, navigate to the Laravel directory and install the necessary dependencies:

cd laravel
composer.phar install
CleanShot 2023 12 05 at 16.37.25@2x

Wait for the installation process to complete!

CleanShot 2023 12 05 at 17.56.14@2x

copy the .env.example file to .env:

sudo cp .env.example .env

Generate the application key:

sudo php artisan key:generate
CleanShot 2023 12 05 at 19.10.49@2x

Step 6: Configure Virtual Host

Use the vi command to create a new configuration file:

vi /etc/nginx/conf.d/default.conf

Insert the following configuration, making sure to note that the document root is the public folder of Laravel. Save and exit the vi editor.

server {
    listen  80;
    server_name laravel.quyenlt.com;

    root   /var/www/html/laravel/public;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php_fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Step 7: Restart Nginx

Next, check if the Nginx configuration has any errors using the commandnginx -t. If there are no errors, restart NGINX to apply the new configurations.

systemctl  restart nginx

Afterward, we need to change the ownership of the Laravel project directory to the nginx user.

chown -R nginx:nginx /var/www/html/laravel

And change the permissions of the storage directory to 755.

sudo chmod 755 /var/www/html/laravel/storage

Step 8: Check the installation result

Now, open your web browser and enter http://IP or http://domain.com to see the results.

CleanShot 2023 12 05 at 19.55.35@2x

Conclusion

On Centos 7, we can install and configure Laravel with Nginx to deploy powerful web applications. In this blog post, we learned how to install Nginx and Laravel on Centos 7, as well as how to configure Nginx to serve Laravel applications. Hopefully, this information will help you successfully deploy your Laravel application on Centos 7.

If you need assistance, you can contact support in the ways below:

  • Hotline 247: 028 888 24768 (Ext 0)
  • Ticket/Email: You use the email to register for the service and send it directly to: support@azdigi.com.
Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign