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

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

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

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

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

PHP

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

Q&A

解決済

3回答

5246閲覧

nginxでphpを動かしたい

takehiro_pink

総合スコア23

nginx

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

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

PHP

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

0グッド

0クリップ

投稿2016/10/08 13:58

編集2016/10/08 14:47

▼やりたいこと
nginxでphpを動かしたい。

▼環境
Vagrantでバーチャルサーバー構築
Ubuntu16.04
Nginx1.10.1
PHP 7.0
PHP-FPM 7.0

▼nginxの設定
/etc/nginx/conf.d/default.conf

server { listen 80; server_name 192.168.33.10; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } #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 /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 /var/www/html; fastcgi_pass unix:/run/php/php7.0-fpm.sock; # fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME /var/www/html$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; } }

▼nginxエラーログ

2016/10/08 13:50:49 [error] 21977#21977: *1 open() "/var/www/html/50x.html" failed (2: No such file or directory), client: 192.168.33.1, server: 192.168.33.10, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock", host: "192.168.33.10"

▼index.phpの置き場所は以下
/var/www/html/index/php
index.phpはuser,groupともにwww-data

問題なく表示されるindex.htmlもwww-data:www-data

-rwxr-xr-x 1 www-data www-data 537 May 31 14:16 50.html -rwxr-xr-x 1 www-data www-data 612 May 31 14:16 index.html -rwxr-xr-x 1 www-data www-data 18 Oct 8 12:22 index.php -rwxr-xr-x 1 www-data www-data 6 Oct 8 12:06 info.php

phpはどうすれば表示されますでしょうか。

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

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

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

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

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

guest

回答3

0

ベストアンサー

ほとんど同じような構成で動いていますよ
自分はmanzyunさんのご指摘のとおり、/etc/nginx/sites-available/defaultを修正する方法でやっています。以下自分の設定です。参考にしてください。
※もう記憶があいまいですが、コメントを外したぐらいしかやらなかったような...

server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.php index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { include snippets/fastcgi-php.conf; # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php7.0-fpm: fastcgi_pass unix:/run/php/php7.0-fpm.sock; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } }

(追記)

/etc/nginx/snippets/fastcgi-php.conf

# regex to split $uri to $fastcgi_script_name and $fastcgi_path fastcgi_split_path_info ^(.+\.php)(/.+)$; # Check that the PHP script exists before passing it try_files $fastcgi_script_name =404; # Bypass the fact that try_files resets $fastcgi_path_info # see: http://trac.nginx.org/nginx/ticket/321 set $path_info $fastcgi_path_info; fastcgi_param PATH_INFO $path_info; fastcgi_index index.php; include fastcgi.conf;

投稿2016/10/09 08:42

編集2016/10/10 04:36
popobot

総合スコア6586

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

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

takehiro_pink

2016/10/09 09:23

snippets/fastcgi-php.conf; これ、私の環境では、snippets/fastcgi-php.conf ってのが存在しないのですが、 nginxのインストールやphp-fpmのインストールなどどのように環境を構築したら生成されるのでしょうか? sites-availableも入っておらずです
popobot

2016/10/09 10:50 編集

nginx-commonパッケージをインストールすると入るようです。
manzyun

2016/10/10 04:22

自分の場合ですと、nginxの公式リポジトリを追加し、nginxをaptでインストールしました。 その際にphp-fpmのインストールのし忘れでPHPが動かなかったので、こちらもaptでphp-fpmをインストールして動作しました。 この際にインストールされるphp-fpmのバージョンは7なので、fastcgi_passの値は、質問に掲載されている設定で大丈夫だと思います。
popobot

2016/10/10 04:37 編集

なるほど! それだと自分もnginxの基本的な設定は理解できていないので、よくわからないです。すみません...。 参考に/etc/nginx/snippets/fastcgi-php.confの内容を貼っておきます...
guest

0

パーミッション周りの可能性が高そうです。こちらが参考になると思います。

投稿2016/10/09 05:33

matsu

総合スコア702

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

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

0

まずエラーメッセージを見て素直に思ったのが

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

この設定のせいでBatgatewayも表示されていないのではないかと思います。

続いて、念の為以下コマンドでnginxの設定ファイルに間違いがないか確認しましょう。

# nginx -t

問題なければ以下のように返答が返ってきます。

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

とりあえずコレで動作しますでしょうか?


P.S.

Ubuntu(Xenial(16.04))の場合、sites-enabledのdefault設定を書き換える方法の方がすんなり行く気がしました。

投稿2016/10/08 14:54

manzyun

総合スコア2244

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

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

takehiro_pink

2016/10/09 05:01

bad gatewayは表示されるようになりました。 が、相変わらずPHPは動いておらずです。。。 何が原因なのでしょうか。。。。
manzyun

2016/10/10 04:19

見当はずれかもしれませんが、 server{ 項目の index のパラメーターに「index.php」を追加してみてはいかがでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問