![見出し画像](https://assets.st-note.com/production/uploads/images/43434167/rectangle_large_type_2_265ea0dfbd444c9a4a0bd39a02409fef.png?width=1200)
nginx+Apache2.4でオンセンルームを動かす方法
PHPだから見た目を改造しやすくて(身内向けにADはいらないなーって)モバイルフレンドリーでBCDice連携もできちゃうオンセンルームさん、いいですよね。マッピングにも強いしカードにも強いぞ。ところがnginxで動かすにあたって躓いたので備忘録を残します。うちのVPSがngix+Apache2.4構成なんだよね。
環境
さくらのVPS1Gプラン
Ubuntu20.04
Apache2.4
nginx
PHP7.4
nginxでリバースプロキシしてApache2.4でバーチャルホストの設定して複数サイト運営しています。その方法はこちらの記事参照で。常時SSL化も狙って行きます。方法はこちら。
まずやる設定
こちらの設定は全部やっときましょう。
nginxでphp-fpmを使えるようにする
sudo apt install php-fpm
出来ないときはsudo apt updateしてあげてください。
/etc/nginx/nginx.confのPHPに関する設定がデフォルトではコメントアウトされています。以下のように修正します。
location ~ \.php$ {
# try_files $uri $uri/ /index.php?$query_string;
# try_files $uri =404;
include /etc/nginx/fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATHINFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_buffers 8 128k;
fastcgi_buffer_size 256k;
fastcgi_read_timeout 60;
}
バーチャルホストのconfにも上記と同じ設定を足します。
/etc/nginx/fastcgi_paramsに以下を追加します。最後の行でいいです。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
nginxでcgiを実行できるようにする
sudo apt -y install fcgiwrap
/etc/nginx/nginx.confのPHP設定の前あたりに以下を追加します。
location ~ \.cgi$ {
root cgi-binとして指定したディレクトリ;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
バーチャルホストのconfにも同様に追加。このときrootをオンセンルームのディレクトリにすることをお忘れなく。
/etc/init.d/fcgiwrapを編集します。
FCGI_USER
FCGI_GROUP
FCGI_SOCKET_OWNER
FCGI_SOCKET_GROUP
これらを全てwww-dataに設定。
/etc/hostsにオンセンルームを設置したドメイン名を追加します。
127.0.1.1 ドメイン名
nginxを再起動します。
常時SSL化する
nginxは動的サイトに弱いです。部屋を新規作成すると405エラーを返してきます。これをエラーを出さないようにし、さらに常時SSL化します。
バーチャルホストのconfの80でリッスンしているserverのとこに以下の設定を足します。
error_page 405 =200 https://$server_name$request_uri;
return 301 https://$server_name$request_uri;
break;
最終的なバーチャルホストのconf(nginx)の設定例
私のサーバーではこんな感じになりました。これで動いてます。
server {
listen 80;
server_name サーバー名;
root /var/www/cgi-bin/onsen;
location ~ .*\.(jpg|JPG|gif|GIF|png|PNG|css|CSS|js|JS|inc|INC|ico|ICO) {
root /var/www/cgi-bin/onsen;
index index.php index.html;
ssi on;
}
location ~ \.cgi$ {
root /var/www/cgi-bin/onsen;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
root /var/www/cgi-bin/onsen;
# try_files $uri $uri/ /index.php?$query_string;
# try_files $uri =404;
include /etc/nginx/fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATHINFO $fastcgi_path_info;
# fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_buffers 8 128k;
fastcgi_buffer_size 256k;
fastcgi_read_timeout 60;
}
index index.php index.html;
error_page 405 =200 https://$server_name$request_uri;
return 301 https://$server_name$request_uri;
break;
}
server {
listen 443 ssl;
server_name サーバー名;
# ssl on;
ssl_certificate let'sencryptの鍵(fullchain.pemの方)のパス;
ssl_certificate_key let'sencryptの鍵(privkey.pemの方)のパス;
ssl_protocols TLSv1 TLSv1.1 TLSV1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
client_max_body_size 20M;
location ~ .*\.(jpg|JPG|gif|GIF|png|PNG|css|CSS|js|JS|inc|INC|ico|ICO) {
root /var/www/cgi-bin/onsen;
index index.php index.html;
ssi on;
}
location ~ \.cgi$ {
root /var/www/cgi-bin/onsen;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
root /var/www/cgi-bin/onsen;
# try_files $uri $uri/ /index.php?$query_string;
# try_files $uri =404;
include /etc/nginx/fastcgi_params;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATHINFO $fastcgi_path_info;
# fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_buffers 8 128k;
fastcgi_buffer_size 256k;
fastcgi_read_timeout 60;
}
location / {
root /var/www/cgi-bin/onsen;
port_in_redirect off;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass リダイレクト先のURLをポート番号まで含めて指定;
}
}
いいなと思ったら応援しよう!
![猫あきら](https://assets.st-note.com/production/uploads/images/23211231/profile_742dd949a2d4f271751494b2039458a5.png?width=600&crop=1:1,smart)