Question

LEMP will not serve php

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]

Submit an answer


This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted 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.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.