Ubuntu 24.04 LTS で Apache2 の CGIを動かす

Install

apt -y install apache2

確認

apachectl -V

Server version: Apache/2.4.58 (Ubuntu)
Server built: 2024-07-17T18:55:23
Server's Module Magic Number: 20120211:129
Server loaded: APR 1.7.2, APR-UTIL 1.6.3, PCRE 10.42 2022-12-11
Compiled using: APR 1.7.2, APR-UTIL 1.6.3, PCRE 10.42 2022-12-11
Architecture: 64-bit
Server MPM: event
threaded: yes (fixed thread count)
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_PROC_PTHREAD_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/apache2"
-D SUEXEC_BIN="/usr/lib/apache2/suexec"
-D DEFAULT_PIDLOG="/var/run/apache2.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="mime.types"
-D SERVER_CONFIG_FILE="apache2.conf"

systemctl status apache2

● apache2.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
Active: active (running) since Thu 2024-09-26 20:04:07 UTC; 44min ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 1772 (apache2)
Tasks: 55 (limit: 4477)
Memory: 5.5M (peak: 5.6M)
CPU: 510ms
CGroup: /system.slice/apache2.service
├─1772 /usr/sbin/apache2 -k start
├─1773 /usr/sbin/apache2 -k start
└─1775 /usr/sbin/apache2 -k start

Sep 26 20:04:07 XXXXXXXX systemd[1]: Starting apache2.service - The Apache HTTP Server...
Sep 26 20:04:07 XXXXXXXX apachectl[1771]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.>
Sep 26 20:04:07 XXXXXXXX systemd[1]: Started apache2.service - The Apache HTTP Server.
lines 1-16/16 (END)

curl http://localhost/

停止と起動

systemctl stop apache2
systemctl start apache2

userdir を 有効化する

ls -l /etc/apache2/mods-enabled/

userdir.* がない

a2enmod userdir

Enabling module userdir.
To activate the new configuration, you need to run:
systemctl restart apache2

ls -l /etc/apache2/mods-enabled/

lrwxrwxrwx 1 root root 30 Sep 26 21:02 userdir.conf -> ../mods-available/userdir.conf
lrwxrwxrwx 1 root root 30 Sep 26 21:02 userdir.load -> ../mods-available/userdir.load

systemctl restart apache2

一般ユーザで

cd
chmod 755 .
mkdir public_html
vi public_html/index.html
curl http://localhost/

CGI を有効化 (非一般ユーザ)

ls -l /etc/apache2/mods-enabled/

cgid.conf.* がない

a2enmod cgid

Enabling module cgid.
To activate the new configuration, you need to run:
systemctl restart apache2

ls -l /etc/apache2/mods-enabled/

lrwxrwxrwx 1 root root 27 Sep 26 21:09 cgid.conf -> ../mods-available/cgid.conf
lrwxrwxrwx 1 root root 27 Sep 26 21:09 cgid.load -> ../mods-available/cgid.load

systemctl restart apache2

設定を確認する

cat /etc/apache2/conf-enabled/serve-cgi-bin.conf
<IfModule mod_alias.c>
        <IfModule mod_cgi.c>
                Define ENABLE_USR_LIB_CGI_BIN
        </IfModule>

        <IfModule mod_cgid.c>
                Define ENABLE_USR_LIB_CGI_BIN
        </IfModule>

        <IfDefine ENABLE_USR_LIB_CGI_BIN>
                ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
                <Directory "/usr/lib/cgi-bin">
                        AllowOverride None
                        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                        Require all granted
                </Directory>
        </IfDefine>
</IfModule>

CGIのファイル置き場が "/usr/lib/cgi-bin/" であることを確認。

vi a.c
cat a.c
void main(){
  printf("Content-type: text/html\n\n");
  printf("Hello, CGI");
}
gcc a.c -o /usr/lib/cgi-bin/a.cgi
curl http://localhost/cgi-bin/a.cgi

Hello, CGI

CGI を有効化 (一般ユーザ)

cp /etc/apache2/mods-available/mime.conf /etc/apache2/mods-available/mime.conf.org
vi /etc/apache2/mods-available/mime.conf
diff /etc/apache2/mods-available/mime.conf.org /etc/apache2/mods-available/mime.conf

218c218
< #AddHandler cgi-script .cgi
---
> AddHandler cgi-script .cgi

cp /etc/apache2/mods-available/userdir.conf /etc/apache2/mods-available/userdir.conf.org
vi /etc/apache2/mods-available/userdir.conf
diff /etc/apache2/mods-available/userdir.conf.org /etc/apache2/mods-available/userdir.conf

6c6
< Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
---
> Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec ExecCGI

systemctl restart apache2
curl http://localhost/~yam/a.cgi

Hello, CGI

#apache #apache2 #cgi #ubuntu  

この記事が気に入ったらサポートをしてみませんか?