How to install WordPress with NGINX on CentOS 8

WordPress is currently the most popular content management system for building blogs. That’s not all it’s great for; you can make shops, resumes, and even portfolios. To be short, it’s an excellent solution to make simple websites without needing to know how to code. You could just go to wordpress.com to get managed hosting, but hosting it yourself unlocks a vast amount of options. You don’t need to know how to code to host it yourself. Just follow this guide to get going.

Pre-requisites:

*Have a CentOS 8 server with the LEMP stack: https://blog.wearedevs.net/lemp-stack-setup-on-centos-8/

SETTING UP THE DATABASE

Step 1: Log into the MySQL database

mysql -u root -p

Step 2: Create a database named “wordpress”

CREATE DATABASE wordpress;

Step 3: Make sure your MySQL user of choice can use the newly created database. Put root if you feel like.

GRANT ALL PRIVILEGES on wordpress.* to ”@’localhost’ identified by ”;

Step 4: Flush the priveleges and exit

FLUSH PRIVILEGES;
EXIT;

INSTALLING THE PACKAGES

Step 1: Install MariaDB, PHP, and NGINX

yum install wget nano curl unzip nginx php php-cli php-curl php-zip php-mbstring php-mysqlnd php-pecl-json php-bcmath php-gd php-fpm -y

Step 2: Start and enable NGINX and php-fpm so they will run even after a server reboot

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

Step 2a: (optional) Check that php-fpm is running

systemctl status php-fpm

Expected output toward the end: “Started The PHP FastCGI Process Manager.”

Step 3: Configure the PHP settings to use the “nginx” user

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

Edit the file's content so you change the user and group from "apache" to "nginx"

…
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
…

Downloading WordPress

Step 1: Navigate to where you want to install WordPress

cd /var/www

Step 2: Download the compressed WordPress file

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

Step 3: Extract/decompress the archive then delete the archive

tar -xzvf latest.tar.gz
rm latest.tar.gz

Step 4: Change the ownership of the extracted folder to “nginx”

chown -R nginx: wordpress

Step 5: Navigate into the new folder

cd wordpress

Step 6: Create your wordpress config file like so

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

Step 7: Edit the new config file

nano wp-config.php

Step 8: Input the recently created database information and your correct MySQL username and password

…
/** The name of the database for WordPress */
define( ‘DB_NAME’, ‘wordpress’ );

/** MySQL database username */
define( 'DB_USER', 'mysqluser' );

/** MySQL database password */
define( 'DB_PASSWORD', 'password' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
…

Step 9: Add this line anywhere in wp-config.php

define(‘FS_METHOD’,’direct’);

Step 10: Edit the file's permissions

chmod 777 ./wp-content/plugins/

CONFIGURING NGINX

Step 1: Create and edit a new NGINX configuration file just for your WordPress website

nano /etc/nginx/conf.d/wordpress.conf

Step 2: Paste in the following

server {
	listen 80;
	server_name example.com; #Replace example.com with your intended domain name
	root /var/www/wordpress; #Replace with your WordPress folder if necessary
	index index.php;

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

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

Note that the configuration above is just the bare minimum to get up and running.

Step 2a(optional): Check that there are no NGINX configuration issues

nginx -t

Step 3: Restart NGINX to apply the configuration changes

systemctl restart nginx

Done

That's it! WordPress is now installed and running your NGINX server on CentOS 8. Now just make sure your domain’s DNS is pointing to the server’s IP address, then navigate to your domain on your web browser. You should see the WordPress configuration setup from there.

Common Issues

You may run into a “critical” error in your Site Health tool stating, “Some files are unwritable by WordPress”. If your write permissions are correct, then you may need to disable SELinux.