A LEMP Stack is a group of open source software commonly used for hosting dynamic websites and web apps. LEMP is an acronym for Linux operating system, Nginx web server, MySQL and PHP. This guide shows you how to install a LEMP stack on a CentOS 7 server with PHP v7.

Deploying your cloud server
If you have not already registered with Cloudwafer, you should begin by getting signed up. Take a moment to create an account after which you can easily deploy your own cloud servers.

Once you have signed up, log into your Cloudwafer Client Area with the password provided in your mail and deploy your Cloudwafer cloud server.

Updating System Packages
It is always advisable to update your system before doing any major installation in order to get the latest version of the system packages and its dependencies.
Use the command below to update with the CentOS package manager yum.

  sudo yum update

Step 1 — Installing Nginx on CentOS 7
Nginx is not officially available in the default CentOS repositories, therefore, type the install EPEL repository by running this command:

sudo yum install epel-release -y

As a non-root user, you will be required to enter the sudo password for the account in use. Enter the password and continue the installation.

Now, you can proceed to install Nginx with the command below:

sudo yum install nginx -y

The next step is to start and enable Nginx to start on boot on your system with the command below:

sudo systemctl start nginx 
sudo systemctl enable nginx


To verify your Nginx web server installation, visit your server's public IP address in your web browser. This webpage should contain the default CentOS 7 Nginx web page, which is there for informational and testing purposes.


Type the command below to check your IP address on the command line:

dig +short myip.opendns.com @resolver1.opendns.com

Step 2 – Install MySQL (MariaDB)
MariaDB is a community-developed fork of the MySQL relational database management system intended to remain free under the GNU GPL.

To install the MariaDB server package, type the command below:

sudo yum install -y mariadb-server mariadb

Enable MariaDB to start at boot and proceed to start the daemon with the commands as shown below:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Our MySQL database is now running so the next thing to do is to run a simple security script that will remove some dangerous defaults and lock down some default access to our database system. Start the script by typing the command below:

sudo mysql_secure_installation

The script will ask a couple of questions including:

  • Current root password - Since this is a new installation of MySQL, you probably don’t have one so you can leave it blank and you will be asked if you want to set one. Type Y and proceed.
  • Remove anonymous users - Type Y or press the enter button to choose the default (which is Y).
  • Disable root login remotely - Type Y or press the enter button to choose the default (which is Y).
  • Remove test database - Type Y or press the enter button to choose the default (which is Y)

Image3 Image4 At this point, our database system is now set up and we can move on to the next step in our installation.

Step 3 — Installing PHP v7
PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. In this case, it will run scripts, connect to our MariaDB databases for information, and deliver processed content over to our web server to display.

We will be installing additional CentOS repo which contains required packages for PHP v7.2.

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

Enable php72 repository which is disabled by default:

sudo yum install yum-utils -y
sudo yum-config-manager --enable remi-php72

Secondly, install PHP package:

sudo yum --enablerepo=remi,remi-php72 install php-fpm php-common

Install common modules:

sudo yum --enablerepo=remi,remi-php72 install php opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml



Step 4 — Configuring Nginx to work with PHP 7

Type the commands below to edit the Nginx
configuration file:

Note: Replace server_ip with your actual server IP.

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

server {
    listen   80;
     server_name  your_server_ip;

# note that these lines are originally from the "location /" block
root   /usr/share/nginx/html;
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/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;


Save and close the file on completion then proceed to restart Nginx.

For processing PHP files, we also need to install a separate application called PHP FPM which will handle the PHP files. To install the PHP FPM package, type the following command:

We also need to make some changes to the PHP-FPM configuration as by default PHP FPM will run as user apache on port 9000, so we need to change the user to nginx and switch from TCP socket to Unix socket.Type the command to edit the www.conf file

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

Find and replace these lines:

user = apache to user = nginx

group = apache to group = nginx

listen.owner = nobody to listen.owner = nginx

listen.group = nobody to listen.group = nginx

Lastly, add the following line under listen =

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


Save and close the file then proceed to start php-fpm and enable it on boot:

systemctl start php-fpm.service
systemctl enable php-fpm.service


To check the version of PHP installed, type the command below:

 php -v