How to Install LEMP Stack (Nginx, MariaDB, PHP 8) on CentOS Stream 9

This is one of the most commonly used Software Stack for building your web applications and it is also known as LEMP (Linux, Nginx, MySQL/MariaDB, PHP/Perl/Python) Stack.

Install Nginx Web Server on CentOS Stream 9

First, we will start by installing the Nginx web server. To complete the installation, use the following command:

yum install nginx -y

Output :

Once the installation is complete, enable Nginx (to start automatically upon system boot), start the webserver, and verify the status using the commands below.

systemctl start nginx

systemctl enable nginx

systemctl status nginx

Output:

To make your pages available to the public, you will have to edit your firewall rules to allow HTTP requests on your web server by using the following commands.

firewall-cmd --permanent --zone=public --add-service=http 

firewall-cmd --permanent --zone=public --add-service=https

firewall-cmd --reload

Output:

[root@server ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@server ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@server ~]# firewall-cmd --reload
success

Verify that the webserver is running and accessible by accessing your server’s IP address.

From your browser,

http://IP_address

Replace the IP_address with the actual IP of the server.

we need to make user Nginx the owner of the web directory. By default, it’s owned by the root user.

chown nginx:nginx /usr/share/nginx/html -R

Install MariaDB Server

MariaDB is a popular database server. The installation is simple and requires just a few steps as shown.

yum install mariadb-server mariadb -y

Output:

Once the installation is complete, enable MariaDB (to start automatically upon system boot), start the mariaDB and verify the status using the commands below.

systemctl start mariadb

systemctl enable mariadb

systemctl status mariadb

Output:

Finally, you will want to secure your MariaDB installation by issuing the following command.

mysql_secure_installation

Output:

Once secured, you can connect to MySQL and review the existing databases on your database server by using the following command.

mysql -e "SHOW DATABASES;" -p

Output:

[root@server ~]# mysql -e "SHOW DATABASES;" -p
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
[root@server ~]#

Install PHP

To Install PHP-FPM on CentOS Stream 9

yum install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y

Output :

Once the installation is complete, enable php-fpm (to start automatically upon system boot), start the php-fpm, and verify the status using the commands below.

systemctl start php-fpm

systemctl enable php-fpm

systemctl status php-fpm

Output:

By default, PHP-FPM runs as the apache user. Since we are using the Nginx web server, we need to change the following line.

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

user = apache
group = apache

Change them to

user = nginx
group = nginx

Once changed, need to reload php-fpm

systemctl reload php-fpm

Test your PHP, by creating a simple info.php file with a phpinfo() function in it. The file should be placed in the directory root for your web server, which is /usr/share/nginx/html/info.php.

To create the file use:

echo "<?php phpinfo() ?>" > /usr/share/nginx/html/info.php

Restart the Nginx and PHP-FPM.

systemctl restart nginx php-fpm

Now again, access http://localhost/info.php or http://yourserver-ip-address/info.php. You should see a page similar to the below one.

Done!

Once confirmed, we recommend removing the info.php as it could be a security issue later on.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS