0
2

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 5 years have passed since last update.

さくらVPSで複数のWordPressブログを運営する方法(バーチャルホストとLet’s Encryptの設定)

Posted at

この記事を書いた背景

さくらのVPSでWordPressブログを1つ運営していたが、同じサーバーでもう1つ別のWordPressブログを立ち上げたいと思ったため。

今後、さらに追加でブログを立ち上げる際のメモ書きとして、この記事を残しておきます。

環境

さくらVPSの初期設定から、1つ目のWordPressインストールまでの流れは、下記の記事を参考に設定。
[ネコでもわかる!さくらのVPS講座 〜第一回:VPSてなんだろう?〜][ネコでも]
[ネコでも]:https://knowledge.sakura.ad.jp/7938/

サーバー さくらのVPS
独自ドメイン1 example.com
独自ドメイン2 example.net
Webサーバー Apache2.4.6
OS CentOS7
データベース MySQL
SSL Let's Encrypt

1つ目のWordPressブログを、example.comという独自ドメインで運営している状態で、2つ目のWordPressブログを独自ドメインexample.netで追加する場合の手順になります。

独自ドメインは、お名前ドットコムを使っています。

さくらVPS契約からWordPressをインストールするまでの流れ

さくらのVPSを契約してから、サーバーの初期設定、WordPressインストールまでの流れは下記のとおりです。
今回は、すでにWordPressを1つインストールしている状態なので、手順8以降を設定していきます。

1.さくらのVPS契約
2.標準OSのインストール(CentOS7)
3.Apache(もしくはnginx)をインストール
4.PHPインストール
5.MySQL(もしくはMariaDB)をインストール
6.phpMyAdminをインストール(※必須ではない)
7.ファイアウォールの設定(※自動で有効になる)
//ここから追加設定
8.独自ドメインの取得(example.net)
9.ネームサーバーの設定
10.Apacheのバーチャルホスト設定
11.Let’s Encryptの設定
12.WordPress用DBの作成
13WordPress(2個目)のインストール
14.ユーザーの作成と権限の付与
15.Wp-configの設定

9.ネームサーバーの設定

ドメイン側のネームサーバーの設定と、VPS側のネームサーバー設定を行います。
お名前ドットコムのネームサーバーを利用する方法と、さくらのネームサーバーを利用する方法の2つがあります。
手順は違いますが、どちらでも同じなので好きな方で設定してください。

手順は下記が参考になります。
お名前.comで取得したドメインをさくらVPSで使う

※ネームサーバーの設定は、反映に時間がかかる(最大72時間)ので、設定が上手く行かないなと思ったら、時間を空けて再度確認してください。

10.Apacheのバーチャルホスト設定

つづいて、Apacheのバーチャルホストを設定していきます。
バーチャルホストというのは、1つの仮想専用サーバー(VPS)上で複数のドメインを運用することができるシステムです。

root権限で作業してください。
まず、バーチャルホスト用のディレクトリを作成します。
追加したドメイン(example.net)のコンテンツを置くディレクトリです。

ターミナル
# mkdir /var/www/html/example.net
# chown vpsuser /var/www/html/example.net (ディレクトリのオーナーをvpsuserに変更)

ディレクトリを作成したら、/etc/httpd/conf.dvhost.confというファイルを作成し、バーチャルホストの設定を書き込んでいきます。

ターミナル
# vi /etc/httpd/conf.d/vhost.conf
/etc/httpd/conf.d/vhost.conf
//1個目のドメインexample.comの設定

<VirtualHost *:80>
  ServerName example.com
  DirectoryIndex index.html
  AddDefaultCharset UTF-8
  DocumentRoot /var/www/html  //1個目のWordPressが入っているディレクトリを記入してください

  <Directory "/var/www/html">  //1個目のWordPressが入っているディレクトリを記入してください
    Require all granted
  </Directory>
</VirtualHost>


//2個目のドメインexample.comの設定

<VirtualHost *:80>
  ServerName example.net
  DirectoryIndex index.html
  AddDefaultCharset UTF-8
  DocumentRoot /var/www/html/example.net   //作成したディレクトリを記入してください

  <Directory "/var/www/html/example.net">   //作成したディレクトリを記入してください
    Require all granted
  </Directory>
</VirtualHost>

作成したら、Apacheを再起動して設定を反映させます。

ターミナル
# systemctl reload httpd

ちなみに、restartは全てのプロセス(WEBサービス)を一旦停止させてから、起動しなおすので、一瞬だけサイトが落ちます。
サイトを落とさず、安全に設定を読み込みなおすには、reloadコマンド、またはgracefulコマンドを使います。
参考:Apacheの設定ファイル(httpd.conf)を安全に読み込む方法

また、service httpd reloadはCentOS6以前の書き方なので、CentOS7ではsystemctl reload httpdを使います。
参考:apacheの起動,停止,再起動に関するまとめ

設定が反映されると、http://example.comhttp://example.netを入力すると、それぞれ異なるページが表示されるようになります。

11.Let’s Encryptの設定

つづいて、サイトのページを暗号化通信で安全に公開するために、常時SSLの設定をしていきます。
簡単に言うと、URLのhttphttpsにする設定です。
無料のSSL証明書発行サービスであるLet’s Encryptというサービスを利用します。

まず、さきほど作成した/etc/httpd/conf.dvhost.confにSSLの設定を追記します。

ターミナル
# vi /etc/httpd/conf.d/vhost.conf
/etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
  ServerName example.com
  DirectoryIndex index.html
  AddDefaultCharset UTF-8
  DocumentRoot /var/www/html

