How to deploy a LEMP Stack with PHP 7.1 on CentOS 7

How to deploy a LEMP Stack with PHP-7.1 on CentOS 7

Spread the love

In this tutorial we will walk you through the steps on how to deploy a LEMP (Linux, Nginx, MySQL and PHP-FPM) stack with PHP-7.1 on a CentOS 7.
A LEMP stack is a synonym of LEMP server or LEMP web server. It refers to a set-up which includes Linux, Nginx, MySQL (MariaDB) and PHP.

1. Update the system

As usual before beginning a new installation on your VPS, update the system first:

# yum update

2. Install MariaDB(MySQL)

MariaDB is a drop-in replacement for MySQL and is the default database server used in CentOS 7 (RHEL7). Install it using yum like this:

# yum -y install mariadb mariadb-server

To stop intruders from trying to access our MariaDB server while we’re setting it up, open the “/etc/my.cnf.d/server.cnf” file for editing and add the “bind-address” directive to have it listen only on the localhost IP address:

# nano /etc/my.cnf.d/server.cnf

[mysqld]
bind-address = 127.0.0.1

Now, restart the MariaDB server and enable it on startup with the following commands:

# systemctl start mariadb
# systemctl enable mariadb

You can also run the mysql_secure_installation script to improve MariaDB (MySQL) security:

# mysql_secure_installation

Enter current password for root (enter for none): ENTER
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

3. Install Nginx

We are going to install Nginx as it is a key component of our LEMP stack and will serve our website to it’s users.
Nginx is not available in the official CentOS repository so we need to install the EPEL repository first:

# yum install epel-release

Type in the following command to install Nginx:

# yum -y install nginx

Once Nginx is installed, run these two commands to start the web server and enable it on boot:

# systemctl start nginx
# systemctl enable nginx

Now, check if Nginx is running by executing this command:

# systemctl status nginx

The output of the command above should be similar to this:

● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running)
 Main PID: 10092 (nginx)
   CGroup: /system.slice/nginx.service
           ├─10092 nginx: master process /usr/sbin/nginx
           ├─10093 nginx: worker process
           └─10094 nginx: worker process

You can also check if Nginx is running by going to http://YOUR_IP in your web browser.

4. Install PHP7.1-FPM

Now we are going to install PHP7.1-FPM as we will need it to process the PHP files our website contains.
Because PHP-7.1 is not available in the official CentOS repository we need to install the Remi repository first:

# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm && rpm -Uvh remi-release-7.rpm

The following command installs PHP7.1-FPM and enables the remi-safe repository which gives priority to the distribution and EPEL repository packages, meaning it won’t conflict with any of these packages if you upgrade your system:

# yum --enablerepo=remi-safe -y install php71-php-fpm

Once PHP7.1-FPM is installed, run these commands to start PHP7.1-FPM and enable it on boot:

# systemctl start php71-php-fpm
# systemctl enable php71-php-fpm

Now, check if PHP7.1-FPM is running by executing this command:

# systemctl status php71-php-fpm

The output of the command above should be similar to this:

● php71-php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php71-php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running)
 Main PID: 10792 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/php71-php-fpm.service
           ├─10792 php-fpm: master process (/etc/opt/remi/php71/php-fpm.conf)
           ├─10793 php-fpm: pool www
           ├─10794 php-fpm: pool www
           ├─10795 php-fpm: pool www
           ├─10796 php-fpm: pool www
           └─10797 php-fpm: pool www

Note: The configuration files for the PHP-7.1 Remi repository package are located in the “/etc/opt/remi/php71” directory.

Next, change the configuration of the default PHP7.1-FPM pool to listen on a unix socket and use Nginx’s user and group:

# nano /etc/opt/remi/php71/php-fpm.d/www.conf

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php7.1-fpm.sock

Save the changes and restart the PHP7.1-FPM service:

# systemctl restart php71-php-fpm

5. Configuring Nginx

Now that you have everything up and running we will create a document root directory and a server block for our site in Nginx, start by typing in the following command:

# mkdir -p /var/www/example.com

We will create the Nginx server block for our domain and configure it to use PHP-7.1:

# nano /etc/nginx/conf.d/example.com.conf

server {
        listen 80;
        
        root /var/www/example.com;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name example.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server using the /run/php/php7.1-fpm.sock socket
        #
        location ~ \.php$ {
                include fastcgi.conf;
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}

Save the changes and test the configuration:

# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If there are no errors copy your website’s contents to the “/var/www/example.com” directory, change the owner of the directory and restart Nginx with the following commands:

# chown -R nginx:nginx /var/www/example.com
# systemctl restart nginx

And now you should see your site up and running when you access http://example.com in your web browser.
Note: Remember to replace example.com with your own domain name.

Of course, you don’t have to install a LEMP Stack with PHP-7.1 on CentOS 7, if you use one of our Linux Support Services, in which case you can simply ask our expert Linux admins to install LEMP with PHP-7.1 on a CentOS 7 server for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to deploy a LEMP Stack with PHP-7.1 on CentOS 7, please share it with your friends on the social networks using the buttons below or simply leave a comment in the Comments Section below. Thanks

8 thoughts on “How to deploy a LEMP Stack with PHP-7.1 on CentOS 7

  1. Check the ‘fastcgi_pass’ setting in your nginx server blocks, and change it to:

    fastcgi_pass unix:/run/php/php7.1-fpm.sock;

  2. When you run the [LISTEN, USER AND GROUP] step on a CentOS 7.4 server, the following message is displayed:

    ERROR: unable to bind listening socket for address ‘/run/php/php7.1-fpm.sock’: No such file or directory

  3. Problem solved!

    I created the php directory inside run [/ run / php /]. Then I created [php7.1-fpm.sock] inside php. By accessing the / run / executed [chown nginx. php -R].

    Note that if you do not execute chown, the error below is reported. Then do everything above, which corrects the fault.

    2018/05/06 09:46:44 [crit] 21007 # 0: * 35 connect () to unix: /run/php/php7.1-fpm.sock failed (13: Permission denied) while connecting to upstream, client : MY_IP, server: MY_DOMAIN, request: “GET /info.php HTTP / 1.1”, upstream: “fastcgi: // unix: /run/php/php7.1-fpm.sock:”, host: “MY_DOMAIN”

    ==> /var/log/nginx/access.log <==
    My_IP – – [06 / May / 2018: 09: 46: 44 -0400] "GET /info.php HTTP / 1.1" 502 575 "-" "Mozilla / 5.0 (Windows NT 6.3; Win64; x64) AppleWebKit / 537.36 KHTML, like Gecko) Chrome / 66.0.3359.139 Safari / 537.36 "" – "

  4. Excellent post, so far the best! Congratulations! Hugs from all over Brazil!

    What do you think of creating Part 2 to include PHPMyAdmin?

  5. Thanks for the post!,
    I was getting an error in the last step 4 (before restart php), then it works for me:

    Change listen = /run/php/php7.1-fpm.sock
    by listen = /var/run/php71.sock

    I also did it :

    – Uncomment and change socket owner
    listen.owner = nginx
    listen.group = nginx
    listen.mode = 0660

  6. Hi

    This is not working.

    Can you tell what chown to php.sock

    As when I load site page it load only html page. PHP code is not loading.

Leave a Reply

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