2

I have a PHP application but I cannot get Nginx to work with it. I have checked permissions and my nginx configuration but I am unable to find the issue.

/etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    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;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /var/www/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

/etc/nginx/conf.d/site.com.conf

server {

        listen 80;

        server_name site.com;

        root /var/www/html/;

        index  index.php index.html;


        #i# Logging

        access_log /var/log/nginx/site.com_access_log;

        error_log   /var/log/nginx/site.com_error_log;


        location / {

                try_files $uri $uri/ /index.php?q=$uri&$args;

        }


        location ~ ^/(README.md|INSTALL|LICENSE|CHANGELOG|UPGRADING)$ {

                deny all;

        }


        location ~ ^/(config|temp|logs)/ {

                deny all;

        }


        location ~ /\. {

                deny all;

                access_log off;

                log_not_found off;

        }


        location ~ \.php$ {

                include /etc/nginx/fastcgi_params;

                #fastcgi_pass 127.0.0.1:9000;

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

                fastcgi_index index.php;

                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        }

}

My permissions

/var/www/html
 dr-xr-xr-x root  root  /
 drwxr-xr-x nginx nginx var
 drwxr-xr-x nginx nginx www
 drwxr-xr-x nginx nginx HTML

Now, when I visit my website I get the index.php file downloading. I know this is an issue with PHP. On an Apache, server i'd check the handler, owner and permissions but with nginx I'm not sure what to do. My permissions look ok

stat /var/www/html/index.php
  File: '/var/www/html/index.php'
  Size: 12843           Blocks: 32         IO Block: 4096   regular file
Device: f650b601h/4132484609d   Inode: 138577      Links: 1
Access: (0644/-rw-r--r--)  Uid: (  997/   nginx)   Gid: (  993/   nginx)

When I visit the installer directory for my application which is located at /var/www/html/installer it gives the 403 error

My error log

2021/02/11 22:12:56 [error] 23013#0: *43 directory index of "/var/www/html/installer/" is forbidden, client: xx.254.113.xx, server: _, request: "GET /installer/ HTTP/1.1", host: "www.xxx.com"

php -v
PHP 7.4.15 (cli) (built: Feb  2 2021 14:19:57) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Can anyone point out my issue? I do think it's PHP because I have tried multiple different configurations for my nginx files.

Rylad
  • 47
  • 1
  • 5
  • The error refers to the directory `/var/www/html/installer/` not `/var/www/html/`. Nginx is looking for `/var/www/html/installer/index.php` and cannot find one. – Richard Smith Feb 12 '21 at 09:49
  • That file exists in the /var/www/html/installer directory. Its permissions are 0664 and is owned by nginx – Rylad Feb 12 '21 at 11:52
  • I have just noticed that the error message refers to `server: _`, which is the default server block and not the one in your `/etc/nginx/conf.d/site.com.conf` file. It also says that you are accessing it using the hostname `www.xxx.com`. Does that exactly match the `server_name` directive in your configuration file? – Richard Smith Feb 12 '21 at 11:57
  • The server name directive in both files is now the same. In the nginx.conf I just had _ do these both need to match? Now I just get the forbidden error directory index of "/var/www/html/installer/" is forbidden directory index of "/var/www/html/" is forbidden – Rylad Feb 12 '21 at 12:15
  • I don't understand. The `server_name` is the hostname used to access the server. So if you use `http://www.example.com/` the `server_name` value should be `www.example.com`. See [this document](http://nginx.org/en/docs/http/request_processing.html) for details. – Richard Smith Feb 12 '21 at 12:48
  • I decided to change to Apache because clearly, I need to read more into how Nginx works. Thanks for your help @RichardSmith – Rylad Feb 12 '21 at 13:02

0 Answers0