1

I have installed nginx and PHP7 on a Ubuntu 16 server.

PHP7 is installed here:

which php
/usr/bin/php

and here's my nginx config file:

upstream unicorn {
  server unix:/home/deployer/apps/myapp/shared/sockets/unicorn.myapp.sock;
}

server {
    listen 443 ssl;
    server_name myapp.com;

    ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    return 301 https://www.myapp.com$request_uri;
}

server {
  #listen 80 default deferred;

  listen 443 ssl; # managed by Certbot
  server_name www.myapp.com;
  root /home/deployer/apps/myapp/current/public;

  location ^~ /blog {
    alias /home/deployer/blog;
    index index.php;
    try_files $uri $uri/ /blog/index.php?$args;
    #rewrite ^/blog/(.*)+$ /blog/index.php?$1;
  }

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location ~ ^/(robots.txt|sitemap.xml.gz)/ {
    root /home/deployer/apps/myapp/current/public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header   X-Forwarded-Proto https; # added because of infinite looping
    proxy_redirect off;
    proxy_pass http://unicorn;
    proxy_read_timeout 120; # added for test purposes
  }

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

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;

ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/ssl/certs/dhparam.pem;

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    # Redirect non-https traffic to https
    # if ($scheme != "https") {
    #     return 301 https://$host$request_uri;
    # } # managed by Certbot
}

I've tried so many different approaches to config the PHP correctly, but still cannot find the right way. The PHP scripts are still being downloaded instead of being executed.

I also tried to clear cache, but it didn't help.

What am I missing yet in the config?

1 Answer 1

1

Your current location ~ \.php$ has the wrong root and will not see any URIs that begin with /blog anyway. As you have two roots and as you are using the ^~ modifier on the prefix location, you need to use a nested location block for PHP. For example:

location ^~ /blog {
    root /home/deployer;
    index index.php;
    try_files $uri $uri/ /blog/index.php?$args;

    location ~ \.php$ {
        ...
    }
}

The alias directive is unnecessary and inefficient is the value of the location is the same as the end of the alias value. Use a root directive instead - as advised in the manual here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.