LNMPアーキテクチャの概要
LNMPはLinux、Nginx、MySQL、PHPからなるWebアプリケーションスタックであり、動的コンテンツを提供するための代表的なオープンソース技術群です。本ガイドでは、単一サーバー上にこの環境を構築し、基本的な運用までを実施します。
Nginxのソースからのインストール
Nginxはリバースプロキシやロードバランシングにも対応した高速なWebサーバーです。以下の手順でカスタムビルドを行います。
1. 必要な開発ツールとライブラリのインストール
yum -y install gcc gcc-c++ make pcre-devel zlib-devel openssl-devel
2. 専用ユーザーアカウントの作成
セキュリティ向上のため、nobodyではなく専用ユーザーでサービスを実行します。
useradd -M -s /sbin/nologin www-data
3. ソースコードの展開とコンパイル設定
cd /opt
tar zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure \
--prefix=/usr/local/nginx \
--user=www-data \
--group=www-data \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module
make && make install
4. 実行パスの簡略化
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
5. 設定ファイルの構文チェック
nginx -t
6. systemdによるサービス登録
システム起動時の自動起動を可能にするためにユニットファイルを作成します。
cat > /etc/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx Web Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
権限設定と自動起動登録:
chmod 644 /etc/systemd/system/nginx.service
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
MySQL 5.7のインストール
データベースエンジンとしてMySQL Community Editionを導入します。
1. 依存パッケージの準備
yum -y install ncurses-devel bison cmake
2. MySQL用ユーザーの作成
useradd -M -s /sbin/nologin mysql
3. CMakeを使ったビルド設定
cd /opt/mysql-5.7.20
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_BOOST=boost \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_SYSTEMD=1
make && make install
4. データディレクトリの所有者変更
chown -R mysql:mysql /usr/local/mysql
5. 設定ファイルの作成(/etc/my.cnf)
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock
[mysqld]
port = 3306
socket = /usr/local/mysql/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
log-error = /usr/local/mysql/data/error.log
pid-file = /usr/local/mysql/mysql.pid
character-set-server = utf8mb4
default-storage-engine = InnoDB
6. 環境変数の追加
echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
source /etc/profile
7. 初期化とサービス登録
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable mysqld
systemctl start mysqld
8. rootパスワードの設定
mysqladmin -u root password 'SecurePass123!'
PHP-FPMの構築
PHPをFastCGI方式で処理するためにPHP-FPMを使用します。
1. 拡張ライブラリのインストール
yum -y install libjpeg-devel libpng-devel freetype-devel libxml2-devel curl-devel openssl-devel
2. PHPのコンパイル
cd /opt/php-7.1.10
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--enable-mbstring \
--enable-sockets \
--enable-opcache
make && make install
3. 設定ファイルの配置
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
4. PHPタイムゾーンの設定
/usr/local/php/etc/php.ini内に以下を追加:
date.timezone = Asia/Tokyo
mysqli.default_socket = /usr/local/mysql/mysql.sock
5. PHP-FPMの起動
/usr/local/php/sbin/php-fpm
netstat -an | grep 9000
NginxとPHPの連携設定
NginxがPHPリクエストをPHP-FPMに転送できるように設定します。
server {
listen 80;
server_name localhost;
root /usr/local/nginx/html;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
動作確認スクリプト
<?php
phpinfo();
?>
設定反映:
nginx -t && systemctl restart nginx
データベース接続テスト
MySQLとの接続をPHPから確認します。
<?php
$host = 'localhost';
$user = 'root';
$pass = 'SecurePass123!';
$db = 'test';
$conn = new mysqli($host, $user, $pass);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "<h1>Database Connected Successfully</h1>";
?>
Discuz! フォーラムのデプロイ
オープンソースのコミュニティソフトウェアを導入して最終確認を行います。
unzip Discuz_X3.4_SC_UTF8.zip
cp -r upload /usr/local/nginx/html/forum
cd /usr/local/nginx/html/forum
chown -R www-data:www-data config data uc_client uc_server
chmod -R 777 config data uc_client uc_server
systemctl restart nginx
ブラウザから http://your-server-ip/forum/install/ にアクセスしてインストールを完了してください。