必要なソフトウェアパッケージとダウンロードリンク
- Nginx - ウェブサーバーコンポーネント
- MySQL - データベースコンポーネント
- PHP - サーバーサイドスクリプト言語
- PCRE - Nginxの正規表現サポート用ライブラリ
- Boost - MySQL 5.7以降に必要なライブラリ
本記事では以下のバージョンを使用します:
- Boost 1.59.0
- MySQL 5.7.19
- Nginx 1.14.1
- PCRE 8.41
- PHP 7.1.24
Nginxのインストール手順
1. 必要な開発ツールとライブラリのインストール
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*
2. 専用ユーザーの作成
useradd -M -s /sbin/nologin nginx
3. ソースコードの展開とコンパイル
tar -zxf pcre-8.41.tar.gz -C /usr/local/src/
tar -zxf nginx-1.14.1.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.14.1/
./configure --prefix=/usr/local/nginx \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-pcre=/usr/local/src/pcre-8.41 \
--user=nginx --group=nginx
make -j 4 && make install
4. PHP連携のための設定
cp /usr/local/nginx/conf/nginx.conf{,.bak}
vim /usr/local/nginx/conf/nginx.conf
以下の設定を変更します:
user nginx;
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
5. 環境変数の設定
echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
source /etc/profile.d/nginx.sh
6. 起動スクリプトの作成と自動起動設定
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 2
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -3 $(cat $PIDF)
;;
restart)
$0 stop &> /dev/null
if [ $? -ne 0 ] ; then continue ; fi
$0 start
;;
reload)
kill -1 $(cat $PIDF)
;;
*)
echo "Usage: $0 { start | stop | restart | reload }"
exit 1
esac
exit 0
chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
7. ファイアウォール設定
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload
MySQLのインストール手順
1. 必要な依存パッケージのインストール
yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel
2. 古いバージョンの削除
yum -y remove mysql mariadb* boost*
3. 専用ユーザーの作成
useradd -M -s /sbin/nologin -r mysql
4. ソースコードの展開とコンパイル
tar zxf boost_1_59_0.tar.gz -C /usr/local/src/
tar zxf mysql-5.7.19.tar.gz -C /usr/local/src/
mkdir -p /var/lib/mysql/data
chown -R mysql.mysql /var/lib/mysql/
cd /usr/local/src/mysql-5.7.19/
cmake -DCMAKE_INSTALL_PREFIX=/var/lib/mysql \
-DMYSQL_DATADIR=/var/lib/mysql/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 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/src/boost_1_59_0
make -j 4 && make install
5. 設定ファイルの編集
vim /etc/my.cnf
[mysqld]
basedir=/var/lib/mysql
datadir=/var/lib/mysql/data
port=3306
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld.pid
6. 環境変数の設定
echo 'export PATH=/var/lib/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
7. 起動スクリプトの作成と自動起動設定
cp /var/lib/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
8. 初期化と起動
/var/lib/mysql/bin/mysqld --initialize-insecure --user=mysql \
--basedir=/var/lib/mysql --datadir=/var/lib/mysql/data
/etc/init.d/mysqld start
PHPのインストール手順
1. 必要な依存パッケージのインストール
yum -y install php-mcrypt libmcrypt libmcrypt-devel php-pear libxml2 libxml2-devel \
curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel
2. ソースコードの展開とコンパイル
tar zxf php-7.1.24.tar.gz -C /usr/local/src/
cd /usr/local/src/php-7.1.24/
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/ --enable-fpm \
--with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-iconv-dir --with-freetype-dir \
--with-jpeg-dir --with-png-dir --with-zlib \
--with-libxml-dir=/usr --enable-xml --disable-rpath \
--enable-bcmath --enable-shmop --enable-sysvsem \
--enable-inline-optimization --with-curl \
--enable-mbregex --enable-mbstring --with-mcrypt \
--enable-ftp --with-gd --enable-gd-native-ttf \
--with-openssl --with-mhash --enable-pcntl \
--enable-sockets --with-xmlrpc --enable-zip \
--enable-soap --without-pear --with-gettext \
--disable-fileinfo --enable-maintainer-zts
make -j 4 && make install
3. 設定ファイルの生成
cp php.ini-production /usr/local/php/php.ini
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
4. 実行ユーザーの変更
vim /usr/local/php/etc/php-fpm.conf
user = nginx
group = nginx
5. 起動スクリプトの作成と自動起動設定
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
6. サービスの起動
systemctl start php-fpm
ss -antup | grep php-fpm
7. Nginxの再設定と再起動
vim /usr/local/nginx/conf/nginx.conf
# SCRIPT_FILENAMEのパスを修正
nginx -s reload
8. 動作確認
vim /usr/local/nginx/html/index.php
<?php phpinfo(); ?>