Junkie Work

access: 202836

FreeBSD 14.3-RELEASEにてPHP(nginx+PHP)


https対応のWebサーバーに設定を追加して、PHPも利用できるようにします。

PHPのインストール

最新のPHPをpkgでインストールします。

# pkg install php84

php-fpmの起動

php-fpmを有効にして、起動します。

# service php_fpm enable
# service php_fpm start

nginxの設定ファイルの変更

nginxの設定ファイルを作成します。

複数のサイトが利用できるようにファイルを分割します。
/usr/local/etc/nginx/sites.d/php.inc というファイルに以下を記載します。

location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

/usr/local/etc/nginx/sites.d/junkie.work.confを以下のように修正します。
index.phpの追加と、php.incの読み込みの追加です。

server {
    listen 80;
    server_name junkie.work www.junkie.work;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name junkie.work www.junkie.work;

    ssl_certificate /usr/local/etc/letsencrypt/live/junkie.work/fullchain.pem;
    ssl_certificate_key /usr/local/etc/letsencrypt/live/junkie.work/privkey.pem;
    include /usr/local/etc/letsencrypt/options-ssl-nginx.conf;

    root /usr/local/www/junkie.work/data;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }
    include /usr/local/etc/nginx/sites.d/php.inc;
}

nginxの設定ファイルの確認と再起動

nginxの設定ファイルを確認して、問題がなければ再起動します。

# nginx -t
# service nginx restart

PHPの動作確認

PHPの動作確認を行います。
Webのrootにinfo.phpというファイルを作成します。

# echo "<?php phpinfo(); ?>" > /usr/local/www/junkie.work/data/info.php

ブラウザで、http://ホスト名/info.phpにアクセスして、PHPの情報が表示されれば成功です。

info.phpは、セキュリティ上の理由から、公開しないようにします。
以下のコマンドで削除します。

# rm /usr/local/www/junkie.work/data/info.php