0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHP CGI を Apache2 で設定する

0
Last updated at Posted at 2025-08-15
.bash
sudo apt update
sudo apt search mod_cgi
Sorting... Done
Full Text Search... Done
libapache2-mod-fcgid/noble 1:2.3.9-4 amd64
  FastCGI interface module for Apache 2

# mod_cgi をインストール
sudo apt install libapache2-mod-fcgid

# インストールによって自動的に有効化するが、一応実行
sudo a2enmod cgi
[sudo] password for username:
Your MPM seems to be threaded. Selecting cgid instead of cgi.
Enabling module cgid.
To activate the new configuration, you need to run:
  systemctl restart apache2
sudo systemctl restart apache2

# mod_cgi が mods-available (モジュールのロードと設定を行うための設定ファイル) に存在することを確認する
sudo cat -n /etc/apache2/mods-available/cgi.load | grep LoadModule
     1  LoadModule cgi_module /usr/lib/apache2/modules/mod_cgi.so

予備知識 (調べたこと)

マシンのネットワークポートをバインドしたり、 リクエストを受け付けたり、リクエストを扱うよう子プロセスに割り当てたり、 といった役割を持ちます。

  • mod_cgid

mod_cgid mod_cgi
コンパイル時にマルチスレッド MPM が選ばれたときは mod_cgi の代わりに必ずこのモジュールが使用されます。設定と動作は mod_cgi とまったく同じ ハンドラ cgi-script が指定されているファイルは CGI スクリプトとして扱われ、 サーバにより実行され、その出力がクライアントに返されます。

設定手順

Apache と PHP の設定をおこないます。

バーチャルホスト

<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI
    AddHandler cgi-script .php
    Require all granted
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

php.ini で強制リダイレクトをオフにする

【超重要】
PHP CGI は、PHP スクリプトにじかで Web ブラウザからアクセスできないようにデフォルトでなっているようです。

なので、php.ini で、この設定をオフにします。デフォルトでコメントアウトされていても、オンになっているので、注意。

.bash
sudo nano /etc/php/8.3/cgi/php.ini

↓ php.ini

; cgi.force_redirect is necessary to provide security running PHP as a CGI under
; most web servers.  Left undefined, PHP turns this on by default.  You can
; turn it off here AT YOUR OWN RISK
; **You CAN safely turn this off for IIS, in fact, you MUST.**
; https://php.net/cgi.force-redirect
-;cgi.force_redirect = 1
+cgi.force_redirect = 0

参考:

テストしてみる

.bash
sudo nano /usr/lib/cgi-bin/test.php

test.php に以下を記述。1 行目 の #! ( shebang ) は必要の行です。ついでphp-cgi のインストールパスを指定します。

#!/usr/bin/php-cgi8.3
<?php header("Content-Type: text/html"); echo "<h1>Hello from PHP CGI!</h1>";?>
.bash
# apache の実行ユーザーを確認
ps aux | grep apache
root        2586  0.0  0.0   6880  5132 ?        Ss   11:06   0:00 /usr/sbin/apache2 -k start
www-data    2587  0.0  0.0   7892  4756 ?        S    11:06   0:00 /usr/sbin/apache2 -k start
.bash
# 所有者、アクセス権限の設定
sudo chown -R www-data:www-data /usr/lib/cgi-bin
sudo chown www-data:www-data /usr/lib/cgi-bin/test.php
sudo chmod +x /usr/lib/cgi-bin/test.php
ls -al /usr/lib/cgi-bin/test.php
-rwxr-xr-x 1 www-data www-data 82 Aug 14 11:17 /usr/lib/cgi-bin/test.php

sudo a2enmod cgi
sudo systemctl restart apache2

http://<your-IP>/cgi-bin/test.php にアクセスできたら完了。

参考になったサイト (公式 )

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?