Introduction
LEMP software is a group of open source softwares that is usually installed at the same time to enable a host server’s dynamic websites and web applications. This term is actually an acronym that represents the Linux operating system, with the ENginx web server. The site data is stored in a MySQL (or MariaDB) database and the dynamic content is processed by PHP.
We will get in this LEMP guide with PHP 7.4 installed on a CentOS 7 server, using MySQL as the database management system.
Requirement
You must have a different, non-root user account on your server before proceeding with this instruction.
Step 1 – Nginx installation
We will use Nginx, a high-performance web server, to serve web pages to site visitors. To obtain the most recent version of Nginx, we will first install the EPEL repository, which offers extra software for the CentOS 7 operating system.
Run the following command to add the CentOS 7 EPEL repository:
$ sudo yum install epel-release
Because we’re using the sudo command, these commands are carried out with root privileges. It will prompt you for your normal user password in order to confirm that you have authorization to run commands with root capabilities. When prompted to confirm the installation, press Y
to proceed.
Now that the EPEL repository has been installed on your server, use the following yum command to install Nginx:
$ sudo yum install nginx
Start the service using Nginx after the installation is complete:
$ sudo systemctl start nginx
You may immediately test that everything is working properly by accessing your server’s public IP address in your web browser.
Navigate in a web browser to this URL : http://server_public_IP/
The default CentOS 7 Nginx web page will be shown for demonstration and testing purposes.
The page is similar to this one :
If you reach this page, your web server has been successfully deployed.
Run the following command to enable Nginx start on boot:
$ sudo systemctl enable nginx
Step 2 – MySQL installation
For a complete guide, follow steps in this tutorial: Install MySQL on CentOS/Redhat 7/6 & Fedora 31/30.
Step 3 – Install PHP
PHP is the part of our system that will process the code in order to show dynamic content. It can execute scripts, connect to our MySQL databases for information, and then provide the processed data to our web server for display.
Because the PHP version supplied by default on CentOS 7 servers is out of date, we’ll need to install a third-party package repository in order to download and install PHP 7+ on your CentOS 7 server. Remi is a prominent package repository that offers the most recent PHP versions for CentOS machines.
Run the following command to install the Remi repository for CentOS 7:
$ sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
Once installation is complete, you must run a command to enable the repository containing your preferred version of PHP. To verify that PHP 7+ releases are available in the Remi repository, run:
$ yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64
You will see the following output:
Output :
Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * remi-safe: mirrors.ukfast.co.uk Available Packages php70.x86_64 2.0-1.el7.remi remi-safe php71.x86_64 2.0-1.el7.remi remi-safe php72.x86_64 2.0-1.el7.remi remi-safe php73.x86_64 2.0-1.el7.remi remi-safe php74.x86_64 1.0-3.el7.remi remi-safe php80.x86_64 1.0-3.el7.remi remi-safe
In this tutorial, we will install PHP 7.4. To obtain PHP 7.4, use the following command to activate the proper Remi package:
$ sudo yum-config-manager --enable remi-php74
Now, we can use yum to install PHP as usual. The following command will install all of the packages required to get PHP 7.4 working in Nginx and connected to MySQL-based databases:
$ sudo yum install php php-mysqlnd php-fpm
To ensure that PHP is installed as your preferred version, execute:
$ php --version
Output :
PHP 7.4.8 (cli) (built: Jul 9 2020 16:09:41) (NTS) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.21, Copyright (c), by Zend Technologies
PHP has now been installed successfully on your machine. Following that, we must make a few changes to the default setup. To make file editing on CentOS easier, we’ll first install nano, a more user-friendly text editor than vi:
$ sudo yum install nano
Edit the /etc/php-fpm.d/www.conf
configuration file in nano
or your preferred text editor:
$ sudo nano /etc/php-fpm.d/www.conf
Examine the user and group directives now. If you’re using nano, you may search for these phrases within the current file by using CTRL + W
.
Output :
… user = apache group = apache …
We need to change these to nginx
:
Output :
… user = nginx group = nginx …
Locate the listen
directive next. php-fpm
will listen on a given host and port via TCP
by default. We wish to update this option so that it listens on a local socket file, as this increases the server’s overall speed.
Find the directives listen.owner
, listen.group
, and listen.mode
. By default, these lines are commented out. Remove the preceding ;
symbol at the beginning of the line to uncomment them. After that, set the owner and group to nginx
.
Change the line that contains the listen directive to:
listen = /var/run/php-fpm/php-fpm.sock; listen.owner = nginx listen.group = nginx listen.mode = 0660
When you’re done editing, save and exit the file.
Run the following command to enable and start the php-fpm
service:
$ sudo systemctl start php-fpm
$ sudo systemctl enable php-fpm
Step 4 – Setup Nginx to Serve PHP Pages
We currently have all the necessary components in place. The only thing left to do is instruct Nginx to use our PHP processor for dynamic content.
Nginx provides a dedicated directory where we may configure each hosted website as a distinct configuration file using a server block. This is comparable to Apache’s virtual hosts.
To proceed, create a new file in the /etc/nginx/conf.d
directory:
$ sudo nano /etc/nginx/conf.d/default.conf
Copy the PHP server definition block below to your configuration file, and remember to change the server_name directive with your server’s domain name or IP address:
server {
listen 80;
server_name server_public_IP;
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;
}
}
When you’re done editing, save and exit the file.
Finally, restart Nginx to apply the changes:
$ sudo systemctl restart nginx
Step 5 – Run PHP on Nginx.
We’ll now create a test PHP page to ensure that the web server functions properly.
In the /usr/share/nginx/html
directory, create a new PHP file named info.php
:
$ nano /usr/share/nginx/html/info.php
<?php
phpinfo();
When you’re done, save and exit the file.
Now let’s access the info file in :
http://server_public_IP/info.php