CentOS 6.4でSuExec

CentOSおよびRedHat系のyumでインストールできるApacheでは、SuExecのルートディレクトリが/var/www に固定されており、ここ以外をルートディレクトリにしたい場合にはApacheを自前でコンパイルしなければいけない。

管理のことを考えてRPMでインストールしてみる

環境

yumdownloaderのインストール

ソースコードRPMSRPM)をyum経由で取得するために、yumdownloaderを使えるようにする。

インストールされているかを確認。

% which yumdownloader
% rpm -qa | grep yum-utils

インストール

% sudo yum install -y yum-utils.noarch

SRPMをダウンロードできるようにする

/etc/yum.repos.d/srpm.repo を作成し、以下の内容にする(Cent OS 6.4用のSRPMをダウンロードするという意味。従来とはディレクトリ名がSRPM から Sourceに変わっている)。

[os-srpm-centos]
name=CentOS OS $releasever - $basearch - Source from centos
baseurl=http://vault.centos.org/6.4/os/Source/
enabled=1
priority=1
 
[updates-srpm-centos]
name=CentOS UPDATES $releasever - $basearch - Source from centos
baseurl=http://vault.centos.org/6.4/updates/Source/
enabled=1
priority=1
 
[extras-srpm-centos]
name=CentOS EXTRAS $releasever - $basearch - Source from centos
baseurl=http://vault.centos.org/6.4/extras/Source/
enabled=1
priority=1
 
[centosplus-srpm-centos]
name=CentOS CENTOSPLUS $releasever - $basearch - Source from centos
baseurl=http://vault.centos.org/6.4/centosplus/Source/
enabled=0
priority=1
 
[contrib-srpm-centos]
name=CentOS CONTRIB $releasever - $basearch - Source from centos
baseurl=http://vault.centos.org/6.4/contrib/Source/
enabled=0
priority=1
 
[fasttrack-srpm-centos]
name=CentOS FASTTRACK $releasever - $basearch - Source from centos
baseurl=http://vault.centos.org/6.4/fasttrack/Source/
enabled=0
priority=1
 
[cr-srpm-centos]
name=CentOS CR $releasever - $basearch - Source from centos
baseurl=http://vault.centos.org/6.4/cr/Source/
enabled=0
priority=1

実際にダウンロードできるか試す。

% sudo yum clean all
% sudo yum update
% yumdownloader --source webalizer

httpdコンパイル

ソースコードのダウンロード

% yumdownloader --source httpd
% ls
httpd-2.2.15-29.el6.centos.src.rpm

ソースコード展開

% rpm -ivh httpd-2.2.15-29.el6.centos.src.rpm

ホームディレクトリに rpmbuild というディレクトリができ、そこに展開されていた。~/rpmbuild/SPEC/httpd.spec を編集する。

% diff httpd.spec.org httpd.spec
1c1
< %define contentdir /var/www
---
> %define contentdir /home

重要な項目は

  • contentdir(--datadir=%{contentdir}):SuExecのルートディレクトリ。このディレクトリ以下でしかSuExecが使えない
  • --with-suexec-uidmin=500: SuExecの対象とするユーザIDの番号。デフォルトは500番以降が対象
  • --with-suexec-gidmin=100: SuExecの対象とするグループIDの番号。デフォルトは100番以降が対象

rpm作成に必要なパッケージをインストール

% sudo yum install -y rpmdevtools
% rpmdev-setuptree
% yum -y install autoconf zlib-devel libselinux-devel apr-devel apr-util-devel pcre-devel openssl-devel 

rpm作成

% rpmbuild -ba SPEC/httpd.spec

インストール

% yum install -y apr-util-ldap
% cd RPM/x86_64
% sudo rpm -ivh httpd-2.2.15-29.el6.x86_64.rpm httpd-devel-2.2.15-29.el6.x86_64.rpm httpd-tools-2.2.15-29.el6.x86_64.rpm mod_ssl-2.2.15-29.el6.x86_64.rpm

設定確認

% sudo /usr/sbin/suexec -V

SE Linuxの場合、ユーザディレクトリにアクセスできない。SE LinuxをONにしたまま設定するのは難しいので今回はOFFにした。

