Blog

How to install LEMP on CentOS 8.2

How to install LEMP on CentOS 8.2

This tutorial is an update to this one. Since PHP had updates, I decided to rewrite the tutorial.

First I will explain what LEMP and CentOS are for anyone who does not know. According to lemp.io:

"LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of Linux, Apache, MySQL, and PHP. Because of its modular nature, the components can be easily swapped. With LEMP, Apache is replaced by the lightweight and powerful Nginx."

It is also possible to use Apache and Nginx on the same server, but that is a topic for another day.

To begin, you need access to a VPS so you can run the commands in this tutorial. In the commands below I will use PHP 7.4, which was the current version at the time.

First, install Nginx.

Install it with:

yum install nginx -y

Start it and enable it as a service:

systemctl start nginx systemctl enable nginx

Now install MariaDB, then start and enable it just like Nginx.

yum install mariadb-server mariadb -y systemctl start mariadb systemctl enable mariadb

To finish MariaDB, run the command below to configure the root password and remove default data. This is recommended.

mysql_secure_installation

To install the newer PHP version, install and enable the EPEL repository.

dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

Confirm the repository installation:

rpm -qa | grep epel

Now install the REMI repository:

dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

Confirm the REMI installation:

rpm -qa | grep remi

To make sure the correct PHP version will be installed, list the modules:

dnf module list php

Then enable the 7.4 module. The system default is 7.2.

dnf module enable php:remi-7.4

With the module enabled, continue with the installation.

dnf install php php-cli php-common

Check which version was installed:

php -v

To finish the LEMP configuration, configure Nginx to work with PHP 7 by creating a new configuration file.

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

Add the configuration below. Replace IPDAVPS with your IP.

server { listen 80; server_name IPDAVPS;

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/www.sock;
    fastcgi\_index index.php;
    fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;
    include fastcgi\_params;
}

}

Press CTRL + X to save and restart Nginx.

systemctl restart nginx

Now configure PHP-FPM.

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

Find and replace:

Where it says "user = apache", replace it with "user = nginx".

Replace "group = apache" with "group = nginx".

"listen.owner = nobody" should become "listen.owner = nginx".

And "listen.group = nobody" should become "listen.group = nginx".

Comment the line below ";listen = 127.0.0.1:9000" and add:

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

Press CTRL + X to save and restart PHP-FPM.

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

If you did everything correctly, test it. Go to Nginx's default directory (/usr/share/nginx/html) and create test.php.

nano test.php

Add:

Now open http://IPDAVPS/test.php in the browser. If everything worked, you should see a blank page with "test". If not, review the tutorial.

Did you like this tutorial and want to configure your own server? Use this Digital Ocean discount link: https://m.do.co/c/1e25c79708c7. You get US$100 to test for 60 days, and after that you are charged for the servers you configure.

Comments

Join the conversation

Loading comments...