ソースコードからLANMP(Linux, Apache, Nginx, MySQL, PHP)スタックを手動でコンパイルしてインストールする手順を解説します。このガイドは、CentOS 6.5 64ビット環境を対象としています。
1. 前提条件と依存関係のインストール
まず、コンパイルに必要な開発ツールやライブラリをインストールします。以下のコマンドを実行してください。
yum -y install gcc gcc-c++ autoconf nss_ldap libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel openldap-clients openldap-servers pcre pcre-devel make wget vim cmake gd gd-devel libevent libevent-devel zip unzip libtool
次に、SELinuxとファイアウォールを無効にします。SELinuxの設定を変更し、ファイアウォールサービスを停止・無効化します。
service iptables stop
chkconfig iptables off
vi /etc/selinux/config
# SELINUX=enforcing を SELINUX=disabled に変更
# 設定を反映させるために再起動
reboot
2. Apacheのインストール
ソースコードを展開するディレクトリに移動し、必要な依存関係(APR, APR-Util)を先にインストールします。
cd /usr/local/src
# APRのインストール
tar -zxvf apr-1.5.1.tar.gz
cd apr-1.5.1
./configure --prefix=/opt/web_server/apr
make && make install
cd ..
# APR-Utilのインストール
tar -zxvf apr-util-1.5.4.tar.gz
cd apr-util-1.5.4
./configure --prefix=/opt/web_server/apr-util --with-apr=/opt/web_server/apr
make && make install
cd ..
# Apache HTTP Serverのインストール
tar -zxvf httpd-2.4.10.tar.gz
cd httpd-2.4.10
./configure --prefix=/opt/web_server/apache --sysconfdir=/etc/web_server --enable-so --enable-rewrite --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-most-shared=most --enable-mpms-shared=all --with-apr=/opt/web_server/apr --with-apr-util=/opt/web_server/apr-util
make && make install
cd ..
Apacheを起動し、動作を確認します。
/opt/web_server/apache/bin/apachectl start
起動時に警告メッセージが表示される場合、設定ファイルを編集します。
vi /etc/web_server/httpd.conf
# ServerNameとServerAdminを設定
ServerName localhost:80
ServerAdmin admin@localhost
# PIDファイルのパスを指定
PidFile "/var/run/web_server.pid"
# 変更を保存してApacheを再起動
/opt/web_server/apache/bin/apachectl restart
3. MySQLのインストール
MySQLのソースコードを展開し、コンパイルオプションを指定してインストールします。
groupadd -r -g 306 db_group
useradd -g 306 -r -u 306 db_user
tar -zxvf mysql-5.6.21-x86_64.tar.gz
cd mysql-5.6.21-x86_64
cmake \
-DCMAKE_INSTALL_PREFIX=/opt/db_server/mysql \
-DMYSQL_DATADIR=/opt/db_server/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
make && make install
cd ..
データディレクトリの権限を設定し、初期化スクリプトを実行します。
chown -R db_user:db_group /opt/db_server/mysql/*
mkdir -p /opt/db_server/data
chown -R db_user:db_group /opt/db_server/data
/opt/db_server/mysql/scripts/mysql_install_db --basedir=/opt/db_server/mysql --datadir=/opt/db_server/data --user=db_user
MySQLをシステムサービスとして設定し、起動します。
cp /opt/db_server/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
4. PHPのインストール
PHPをコンパイルする前に、必要な依存ライブラリをインストールします。
cd /usr/local/src
# libmcryptのインストール
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
cd ..
# mhashのインストール
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make && make install
cd ..
# mcryptのインストール
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
./configure
make && make install
cd ..
# libiconvのインストール
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/opt/php_deps/libiconv
make && make install
cd ..
PHPをApacheモジュールとしてコンパイルします。
tar zxvf php-5.6.x.tar.gz
cd php-5.6.x
./configure --prefix=/opt/php_engine --with-apxs2=/opt/web_server/apache/bin/apxs --with-mysql=/opt/db_server/mysql/ --with-openssl --with-zlib --with-freetype-dir --with-gd --with-jpeg-dir --with-png-dir --with-iconv=/opt/php_deps/libiconv --enable-short-tags --enable-sockets --enable-zend-multibyte --enable-soap --enable-mbstring --enable-static --enable-gd-native-ttf --with-curl --with-mcrypt
make && make install
cd ..
Apacheの設定ファイルにPHPを有効にするための記述を追加します。
vi /etc/web_server/httpd.conf
# 以下の行を追加
AddType application/x-httpd-php .php .php3 .htm .phtml .php4
AddType application/x-httpd-php-source .phps
# DirectoryIndexにindex.phpを追加
DirectoryIndex index.php index.html
# Apacheを再起動
/opt/web_server/apache/bin/apachectl restart
5. Nginxのインストール
Nginxをコンパイルしてインストールします。Nginxは静的ファイルの処理とリバースプロキシとして機能します。
groupadd -r -g 306 web_proxy
useradd -g 306 -r -u 306 web_proxy -s /sbin/nologin
tar -zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure \
--prefix=/opt/web_gateway \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/web_gateway/nginx.conf \
--error-log-path=/var/log/web_gateway/error.log \
--http-log-path=/var/log/web_gateway/access.log \
--pid-path=/var/run/web_gateway/nginx.pid \
--lock-path=/var/lock/web_gateway.lock \
--user=web_proxy \
--group=web_proxy \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/web_gateway/client/ \
--http-proxy-temp-path=/var/tmp/web_gateway/proxy/ \
--http-fastcgi-temp-path=/var/tmp/web_gateway/fcgi/
make && make install
cd ..
Nginxをシステムサービスとして設定し、起動します。
cp /opt/web_gateway/nginx/sbin/nginx /etc/init.d/web_gateway
chmod +x /etc/init.d/web_gateway
chkconfig --add web_gateway
chkconfig web_gateway on
service web_gateway start
6. LANMPの統合:Nginxをリバースプロキシとして設定
NginxをApacheのフロントエンドとして設定し、静的ファイルはNginxが処理し、PHPリクエストはApacheに転送します。
まず、Apacheのリスニングポートを8080に変更します。
vi /etc/web_server/httpd.conf
# Listen 80 を Listen 8080 に変更
# Apacheを再起動
/opt/web_server/apache/bin/apachectl restart
次に、Nginxの設定ファイルを編集します。
vi /etc/web_gateway/nginx.conf
# userとworker_processesを設定
user web_proxy web_proxy;
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# ログの設定
access_log /var/log/web_gateway/access.log main;
# サーバー設定
server {
listen 80;
server_name localhost;
# 静的ファイルの処理
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
root /var/www/html;
expires 1d;
}
# PHPリクエストをApacheにプロキシ
location ~ \.php$ {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
Nginxを再起動して設定を反映させます。
service web_gateway restart
これで、LANMPスタックのインストールと基本的な設定が完了しました。Webサーバーはポート80でリッスンし、静的ファイルはNginxが、動的コンテンツ(PHP)はApacheが処理するようになります。