1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vultr VPSのCentOS8でRailsとWordPressを共存させる

Last updated at Posted at 2021-09-25

0. はじめに

過去の記事でCentOS 8にRails 6 + MySQL 8環境を構築する手順を書いたが、RailsアプリにWordPressを共存させて、動的コンテンツと静的コンテンツを分けて、アプリ開発者に依存せずにマーケティング担当者などが静的コンテンツやBlogを更新できるようにしたので、その手順を記しておく。

このリンクで$100もらえます

スクリーンショット 2020-03-09 午後7.03.25.png

私もリファラー経由でアカウントを作ったので$100もらえました。これで最初にあれやこれや試すのは無料でできます。
スクリーンショット 2020-03-09 午後7.17.41.png

1. EPEL と REMI リポジトリを追加

$ sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
  途中省略
Installed:
  epel-release-8-8.el8.noarch                                                                                                                                                   

Complete!

$ sudo yum -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
  途中省略
Installed:
  remi-release-8.1-2.el8.remi.noarch                                                                                                                                            

Complete!

2. PHP 7.4をCentOS 8にインストール

(1) dnfユーティリティをインストール

$ sudo dnf -y install dnf-utils
$ sudo dnf module install php:remi-7.4
Last metadata expiration check: 0:00:31 ago on Sun 31 May 2020 04:17:52 AM UTC.
Dependencies resolved.
The operation would result in switching of module 'php' stream '7.2' to stream 'remi-7.4'
Error: It is not possible to switch enabled streams of a module.
It is recommended to remove all installed content from the module, and reset the module using 'dnf module reset <module_name>' command. After you reset the module, you can install the other stream.

エラーになったので、メッセージに従ってphp:remi-7.4をリセット。

$ sudo dnf module reset php:remi-7.4
Last metadata expiration check: 0:02:02 ago on Sun 31 May 2020 04:17:52 AM UTC.
Only module name is required. Ignoring unneeded information in argument: 'php:remi-7.4'
Dependencies resolved.
================================================================================================================================================================================
 Package                                   Architecture                             Version                                     Repository                                 Size
================================================================================================================================================================================
Resetting modules:
 php                                                                                                                                                                           

Transaction Summary
================================================================================================================================================================================

Is this ok [y/N]: y
Complete!

再実行

$ sudo dnf module install php:remi-7.4
Last metadata expiration check: 0:03:32 ago on Sun 31 May 2020 04:17:52 AM UTC.
Dependencies resolved.
================================================================================================================================================================================
 Package                                 Architecture                  Version                                                        Repository                           Size
================================================================================================================================================================================
Installing group/module packages:
 php-cli                                 x86_64                        7.4.6-1.el8.remi                                               remi-modular                        4.6 M
 php-common                              x86_64                        7.4.6-1.el8.remi                                               remi-modular                        1.2 M
 php-fpm                                 x86_64                        7.4.6-1.el8.remi                                               remi-modular                        1.6 M
 php-json                                x86_64                        7.4.6-1.el8.remi                                               remi-modular                         74 k
 php-mbstring                            x86_64                        7.4.6-1.el8.remi                                               remi-modular                        527 k
 php-xml                                 x86_64                        7.4.6-1.el8.remi                                               remi-modular                        215 k
Installing dependencies:
 httpd-filesystem                        noarch                        2.4.37-16.module_el8.1.0+256+ae790463                          AppStream                            35 k
 oniguruma                               x86_64                        6.8.2-1.el8                                                    AppStream                           188 k
Installing weak dependencies:
 nginx-filesystem                        noarch                        1:1.14.1-9.module_el8.0.0+184+e34fea82                         AppStream                            24 k
Installing module profiles:
 php/common                                                                                                                                                                    
Enabling module streams:
 nginx                                                                 1.14                                                                                                    
 php                                                                   remi-7.4                                                                                                

Transaction Summary
================================================================================================================================================================================
Install  9 Packages

Total download size: 8.4 M
Installed size: 46 M
Is this ok [y/N]: y

(2) インストール成功を確認したら、dnf updateで更新。時間がかかるので、しばらく待つ。

$ sudo dnf update

いくつかインストール可否の[y/N]を聞かれるのでyでインストールするが、細かな画面表示は省略。

Installed:
  kernel-4.18.0-147.8.1.el8_1.x86_64     kernel-core-4.18.0-147.8.1.el8_1.x86_64     kernel-devel-4.18.0-147.8.1.el8_1.x86_64     kernel-modules-4.18.0-147.8.1.el8_1.x86_64    

Complete!

(3) EPEL and Remiが成功したら、PHPモジュールのリストを確認。

$ sudo dnf module list php
$ sudo  dnf module list php
Last metadata expiration check: 0:01:28 ago on Sun 31 May 2020 04:43:51 AM UTC.
CentOS-8 - AppStream
Name                            Stream                                 Profiles                                                 Summary                                         
php                             7.2 [d]                                common [d], devel, minimal                               PHP scripting language                          
php                             7.3                                    common, devel, minimal                                   PHP scripting language                          

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name                            Stream                                 Profiles                                                 Summary                                         
php                             remi-7.2                               common [d], devel, minimal                               PHP scripting language                          
php                             remi-7.3                               common [d], devel, minimal                               PHP scripting language                          
php                             remi-7.4 [e]                           common [d] [i], devel, minimal                           PHP scripting language                          

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

(4) 最新のPHPストリームを使うように構成する。

