1080*80 ad

Setting up WordPress on CentOS 8 with Nginx and MySQL 8

How to Install WordPress on CentOS 8 with Nginx and MySQL 8: A Comprehensive Guide

Building a website on a powerful, self-managed server gives you ultimate control over performance and security. By combining the stability of CentOS 8 with the high-performance Nginx web server and the robust MySQL 8 database, you can create an optimized environment for WordPress. This combination, often part of a LEMP stack (Linux, Nginx, MySQL, PHP), is a popular choice for developers and businesses seeking speed and reliability.

This guide provides a step-by-step walkthrough to deploy WordPress on a CentOS 8 server, covering everything from initial server setup to final configuration and security hardening.

Prerequisites

Before you begin, ensure you have the following:

  • A server running a fresh installation of CentOS 8.
  • A non-root user with sudo privileges.
  • A domain name pointed to your server’s public IP address.

Step 1: Install the Nginx Web Server

Nginx is a lightweight, high-performance web server that excels at serving static content and acting as a reverse proxy. It’s an excellent foundation for a fast WordPress site.

First, update your system’s package repository to ensure you have the latest versions available.

sudo dnf update -y

Next, install Nginx using the dnf package manager.

sudo dnf install nginx -y

Once the installation is complete, you need to start and enable the Nginx service. Enabling it ensures that Nginx will automatically restart if the server reboots.

sudo systemctl start nginx
sudo systemctl enable nginx

To confirm Nginx is running, you can visit your server’s IP address in a web browser. You should see the default Nginx welcome page.

Step 2: Install and Secure MySQL 8

WordPress uses a database to store all your site’s content, from posts and pages to user settings. We’ll use MySQL 8, a powerful and widely-used relational database management system.

Install the MySQL server package.

sudo dnf install @mysql -y

After installation, start and enable the MySQL service.

sudo systemctl start mysqld
sudo systemctl enable mysqld

The default MySQL installation is not secure. Running the included security script is a critical step. This script will prompt you to set a root password, remove anonymous users, disallow remote root login, and remove the test database.

sudo mysql_secure_installation

Follow the on-screen prompts, choosing strong security settings.

Create a WordPress Database

Now, log in to MySQL as the root user and create a dedicated database and user for your WordPress installation.

sudo mysql -u root -p

Enter the root password you just set. Once inside the MySQL prompt, run the following commands. Be sure to replace 'your_password' with a strong, unique password.

CREATE DATABASE wordpress_db;
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

You have now successfully created a secure database for WordPress to use.

Step 3: Install and Configure PHP-FPM

Nginx does not process PHP files natively. It requires a separate processor, like PHP-FPM (FastCGI Process Manager), to handle dynamic PHP content.

We will install PHP-FPM along with several common PHP extensions that WordPress requires to function correctly.

sudo dnf install php-fpm php-mysqlnd php-gd php-xml php-mbstring php-json -y

Once installed, we need to make a small configuration change to improve security. Open the PHP-FPM configuration file with a text editor like nano.

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

Find the user and group directives and change their values from apache to nginx.

; Find these lines:
user = apache
group = apache

; And change them to:
user = nginx
group = nginx

Save and close the file. Now, start and enable the php-fpm service.

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

Step 4: Configure Nginx to Serve WordPress

The next step is to tell Nginx how to handle requests for your domain and how to pass PHP files to PHP-FPM for processing. We will create a new server block configuration file for our site.

Create a new configuration file in the /etc/nginx/conf.d/ directory. Replace yourdomain.com with your actual domain name.

sudo nano /etc/nginx/conf.d/yourdomain.com.conf

Paste the following configuration into the file. Be sure to replace yourdomain.com and /var/www/yourdomain.com with your own information.

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain.com;

    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }
}

This configuration tells Nginx where your website files are located and instructs it to pass any file ending in .php to the PHP-FPM socket. The try_files directive is crucial for enabling WordPress permalinks to work correctly.

Before applying the changes, test your Nginx configuration for syntax errors.

sudo nginx -t

If the test is successful, restart Nginx to load the new configuration.

sudo systemctl restart nginx

Step 5: Install WordPress

With the server configured, you can now install the WordPress software.

First, create the web root directory specified in your Nginx configuration.

sudo mkdir -p /var/www/yourdomain.com

Navigate to a temporary directory and download the latest version of WordPress.

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

Extract the archive and copy the contents into your website’s root directory.

tar xzvf latest.tar.gz
sudo cp -r /tmp/wordpress/* /var/www/yourdomain.com/

Proper file permissions are essential for security and functionality. You must set the correct ownership so that Nginx can read and write to the files.

sudo chown -R nginx:nginx /var/www/yourdomain.com

Next, create the wp-config.php file, which contains your database connection details. WordPress provides a sample file you can copy.

cd /var/www/yourdomain.com
sudo cp wp-config-sample.php wp-config.php

Now, edit the new configuration file to add the database details you created earlier.

sudo nano wp-config.php

Update the following lines with your database name, username, and password.

define( 'DB_NAME', 'wordpress_db' );
define( 'DB_USER', 'wordpress_user' );
define( 'DB_PASSWORD', 'your_password' );

Save and close the file.

Step 6: Finalize Installation and Secure Your Server

You are now ready to complete the installation through the WordPress web interface.

Navigate to your domain name (http://yourdomain.com) in your web browser. You will be greeted by the WordPress installation screen. Follow the on-screen instructions to select your language, set up your site title, create an administrator account, and finalize the installation.

Essential Security Tips

Your site is running, but there are a few more steps to ensure it is secure.

  1. Configure the Firewall: CentOS 8 uses firewalld. You must allow HTTP and HTTPS traffic through the firewall for your site to be accessible.

    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo firewall-cmd --reload
    
  2. Install an SSL Certificate: Running your site over HTTPS is non-negotiable for security and SEO. Use Let’s Encrypt to get a free SSL certificate. You can easily install one using the certbot tool.

    sudo dnf install certbot python3-certbot-nginx -y
    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
    

    Follow the prompts to obtain and install your certificate. Certbot will automatically update your Nginx configuration to enable SSL.

By completing these steps, you have successfully deployed a secure, high-performance WordPress website on a powerful server stack that you fully control.

Source: https://kifarunix.com/install-wordpress-with-nginx-and-mysql-8-on-centos-8/

900*80 ad

      1080*80 ad