LoginSignup
3
3

More than 3 years have passed since last update.

WSL で apache を動かす

Posted at

WSL で apache を動かす

動作環境

$ uname -srvmpi
Linux 4.4.0-18362-Microsoft #476-Microsoft Fri Nov 01 16:53:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux
$ apache2 -v
Server version: Apache/2.4.29 (Ubuntu)
Server built:   2019-09-16T12:58:48
$ perl --version

This is perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-linux-gnu-thread-multi
...

各種コマンド

service の状態確認/停止/起動/再起動/設定再読込

service apache2 status
sudo service apache2 stop
sudo service apache2 start
sudo service apache2 restart
sudo service apache2 reload

モジュールの有効/無効

sudo a2enmod cgid
sudo a2dismod cgid

設定の有効/無効

sudo a2enconf serve-cgi-bin
sudo a2disconf serve-cgi-bin

apache2

apache2 をインストール

sudo apt install apache2

service の起動後にこの辺にアクセスできるかを確認

cgid

設定の初期値(編集前)

  • cgid モジュールは最初から有効になっている
  • 設定 mime, serve-cgi-bin は最初から有効になっている
【設定-A1】/etc/apache2/mods-available/mime.conf
...
    #
    # AddHandler allows you to map certain file extensions to "handlers":
    # actions unrelated to filetype. These can be either built into the server
    # or added with the Action directive (see below)
    #
    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    #
    #AddHandler cgi-script .cgi
...
【設定-B1】/etc/apache2/conf-available/serve-cgi-bin.conf
...
    <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>
...

テスト用のCGIプログラムを配置

  • 実行権限があること(chmod +x /usr/lib/cgi-bin/env.cgi)
/usr/lib/cgi-bin/env.cgi
#!/usr/bin/perl
use Cwd qw(getcwd);
print "Content-type: text/html\n\n";
print "<html><head><title>ENV</title></head><body>";
print "\n<p>Current Working Directory is ",getcwd(),"</p>\n<table border=1>";
my @keys = qw/MOD_PERL DOCUMENT_ROOT SCRIPT_NAME SCRIPT_FILENAME/;
for my $i (@keys){ print "\n<tr><td>ENV{$i}</td><td>$ENV{$i}</td></tr>"; }
print "\n</table></body></html>";

service の再起動後にアクセスできるかを確認

getcwd() /usr/lib/cgi-bin
ENV{MOD_PERL}
ENV{DOCUMENT_ROOT} /var/www/html
ENV{SCRIPT_NAME} /cgi-bin/env.cgi
ENV{SCRIPT_FILENAME} /usr/lib/cgi-bin/env.cgi

設定 ("ScriptAliase" が指定されていないディレクトリでCGIを有効にする)

【設定-A2】/etc/apache2/mods-available/mime.conf
...
    #
    # AddHandler allows you to map certain file extensions to "handlers":
    # actions unrelated to filetype. These can be either built into the server
    # or added with the Action directive (see below)
    #
    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    #
    AddHandler cgi-script .cgi
...

設定 serve-mine を追加し、有効にする

【設定-C2】/etc/apache2/conf-available/serve-mine.conf
Alias /perl /var/www/perl
<Directory "/var/www/perl">
    Options +ExecCGI
</Directory>
sudo a2enconf serve-mine

テスト用のCGIプログラムを配置

  • 実行権限があること(chmod +x /var/www/perl/env.cgi)
/var/www/perl/env.cgi
# /usr/lib/cgi-bin/env.cgi と同じ内容

service の再起動後にアクセスできるかを確認

getcwd() /var/www/perl
ENV{MOD_PERL}
ENV{DOCUMENT_ROOT} /var/www/html
ENV{SCRIPT_NAME} /perl/env.cgi
ENV{SCRIPT_FILENAME} /var/www/perl/env.cgi

mod-perl2

mod-perl2 をインストール

sudo apt install libapache2-mod-perl2

設定 (ModPerl::Registry を使用)

  • 【設定-A1】のままで良い
【設定-C3A】/etc/apache2/conf-available/serve-mine.conf
Alias /perl /var/www/perl
<Directory "/var/www/perl">
    SetHandler perl-script
    PerlResponseHandler ModPerl::Registry
#   PerlResponseHandler ModPerl::RegistryPrefork
    PerlOptions +ParseHeaders
    Options +ExecCGI
</Directory>

モジュール

  • cgid を無効にしないと、apache2 が起動しない
  • perl を有効にする
sudo a2dismod cgid
sudo a2enmod perl

service の再起動後にアクセスできるかを確認

getcwd() /
ENV{MOD_PERL} mod_perl/2.0.10
ENV{DOCUMENT_ROOT} /var/www/html
ENV{SCRIPT_NAME} /perl/env.cgi
ENV{SCRIPT_FILENAME} /var/www/perl/env.cgi

カレントディレクトリは、env.cgi の配置場所ではなく、"/" になっている。

設定 (ModPerl::RegistryPrefork を使用)

  • 【設定-A1】のままで良い
【設定-C3B】/etc/apache2/conf-available/serve-mine.conf
Alias /perl /var/www/perl
<Directory "/var/www/perl">
    SetHandler perl-script
#   PerlResponseHandler ModPerl::Registry
    PerlResponseHandler ModPerl::RegistryPrefork
    PerlOptions +ParseHeaders
    Options +ExecCGI
</Directory>

モジュール

  • cgid を無効にしないと、apache2 が起動しない
  • perl を有効にする
  • mpm_event を無効にしないと、アクセス時にエラーとなる
  • mpm_prefork を有効にする
sudo a2dismod cgid
sudo a2enmod perl
sudo a2dismod mpm_event
sudo a2enmod mpm_prefork

service の再起動後にアクセスできるかを確認

getcwd() /var/www/perl
ENV{MOD_PERL} mod_perl/2.0.10
ENV{DOCUMENT_ROOT} /var/www/html
ENV{SCRIPT_NAME} /perl/env.cgi
ENV{SCRIPT_FILENAME} /var/www/perl/env.cgi

カレントディレクトリは、env.cgi の配置場所と同じになっている。

3
3
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
3
3