LAMP構築-Linux(CentOS)へApacheをソースコードからインストール

CentOS Linux
この記事は約10分で読めます。

※当ブログではアフィリエイト広告を利用しています。

LAMP」とは、Webアプリケーションのオープンソースソフトウェアの組み合わせを指す。

組み合わせは、以下のよう。

  • OS…「L」(Linux)
  • Webサーバ…「A」(Apache HTTP Server)
  • DBサーバ…「M」(MySQL)
  • 言語…「P」(PHP/Perl/Python)

個人的には、「MySQL」ではなく「PostgreSQL」、「PHP」ではなく「Ruby」が好きなんだが。

これだと「LAPR」となり、なんとも語呂が悪い。

まあそんなことは置いといて、LAMP環境を構築してみる。

今回は、LAMP環境のWebサーバ「A」。

Linux(CentOS)へ「Apache HTTP Server」をソースコードからインストールします。

また、Apache HTTP Serverは、「APR(Apache Portable Runtime Library)」、「APR-Util(Apache Portable Runtime Utility Library)」が必要なので、事前にインストールします。

ちなみに、LAMP環境のOS「L」のインストールについては、こちらの記事参照。

なお、今回のバージョンは以下です。

  • CentOS-6.4-i386
  • Apache HTTP Server 2.4.6
  • Apache Portable Runtime Library 1.4.8
  • Apache Portable Runtime Utility Library 1.5.2

Apache Portable Runtime Libraryのインストール

こちらをインストールしていない場合、Apache HTTP Serverの「configure」を実行し、Makefile生成時に「APR not found.」とエラーになる。

ダウンロードURLは、Apache Portable Runtimeの公式サイトよりミラーサイトのURLをコピーしておく。

#「Apache Portable Runtime Library」のダウンロード
$ wget http://ftp.yz.yamagata-u.ac.jp/pub/network/apache//apr/apr-1.4.8.tar.gz

#アーカイブを展開
$ tar xvzf apr-1.4.8.tar.gz

#ディレクトリ移動
$ cd apr-1.4.8

#「configure」を実行し、Makefile生成
$ ./configure

#makeコマンドでコンパイル
$ make

#makeコマンドでインストール
$ make install

Apache Portable Runtime Utility Libraryのインストール

こちらをインストールしていない場合、Apache HTTP Serverの「configure」を実行し、Makefile生成時に「APR-util not found.」とエラーになる。

ダウンロードURLは、Apache Portable Runtimeの公式サイトよりミラーサイトのURLをコピーしておく。

なお、インストール時には先ほどインストールした「Apache Portable Runtime Library」を指定してやる必要があるみたい。

指定の方法は、ヘルプにあったように「–with-apr=/usr/local/apr/bin/apr-1-config」とした。

#「Apache Portable Runtime Utility Library」のダウンロード
$ wget http://ftp.yz.yamagata-u.ac.jp/pub/network/apache//apr/apr-util-1.5.2.tar.gz

#アーカイブを展開
$ tar xvzf apr-util-1.5.2.tar.gz

#ディレクトリ移動
$ cd apr-util-1.5.2

#「configure」を実行し、Makefile生成
$ ./configure
checking for APR... no
configure: error: APR could not be located. Please use the --with-apr option.

#「configure」のヘルプを表示
$ ./configure --help
Optional Packages:
  --with-apr=PATH         prefix for installed APR or the full path to
                             apr-config

#再度「configure」を実行し、Makefile生成
$ ./configure --with-apr=/usr/local/apr/bin/apr-1-config

#makeコマンドでコンパイル
$ make

#makeコマンドでインストール
$ make install

Apache HTTP Serverのインストール

ようやく「Apache HTTP Server」のインストールへ。

ダウンロードURLは、Apache HTTP Serverの公式サイトよりミラーサイトのURLをコピーしておく。

なお、インストールオプションは「–enable-mods-shared=all」をつけた。

このオプションで、Apacheのほとんど/全てのモジュールが動的にインストールされる。

#「Apache HTTP Server」のダウンロード
$ wget http://ftp.jaist.ac.jp/pub/apache//httpd/httpd-2.4.6.tar.gz

#アーカイブを展開
$ tar xvzf httpd-2.4.6.tar.gz

#ディレクトリ移動
$ cd httpd-2.4.6

#「configure」を実行し、Makefile生成
$ ./configure --enable-mods-shared=all
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

#yumコマンドで「PCRE」を検索
$ yum search PCRE
pcre-devel.i686 : Development files for pcre
pcre-static.i686 : Static library for pcre
pcre.i686 : Perl-compatible regular expression library

#yumコマンドで「pcre」をインストール
$ yum install pcre

#yumコマンドで「pcre-devel」をインストール
$ yum install pcre-devel

#再度「configure」を実行し、Makefile生成
$ ./configure --enable-mods-shared=all

#makeコマンドでコンパイル
$ make

#makeコマンドでインストール
$ make install

Apache HTTP Serverの設定

Apacheの実行ユーザーを作成する。

#ユーザー作成
$ useradd -d /tmp -s /sbin/nologin apache

#パスワード変更
$ passwd apache

Apacheのインストールディレクトリは「/usr/local/apache2/」のよう。

まずは、ディレクトリごと所有者とグループを作成したApacheユーザーへ変更する。

#所有者変更
$ chown -R apache /usr/local/apache2/

#グループ変更
$ chgrp -R apache /usr/local/apache2/

続いて、設定ファイルは「/usr/local/apache2/conf/httpd.conf」なので編集する。

#「httpd.conf」の編集
$ vi /usr/local/apache2/conf/httpd.conf

設定を変更したのは、ユーザー・グループとサーバー名。

以下のように編集する。

<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User apache
Group apache

</IfModule>

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName myserver:80

編集終了後は、以下のコマンドで文法チェックを行う。

#「httpd.conf」の文法チェック
$ /usr/local/apache2/bin/apachectl configtest
Syntax OK

Apache HTTP Serverの起動

以下のコマンドで、Apacheを起動させる。

起動時に「ServeName」が指定されていないと警告メッセージが出る。

上記「Apache HTTP Serverの設定」のように、「httpd.conf」ファイルの「ServerName」に自ホスト名を指定し、「/etc/hosts」ファイルに自身を示す「127.0.0.1」と自ホスト名を紐付ける。

そうすることで、警告メッセージが出なくなった。

#Apache HTTP Serverの起動
$ /usr/local/apache2/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

#「/etc/hosts」の編集
$ vi /etc/hosts
127.0.0.1 myserver

#Apache HTTP Serverの再起動
$ /usr/local/apache2/bin/apachectl restart

iptablesで80番ポートを開放

最後に、iptablesで80番ポートを開放してやる。

設定の方法は、「SSH」の記載をコピーした。

#iptablesの設定を編集
$ vi /etc/sysconfig/iptables

# SSH
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
# Apache HTTP Server
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

#iptablesの再起動
$ /etc/rc.d/init.d/iptables restart

以上の設定で、無事に「http://CentOSのIPアドレス/」で「It works!」という画面が表示されました。

参考サイト

CentOS5.5/Apache/homeディレクトリ解放 – オヤジのサーバ構築に挑戦! Wiki*

【apache】httpd.confの文法チェックを行う

Linux
\よかったらシェアしてね!/
えふめん

大阪在住、30代。
業務系SE・社内SE。

PCトラブルの調査、自作デスクトップPCのこと、PC周辺機器のレビューなどの記事を書いています。

えふめんをフォローする
この記事が気に入ったら
いいね!しよう
最新情報をお届けします。
俺の開発研究所

コメント

タイトルとURLをコピーしました