HugeServer Knowledgebase

How to install Laravel on Centos 7 with Nginx

Introduction

Laravel is a powerful MVC PHP framework, designed for developers who need a simple and elegant toolkit to create full-featured web applications. Laravel was created by Taylor Otwell. In this guide, we will install Laravel on CentOS 7 and will be using Nginx as our web server and will be working with the most recent version of Laravel.

We are assuming that you have root permission, otherwise, you may start commands with “sudo”.

Installing Laravel With Nginx

As you might know, Laravel only works with PHP 5.6 and later so we have to add some repositories to install required version of PHP.

Adding Epel and Webstatic Repositories

You can easily install Epel via yum:

yum install epel-release

You can install Webstatic with RPM package:

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

Run the following command to update your repositories lists:

yum repolist

Install and Configure Nginx and PHP

Nginx can be installed easily using Yum:

yum install nginx

Now we have to install PHP 5.6 with some dependencies:

yum install php56w php56w-gd php56w-intl php56w-mbstring php56w-pdo php56w-process php56w-xml php56w-cli php56w-mcrypt php56w-fpm

Start Nginx and PHP-FPM services

To start and make PHP-FPM and Nginx run as startup execute the following commands:

systemctl enable nginx

systemctl enable php-fpm

systemctl start nginx

systemctl start php-fpm

Configuring Nginx and PHP-FPM

Now we have to make Nginx and PHP-FPM work with each other.

Open the following config file using your favorite text editor:

nano /etc/php-fpm.d/www.conf

Find the line that refers to “listen = 127.0.0.1:9000” and change it like below:

listen = /var/run/php-fpm/php-fpm.sock

Then find and uncomment the following lines:

listen.owner = nobody
listen.group = nobody
listen.mode = 0666

At last, find the two lines that refer to “user” and “group”, they are set with “Apache” by default you have to change the to “nginx” like below:

user = nginx

group = nginx

Save and Exit.

 

Now you have to make some change in Nginx configurations.

Create a new config file using the following command:

nano /etc/nginx/conf.d/default.conf

Paste the following configuration in you new file then save and exit. ( make sure that you replace your domain or your public IP address with the red area)

server {
    listen       80;
    server_name  server_domain_name_or_IP;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html/laravel/public/;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    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/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Save and Exit.

Open the following file with your text editor:

nano /etc/php.ini

Look for “cgi.fix_pathinfo” then uncomment it and set it equal to 0.

cgi.fix_pathinfo=0

Save and Exit.

Installing Laravel

For installing the latest version of Laravel we need to get the Composer dependency manager:

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

We have to move our Composer binary file to an executable path:

mv composer.phar /usr/local/bin/composer

Now execute the following command to get the latest version of Laravel Framework in your web servers root directory.

composer create-project laravel/laravel /usr/share/nginx/html/laravel

It may take a few minutes.

Laravel Permission Adjustments

Move to the following directory:

cd /usr/share/nginx/html/

You have to set the Nginx as the owner of the whole Laravel files with the command below:

chown -R nginx:nginx laravel/

Now move to the Laravel directory:

cd laravel

and change the “storage” directory permission to “775”.

chmod -R 775 storage/

Restart everything

You have to restart your services with the commands blow:

systemctl restart nginx

systemctl restart php-fpm

Test Your Laravel

If you have done everything right you can easily open your browser and see your Laravels index page through your domain or your Public IP address.

You have to see something like the picture below:

Check out Laravel official website for more information and news!

Was this tutorial helpful?

Thank you for your vote.Thank you for your vote.

Similar Posts

7 thoughts on “How to install Laravel on Centos 7 with Nginx”

  1. Thanks for the blog, may I know if I have multiple laravel apps in nginx vhost so how should I trigger multiple queue with supervisorD?
    Let’s say, 2 or 3 Vhost (exp.com, exp2.com etc) so how I run these multiple processes with same SupervisorD?
    Thanks.

    1. Hi Ahmed,

      I don’t think you can. Supervisor can control and monitor Processes and Services, you can add Nginx for monitoring but you can’t manage Vhosts separately.

      Thanks

  2. In case of “Access denied” or “404” response even after you change the folders chmod permissions. Use the command:
    setenforce 0
    I don’t know why, because I discover that solution in a forum, but it resolve the problem 😉

    1. Hello Avargon,

      I’m glad that the article was useful to you.

      The command “setenforce 0” is for disabling the enforcement of the SELinux which may solve the problem but it’s not recommended.

      Make sure that you have correctly set Nginx as the owner of Laravel main directory with the command below:

      # chown -R nginx:nginx laravel/

      Thanks

  3. I am stuck at the end. Http Error 500 after all procedure complete.
    Nginx error.log says 2020/08/26 12:01:43 [error] 2739#0: *1 FastCGI sent in stderr: “PHP message: PHP Warning: require(/usr/share/nginx/html/laravel/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /usr/share/nginx/html/laravel/bootstrap/autoload.php on line 17
    PHP message: PHP Fatal error: require(): Failed opening required ‘/usr/share/nginx/html/laravel/bootstrap/../vendor/autoload.php’ (include_path=’.:/usr/share/pear:/usr/share/php’) in /usr/share/nginx/html/laravel/bootstrap/autoload.php on line 17” while reading response header from upstream, client: 159.224.59.149, server: baton.intent-demo.com, request: “GET / HTTP/1.1”, upstream: “fastcgi://unix:/var/run/php-fpm/php-fpm.sock:”, host: “baton.intent-demo.com”
    I am newbie, so cant resolve the issue.

Leave a Reply to Andrew Cancel reply

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

*