How to Install WordPress on Nginx on CentOS 7 (LEMP)

Published: February 11, 2018 (Updated: Feb 11, 2018)

Enjoying this content? Subscribe to the Channel!

Master the LEMP Stack: How to Install WordPress on CentOS 7 Using Nginx

Hello Tech Fans, and welcome back to Darren’s Tech Tutorials!

If you’re looking for a fast, powerful, and highly scalable environment for your web projects, the LEMP stack is the way to go. LEMP (Linux, Nginx, MariaDB, PHP) provides an excellent alternative to the classic LAMP stack, offering superior performance for handling static content and managing high traffic.

In this comprehensive guide, we are focusing on how to successfully install and configure WordPress—the world’s most popular CMS—on your CentOS 7 server using the Nginx web server. We’ll walk through installing PHP-FPM, configuring Nginx, and setting up the database so you can launch your site quickly and securely!


Prerequisites for the WordPress Installation

Before we dive into the WordPress specific steps, it’s crucial that you have the foundational components of the LEMP stack already running. This tutorial builds directly upon our previous guides:

Component Required Helpful Video Guide
CentOS 7 Installed https://youtu.be/qZXZepdUjRI
MariaDB Installed https://youtu.be/CEojwKtOTrc
Nginx Installed https://youtu.be/InEewR7PyQQ

It’s also highly recommended that you know How to use VI for configuration file editing, and that you have followed our guide on How to Disable SELinux temporarily, as strict security policies can complicate the installation process.


Step 1: Installing and Configuring PHP-FPM

Nginx does not natively process PHP scripts; it relies on a separate component called PHP-FPM (FastCGI Process Manager) to handle PHP requests and pass the results back to the web server.

First, let’s install the necessary PHP packages, including the essential extensions that WordPress requires:

sudo yum install php php-fpm php-mysql php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel -y

Once installed, we need to ensure PHP-FPM is configured to run under the nginx user for proper communication with the Nginx web server.

Open the PHP-FPM configuration file (usually located at /etc/php-fpm.d/www.conf) using your preferred text editor (like vi):

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

Find the following lines and change the user and group from apache to nginx:

; Change these lines:
user = nginx
group = nginx

Now, save the file and enable and start the PHP-FPM service:

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

Step 2: Downloading and Preparing WordPress Files

With PHP installed, we can now download the latest version of WordPress directly to your server. We will use wget to grab the compressed archive and then extract it to the Nginx document root.

Navigate to a temporary directory, download the latest package, and extract it:

cd /tmp
wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz

Now, move the extracted wordpress directory contents into the default Nginx document root, which is typically /usr/share/nginx/html/ on CentOS 7:

sudo mv /tmp/wordpress/* /usr/share/nginx/html/

Setting Permissions

Permissions are critical! The Nginx user (nginx) must have read/write access to the files in order for WordPress to function correctly, particularly when installing plugins or uploading media.

Set the ownership of the files to the nginx user and group:

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

Step 3: Configuring Nginx for PHP Processing

Nginx needs to be told how to handle incoming requests that end in .php. We do this by editing the Nginx configuration file (usually /etc/nginx/nginx.conf or a site-specific configuration file).

Open the main Nginx configuration file:

sudo vi /etc/nginx/nginx.conf

Inside the server block, you need to uncomment or add a section that routes PHP requests to the PHP-FPM socket. Ensure your server block looks similar to the following, paying close attention to the location ~ \.php$ block:

server {
    listen       80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;
    index index.php index.html index.htm; # Ensure index.php is listed first

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
        try_files $uri $uri/ /index.php?$args; # Important for WordPress permalinks
    }

    # Pass PHP scripts to PHP-FPM
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000; # Use this if php-fpm is configured to listen on a TCP port
        # OR use a UNIX socket if preferred:
        # fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

After making these changes, always test your configuration syntax and restart Nginx:

sudo nginx -t
sudo systemctl restart nginx

Step 4: Creating the WordPress Database

Before running the web installer, WordPress needs a dedicated database, a user, and appropriate permissions within MariaDB.

Log into your MariaDB server as the root user:

mysql -u root -p

(Enter your MariaDB root password when prompted.)

Execute the following SQL commands to set up the necessary components. Remember to replace wpuser and strong_password with your desired credentials:

-- 1. Create the database
CREATE DATABASE wordpress_db;

-- 2. Create the dedicated user and set a strong password
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'strong_password';

-- 3. Grant the user full privileges on the new database
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wpuser'@'localhost';

-- 4. Apply the changes
FLUSH PRIVILEGES;

-- 5. Exit MariaDB
EXIT;

Finalizing the WordPress Installation

The final step is to configure the wp-config.php file and run the web installer.

  1. Navigate to your WordPress directory and copy the sample configuration file:

    cd /usr/share/nginx/html
    cp wp-config-sample.php wp-config.php
    
  2. Edit wp-config.php and enter the database details you just created:

    define( 'DB_NAME', 'wordpress_db' );
    define( 'DB_USER', 'wpuser' );
    define( 'DB_PASSWORD', 'strong_password' );
    define( 'DB_HOST', 'localhost' );
    
  3. Now, open your web browser and navigate to your server’s IP address (e.g., http://192.168.1.100).

You should be greeted by the classic WordPress setup screen! Follow the prompts to set your site title, admin username, and password, and click Install WordPress.


Conclusion: You’ve Mastered LEMP!

Congratulations! You have successfully installed WordPress on a high-performance CentOS 7 server utilizing the powerful Nginx web server and PHP-FPM integration. You are now running the full LEMP stack! This robust foundation sets you up perfectly for any personal or professional website you wish to launch.

Ready to dive deeper into server optimization or tackle the next big tutorial? Let us know in the comments below what you want to see next!

If you found this guide helpful, please Like this post, Subscribe to Darren’s Tech Tutorials for more comprehensive guides, and click the notification bell so you never miss a new video!


P.S. Need the full command set in a text document? You can find our complete text tutorial here: https://darrenoneill.eu/?p=455