LEMP will not serve php

April 11, 2017 1.5k views
Nginx PHP Ubuntu 16.04

I can’t seem to get php working with nginx…

I’ve used the LEMP 1 click deploy and tried doing it on my own, and tried following countless tutorials out there.

Here is my config for this vhost

server {
  listen 80;
  server_name SERVER.COM;
    return 301 https://$host$request_uri;

    location /.well-known {
        alias /var/www/DIR/.well-known;
    }
}

server {
  listen 443;
  server_name SERVER.COM;
  ssl_certificate /etc/letsencrypt/live/SERVER.COM/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/SERVER.COM/privkey.pem;
  add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
  ssl on;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;

  location / {
    root /var/www/SERVER.COM/;
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$args;

        }

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

        location ~ /\.ht {
          deny all;
        }

        location /monit/ {
        rewrite ^/monit/(.*) /$1 break;
        proxy_ignore_client_abort on;
        proxy_pass   http://localhost:2812;
        }
}

All I am trying to do is to get a simple phpinfo to run.
I see the request served in the access logs, nothing in the error logs

Relevant info

php/xenial,now 1:7.0+35ubuntu6 all [installed]
php-apcu/xenial,now 5.1.3+4.0.10-1build1 amd64 [installed]
php-common/xenial,now 1:35ubuntu6 all [installed,automatic]
php-curl/xenial,now 1:7.0+35ubuntu6 all [installed]
php-fpm/xenial,now 1:7.0+35ubuntu6 all [installed]
php-mcrypt/xenial,now 1:7.0+35ubuntu6 all [installed]
php-mysql/xenial,now 1:7.0+35ubuntu6 all [installed]
php7.0/xenial-updates,xenial-security,now 7.0.15-0ubuntu0.16.04.4 all [installed,automatic]
php7.0-cli/xenial-updates,xenial-security,now 7.0.15-0ubuntu0.16.04.4 amd64 [installed,automatic]
php7.0-common/xenial-updates,xenial-security,now 7.0.15-0ubuntu0.16.04.4 amd64 [installed,automatic]
php7.0-curl/xenial-updates,xenial-security,now 7.0.15-0ubuntu0.16.04.4 amd64 [installed,automatic]
php7.0-fpm/xenial-updates,xenial-security,now 7.0.15-0ubuntu0.16.04.4 amd64 [installed,automatic]
php7.0-json/xenial-updates,xenial-security,now 7.0.15-0ubuntu0.16.04.4 amd64 [installed,automatic]
php7.0-mcrypt/xenial-updates,xenial-security,now 7.0.15-0ubuntu0.16.04.4 amd64 [installed,automatic]
php7.0-mysql/xenial-updates,xenial-security,now 7.0.15-0ubuntu0.16.04.4 amd64 [installed,automatic]
php7.0-opcache/xenial-updates,xenial-security,now 7.0.15-0ubuntu0.16.04.4 amd64 [installed,automatic]
php7.0-readline/xenial-updates,xenial-security,now 7.0.15-0ubuntu0.16.04.4 amd64 [installed,automatic]
nginx/xenial-updates,xenial-security,now 1.10.0-0ubuntu0.16.04.4 all [installed]
nginx-common/xenial-updates,xenial-security,now 1.10.0-0ubuntu0.16.04.4 all [installed,automatic]
nginx-core/xenial-updates,xenial-security,now 1.10.0-0ubuntu0.16.04.4 amd64 [installed,automatic]
1 Answer

@madbuda

Unless you have a specific need, you shouldn’t be clustering the location blocks are you currently are. You need to separate / from others.

So you’re location blocks should look like this:

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

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

location ~ /\.ht {
    deny all;
}

location /monit/ {
    rewrite ^/monit/(.*) /$1 break;
    proxy_ignore_client_abort on;
    proxy_pass http://localhost:2812;
}

Though I would organize the configuration a little more, which I’ve done for you below.

server {
    listen 80;
    server_name SERVER.COM;
    return 301 https://$host$request_uri;

    location /.well-known {
        alias /var/www/DIR/.well-known;
    }
}

server {
    listen 443;
    server_name SERVER.COM;
    root /var/www/SERVER.COM/;

    index index.php index.html index.htm;

    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";

    ssl on;
    ssl_certificate /etc/letsencrypt/live/SERVER.COM/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/SERVER.COM/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;

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

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

    location /monit/ {
        rewrite ^/monit/(.*) /$1 break;
        proxy_ignore_client_abort on;
        proxy_pass http://localhost:2812;
    }
}

After changing the configuration, you’ll need to reload or restart NGINX.

  • So.. nginx cares about white space… This is a new tidbit of knowledge.

    smh, and thanks!! I was about to give up and do something in node instead of php

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!