本日も乙

ただの自己満足な備忘録。

さくらVPS(CentOS 6.2)でソースからApache(2.2系&2.4系)をインストール

前回Apacheyumでインストールした。 yumでインストールすると簡単だし、起動するのに手間がかからないのだけど 個別にモジュールを入れたり、インストールしたいバージョンがなかったりするので不便な場合がある。 そういう時はソースからインストールするのが良い。 ソースからのインストールは基本的に以下の通りにやることが多い。

  1. wgetコマンドでソース(tar.gz or zip)をダウンロード
  2. tar or unzipコマンドで解凍
  3. ./configure で設定
  4. make -> make install

しかし、入れるアプリケーションによってconfigureでオプションを付けたり、必要なパッケージをyumでインストールしたりと試行錯誤しながらやるので面倒だったりする。 本やWebで調べながら実際にインストールをやって覚えるのが一番だと思う。

というわけで、Apacheをソースからインストールしていきます。 今回は、Apache 2.2.23, 2.4.2 をインストールしました。 2.2系と2.4系は若干インストール方法が異なるので注意!

  1. 必要なパッケージのインストール
  2. $ sudo yum install openssl openssl-devel
    
  3. Apache Portable Runtim(APR)のインストール(2.4系の場合のみ)
  4. 2.2系の場合は、4.Apacheのインストール まで飛ばしてOK。
    1. ソースのダウンロード、解凍
    2. $ wget http://ftp.riken.jp/net/apache//apr/apr-1.4.6.tar.gz
      $ tar zxvf apr-1.4.6.tar.gz
      
      最新版は、https://apr.apache.org/download.cgi から探す
    3. インストール
    4. $ cd apr-1.4.6
      $ ./configure
      $ make
      $ sudo make install
      
  5. APR-utilのインストール(2.4系の場合のみ)
    1. ソースのダウンロード、解凍
    2. $ wget http://ftp.riken.jp/net/apache//apr/apr-util-1.4.1.tar.gz
      $ tar zxvf apr-util-1.4.1.tar.gz
      
      最新版は、https://apr.apache.org/download.cgi から探す
    3. インストール
    4. $ cd apr-util-1.4.1
      $ ./configure --with-apr=/usr/local/apr
      $ make
      $ sudo make install
      
      with-aprオプションは、APRがインストールされているディレクトリのパスを指定する。 TABキーを押しながらやるとディレクトリ名を間違えずに済む。
  6. Apacheのインストール
    1. 2.4系の場合
    2. $ wget http://ftp.meisei-u.ac.jp/mirror/apache/dist//httpd/httpd-2.4.2.tar.gz
      $ tar zxvf httpd-2.4.2.tar.gz
      
      最新版は、https://httpd.apache.org/download.cgi から探す
      $ cd httpd-2.4.2
      $ ./configure \
      --enable-so \
      --enable-ssl \
      --enable-rewrite \
      --with-apr=/usr/local/apr \
      --with-apr-util=/usr/local/apr
      
      上記のオプションはDSO, SSL, mod_rewrite モジュールをApacheに組み込んでいる。 with-apr, with-apr-util オプションは、APR, APR-utilがインストールされているディレクトリのパスを指定する。
      $ make
      $ sudo make install
      
    3. 2.2系の場合
    4. $ wget http://ftp.meisei-u.ac.jp/mirror/apache/dist//httpd/httpd-2.2.23.tar.gz
      $ tar zxvf httpd2.2.23.tar.gz
      $ cd httpd2.2.23
      $ ./configure \
      --prefix=/usr/local/apache2 \
      --enable-ssl \
      --enable-so \
      --enable-rewrite \
      --with-ssl=/usr \
      --with-included-apr
      
      $ make
      $ sudo make install 
      
  7. 起動確認
  8. $ sudo /usr/local/apache2/bin/apachectl start
    
    「AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message」 と表示される場合はhttpd.confを編集する。
    $ sudo vim /usr/local/apache2/conf/httpd.conf 
    # ServerName example.com:80
    ServerName localhost:80  ←サーバーのホスト名
    
    再起動する。
    $ sudo /usr/local/apache2/bin/apachectl restart
    
    何も表示されないのでプロセスを見て確認する
    $ ps aux | grep httpd
    root     14026  0.0  0.3  71340  3088 ?        Ss   11:29   0:00 /usr/local/apache2/bin/httpd -k start
    daemon   14124  0.1  0.4 415600  4536 ?        Sl   11:31   0:00 /usr/local/apache2/bin/httpd -k start
    daemon   14125  0.1  0.4 481136  5092 ?        Sl   11:31   0:00 /usr/local/apache2/bin/httpd -k start
    daemon   14126  0.1  0.4 415600  4544 ?        Sl   11:31   0:00 /usr/local/apache2/bin/httpd -k start
    root     14277  0.0  0.0 103244   800 pts/1    D+   11:33   0:00 grep httpd
    
    ブラウザからも確認する
    http://サーバーのIPアドレス/
    
  9. 自動起動の設定
    1. 一度Apacheを停止する
    2. $ sudo /usr/local/apache2/bin/apachectl stop
      
      $ cd [ソースを解凍したディレクトリ]/httpd-2.4.2
      $ sudo install -m755 bulid/rpm/httpd.init /etc/rc.d/init.d/httpd
      
      installコマンドはコピーだけではなく、アクセス権限や所有者などをファイルに設定できる。
    3. 起動スクリプトを編集する
    4. $ sudo vim /etc/rc.d/init.d/httpd
      
      以下を編集する
      httpd=${HTTPD-/usr/sbin/httpd}
      ↓
      httpd=${HTTPD-/usr/local/apache2/bin/httpd}
      
      pidfile=${PIDFILE-/var/log/httpd/${prog}.pid}
      ↓ 
      pidfile=${PIDFILE-/usr/local/apache2/logs/httpd.pid}
      
      # check for 1.3 configuration
      check13 () {
              CONFFILE=/etc/httpd/conf/httpd.conf
      ↓
      # check for 1.3 configuration
      check13 () {
              CONFFILE=/usr/local/apache2/conf/httpd.conf
      
    5. 自動起動の設定
    6. $ sudo chkconfig --add httpd
      $ sudo chkconfig httpd on
      
    7. 確認
    8. $ chkconfig --list httpd
      
      httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
      
    9. Apache起動
    10. $ sudo /etc/rc.d/init.d/httpd start 
      
      Starting httpd:                                            [  OK  ]
      

インストール及び、自動起動の設定が完了した。 最後にメモ。

  • ドキュメントルードは /usr/local/apache2/htdocs にある。
  • 設定ファイルは /usr/local/apache2/conf にある。
  • ログは /usr/local/apache2/logs にある(デフォルトはaccess_log, error_logのみ)。