WordPress NGINX config

Here is what worked for me locally – “on my machine” at least:

server {

    # https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04
    listen 80;
    server_name wordpress.darius;
    root /home/darius/Private/Projects/wordpress;

    index index.html index.htm index.php;

    location / {

      # https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-20-04#prerequisites
       try_files $uri $uri/ /index.php$is_args$args;
      # end https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-20-04#prerequisites

    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
    # end https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-20-04

    # https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-20-04#prerequisites
    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
    }
    #end https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lemp-on-ubuntu-20-04#prerequisites

}

I have created it from parts of examples on digital ocean, added link in comments in those parts.

Here is what else you will likely need to change:

server_name wordpress.darius;

This is the url which we go to to load the site, in this case http://wordpress.darius/ . This url is defined in the hosts file. In case you do not know, you can google “hosts file” and there is one of results: https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/

Another line is

root /home/darius/Private/Projects/wordpress;

This is the path where your wordpress project is placed.

Next line:

fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;

Here the digital ocean had this in the example:

fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

So the difference is the version of php which you want to run. I have tested this on linux. So at least on Xubuntu terminal typing php and pressing tab 2 times shows which commands start with php, in my case for example

darius@darius-Vostro-5481:~/Private/Projects/wordpress$ php
php         php7.1      php7.4      phpenmod    php-fpm7.2  php-fpm8.0
php5.6      php7.2      php8.0      php-fpm5.6  php-fpm7.3  phpquery
php7.0      php7.3      phpdismod   php-fpm7.1  php-fpm7.4 

So in the fastcgi_pass line I could use php5.6 or php7.0 etc. So choose latest of what you have installed.

What if WordPress downloads a file instead of opening in the browser?

You might have missed this part:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
}

If there is no part like this, then when visiting page, it downloads php file instead of executing it.

Also if it still downloads – make sure you have restarted nginx and clear browser cache. Chrome was downloading file even with stopped nginx until I cleared cache.

The end

Sofware versions which this was tested on: php 8, WordPress 5.8.1, nginx/1.14.0 (Ubuntu).

And looks like thats it. Please write in the comments if it works for you or it does not. I would be happy to know if it helped for somebody. Maybe need more info, if there will be demand and I will be motivated, I might add more details.

Leave a comment

Your email address will not be published. Required fields are marked *