$ sudo dnf module enable php:remi-7.4
Last metadata expiration check: 0:01:21 ago on Sun 31 May 2020 04:43:51 AM UTC.
Dependencies resolved.
Nothing to do.
Complete!

(5) PHPインストール

$ sudo dnf install php php-cli php-common
Last metadata expiration check: 0:01:51 ago on Sun 31 May 2020 04:43:51 AM UTC.
Package php-cli-7.4.6-1.el8.remi.x86_64 is already installed.
Package php-common-7.4.6-1.el8.remi.x86_64 is already installed.
Dependencies resolved.
================================================================================================================================================================================
 Package                                   Architecture                  Version                                                      Repository                           Size
================================================================================================================================================================================
Installing:
 php                                       x86_64                        7.4.6-1.el8.remi                                             remi-modular                        3.0 M
Installing dependencies:
 centos-logos-httpd                        noarch                        80.5-2.el8                                                   AppStream                            24 k
 httpd                                     x86_64                        2.4.37-16.module_el8.1.0+256+ae790463                        AppStream                           1.7 M
 mod_http2                                 x86_64                        1.11.3-3.module_el8.1.0+213+acce2796                         AppStream                           158 k
 libsodium                                 x86_64                        1.0.18-2.el8                                                 epel                                162 k
Installing weak dependencies:
 php-opcache                               x86_64                        7.4.6-1.el8.remi                                             remi-modular                        334 k
 php-pdo                                   x86_64                        7.4.6-1.el8.remi                                             remi-modular                        143 k
 php-sodium                                x86_64                        7.4.6-1.el8.remi                                             remi-modular                         87 k

Transaction Summary
================================================================================================================================================================================
Install  8 Packages

Total download size: 5.6 M
Installed size: 19 M
Is this ok [y/N]: y

  途中省略

Installed:
  php-7.4.6-1.el8.remi.x86_64                                      php-opcache-7.4.6-1.el8.remi.x86_64             php-pdo-7.4.6-1.el8.remi.x86_64                              
  php-sodium-7.4.6-1.el8.remi.x86_64                               centos-logos-httpd-80.5-2.el8.noarch            httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64           
  mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86_64            libsodium-1.0.18-2.el8.x86_64                  

Complete!

(6) PHPのバージョンを確認。

PHP 7.4.6がインストールされた。

$ php -v
PHP 7.4.6 (cli) (built: May 12 2020 08:09:15) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies

3. PHP拡張をインストール

WordPressに必要なPHP拡張をインストールする。

$ sudo yum install -y php-mysqlnd php-dom php-simplexml php-xml php-xmlreader php-curl php-exif php-ftp php-gd php-iconv  php-json php-mbstring php-posix php-sockets php-tokenizer

  途中省略

Installed:
  php-gd-7.4.6-1.el8.remi.x86_64                         php-mysqlnd-7.4.6-1.el8.remi.x86_64                         php-process-7.4.6-1.el8.remi.x86_64                        

Complete!

4. WordPressのインストール

(1) WordPress用のDB作成

既にMySQLは動いているのでDBだけ作成しておく。

$ mysql -u root -p

DB名はwordpressとした。
mysql> CREATE DATABASE wordpress;

WordPressのユーザーはwpuserとした。
mysql> CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'あなたのパスワード';

mysql> GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';

mysql> exit
Bye

(2) WordPressのダウンロード

$ pwd
/var/www
$ sudo curl -O https://wordpress.org/latest.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11.6M  100 11.6M    0     0   9.9M      0  0:00:01  0:00:01 --:--:--  9.9M
$ sudo tar -zxvf latest.tar.gz
$ ls -l
total 11964
drwxr-xr-x. 2 root   root       4096 Dec 23 20:47 cgi-bin
drwxr-x---. 5 deploy www        4096 May  1 01:14 Railsアプリディレクトリ
drwxr-xr-x. 2 root   root       4096 Dec 23 20:47 html
-rw-r--r--. 1 root   root   12234700 May 31 05:17 latest.tar.gz
drwxr-xr-x. 5 nobody nobody     4096 Apr 29 18:58 wordpress
$ sudo chown -R deploy.www wordpress

(4) WordPressを構成

$ cd wordpress
$ cp -p wp-config-sample.php wp-config.php
wp-config.php
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );

/** MySQL database username */
define( 'DB_USER', 'wpuser' );

/** MySQL database password */
define( 'DB_PASSWORD', 'あなたのパスワード' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

5. nginxの設定変更

さて、ここからが重要です。要するにnginxに hogehoge.com/wordpress へのアクセスはWordPressに処理させ、それ以外はRailsに処理させるようにすれば良いので、理屈としては簡単なのですが、注意しないと何も表示されなくなってしまいます。

以下のConfigは/etc/nginx/nginx.confでインクルードしています。

/etc/nginx/sites-available/あなたの任意のファイル名
  # example.com/wp will be processed by PHP
  location ^~ /wp {
    alias /var/www/wordpress;
    index index.php index.html index.htm;
    try_files $uri $uri/ /wp/index.php?$args;
 
    location ~ \.php$ {
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      include        fastcgi_params;
      fastcgi_param  SCRIPT_FILENAME $request_filename;
    }
  }

参考記事

How To Install PHP 7.4 on CentOS 8 / RHEL 8
How to Install PHP 7.4 on CentOS 8
How To Install WordPress with Nginx on CentOS 8 / RHEL 8

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?