nginx+php on Ubuntu16.04で502 BadGatewayが出る

この記事はまだ移植しても良いかなって記事だけ移植してます
移植第2弾です。

タイトルの通りの現象が出て他の人の記事色々みたけどちょっと特殊なハマり方だったので記録。

とりあえずエラーログをみる

/var/log/nginx/error.log

2017/08/17 08:22:51 [crit] 4801#4801: *8 connect() to unix:/run/php/php7.0-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: _, request: "GET /a.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "hogehoge"

php7.0-fpmが見当たらないとのことです。

まあ、そうですよね。

それもそのはずで今回環境作ってる時に最初nginxと一緒にphp7.0をインストールしたものの、「php7.1あるやんけ」と、気づいて7.0をアンインストール、7.1をインストールしました。

その際に

sudo apt-get remove php7.0-fpm

を実行したもののそれだと不十分でconf諸々を書き直さないといけないみたい。 そこで

sudo apt list --installed

とすると

....
php7.0-cli/xenial,now 7.0.22-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,auto-removable]
php7.0-common/xenial,now 7.0.22-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,auto-removable]
php7.0-json/xenial,now 7.0.22-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,auto-removable]
php7.0-opcache/xenial,now 7.0.22-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,auto-removable]
php7.0-readline/xenial,now 7.0.22-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,auto-removable]
php7.1-cli/xenial,now 7.1.8-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,automatic]
php7.1-common/xenial,now 7.1.8-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,automatic]
php7.1-fpm/xenial,now 7.1.8-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,automatic]
php7.1-json/xenial,now 7.1.8-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,automatic]
php7.1-opcache/xenial,now 7.1.8-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,automatic]
php7.1-readline/xenial,now 7.1.8-2+ubuntu16.04.1+deb.sury.org+4 amd64 [installed,automatic]
...

という7.0と7.1が存在するカオスな状態に うまく消しきれてなかったみたいです。 そこで

sudo apt-get remove php7.0*

とするとうまく全部消えてくれました。 その後

以下のように編集するとうまいこと行ってくれました。

/etc/nginx/conf.d/default.conf

    location ~ \.php$ {
        root           /var/www/html;
        #fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_pass   unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

/etc/nginx/sites-enabled/default

        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;
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        }

また、sudo service hoge restartする際にphp7まで入力してtabを押しても7.0と7.1が残っていて鬱陶しかったので

cd /etc/init.d
sudo rm php7.0-fpm

をしてスッキリさせました。

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です