08.21.2019

Install Laravel 5.6 with Nginx on Centos 7

As we know, Laravel 5.6 only work on PHP 7.1.3 and later, so we need to install required version of PHP and its dependencies

Adding Epel and Remi Repositories

In order to install PHP 7.1 or later on Centos we need to import another repository

In this guide, I use Remi Repo

Remi Repo require epel-realse el7 to install so we need to install epel-release first

yum install epel-release

Now we can install Remi Repo 

rpm -Uvh https://rpms.remirepo.net/enterprise/remi-release-7.rpm

Install Nginx

Default Nginx on Centos 7 is version 1.12

We can install by

yum install nginx

If you want to use newer version of Nginx, you can find it here

http://nginx.org/packages/mainline/centos/7/x86_64/RPMS/

I will install Nginx 1.15

rpm -Uvh http://nginx.org/packages/mainline/centos/7/x86_64/RPMS/nginx-1.15.0-1.el7_4.ngx.x86_64.rpm

Install PHP

I will install newest PHP version (now PHP 7.2.7)

yum --enablerepo=remi,remi-php72 install php php-common php-devel php-mysql php-mbstring php-xml php-fpm php-mcrypt php-gd composer

Install MySQL 

Import MySQL Repository

rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

Latest mysql version is 8.0.*, you can install by

yum install mysql-community-server

If you wish to install MySQL 5.7, please do as follow

vim /etc/yum.repos.d/mysql-community.repo

Find [mysql57-community]  and change

enabled=0

enabled=1

Next find [mysql80-community] and change

enabled=1

enabled=0

Save and exit

Then install MySQL 5.7 by running

yum install mysql-community-server

Install Laravel

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

cd mysite

chown -R nginx: storage/ bootstrap/

Setting Nginx and PHP-FPM

First we will setting php-fpm, open:

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

Replace listen = 127.0.0.1:9000 as below

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

change 2 line as below

user = apache

user = nginx

group = apache

group = nginx

uncomment following lines

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

......

listen.acl_users = apache,nginx

Save and exit file.

Now we will setting Nginx

create new file in conf.d of Nginx

vim /etc/nginx/conf.d/mysite.conf

add content to mysite.conf as  below

server {
    listen       80;
    server_name  mysite.example.com;

    root   /path/to/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 file.

Start Nginx and PHP-FPM

systemctl start nginx

systemctl start php-fpm

Check your work by access IP or your domain name

If page does not appear, try to edit php.ini as below

vim /etc/php.ini

uncomment and set to 0 fix_pathinfo

cgi.fix_pathinfo=0

Save and exit.

Then try again. If you still have problem, please ask below this post.

If you see something like this. You are success.

Leave a comment