Nginx cannot find ~/public_html/info.php












1















I am new to PHP and want to learn it. So I install Nginx, PHP, MariaDB in my computer:




  1. Ubuntu 18.04 LTS 64-bit.

  2. Nginx (don't know how to check version)

  3. PHP 7.2

  4. The default www is /var/www/html. It works fine for HTML and PHP file. (info.php only contain phpinfo();)

  5. A normal user with directory ~/public_html/index.html and info.php. index.html could shown (Hello world), but info.php (same as above) got 404.


/etc/nginx/site-available/default



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

root /var/www/html;

index index.php index.html index.htm index.nginx-debian.html;

server_name _;

location / {
try_files $uri $uri/ =404;
}

location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
}

location ~ /.ht {
deny all;
}

location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.php index.html index.htm;
autoindex on;
}
}


Please help.










share|improve this question



























    1















    I am new to PHP and want to learn it. So I install Nginx, PHP, MariaDB in my computer:




    1. Ubuntu 18.04 LTS 64-bit.

    2. Nginx (don't know how to check version)

    3. PHP 7.2

    4. The default www is /var/www/html. It works fine for HTML and PHP file. (info.php only contain phpinfo();)

    5. A normal user with directory ~/public_html/index.html and info.php. index.html could shown (Hello world), but info.php (same as above) got 404.


    /etc/nginx/site-available/default



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

    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
    try_files $uri $uri/ =404;
    }

    location ~ .php$ {
    fastcgi_split_path_info ^(.+.php)(/.+)$;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    include fastcgi_params;
    }

    location ~ /.ht {
    deny all;
    }

    location ~ ^/~(.+?)(/.*)?$ {
    alias /home/$1/public_html$2;
    index index.php index.html index.htm;
    autoindex on;
    }
    }


    Please help.










    share|improve this question

























      1












      1








      1








      I am new to PHP and want to learn it. So I install Nginx, PHP, MariaDB in my computer:




      1. Ubuntu 18.04 LTS 64-bit.

      2. Nginx (don't know how to check version)

      3. PHP 7.2

      4. The default www is /var/www/html. It works fine for HTML and PHP file. (info.php only contain phpinfo();)

      5. A normal user with directory ~/public_html/index.html and info.php. index.html could shown (Hello world), but info.php (same as above) got 404.


      /etc/nginx/site-available/default



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

      root /var/www/html;

      index index.php index.html index.htm index.nginx-debian.html;

      server_name _;

      location / {
      try_files $uri $uri/ =404;
      }

      location ~ .php$ {
      fastcgi_split_path_info ^(.+.php)(/.+)$;
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
      include fastcgi_params;
      }

      location ~ /.ht {
      deny all;
      }

      location ~ ^/~(.+?)(/.*)?$ {
      alias /home/$1/public_html$2;
      index index.php index.html index.htm;
      autoindex on;
      }
      }


      Please help.










      share|improve this question














      I am new to PHP and want to learn it. So I install Nginx, PHP, MariaDB in my computer:




      1. Ubuntu 18.04 LTS 64-bit.

      2. Nginx (don't know how to check version)

      3. PHP 7.2

      4. The default www is /var/www/html. It works fine for HTML and PHP file. (info.php only contain phpinfo();)

      5. A normal user with directory ~/public_html/index.html and info.php. index.html could shown (Hello world), but info.php (same as above) got 404.


      /etc/nginx/site-available/default



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

      root /var/www/html;

      index index.php index.html index.htm index.nginx-debian.html;

      server_name _;

      location / {
      try_files $uri $uri/ =404;
      }

      location ~ .php$ {
      fastcgi_split_path_info ^(.+.php)(/.+)$;
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
      include fastcgi_params;
      }

      location ~ /.ht {
      deny all;
      }

      location ~ ^/~(.+?)(/.*)?$ {
      alias /home/$1/public_html$2;
      index index.php index.html index.htm;
      autoindex on;
      }
      }


      Please help.







      php nginx






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 7 at 13:50









      phpNewbiephpNewbie

      61




      61






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You have PHP files in two roots, so you need two location blocks to process those URIs. The simplest solution is to use a nested location block.



          For example:



          location / {
          try_files $uri $uri/ =404;
          }
          location ~ /.ht {
          deny all;
          }
          location ~ ^/~(?<user>[^/]+)(?<path>/.*)?$ {
          alias /home/$1/public_html$2;
          index index.php index.html index.htm;
          autoindex on;

          location ~ .php$ {
          if (!-f $request_filename) { return 404; }
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $request_filename;
          }
          }
          location ~ .php$ {
          ...
          }


          Place the outer location ~ .php$ block below, otherwise it will match all PHP URIs first. See this document for details.



          Use named captures, as numeric captures will be out of scope in the nested location block.



          I don't know what's in your snippets file, but you probably want to avoid try_files (because of this issue with alias) and you need to use $request_filename to locate the path to the SCRIPT_FILENAME.



          The if block is added to avoid passing uncontrolled requests to PHP. See this caution on the use of if.






          share|improve this answer
























          • Surely that if could be replaced with try_files $url =404 or something similar?

            – grawity
            Feb 7 at 15:18











          • I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.

            – Richard Smith
            Feb 7 at 15:28











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1403134%2fnginx-cannot-find-public-html-info-php%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You have PHP files in two roots, so you need two location blocks to process those URIs. The simplest solution is to use a nested location block.



          For example:



          location / {
          try_files $uri $uri/ =404;
          }
          location ~ /.ht {
          deny all;
          }
          location ~ ^/~(?<user>[^/]+)(?<path>/.*)?$ {
          alias /home/$1/public_html$2;
          index index.php index.html index.htm;
          autoindex on;

          location ~ .php$ {
          if (!-f $request_filename) { return 404; }
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $request_filename;
          }
          }
          location ~ .php$ {
          ...
          }


          Place the outer location ~ .php$ block below, otherwise it will match all PHP URIs first. See this document for details.



          Use named captures, as numeric captures will be out of scope in the nested location block.



          I don't know what's in your snippets file, but you probably want to avoid try_files (because of this issue with alias) and you need to use $request_filename to locate the path to the SCRIPT_FILENAME.



          The if block is added to avoid passing uncontrolled requests to PHP. See this caution on the use of if.






          share|improve this answer
























          • Surely that if could be replaced with try_files $url =404 or something similar?

            – grawity
            Feb 7 at 15:18











          • I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.

            – Richard Smith
            Feb 7 at 15:28
















          0














          You have PHP files in two roots, so you need two location blocks to process those URIs. The simplest solution is to use a nested location block.



          For example:



          location / {
          try_files $uri $uri/ =404;
          }
          location ~ /.ht {
          deny all;
          }
          location ~ ^/~(?<user>[^/]+)(?<path>/.*)?$ {
          alias /home/$1/public_html$2;
          index index.php index.html index.htm;
          autoindex on;

          location ~ .php$ {
          if (!-f $request_filename) { return 404; }
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $request_filename;
          }
          }
          location ~ .php$ {
          ...
          }


          Place the outer location ~ .php$ block below, otherwise it will match all PHP URIs first. See this document for details.



          Use named captures, as numeric captures will be out of scope in the nested location block.



          I don't know what's in your snippets file, but you probably want to avoid try_files (because of this issue with alias) and you need to use $request_filename to locate the path to the SCRIPT_FILENAME.



          The if block is added to avoid passing uncontrolled requests to PHP. See this caution on the use of if.






          share|improve this answer
























          • Surely that if could be replaced with try_files $url =404 or something similar?

            – grawity
            Feb 7 at 15:18











          • I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.

            – Richard Smith
            Feb 7 at 15:28














          0












          0








          0







          You have PHP files in two roots, so you need two location blocks to process those URIs. The simplest solution is to use a nested location block.



          For example:



          location / {
          try_files $uri $uri/ =404;
          }
          location ~ /.ht {
          deny all;
          }
          location ~ ^/~(?<user>[^/]+)(?<path>/.*)?$ {
          alias /home/$1/public_html$2;
          index index.php index.html index.htm;
          autoindex on;

          location ~ .php$ {
          if (!-f $request_filename) { return 404; }
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $request_filename;
          }
          }
          location ~ .php$ {
          ...
          }


          Place the outer location ~ .php$ block below, otherwise it will match all PHP URIs first. See this document for details.



          Use named captures, as numeric captures will be out of scope in the nested location block.



          I don't know what's in your snippets file, but you probably want to avoid try_files (because of this issue with alias) and you need to use $request_filename to locate the path to the SCRIPT_FILENAME.



          The if block is added to avoid passing uncontrolled requests to PHP. See this caution on the use of if.






          share|improve this answer













          You have PHP files in two roots, so you need two location blocks to process those URIs. The simplest solution is to use a nested location block.



          For example:



          location / {
          try_files $uri $uri/ =404;
          }
          location ~ /.ht {
          deny all;
          }
          location ~ ^/~(?<user>[^/]+)(?<path>/.*)?$ {
          alias /home/$1/public_html$2;
          index index.php index.html index.htm;
          autoindex on;

          location ~ .php$ {
          if (!-f $request_filename) { return 404; }
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $request_filename;
          }
          }
          location ~ .php$ {
          ...
          }


          Place the outer location ~ .php$ block below, otherwise it will match all PHP URIs first. See this document for details.



          Use named captures, as numeric captures will be out of scope in the nested location block.



          I don't know what's in your snippets file, but you probably want to avoid try_files (because of this issue with alias) and you need to use $request_filename to locate the path to the SCRIPT_FILENAME.



          The if block is added to avoid passing uncontrolled requests to PHP. See this caution on the use of if.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 7 at 14:43









          Richard SmithRichard Smith

          513148




          513148













          • Surely that if could be replaced with try_files $url =404 or something similar?

            – grawity
            Feb 7 at 15:18











          • I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.

            – Richard Smith
            Feb 7 at 15:28



















          • Surely that if could be replaced with try_files $url =404 or something similar?

            – grawity
            Feb 7 at 15:18











          • I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.

            – Richard Smith
            Feb 7 at 15:28

















          Surely that if could be replaced with try_files $url =404 or something similar?

          – grawity
          Feb 7 at 15:18





          Surely that if could be replaced with try_files $url =404 or something similar?

          – grawity
          Feb 7 at 15:18













          I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.

          – Richard Smith
          Feb 7 at 15:28





          I don’t use try_files with alias. But it might work and there are strange workarounds. See the link in my answer.

          – Richard Smith
          Feb 7 at 15:28


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1403134%2fnginx-cannot-find-public-html-info-php%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

          Mangá

           ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