//httpでアクセスがあった場合に、リダイレクトさせる指示
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
  </IfModule>

  <Directory "/var/www/html">
    Require all granted
  </Directory>
</VirtualHost>

//SSLの設定を追記
<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/exapmle.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/exapmle.com/fullchain.pem
</VirtualHost>

<VirtualHost *:80>
  ServerName example.net
  DirectoryIndex index.html
  AddDefaultCharset UTF-8
  DocumentRoot /var/www/html/example.net

//httpでアクセスがあった場合に、リダイレクトさせる指示
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
  </IfModule>

  <Directory "/var/www/html/example.net">
    Require all granted
  </Directory>
</VirtualHost>

//SSLの設定を追記
<VirtualHost *:443>
    ServerName example.net
    DocumentRoot /var/www/html/example.net

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/fullchain.pem
</VirtualHost>

編集したら、Apacheを再起動して設定を反映させます。

ターミナル
# systemctl reload httpd

その後、certbotコマンドを実行して、SSL証明書をインストールします。
--webroot -wでディレクトリを指定し、-dでドメインを指定します。
certbotコマンドは、バージョンによって使えないことがあるので、適宜変更してください。(コマンドはググれば出てきます)

ターミナル
# certbot certonly --webroot -w /var/www/html -d example.com -w /var/www/html/example.net -d example.net

1個目のWordPressインストール時、すでにSSL証明書を発行している場合は、「expand?(拡張しますか?)」と聞かれるのでYesを入力してください。

完了すると
http://example.comhttps://example.com
http://example.nethttps://example.net
がそれぞれ同じページにアクセスできるようになります。

12.WordPress用DBの作成

MySQLで、追加するWordPress用のデータベースを作成します。
MySQLをインストールしていない場合は、下記のページを参考に設定してください。
MySQLの設定(CentOS 7)

MySQLにrootでログインして、データベースの作成と権限の付与を行います。

ターミナル
# mysql -u root -p
Enter password:
MySQLコンソール
mysql> create database example_net_db;
mysql> grant all privileges on examplen_net_db.* to 'example_net_user'@'localhost'; 
mysql> flush privileges;
mysql> exit

//ここでは、examplen_net_dbがデータベース名、example_net_userがユーザーネームです

MySQLからログアウトした後、ユーザーネームexample_net_userでログインし、データベースが作成されていることを確認します。

ターミナル
# mysql -u example_net_user -p
Enter password:
MySQLコンソール
mysql> show databases;
+-----------------------+
| Database              |
+-----------------------+
| examplen_net_db       |
| information_schema    |
| mysql                 |
| performance_schema    |
| sys                   |
| wordpressdb           |
+-----------------------+
6 rows in set (0.02 sec)

mysql> exit

13WordPress(2個目)のインストール

ここまできたら、いよいよ2個目のWordPressをインストールしていきます。
まず、WordPressの公式サイト(https://ja.wordpress.org/download/)で、最新のWordPressファイル(.tar.gz形式)をダウンロードします。
ローカルを経由せず、直接サーバーにダウンロードする場合は、wgetコマンドを使います。

ターミナル
# wget https://ja.wordpress.org/latest-ja.tar.gz
# tar -xzvf latest-ja.tar.gz

http://xxx.xxx.xxx.xxx/wordpressではなく、http://xxx.xxx.xxx.xxx/でWordPressにアクセスできるようにしたいので、wordpressディレクトリの下にあるすべてのファイルを、ドメインexample.netのドキュメントルート/var/www/html/example.netにmvコマンドで移動させます。

ターミナル
# mv wordpress/* /var/www/html/example.net
# rmdir wordpress

14.ユーザーの作成と権限の付与

つづいて、さきほどインストールしたWordPressファイルが、apacheユーザー、vpsuserユーザーのいずれでも更新できるようにするため、すべてのファイルの所有者をapacheにして、グループをvpsuserにします。
そしてグループに書き込み権限をつけておきます。

ターミナル
# chown -R apache:vpsuser *
# chmod -R g+w *

15.Wp-configの設定

さいごに、WordPressの基本設定を行うwp-config.phpの編集を行います。
wp-config-sample.phpというファイルがあるので、そのファイルをコピーしてwp-config.phpを作成、編集していきます。

ターミナル
# cd /var/www/html/example.net
# cp wp-config-sample.php wp-config.php
# vi wp-config.php

データベース名、ユーザー名、パスワードの欄をそれぞれ設定した値に変更します。

wp-config.php
define('DB_NAME', 'examplen_net_db');

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

/** MySQL database password */
define('DB_PASSWORD', 'password for examplen_net_user');

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

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY', '*');
define('SECURE_AUTH_KEY', '*');
define('LOGGED_IN_KEY', '*');
define('NONCE_KEY', '*');
define('AUTH_SALT', '*');
define('SECURE_AUTH_SALT', '*');
define('LOGGED_IN_SALT', '*');
define('NONCE_SALT', '*');

下から8行のAUTH_KEYなどは、Authentication Unique Keys(認証キー)で、https://api.wordpress.org/secret-key/1.1/salt/が出力する64桁のランダムな文字列を入力します。

インストールスクリプトの実行

以上でWordPressをインストールするまでの設定は終了です。
https://example.net/wp-admin/install.phpにアクセスして、WordPressのインストール画面が表示されたら、サイト名などを入力し、WordPressをインストールします。

参考にしたサイト

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?