Introduction

Setting up a reliable and high-performance web server is one of the first steps in hosting dynamic web applications. The LEMP stack—Linux, Nginx, MariaDB, and PHP-FPM—is a popular choice due to its speed, stability, and efficient resource utilization. Although CentOS 7 has reached its end of life, many organizations continue to maintain legacy servers that require ongoing support and administration.

In this tutorial, you’ll learn how to install and configure MariaDB, Nginx, and PHP-FPM on a CentOS 7 server. You’ll also learn how to deploy a WordPress website by configuring a virtual host, creating a database, and completing the WordPress installation. By the end of this guide, you’ll have a fully functional LEMP environment capable of hosting PHP-based applications.

Step 1: Install MariaDB

Add the MariaDB Repository

Create a new repository file.

vi /etc/yum.repos.d/MariaDB.repo

Add the following content:

# MariaDB 10.1 CentOS repository
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey = https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 1

Install MariaDB

yum install -y MariaDB-server MariaDB-client

Enable and start the service.

systemctl enable mariadb
systemctl start mariadb

Verify the status.

systemctl status mariadb

Secure MariaDB Installation

Run the security script to remove anonymous users, disable remote root login, and remove the test database.

mysql_secure_installation

Step 2: Install Nginx

Create the Nginx Repository

vi /etc/yum.repos.d/nginx.repo

Add:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Install Nginx

yum install -y nginx

Enable and start the service.

systemctl enable nginx
systemctl start nginx

Check the status.

systemctl status nginx

Step 3: Install PHP-FPM and Required Extensions

Install PHP-FPM along with commonly used PHP extensions.

yum install -y \
php-fpm \
php-cli \
php-mysql \
php-gd \
php-ldap \
php-odbc \
php-pdo \
php-pecl-memcache \
php-pear \
php-mbstring \
php-xml \
php-xmlrpc \
php-snmp \
php-soap

Step 4: Improve PHP Security

Edit the PHP configuration.

vi /etc/php.ini

Locate the following setting.

expose_php = Off

This prevents PHP from exposing its version information in HTTP headers.


Step 5: Configure PHP-FPM

Edit the PHP-FPM pool configuration.

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

Ensure the following line is enabled.

listen = 127.0.0.1:9000

Start and enable PHP-FPM.

systemctl enable php-fpm
systemctl start php-fpm

Step 6: Create a Website User

Create a dedicated user for the website.

useradd wordpress -d /var/www/wordpress

Create the document root.

mkdir -p /var/www/wordpress/html

Assign ownership.

chown -R wordpress:wordpress /var/www/wordpress

Step 7: Configure Nginx

Enable Virtual Hosts

Edit the main Nginx configuration.

vi /etc/nginx/nginx.conf

Inside the http block, add:

include /etc/nginx/sites-enabled/*.conf;

Create the directory if it does not exist.

mkdir -p /etc/nginx/sites-enabled

Create a Virtual Host

vi /etc/nginx/sites-enabled/domain.conf

Replace sites.tld and www.sites.tld with your domain.

server {
    listen 80;
    server_name sites.tld www.sites.tld;

    root /var/www/wordpress/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /var/www/wordpress/html;
    }
}

Restart the services.

systemctl restart nginx
systemctl restart php-fpm

Step 8: Install WordPress

Navigate to the document root.

cd /var/www/wordpress/html

Download the latest WordPress release.

wget https://wordpress.org/latest.tar.gz

Extract the archive.

tar -xzf latest.tar.gz

Copy the files.

cp -pr wordpress/* .

Remove unnecessary files.

rm -rf wordpress latest.tar.gz

Step 9: Create the WordPress Database

Log in to MariaDB.

mysql

Create a database and user.

CREATE DATABASE wordpress;

GRANT ALL PRIVILEGES
ON wordpress.*
TO 'wordpress'@'localhost'
IDENTIFIED BY 'StrongPasswordHere';

FLUSH PRIVILEGES;

EXIT;

Step 10: Complete the Installation

Open your browser and visit your domain.

http://your-domain.com

Follow the WordPress installation wizard.

You’ll be asked for:

  • Database Name
  • Database User
  • Database Password
  • Database Host (localhost)
  • Table Prefix (wp_)

After completing the wizard, your WordPress site will be ready.


Useful Commands

Restart all services.

systemctl restart mariadb
systemctl restart nginx
systemctl restart php-fpm

Enable services at boot.

systemctl enable mariadb
systemctl enable nginx
systemctl enable php-fpm

Check service status.

systemctl status mariadb
systemctl status nginx
systemctl status php-fpm

Troubleshooting

PHP files download instead of executing

  • Verify that php-fpm is running.
  • Confirm that the fastcgi_pass directive points to the correct PHP-FPM listener.
  • Restart both Nginx and PHP-FPM.

403 Forbidden

Check permissions.

chown -R wordpress:wordpress /var/www/wordpress
chmod -R 755 /var/www/wordpress

Database Connection Error

Verify:

  • Database name
  • Username
  • Password
  • MariaDB service status

Conclusion

You have successfully installed a complete LEMP (Linux, Nginx, MariaDB, PHP) stack on CentOS 7 and deployed a WordPress website.

Although this setup remains useful for maintaining legacy servers, newer deployments should consider supported operating systems such as Rocky Linux, AlmaLinux, or RHEL for improved security, stability, and long-term support.

Leave a Reply