1

I have set up a local server on my laptop for web development and to check the website I'm working on I should have my code base in /var/www/example.domain which is not so convenient, I want Nginx to retrieve the files and parse PHP from my home directory, something like /home/projects/test-one/ but if I add this directory to root it'll throw me a 502 Bad Gateway error

here's my configuration:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    location / {
            root /home/amirreza/Projects/escribir/;
            index index.php index.html index.htm;
    }
    location ~* \.php$ {
            fastcgi_index   index.php;
            fastcgi_pass    127.0.0.1:9000;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }
}

in /etc/nginx/sites-available/default, I'm on Debian 11

4
  • 1
    The user running nginx needs to have read-access to the files it tries to serve. Commented Dec 16, 2022 at 10:13
  • rathier, that's an additional consideration, but 502 "Bad gateway" is probably not caused by file access, but by failure to talk to the fastcgi server. Commented Dec 16, 2022 at 10:21
  • Is your PHP fastcgi server up and running, Amirreza? Commented Dec 16, 2022 at 10:21
  • the issue was a mix of fastcgi and permissions issue, I'll edit my question Commented Dec 16, 2022 at 11:39

1 Answer 1

0

I was using fpm but my configuration didn't include it, so I changed my sites-available/default to this:

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

and also there was an issue with file permissions in my home folder, I changed it this way:

1- I added my user to www-data group using

sudo usermod -aG www-data amirreza

2- and then set the permissions of my code base to 664 (instead of 644) so that I can edit it as well:

-rw-rw-r--   1 amirreza www-data     22 دسامبر  16 13:19 index.php

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.