4

I'm installing Wordpress on my Ubuntu server running nginx. The installation went pretty smoothly (following Installing LEMP and Installing Wordpress tutorials - took me through mysql, php5-fpm and wordpress setup), and seems to mostly work. I can view the Wordpress admin page, create blog posts and even view those posts. But when I try to access the homepage of my blog (eg. index.php), nginx serves the file as a download rather than executing it. I have already tried the configurations in Nginx serves .php files as downloads, instead of executing them to no avail.

Here is my virtual server file:

server {
    listen 80;
    server_name my.domain.com;

    root /my/wordpress/home/dir;
    index index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

        rewrite ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ redirect;
        if (-e $request_filename) {
            rewrite ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 break;
        }
        rewrite ^/([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 break;
        rewrite ^(.*)$ /index.php break;
    }
}

And nginx.conf:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

I removed commented lines for clarity. sudo nginx -t reports no errors.

How can I get index.php to execute instead of being served as a download? Thanks for your help.

Edit: Looks like it was a browser caching issue. I tried deleting cached data from the past 24 hours but it didn't change anything. After deleting everything it now loads properly instead of downloading. It also loads on other browsers/computers just fine.

Community
  • 1
  • 1
tytk
  • 2,082
  • 3
  • 27
  • 39
  • What is the purpose of the `rewrite` rules and the `if`. That is non-standard and that is causing major conflicts with your `try_files` rule. – Richard Smith Jan 17 '16 at 21:44
  • This is the nginx translation of the configuration Wordpress told me to add. Do you mind explaining what the conflict is? Nginx matches the longest `location` possible so any URL ending in `.php` should get handled by the first block. I guess that means a couple lines from the 2nd block won't be used but I don't see why it would cause problems. I'm new to nginx though so please explain. – tytk Jan 19 '16 at 06:17
  • First issue I see is `rewrite ^(.*)$ /index.php break;` which if triggered would prevent the PHP file from being executed (see `break` [here](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite)). Second issue is `-e $request_filename` which is an old way of implementing `try_files`. A resource for implementing WordPress on `nginx` is [here](https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/). – Richard Smith Jan 19 '16 at 08:56
  • Thanks @RichardSmith!! Your second link ended up being exactly the configuration I needed and got my multisite setup working properly. All the other blogs/documentation/forum posts I had been reading were from years and years ago... Glad so see the nginx people wrote up their own how-to for wordpress. – tytk Jan 24 '16 at 07:15

1 Answers1

3

For running Wordpress behind Nginx, this works for me:

    location / {
            # try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    location /wp-content/updraft {
      deny all;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/html;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

I also recommend reviewing the sample files from the Wordpress-Nginx project. In particular, you may also be interested the globals/restrictions.conf file, which hardens your installation somewhat. If you have several Wordpress sites on the same server, they can all share these "global" configuration files, making your life as a Wordpress admin easier.

Mark Stosberg
  • 12,961
  • 6
  • 44
  • 49
  • Thanks for the links! Marking your answer as correct as it's probably the most helpful. I was able to solve my issue by clearing my browsing data from my browser. – tytk Jan 19 '16 at 06:19