Deploy WordPress with Nginx on Ubuntu 22.04

Some days before, I shared an article about how to Deploy Laravel with Nginx on the Ubuntu server.

You can follow everything as it is except the Nginx configuration.

Here I'm sharing the configuration for the Nginx.


server {
    server_name example.com www.example.com;
    root /var/www/WORDPRESS_PROJECT_PATH;

    error_log /var/log/nginx/current.log;
    client_max_body_size 2M;  
    index index.php index.html index.htm;

    # Enable gzip compression
    gzip on;
    gzip_vary on;
    gzip_min_length 1000;
    gzip_comp_level 5;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_proxied any;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    location / {
        try_files $uri $uri/ /index.php;
    }
    location ~ ^/wp-json/ {
        rewrite ^/wp-json/(.*?)$ /?rest_route=/$1 last;
    }
    location ~* /wp-sitemap.*\.xml {
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location = /50x.html {
        root /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        include snippets/fastcgi-php.conf;
        fastcgi_buffers 1024 4k;
        fastcgi_buffer_size 128k;
    }
    # A long browser cache lifetime can speed up repeat visits to your page
    location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
        access_log        off;
        log_not_found     off;
        expires           1y;
    }
    # disable access to hidden files
    location ~ /\.ht {
        access_log off;
        log_not_found off;
        deny all;
    }
}

— Troubleshoots

Set permissions on the WordPress project directory using the following command:

sudo chmod -R 777 /var/www/WORDPRESS_PROJECT_PATH
  • It will enable the file upload feature also.

For enabling the plugin install/update feature:

Insert into the wp-config.php

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
        define( 'ABSPATH', __DIR__ . '/' );
}  /**  Place after this line */

define('FS_METHOD', 'direct');