How to configure nginx for wordpress

Covering nearly 30 percent of all websites, WordPress is certainly the Content Management System (CMS) of choice by many. Although it came from a blogging background, WordPress is a very powerful CMS for all content types and powers some of the world’s busiest websites.

By combining it with NGINX, you can deploy a highly scalable web platform. https://wordpress.org/support/article/nginx/

System requirements

  • Install a LEMP stack (Linux + Nginx + MySQL + PHP).

  • Create WordPress Database.

Configuring NGINX for WordPress

We’re simply using a standalone WordPress site, which would be deployed in many personal and business scenarios. This is the typical deployment for WordPress.

For ease of management, I’ve created a dedicated config file just for the WordPress site (/etc/nginx/conf.d/wordpress.conf)

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index  index.php index.html index.htm;
    server_name  site.local www.site.local;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Reload NGINX to read the new configuration file.

sudo service nginx reload

You can complete the WordPress installation.

try_files : we have a try_files directive, which will attempt to load the files in the order specified, but will fall back to the last parameter if they all fail. it means that any static files will be served if they exist on the system, then fall back to /index.php?args if this fails.

args : The args rewrite allows the permalinks of the site to be in a much more human form. For example, if you have a working WordPress installation

Lastly, we process all PHP files via the FastCGI interface to PHP-FPM

NGINX is simply proxying the connection to the PHP-FPM instance, rather than being part of NGINX itself. This separation allows for greater resource control, especially since the number of incoming requests to the web server doesn’t necessarily match the number of PHP requests for a typical website.

Troubleshoot Nginx

  • 502 Bad Gateway : File ownership permissions for the PHP-FPM socket file
  • 404 File Not Found : Check for the missing index index.php directive
  • 403 Forbidden : Check for the correct path in the root directive and/or check for correct file permissions

Your error log (defaults to /var/log/nginx/error.log) will generally contain a lot more detail regarding the issue you’re seeing compared with what’s displayed in the browser.

NGINX does not support .htaccess files. If you see examples on the web referencing a .htaccess files, these are Apache specific.

Add Comment