Nginx is a growing open-source web server software and PHP v7 is the latest version of PHP engine. In this tutorial, we will use it to build a LEMP (Linux, ENginx, MySQL, PHP) stack server. Nginx replaces the popular Apache package found in LAMP stack.

What you’ll need

Before you begin this guide you’ll need the following:

  • root access to the VPS

Step 1 — Installing Nginx on CentOS 7

Since Nginx is not available in default CentOS repositories, we will install EPEL repository by running this command:

yum install epel-release -y

Next, we will install Nginx itself:
yum install nginx -y
After installation completes, enable Nginx start on boot and run it:
systemctl start nginx
systemctl enable nginx

In order to check if Nginx is running, you can visit your IP address via the browser. Firstly, locate your IP:
Then, just copy-paste it into the browser and you should see similar page:

Step 2 — Installing MySQL (MariaDB)

Once the web server is installed, we can proceed further towards MySQL installation. MariaDB is a community fork of the old and well known MySQL service. Since MariaDB comes with default CentOS repositories we can run Yum to install it:
yum install mariadb-server mariadb -y
After finishing the installation, enable and start the service:
systemctl start mariadb
systemctl enable mariadb

Lastly, run initial setup script which will remove some of the default settings:
MariaDB will ask you for the root password, however, since this is initial installation, you don’t have any, so just press enter. Next prompt will ask if you want to set a root password, enter Yand follow the instructions:
Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorization.

New password: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
... Success!

You can safely click ENTER key and accept default settings for all other questions. After you complete the setup, proceed further to PHP installation.

Step 3 — Installing PHP v7.3.0

The first thing that we will do is install additional CentOS repo which contains required packages for PHP v7.3:

The first thing that we will do is install additional CentOS repo which contains required packages for PHP v7.1:

rpm -Uvh remi-release-7.rpm

Enable php73 repository which is disabled by default:

yum install yum-utils -y
yum-config-manager --enable remi-php73

Secondly, install PHP package:

yum --enablerepo=remi,remi-php73 install php-fpm php-common

Install common modules:

yum --enablerepo=remi,remi-php73 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

After finishing the installation

Step 4 — Configuring Nginx to work with PHP 7

Create a new Nginx configuration file by running vim or nano text editor:
nano /etc/nginx/conf.d/default.conf Input this code:

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;

IMPORTANT! Replace your_server_ip with your actual server IP.
Save the file by hitting CTRL + X (CMD + X for Mac users). Restart Nginx for change to take effect:
systemctl restart nginx
Now, open PHP-FPM configuration:
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 = nobody to = nginx

And, lastly, under ;listen = add this line:

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

Once again, save the file by hitting CTRL + X. And finally, start php-fpm and enable it on boot:
systemctl start php-fpm.service
systemctl enable php-fpm.service


Maybe LEMP stack installation requires more configuration than the very well known LAMP setup, but you can be assured that you will be using the latest technology from both worlds – fast PHP-FPM v7 processor package with modern Nginx web service