現状・条件
- CentOS 7 の自宅サーバ環境です。
- LAMP 環境 ( 開発環境 ) は 構築済み であり、加えて LNMP 環境 ( 本番環境 ) を追加する予定です。
- 構築手順は LAMP 環境通りですが、Apache 設定を Nginx 設定に変更し、LNMP 環境を追加します。
Nginx を設定
Nginx 本家の RHEL/CentOS ガイドラインに沿って設定します。
Nginx のリポジトリをセットアップし、リポジトリから Nginx をインストールおよび更新できます。
必要なパッケージをインストールします。
[admin@centos7 ~]$ sudo yum install yum-utils
yum リポジトリをセットアップします。
Nginx 用のリポジトリファイルを下記のように作成します。
[admin@centos7 ~]$ sudo vi /etc/yum.repos.d/nginx.repo ↓ 以下をコピペします。 [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [admin@centos7 ~]$
※ デフォルトで Stable パッケージ が使用されますが、Mainline パッケージ を使用する際は、sudo yum-config-manager –enable nginx-mainline コマンドを使用できます。
Nginx をインストールします。
sudo yum install nginx コマンドでインストールします。
途中、GPG キー のインポート処理が行われますが、Fingerprint 値が 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62 と一致すれば y を入力して進めます。
…
総ダウンロード容量: 766 k
Is this ok [y/d/N]: y << ここ!
…
https://nginx.org/keys/nginx_signing.key から鍵を取得中です。
Importing GPG key 0x7BD9BF62:
Userid : “nginx signing key signing-key@nginx.com”
Fingerprint: 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62
From : https://nginx.org/keys/nginx_signing.key
上記の処理を行います。よろしいでしょうか? [y/N]y << ここ!
Running transaction check
Running transaction test
Transaction test succeeded
…
これで、Nginx のインストールは完了です。
以下はインストール情報と主なパス情報です。
- 設置られたパッケージ名:nginx.x86_64
- 実行ファイル:/sbin/nginx
- ドキュメントルート:/usr/share/nginx/html/
- 設定ファイル:/etc/nginx
[admin@centos7 ~]$ sudo which nginx /sbin/nginx [admin@centos7 ~]$ sudo yum list installed | cut -d " " -f 1 | grep nginx nginx.x86_64
Nginx を起動 & サービス自動起動を有効にします。
[root@centos7 ~]# systemctl enable nginx [root@centos7 ~]# systemctl start nginx
Nginx の Config ファイル作成は、当記事の最後に行います。
PHP を設定
WordPress に必要な PHP を使用するため設定を行います。
上記の記事通りPHP-FPM設定まで行えば良いですが、1つだけ下記の設定に変更してください。php-fpm の実行ユーザやグループを nginx 用に変更することです。
group = apache
↓↓↓
group = daemon
↓↓↓
user = nginx
group = nginx
SSL 証明書を設定
無料の SSL証明書 を取得および更新設定を行います。
MySQL 設置・設定
WordPress 用の MySQL データベースを設定します。
WordPress 用データバースとユーザを作成
MySQL の設定で デフォルトの認証プラグインを mysql_native_password に変更したか確かめてください。変更方法は こちら 。
CREATE DATABASE your-wordpress-database-name;
CREATE USER your-user-id@localhost IDENTIFIED BY ‘your-password’;
GRANT ALL ON your-wordpress-database-name.* TO your-user-id@localhost;
[root@centos7 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.16 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CREATE DATABASE your-wordpress-database-name; # << WP用 DBを作成
Query OK, 1 row affected (0.10 sec)
mysql> CREATE USER your-user-id@localhost IDENTIFIED BY ‘your-password'; # << adminユーザを作成
Query OK, 0 rows affected (0.07 sec)
mysql> GRANT ALL ON your-wordpress-database-name.* TO your-user-id@localhost; # << 作成したDBに権限を付与
Query OK, 0 rows affected (0.07 sec)
mysql> \q
Bye
最新の WordPress を設定
WordPress の 最新版 を確認できます。
WordPress の最新版を解凍し、Nginx の DocumentRoot ( ドキュメントルート ) にコピーまたは移動するだけで、インストールが完了します。
cd /usr/local/src
wget https://wordpress.org/latest.tar.gz
tar xvzf latest.tar.gz
cd wordpress/
# すべてのファイルとディレクトリを DocumentRoot ディレクトリに移動
mkdir /usr/share/nginx/html/example.com
cp -R ./* /usr/share/nginx/html/example.com
# 所有者を nginx に変更
chown -R nginx /usr/share/nginx/html/example.com
コピーが終わったら下記のように WordPress ファイルは見られます。
[root@centos7 example.com]# ls index.php wp-admin wp-content wp-load.php wp-signup.php license.txt wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php readme.html wp-comments-post.php wp-includes wp-mail.php xmlrpc.php wp-activate.php wp-config.php wp-links-opml.php wp-settings.php [root@centos7 example.com]#
WordPress 用の設定ファイルを編集
cd /usr/share/nginx/html/example.com
mv -i wp-config-sample.php wp-config.php
vi /usr/share/nginx/html/example.com/wp-config.php
# 先ほど設定した DB の情報を入力します。
define('DB_NAME', 'your-wordpress-database-name');
define('DB_USER', 'your-user-id');
define('DB_PASSWORD', 'your-password');
# 認証用ユニークキーは設定ファイル内に書かれているリンク↓↓↓をクリックしてコピペします。
# https://api.wordpress.org/secret-key/1.1/salt/
define('AUTH_KEY', 'kDSR^2:!)sdfsdfsdfRY8nB<lVpgZ/sxF[xFO;_)RF1 0I{fNZ43M');
define('SECURE_AUTH_KEY', '<^ *+~kmW&|f,XnD6da=sdfsdfsdfsWvB,1sdfsf-i_5)Ch');
define('LOGGED_IN_KEY', 'wya;Kf=Oz-UO+Xsdfsdfsdffsdfs&!PTRtpPGm]&=sdfi+[0B_TPR)Y');
define('NONCE_KEY', 'JnP+.x|(9,3n5_n:C5E#;lsdfsdfs<wd{}ia}#2_Yg+Asdfn=lse6u^L');
define('AUTH_SALT', 'A-Z`6>!v|BO&5sdfsdfsdt.W,JdK:a}J9FNosdfsdf/,T&2${UPN-S');
define('SECURE_AUTH_SALT', 'PnOojakOe/k|#9L0`:;75hsdfsdfsdfGn23ewds//sZ5}_s>C#>b%_x<sIf');
define('LOGGED_IN_SALT', 'VxuB(>As_=h322EwKRnIc)21?p|kqsdfsdfAkcRXw&VWGX61');
define('NONCE_SALT', '|O?)ZZ|</-g_m,1213asdfsdfsdJ#DLLxd3HU];C+UmNZ[ZsY{');
所有者だけが見れるように権限を変更しておきます。
[root@centos7 html]# chmod 600 /usr/share/nginx/html/example.com/wp-config.php [root@centos7 html]# ll /usr/share/nginx/html/example.com/wp-config.php -rw------- 1 nginx root 3162 Oct 1 12:07 /usr/share/nginx/html/example.com/wp-config.php
WordPress の WEB 設定
詳細は下記の記事をご参考ください。
これで、WordPress を Nginx 上で動かすことができました。
それでは、Nginx の詳細な設定を行います。
Nginx Config ファイルを設定
example_com_nginx.conf ( ファイル名は任意 ) ファイルを作成します。
主に SSL 証明書指定、HTTPS 設定、301リダイレクト設定、PHP-FPM 設定 を追加します。
[admin@centos7 ~]$ cd /etc/nginx/conf.d
[admin@centos7 conf.d]$ vi example_com_nginx.conf # ファイル名は任意
server {
listen 80;
server_name www.example.com example.com;
# 301 Redirect
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com example.com;
ssl_certificate /etc/letsencrypt/live/example.com/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
charset utf-8;
# max upload size
client_max_body_size 75M; # Size は任意
access_log /var/log/nginx/access.log main;
# favicon ファイル関連ログを記録しない。
location = favicon.ico { access_log off; log_not_found off; }
location / {
root /usr/share/nginx/html/example.com;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
root /usr/share/nginx/html/example.com;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
# Scrpits ディレクトリが存在しませんので、/scripts を $document_root に変更
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
WordPress サイトを確認
Nginx を再起動します。
[admin@centos7 conf.d]$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[admin@centos7 conf.d]$
[admin@centos7 conf.d]$ sudo systemctl restart nginx
[admin@centos7 conf.d]$
[admin@centos7 conf.d]$ sudo systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-10-15 18:13:42 JST; 17s ago
Docs: http://nginx.org/en/docs/
Process: 4712 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 4715 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 4716 (nginx)
CGroup: /system.slice/nginx.service
├─4716 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─4717 nginx: worker process
Oct 15 18:13:42 centos7 systemd[1]: Starting nginx - high performance web server...
Oct 15 18:13:42 centos7 systemd[1]: Started nginx - high performance web server.
[admin@centos7 conf.d]$
wp-admin にアクセスします。
https://example.com/wp-admin/ へアクセスし、新しい記事を作成して見ましょう!






コメント