Table of Contents
1 apache2のインストール
aptでapache2をインストールします。
$ sudo apt-get install -y apache2
2 userdirモジュールの有効化
userdirモジュールを有効にします。
$ sudo a2enmod userdir$ sudo systemctl restart apache2
3 public_htmlの作成
$ mkdir ~/public_html
このまま以下のURLにアクセスすると以下の様な画面が表示されます。
ファイルをダウンロードさせるだけならばこのままpublic_htmlにファイルを置くだけでも良いでしょう。
http://<server>/~<username>
URLにアクセスするとDirectoryIndexにindex.htmlやindex.cgiがデフォルトで呼ばれます。
index.htmlを作成して簡易なページを表示させることもできます。
4 Digest認証の導入
ページアクセスにDigest認証を使用するようにします。
auth_digestモジュールを有効にします。
$ sudo a2enmod auth_digest$ sudo systemctl restart apache2
以下の様なpublic_html/.htaccessを作成します。”hiroom2″はレルムと呼ばれるものです。
AuthType DigestAuthName “hiroom2″AuthUserFile /home/hiroom2/.htdigestrequire valid-user
レルム”hiroom”へアクセスするユーザhiroom2を追加します。
$ htdigest -c ~/.htdigest “hiroom2” hiroom2Adding password for hiroom2 in realm hiroom2.New password:Re-type new password:
ページにアクセスするとユーザ名とパスワードを求められます。
5 CGIの導入
userdirはデフォルトでExecCGIを許可していません。
CGI導入するならDocker等でコンテナを作成した方が良いのかもしれません。
userdir.confのOptionsにExecCGIを追加します。
$ diff -uprN /etc/apache2/mods-available/userdir.conf{.org,}— /etc/apache2/mods-available/userdir.conf.org 2016-05-11 07:22:10.861339651 +0900+++ /etc/apache2/mods-available/userdir.conf 2016-05-11 08:45:45.955093386 +0900@@ -4,7 +4,7 @@ <Directory /home/*/public_html> AllowOverride FileInfo AuthConfig Limit Indexes- Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec+ Options ExecCGI MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec <Limit GET POST OPTIONS> Require all granted </Limit>
cgiモジュールを有効にします。
$ sudo a2enmod cgi$ sudo systemctl restart apache2
以下の様なpublic_html/.htaccessを作成します。
AddHandler cgi-script .cgi
以下のindex.cgiを追加します(chmod a+xで実行権限を与えて下さい)。
#!/bin/shecho “Content-type: text/html”echo “”echo “hello”
URLにアクセスするとindex.cgiによるHTMLが出力されました。