How to install LEMP (Linux, Nginx, MariaDB & PHP-FPM) on a CentOS 7 VPS

Leave reply

Nginx-logoThe following article will walk you through the steps on how to install LEMP (Linux, Nginx, MariaDB & PHP-FPM) on one of our CentOS 7 Linux Virtual Servers.

If instead, you are looking for how to set-up LAMP, then please refer to our guide on how to install LAMP (Linux Apache, MariaDB & PHP) on a CentOS 7 VPS

What is LEMP?

A LEMP stack is a synonym of LEMP server or LEMP web server. It refers to a set-up which includes Linux, Nginx, MariaDB (MySQL) and PHP.

 

UPDATE THE SYSTEM

As usual, SSH to your Linux VPS, initiate a screen session and make sure your CentOS 7 is fully up-to-date by running the following commands:

## screen -U -S lemp-centos7
## yum update

 

INSTALL MARIA DB (MYSQL)

MariaDB is a drop-in replacement for MySQL and is the default database server used in CentOS 7 (RHEL7). Proceed with installing it using yum as in:

## yum install mariadb mariadb-server mysql

Next, open /etc/my.cnf.d/server.cnf using your favorite text editor and add bind-address = 127.0.0.1 within the [mysqld] block. For example:

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

[mysqld]
#log-bin=mysql-bin
#binlog_format=mixed
bind-address = 127.0.0.1

This will bind MariaDB to listen on localhost only, which is considered to be a good security practice. OK, now restart the MariaDB database server and enable it to start on system start-up using:

## systemctl restart mariadb
## systemctl status mariadb
## systemctl enable mariadb

Optionally, you can run the mysql_secure_installation post-installation script to improve MariaDB (MySQL) installation security. For example:

## 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

 

INSTALL NGINX HTTP SERVER

Nginx is not yet available in CentOS 7 official repositories at the time of writing this article. So, to be easily installed and managed using yum, we can use the repository for the latest stable version of Nginx for CentOS 7.

For example:

## rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
## yum install nginx

Once it’s installed, run the following command to find out the number of CPUs available in your SSD VPS:

## grep -c processor /proc/cpuinfo
2

This number should represent the number of nginx processes set in Nginx main configuration file in /etc/nginx/nginx.conf.

## vim /etc/nginx/nginx.conf
...
worker_processes  2;

Stop Apache if it’s running on the system using the following command:

## [[ $(pgrep httpd) ]] && ( systemctl stop httpd; systemctl disable httpd )

and test, start and add Nginx to system’s start-up using:

## nginx -t
## systemctl restart nginx
## systemctl enable nginx

Navigate to http://server_ip and you should get something like:

nginx-default

This means that your have Nginx up and running just fine.

 

INSTALL PHP-FPM

We are going to run PHP as FastCGI using PHP-FPM, so install PHP support using yum:

## yum install php-fpm php-mysql

also, you may want to install some other PHP extensions required by your applications. Here is the list:

php-bcmath          : A module for PHP applications for using the bcmath library
php-cli             : Command-line interface for PHP
php-common          : Common files for PHP
php-dba             : A database abstraction layer module for PHP applications
php-devel           : Files needed for building PHP extensions
php-embedded        : PHP library for embedding in applications
php-enchant         : Enchant spelling extension for PHP applications
php-fpm             : PHP FastCGI Process Manager
php-gd              : A module for PHP applications for using the gd graphics library
php-intl            : Internationalization extension for PHP applications
php-ldap            : A module for PHP applications that use LDAP
php-mbstring        : A module for PHP applications which need multi-byte string handling
php-mysql           : A module for PHP applications that use MySQL databases
php-mysqlnd         : A module for PHP applications that use MySQL databases
php-odbc            : A module for PHP applications that use ODBC databases
php-pdo             : A database access abstraction module for PHP applications
php-pear.noarch     : PHP Extension and Application Repository framework
php-pecl-memcache   : Extension to work with the Memcached caching daemon
php-pgsql           : A PostgreSQL database module for PHP
php-process         : Modules for PHP script using system process interfaces
php-pspell          : A module for PHP applications for using pspell interfaces
php-recode          : A module for PHP applications for using the recode library
php-snmp            : A module for PHP applications that query SNMP-managed devices
php-soap            : A module for PHP applications that use the SOAP protocol
php-xml             : A module for PHP applications which use XML
php-xmlrpc          : A module for PHP applications which use the XML-RPC protocol

Edit PHP main configuration file in /etc/php.ini and set the following:

## vim /etc/php.ini

date.timezone = America/New_York
memory_limit = 64M
expose_php = Off

Also, edit /etc/php-fpm.d/www.conf and change the user and group the fpm pool will be running under to nginx:

## vim +/^user /etc/php-fpm.d/www.conf

user = nginx
group = nginx

set-up log directory ownership:

## chown nginx:root -R /var/log/php-fpm/

start and add the PHP server to the system’s start-up using systemctl

## systemctl restart php-fpm
## systemctl enable php-fpm

 

SET-UP NGINX VHOST

Let’s say you have a domain mydomain.com and you like to use it to host a PHP based web application in /srv/www/mydomain.com.com like WordPress, Joomla, Laravel etc. To set-up Nginx serve requests for mydomain.com, and serve the PHP scripts in /srv/www/mydomain.com.com you would have to create a server block in /etc/nginx/conf.d/mydomain.com.conf which would look something like:

## vim /etc/nginx/conf.d/mydomain.com.conf

server {
    server_name mydomain.com;
    listen 80;
    root /srv/www/mydomain.com;
    access_log /var/log/nginx/mydomain.com-access.log;
    error_log /var/log/nginx/mydomain.com-error.log;
    index index.php;

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

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    }
    location ~ /\.ht {
        deny  all;
    }
    location ~ \.php {
        try_files $uri = 404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

test and re-start Nginx using:

## nginx -t
## systemctl restart nginx

Optionally, create a test info.php script using the following command:

## mkdir -p /srv/www/mydomain.com
## echo -e "<?php\n\tphpinfo();" > /srv/www/mydomain.com/info.php
## chown nginx: -R /srv/www/

and try to access it in your browser at http://mydomain.com/info.php


Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install LEMP for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

 

One Response to “How to install LEMP (Linux, Nginx, MariaDB & PHP-FPM) on a CentOS 7 VPS”

  1. How to install Varnish and phpMyAdmin on a CentOS 7 VPS with Nginx, MariaDB and PHP-FPM | RoseHosting.com Linux VPS Hosting Blog

    […] created to handle the administration of MySQL databases through a web browser. Use the following article to install Nginx, MariaDB and PHP-FPM on a CentOS 7 […]

    Reply

Leave a Reply