CentOs-Nginx
CentOs-Nginx

How to Install WordPress with Nginx on CentOS 8

WordPress is one of the most popular content management systems that powers millions of websites worldwide. Its flexibility and user-friendly interface make it an ideal choice for bloggers, businesses, and developers alike. In this blog post, we’ll guide you through the process of installing WordPress with Nginx on CentOS 8. Let’s get started!

Prerequisites

Before we begin, ensure you have the following:

  1. A CentOS 8 server with root access. You can use any cloud provider, such as Greenhost.Cloud.
  2. Basic knowledge of the command line.
  3. Nginx and MySQL or MariaDB database installed on your server. If you don’t have them installed, we’ll cover that too.

Step 1: Update Your System

Start by updating your system to ensure all packages are up to date. Connect to your server using SSH and execute the following command:

sudo dnf update -y

Step 2: Install Required Packages

Next, you need to install Nginx, PHP, and other required extensions. Run the following command:

sudo dnf install nginx php php-fpm php-mysqlnd php-xml php-mbstring php-json php-curl -y

Step 3: Start and Enable Nginx and PHP-FPM

Now that Nginx and PHP are installed, you need to start the services and enable them to run on boot:

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

Step 4: Configure Nginx for WordPress

Create a new configuration file for your WordPress site in the /etc/nginx/conf.d/ directory. For example, you can name it example.com.conf (replace example.com with your domain).

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

Add the following configuration to the file:

server {
    listen 80;
    server_name example.com www.example.com;

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

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

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        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;
    }

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

Make sure to replace example.com with your actual domain. Save and exit the editor (in Nano, you can do this with CTRL + X, then Y, then ENTER).

Step 5: Test Nginx Configuration

Before restarting Nginx, it’s a good idea to test the configuration for any syntax errors:

sudo nginx -t

If there are no errors, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 6: Install MariaDB

If you haven’t installed a database server yet, you can install MariaDB:

sudo dnf install mariadb-server -y

Start and enable MariaDB:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Secure your MariaDB installation:

sudo mysql_secure_installation

Step 7: Create a Database and User for WordPress

Log in to the MariaDB prompt:

sudo mysql -u root -p

Inside the MariaDB shell, run the following commands to create a database and a user for WordPress (replace wpuser and password with your desired username and password):

CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 8: Download WordPress

Change to the web root directory and download the latest version of WordPress:

cd /var/www/html
wget http://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
mv wordpress/* ./
rm -rf wordpress latest.tar.gz

Step 9: Configure WordPress

Create a configuration file for WordPress:

cp wp-config-sample.php wp-config.php

Edit the configuration file:

nano wp-config.php

Update the database details:

define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wpuser' );
define( 'DB_PASSWORD', 'password' );

Step 10: Set Permissions

Set the correct ownership and permissions for WordPress:

sudo chown -R nginx:nginx /var/www/html/*
sudo find /var/www/html/ -type d -exec chmod 755 {} \;
sudo find /var/www/html/ -type f -exec chmod 644 {} \;

Step 11: Complete the Installation via the Web Interface

Now that WordPress is set up, you can complete the installation through the web interface. Open your web browser and navigate to your domain:

http://example.com

Follow the on-screen instructions to set up WordPress. You will be asked to select your language, set up your site title, create an admin account, and more.

Conclusion

Congratulations! You have successfully installed WordPress with Nginx on CentOS 8. You can now start customizing your site with themes and plugins. If you have any questions or run into issues during installation, feel free to leave a comment below or check the Nginx and WordPress documentation for more help.

Happy blogging! 🌱