Ubuntu21.04からUbuntu21.10にアップグレードした際に、Nginxで「Bad Gateway 402」のエラーが出た場合は、PHPが7.4から8.0にバージョンアップしていることが原因のこともあります。
そろそろUbuntu22.04提供の時期に入ってきましたので、22.04を意識したUbuntu21.10(Impish Indri)へと20.04からディストリビューションアップグレードしてみました。サイト管理者(筆者)はデスクトップ版に多少のサーバーアプリケーションを入れて、WordpressなどのWebサイト作成用のCMSを検証しています。
確か、Ubuntu21.04では問題なかったと思いますが本日10月24日日曜日に検証用のWordpressで作成中のサイトをChromeで開いたところ「Bad Gateway 402」のエラーが出て、サイトが表示されません。Googleで少し調べたところ、PHPが7.4から8.0にバージョンアップしていることが問題のようでした。そこで、synapticでPHPは7がインストール済みになっていましたが、PHPの8とPHP8.0-fpmをインストールしました。
その次に、/etc/nginx/sites-enabled/defaultで、7.4になっているところを8.0に変更しました。SSLの設定は本番用で、テスト環境ではよほどのことがない限り必要ありませんので、コメント記号をつけています。php-fpmの変更の必要な箇所はやや暗い赤色で分かるようにしておきました。
server {
listen 80;
# listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;# httpをhttpsにリダイレクト
# listen 80;### SSL Setting
# listen 443;
# ssl on;
# ssl_certificate /etc/ssl/private/combined01.crt;
# ssl_certificate_key /etc/ssl/private/server01-noph.key;# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
# ssl_prefer_server_ciphers on;
# ssl_ciphers 'ECDH !aNULL !eNULL !SSLv2 !SSLv3';
# ssl_certificate /etc/letsencrypt/live/primer.it-ishin.com/fullchain.pem;
# ssl_certificate_key /etc/letsencrypt/live/primer.it-ishin.com/privkey.pem;
root /var/www/primer;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name primer.it-ishin.com;
client_max_body_size 50M;
access_log /var/log/nginx/primer-access.log;
error_log /var/log/nginx/primer-error.log;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php last;
}
}
#error_page 404 /404.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
include snippets/fastcgi-php.conf; # by Nomura
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; /var/run/php/php7.4-fpm.sockになっていたのを変更
# fastcgi_index index.php;
include fastcgi_params;
# time out
fastcgi_buffers 8 128k;
fastcgi_buffer_size 256k;
fastcgi_read_timeout 300;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
そのうえで、php8.0-fpmとnginxを再起動しました。
sudo systemctl restart php8.0-fpm sudo systemctl restart nginx
次のように、起動しました。
ディストリビューションのアップグレードの際は、パッケージが最新版になっているので、その点を確認してから設定の変更が必要になりますね。