Creating correct NGINX Directives for PHP app

Discussion in 'Server Operation' started by traezi, Mar 14, 2025.

  1. traezi

    traezi New Member

    Hey everyone,
    as this isnt directly related to ISPconfig and more to creating a correct nginx directive setup for a PHP app, i'm coming up here.

    So i actually trying to get the Pelican Panel to work on my ISPconfig 3.2 NGINX based Webserver to work, but ending in a 500 server error cause it cant find my composer autoload.php as i read this error.

    So it is regarding this setup:
    https://pelican.dev/docs/panel/webserver-config

    So the actual nginx directive is:
    Code:
    ##subroot public ##
    
    location ~ \.php$ { ##delete##
    }
    
    location @php { ##delete##
    }
    
    client_max_body_size 100m;
    client_body_timeout 120s;
    
    sendfile off;
    
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header Content-Security-Policy "frame-ancestors 'self'";
    add_header X-Frame-Options DENY;
    add_header Referrer-Policy same-origin;
    
    location ~ \.php(?:$|/) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        {FASTCGIPASS}
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param PHP_VALUE "upload_max_filesize = 100M \n post_max_size=100M";
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param HTTP_PROXY "";
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        include /etc/nginx/fastcgi_params;
    }
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    Which ends in the following error in the nginx error log: (client ip and domain reducted)
    Code:
    2025/03/14 13:17:39 [error] 14721#14721: *69 FastCGI sent in stderr: "PHP message: PHP Warning:  require(/var/www/clients/client1/web13/web/public/../vendor/autoload.php): Failed to open stream: No such file or directory in /var/www/clients/client1/web13/web/public/index.php on line 13; PHP message: PHP Fatal error:  Uncaught Error: Failed opening required '/var/www/clients/client1/web13/web/public/../vendor/autoload.php' (include_path='.:/usr/share/php') in /var/www/clients/client1/web13/web/public/index.php:13
    Stack trace:
    #0 {main}
      thrown in /var/www/clients/client1/web13/web/public/index.php on line 13" while reading response header from upstream, client: 123.123.123.123, server: sub.domain.tld, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/lib/php8.2-fpm/web13.sock:", host: "sub.domain.tld", referrer: "http://sub.domain.tld"
    
    To install composer i have run as root:
    Code:
    curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
    
    and then as the web13 user within the web folder:
    Code:
    composer install --no-dev --optimize-autoloader
    
    I've always had some problems configuring the PHP location blocks correctly - maybe there's someone here who could take a look.

    Best regards
     
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    I would recommend starting simpel, so the app needs a different doc root and wants everything redirected to its index file (like WordPress). Then add:

    Code:
    ##subroot public ##
    
    location / {
       try_files $uri $uri/ /index.php?$query_string;
    }
    Removing or replacing the PHP block is rarely needed, but if you do so, it will likely cause you issues with non-working PHP.
     
    traezi likes this.
  3. traezi

    traezi New Member

    That's it ^^ ... Sometimes starting simple is the solution :D Thank you ^^
     
  4. till

    till Super Moderator Staff Member ISPConfig Developer

    If it works now, then you might add these parts again, which set higher limits and add a few security headers:

    Code:
    client_max_body_size 100m;
    client_body_timeout 120s;
    
    sendfile off;
    
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header Content-Security-Policy "frame-ancestors 'self'";
    add_header X-Frame-Options DENY;
    add_header Referrer-Policy same-origin;
     

Share This Page