質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

2回答

1320閲覧

nginxでブラウザにPHPを表示できない。(404で帰ってくる)

Rikyu00

総合スコア6

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2020/07/28 13:28

前提・実現したいこと

nginxでブラウザにindex.phpを表示させようとすると404で帰ってきます。

該当のソースコード

nginxの設定

server { listen 80; server_name ec2-***-***-**-**.ap-northeast-1.compute.amazonaws.com; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { #root /usr/share/nginx/html; root /var/www/html; index index.php index.html index.htm; try_files $uri $uri/ /index.php?$query_string; } location ~ .php$ { root /var/www/html;   fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;   fastcgi_index index.php;   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;   include fastcgi_params; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { #root /usr/share/nginx/html; root /var/www/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} } ``` php-fpmの設定 ``` ; Start a new pool named 'www'. ; the variable $pool can be used in any directive and will be replaced by the ; pool name ('www' here) [www] ; Per pool prefix ; It only applies on the following directives: ; - 'access.log' ; - 'slowlog' ; - 'listen' (unixsocket) ; - 'chroot' ; - 'chdir' ; - 'php_values' ; - 'php_admin_values' ; When not set, the global prefix (or @php_fpm_prefix@) applies instead. ; Note: This directive can also be relative to the global prefix. ; Default Value: none ;prefix = /path/to/pools/$pool ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache user chosen to provide access to the same directories as httpd user = nginx ; RPM: Keep a group allowed to write in log dir. group = nginx ; The address on which to accept FastCGI requests. ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on ; a specific port; ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses ; (IPv6 and IPv4-mapped) on a specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. listen = 127.0.0.1:9000 listen = /var/run/php-fpm/php-fpm.sock ; Set listen(2) backlog. ; Default Value: 511 ;listen.backlog = 511 ; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web server. ; Default Values: user and group are set as the running user ; mode is set to 0660 listen.owner = nginx listen.group = nginx listen.mode = 0660 ; When POSIX Access Control Lists are supported you can set them using ; these options, value is a comma separated list of user/group names. ; When set, listen.owner and listen.group are ignored ;listen.acl_users = apache,nginx ;listen.acl_groups = ``` vi /var/www/html/index.php でファイル作成後、中身を <?php echo phpinfo(); ?> としてブラウザに表示させようとすると404で帰ってきてしまいます。 構文チェックは問題なしでした。 色々調べてみたのですが原因が分からず、 分かる方がいらっしゃればご教示いただければと存じます。 よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TaichiYanagiya

2020/07/28 14:20

設定を見ただけで検証していないのですが、問題ないように思います。 404 ということはドキュメントルートに index.php が本当に存在しないか(この場合 directory index of "/var/www/html/" is forbidden で 403 になりそうですが)、別の server { } 設定があって、別のドキュメントルートを見にいっているとか?
hentaiman

2020/07/28 14:58 編集

色々ってのは何を調べました?調べて対応してみた内容全部記載してください。 原因予測はつくけど質問に記載されている内容だけでは読み取れない部分もあるし、回答しても「それはもう試しました」って言われるのが嫌なので。 調査内容と試した内容全部書いてもらった時点で回答しようと思います。
yukky1201

2020/07/28 15:12

listen = 127.0.0.1:9000 listen = /var/run/php-fpm/php-fpm.sock いずれかしか設定できないはず。php-fpmは起動してますか?
guest

回答2

0

みなさまたくさんアドバイスいただきありがとうございます。

自分で再度確認してみたところ、必要な部分をコメントアウトしており動作していなかったことが原因でした。
大変失礼いたしました。

投稿2020/07/28 15:25

Rikyu00

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

自己解決

みなさまたくさんアドバイスいただきありがとうございます。

自分で再度確認してみたところ、必要な部分をコメントアウトしており動作していなかったことが原因でした。
大変失礼いたしました。

投稿2020/07/28 15:20

Rikyu00

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/07/28 20:47

> 自分で再度確認してみたところ、必要な部分をコメントアウトしており動作していなかったことが原因でした。 どこのことでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問