設定

/etc/httpd/conf/httpd.confを編集する

% cd /etc/httpd/conf/
% cp -p httpd.conf httpd.conf.org
% vi httpd.conf

編集したのは以下のとおり。ユーザディレクトリを有効にし、cgi-binを無効にしている。

% diff httpd.conf.org httpd.conf
262c262
< ServerAdmin root@localhost
---
> ServerAdmin hogehoge@hogehoge.jp
276a277
> ServerName hogehoge.jp:80
292c293,294
< DocumentRoot "/var/www/html"
---
> #DocumentRoot "/var/www/html"
> DocumentRoot "/home/apache/html"
366c368,369
<     UserDir disabled
---
>     #UserDir disabled
>     UserDir disabled root
373c376
<     #UserDir public_html
---
>     UserDir public_html
381,392c384,395
< #<Directory /home/*/public_html>
< #    AllowOverride FileInfo AuthConfig Limit
< #    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
< #    <Limit GET POST OPTIONS>
< #        Order allow,deny
< #        Allow from all
< #    </Limit>
< #    <LimitExcept GET POST OPTIONS>
< #        Order deny,allow
< #        Deny from all
< #    </LimitExcept>
< #</Directory>
---
> <Directory /home/*/public_html>
>     AllowOverride FileInfo AuthConfig Limit
>     Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec ExecCGI
>     <Limit GET POST OPTIONS>
>         Order allow,deny
>         Allow from all
>     </Limit>
>     <LimitExcept GET POST OPTIONS>
>         Order deny,allow
>         Deny from all
>     </LimitExcept>
> </Directory>
536c539,540
< ServerSignature On
---
> #ServerSignature On
> ServerSignature Off
576c580
< ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
---
> #ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
582,587c586,591
< <Directory "/var/www/cgi-bin">
<     AllowOverride None
<     Options None
<     Order allow,deny
<     Allow from all
< </Directory>
---
> #<Directory "/var/www/cgi-bin">
> #    AllowOverride None
> #    Options None
> #    Order allow,deny
> #    Allow from all
> #</Directory>
743c747,748
< LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW
---
> #LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW
> LanguagePriority ja en ca cs da de el eo es et fr he hr it ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW
796c801
< #AddHandler cgi-script .cgi
---
> AddHandler cgi-script .cgi
990c995
< #NameVirtualHost *:80
---
> NameVirtualHost *:80

名前ベースのバーチャルホスト

参考:

IPベース(ポートベース)などいろいろバーチャルホストができる。今回は名前ベースのバーチャルホストにしている。/etc/httpd/conf.d/vhost.confを作成し、以下のようにする。

<VirtualHost *:80>
    SuexecUserGroup hoge hoge
    ServerAdmin webmaster@dummy-host.example.com
    ServerName hoge.jp
    DocumentRoot /home/hoge/public_html
    ErrorLog logs/hoge-virtual-error_log
    CustomLog logs/hoge-virtual-access_log common
</VirtualHost>

ディレクトリを用意する。

% groupadd hoge
% useradd -m -d /home/hoge -s /bin/bash -g hoge hoge
% usermod -a -G hoge apache
% mkdir -p /home/hoge/public_html
% chown -R hoge:hoge /home/hoge/public_html
% chmod 710 /home/hoge
% chmod 710 /home/hoge/public_html
% touch /home/hoge/public_html/index.html
% chmod 640 /home/hoge/public_html/index.html
% touch /home/hoge/public_html/test.cgi
% chmod 700 /home/hoge/public_html/test.cgi

index.htmlの中身

<html>
<head><title>hoge-virtual</title>
</head>
<body>
hoge-virtual
</body>
</html>

test.cgiの中身

#!/usr/bin/perl

print <<_HTML_;
Content-Type: text/html

<html>
<head>
<title>Hello World!</title>
</head>
<body>
Hello World!
</body>
</html>
_HTML_

アクセスしてちゃんと動くか確かめてみる。

% sudo /usr/sbin/apachectl configtest
% sudo /usr/sbin/apachectl start

うまく動かない場合は、/var/log/httpd/ 以下のログを調べる。