情報科学屋さんを目指す人のメモ

方法・手順・解説を書き残すブログ。私と同じことを繰り返さずに済むように。

WordPress引越手順メモ(さくらのVPS+Ubuntu 14.04)

Linux (29) Ubuntu (27) さくらのVPS (7)

さくらのVPSで運用中のWordPressを、別のさくらのVPSへ、移動しました。前回のさくらのレンタルサーバからさくらのVPS 4Gへの引っ越しから、およそ2年8ヶ月後の移転です。作業内容を、ここにメモしておきます。

移転内容

移転前は、さくらのVPS 4G(CentOS)上に、リバースプロキシとしてのNginxがあり、その裏に、WordPressが稼働しているApacheも共存している構成でした。MySQLサーバーも同居しています。

そこから、WordPress本体に相当する、ApacheとMySQLサーバを、新しく契約したさくらのVPS 16G(Ubuntu 14.04)へ移動しました。Nginxは残しているため、IPアドレスに変更はありませんが、今後構成は変更するかも知れません。

移転理由

一番大きな移転の理由は、CentOS をやめて Ubuntu にしたかったからです。

その他いろいろ理由はあるのですが、きっかけはそれです。普段扱うLinuxを、Ubuntuに統一したかった。

あと特別付け加えるなら、ちょっと色々やりたいと思った、とかですかね。

さくらのVPS契約後のUbuntuカスタムインストール

というわけで、早速VPSを契約した私は、Ubuntuのインストール作業に取りかかりました。

さくらのVPSは、デフォルトでOSがインストール済みです。そのOSは、必ずCentOSです。

なので、その他のOSを利用したい場合は、自分でインストールする必要があります。

インストール作業

https://secure.sakura.ad.jp/vps/ からログインして、VPSを選択肢、「OSインストール>カスタムOSインストール」と進み、「インストールOS」に「Ubuntu 14.04 amd64」を選択して、「設定内容を確認する」をクリック。

確認画面になるので、「インストールを実行する」をクリックします。

カスタムインストールを開始しました。VNCコンソールを起動してインストール作業を行ってください。

と表示されたら、「VNCコンソール(HTML5版)を起動」をクリックします。

Ubuntu インストール画面

Ubuntuのインストール画面が開いたので、「Install」を選択したままの状態でEnterを押します。

キーボード選択で「Japanese>Japanese」と進みます。

次に、IPアドレス設定が表示されるので、VPSコントロールパネルに表示されている通りに、IPv4のアドレスを入力します。

ネットマスク、ゲートウェイ、ネームサーバー(プライマリDNS)の設定が続きます。

これを抜けると、しばらく待ちになります(Loading additional components)。

次に、ユーザーの名前の設定(Full name for the new user)と、ユーザー名(Username for your account)、そのユーザー向けのパスワード(Choose a password for the new user)、その確認入力、と続きます。

Encrypt your home directory? と表示されますが、今回は「No」を選択しました。

またここで少々待ちが入ります。

「Unmount partitions that are in use?」と表示されたら「Yes」を選択します。

ディスクのパーティション設定

すると、「Partition disks」が表示されます。

表示される「Partitioning method」は、以下の通りです。

  • Guided - resize Virtual disk 1, partition #3 (vda3) and use freed space
  • Guided - use entire disk
  • Guided -use the largest continuous free space
  • Guided - use entire disk and set up LVM
  • Guided - use entire disk and set up encrypted LVM
  • Manual

今回は、「Guided - use entire disk」を選択しました。

最初に「Select disk to partition」が表示されたので、「Virtual disk 1 (vda) - 429.5 GB Virtio Block Device」を選択しました。

次に、以下の説明文が表示されます。

[!!] Partition disks

If you continue, the changes listed below will be written to the disks. Otherwise, you will be able to make further changes manually.

WARNING: This will destroy all data on any partitions you have removed as well as on the partitions that are going to be formatted.

The partition tables of the following devices are changed:
Virtual disk 1 (vda)

The following partitions are going to be formatted:
partition #1 of Virtual disk 1 (vda) as ext4
partition #5 of Virtual disk 1 (vda) as swap

Write the changes to disks?

これに対して、「Yes」を選択します。

すると、「Installing the base system」が始まります。これは、かなり時間がかかります。

「Finish the installation」が表示されるとインストール終了です。「Continue」をクリックします。

ここで一度終了するため、「Server disconnected (code: 1000, reason: Target closed)」となるので、さくらのVPSの画面に戻り、「起動」をクリックし、確認画面で「起動する」をクリックします。

そして、コンソールを開き直します。

再ログイン

黒い画面に白い文字で

Ubuntu 14.04.3 LTS tk2-... tty1

tk2-***-***** login:

と表示されるので、アカウント名とパスワードを入力します。

Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-71-generic x86_64)

* Documentation: https://help.ubuntu.com/

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the indivisdual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.

did2@tk2-***-*****:~$ _

これで、Ubuntuのインストール作業からの初回ログインが完了です。

SSHログイン設定

ここまでできたら、既にSSHログインが22版ポートに対して可能になっているので、MobaXtermを使って、ログインしました。

これでしばらくは、もうさくらのVPSコンソールとはおさらばです

/usr/bin/xauth: file /home/did2/.Xauthority does not exist

このとき、と出ますが気にしません。

インストール準備

とりあえず、「$ sudo apt-get update」をしました。

zsh

そこから、「$ zsh --version」してみたところ、「The program 'zsh' is currently not installed. You can install it by typing: sudo apt-get install zsh」だったので、「$ sudo apt-get install zsh」で、まずzshをインストールしました。

次に、「chsh -s $(which zsh)」で、zshをデフォルトシェルにしました(which zsh → /usr/bin/zsh、/etc/shellsに/bin/zsh, /usr/bin/zsh あり)。

そうしたら、ここで一度「$ exit」して、再びSSHでログインし直します

すると、いつもの↓が表示されます。

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

(2)  Populate your ~/.zshrc with the configuration recommended
     by the system administrator and exit (you will need to edit
     the file by hand, if so desired).

--- Type one of the keys in parentheses ---

ここはひとまず、「0」を入力して、空の~/.zshrcを生成させました。

一応、「$ echo $SHELL」で確認すると、ちゃんと「/usr/bin/zsh」でした。

Oh My Zsh

次に、「sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"」を実行して、Oh My Zshをインストールしようとしました。

しかし、「Error: git is not installed」と表示されてしまったので、「$ sudo apt-get install git」を実行しました(「git git-man liberror-perl patch」がインストールされた)。

そして、もう一度先ほどのコマンドを実行し直し、Oh My Zshのインストールは完了です。

Looking for an existing zsh config...
Found ~/.zshrc. Backing up to ~/.zshrc.pre-oh-my-zsh
Using the Oh My Zsh template file and adding it to ~/.zshrc
Copying your current PATH and adding it to the end of ~/.zshrc for you.

次に、「$ wget https://gist.githubusercontent.com/xfanwu/18fd7c24360c68bab884/raw/f09340ac2b0ca790b6059695de0873da8ca0c5e5/xxf.zsh-theme -P ~/.oh-my-zsh/themes」で、いつも使っている「xxf」というテーマをダウンロードしました(wgetで保存先ディレクトリを指定するには-Pオプション)。

そして、「$ vi ~/.zshrc」で、「ZSH_THEME="robbyrussell"」を、「ZSH_THEME="xxf"」にして、またexit後に再ログインしました。

vimの(再)インストール

次に、始めからインストールされているvimでは困るので、「$ sudo apt-get install vim」を実行して、vimを改めてインストールします。

byobu (tmux)

続いて、「$ sudo apt-get install byobu」を実行して、byobuをインストールしました(byobu gawk libevent-2.0-5 libsigsegv2 python3-newt run-one screen tmux)。

htop

「$ sudo apt-get install htop」を実行して、htopをインストールしました。

/proc/cpuinfo の確認

さくらのVPS 16G 上で、/proc/cpuinfo を確認しました。

cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 62
model name      : Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHz
stepping        : 4
microcode       : 0x1
cpu MHz         : 2599.998
cache size      : 20480 KB
cpu family      : 6
model           : 62
model name      : Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHz
stepping        : 4
microcode       : 0x1
cpu MHz         : 2599.998
cache size      : 20480 KB
physical id     : 5
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 5
initial apicid  : 5
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl eagerfpu pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm xsaveopt vnmi ept fsgsbase tsc_adjust smep erms
bogomips        : 5199.99
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 6
vendor_id       : GenuineIntel
cpu family      : 6
model           : 62
model name      : Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHz
stepping        : 4
microcode       : 0x1
cpu MHz         : 2599.998
cache size      : 20480 KB
physical id     : 6
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 6
initial apicid  : 6
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl eagerfpu pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm xsaveopt vnmi ept fsgsbase tsc_adjust smep erms
bogomips        : 5199.99
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 7
vendor_id       : GenuineIntel
cpu family      : 6
model           : 62
model name      : Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHz
stepping        : 4
microcode       : 0x1
cpu MHz         : 2599.998
cache size      : 20480 KB
physical id     : 7
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 7
initial apicid  : 7
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl eagerfpu pni pclmulqdq vmx ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm xsaveopt vnmi ept fsgsbase tsc_adjust smep erms
bogomips        : 5199.99
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

ssh用公開鍵

「$ mkdir ~/.ssh」「$ chmod 700 ~/.ssh」としたあと、~/.sshディレクトリに、秘密鍵ファイル(id_rsa.pub)をWinSCPを使用してコピーしました。秘密鍵の名前は、「authorized_keys」にしておきます(名前を間違えていて、「Server refused our key」になっていました)。

そして、「$ sudo vim /etc/ssh/sshd_config」で、「Port 22」を書き換えて、ポート番号を22以外にします。そして「PermitRootLogin without-password」にして「PermitRootLogin no」と書き換えます。

そうしたら、「$ sudo service ssh restart」でsshdを再起動します。

そして、公開鍵認証でログインできるかどうかを確認します(一応)。

MobaXterm的には、「Authenticating with public key "imported-openssh-key"」が表示されて、成功です。

もう一度「$ sudo vim /etc/ssh/sshd_config」をして、コメントアウトされている「#PasswordAuthentication yes」を「PasswordAuthentication no」に変更して、「$ sudo service ssh restart」しました。

※そのうち、sshからmoshへ移行する

バックアップファイルのダウンロード

Dropboxにバックアップされている、WordPressのデータファイルをダウンロードするために、Dropboxの共有機能から、ダウンロードURLを取得して、「$ wget https://www.dropbox.com/s/...」を実行し、ダウンロードを開始しておきます。

時間がかかるので、早めに始めておきます。

Apache・MySQLサーバのインストール

$ sudo apt-get update
$ sudo apt-get install apache2
...
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3
  libaprutil1-ldap ssl-cert
...
$ sudo apt-get install mysql-server
...
The following NEW packages will be installed:
  libaio1 libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  libterm-readkey-perl mysql-client-5.5 mysql-client-core-5.5 mysql-common
  mysql-server mysql-server-5.5 mysql-server-core-5.5
...

While not mandatory, it is highly recommended that you set a password for the MySQL administrative "root" user.
If this field is left blank, the password will not be changed.
New password for the MySQL "root" user:

が表示されたら、mysql の root 用のパスワードを設定します。

apt-get install wordpress を試しに実行

今回、wordpressパッケージはインストールせず、直接持ってくるのですが、一度「$ sudo apt-get install wordpress」を試しに実行してみて、どんなパッケージがインストールされるのかを調べておきます。

$ sudo apt-get install wordpress
...
The following extra packages will be installed:
  fontconfig-config fonts-dejavu-core javascript-common libapache2-mod-php5
  libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libjs-cropper
  libjs-prototype libjs-scriptaculous libphp-phpmailer libphp-snoopy libtiff5
  libvpx1 libxpm4 php5-cli php5-common php5-gd php5-json php5-mysql
  php5-readline tinymce wordpress-l10n wordpress-theme-twentyfourteen
  wordpress-theme-twentytwelve
Suggested packages:
  php-pear libgd-tools mail-transport-agent curl php5-user-cache
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core javascript-common libapache2-mod-php5
  libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libjs-cropper
  libjs-prototype libjs-scriptaculous libphp-phpmailer libphp-snoopy libtiff5
  libvpx1 libxpm4 php5-cli php5-common php5-gd php5-json php5-mysql
  php5-readline tinymce wordpress wordpress-l10n
  wordpress-theme-twentyfourteen wordpress-theme-twentytwelve
...

PHPをインストール

続いて、PHPをインストールします。

$ sudo apt-get install php5 libapache2-mod-php5 php5-cli php5-mysql
...
The following extra packages will be installed:
  php5-common php5-json php5-readline
Suggested packages:
  php-pear php5-user-cache
The following NEW packages will be installed:
  libapache2-mod-php5 php5 php5-cli php5-common php5-json php5-mysql
  php5-readline
...

ファイルの展開

$ sudo apt-get install unzip
$ mkdir wp
$ unzip backup.zip -d wp

このあたりは、ファイルの移動方法次第です。

Nginxのインストール

$ sudo apt-get install nginx
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
  libjpeg-turbo8 libjpeg8 libtiff5 libvpx1 libxpm4 libxslt1.1 nginx
  nginx-common nginx-core

当面使う予定はないのですが、次のユーザー作成と関連して(結局、nginxユーザーは作られませんでしたが)。

ユーザーの追加

既存環境に合わせて、ユーザーやグループを作成します。

今までのApacheサーバーが、nginxユーザーとwwwグループで動作していたので、存在しないこれらのユーザーを追加します。

$ sudo groupadd www
$ sudo useradd nginx -G www
#$ sudo usermod -G www did2 # ダメ
$ sudo gpasswd -a did2 www

「$ id」を実行するとわかるように、これでは反映されていないので、ログインログアウトをします。

WordPress本体の配置

$ sudo mkdir /var/www/did2memo.net
$ sudo chown nginx:www /var/www/did2memo.net
$ sudo mv wp /var/www/did2memo.net
$ sudo chown -R nginx:www /var/www/did2memo.net
$ sudo chmod -R 770 /var/www/did2memo.net
$ sudo chmod -R g+s /var/www/did2memo.net

MySQLの設定

$ sudo service mysql stop
$ sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.backup
$ sudo vim /etc/mysql/my.cnf
$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
$ sudo service mysql start
$ mysql -u root -p
mysql> create database did2memo_net_wp default character set utf8mb4;
mysql> grant all privileges on did2memo_net_wp.* to did2@localhost identified by '(wp-configに書くMySQLのパスワード)';
mysql> exit;
$ mysql -u did2 -p
mysql> source did2memo_net_wp.sql

試しにutf8mb4にしてみたものの、これでいいのかはよく分からず。

Apacheの設定

実行ユーザーは、「/etc/apache2/envvars」で変更が可能です。

なので、以下の部分を、nginx, www に変更しました。

$ sudo vim /etc/apache2/envvars
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

サイトの設定

まず最初に、設定の見本をコピーします。

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/did2memo.net.conf

設定を、だいたい以下のように書き換えました。

$ sudo vim /etc/apache2/sites-available/did2memo.net.conf
<Directory "/var/www/did2memo.net/wp">
        Options Includes FollowSymLinks ExecCGI MultiViews
        AllowOverride All
</Directory>
<VirtualHost *:8000>
ServerName did2memo.net
ServerAdmin did2@localhost
DocumentRoot /var/www/did2memo.net/wp
ErrorLog ${APACHE_LOG_DIR}/did2memo.net_error.log
CustomLog ${APACHE_LOG_DIR}/did2memo.net_access.log combined
$ sudo a2ensite did2memo.net
Enabling site did2memo.net.
To activate the new configuration, you need to run:
  service apache2 reload
$ sudo service apache2 reload

この時点で、default設定がまだ有効なので、「$ sudo a2dissite 000-default.con」で無効化し、「/etc/apache2/ports.conf」で、Listenするポート番号「Listen 8000」にしました。

ここで、「Forbidden You don't have permission to access / on this server」になってしまいました。

調べてみると、httpdプロセスが、www-data(デフォルトのユーザー)で動いていることが分かりました。

どうやら、環境変数がreloadじゃ読み込まれていなかったみたいなので、「$ sudo service apache2 restart」で再起動すると、直りました。

この後の手順は割愛

このあと、いろいろな細かい設定変更や、エラー発生とそれに対する対策をしたのですが、とりあえずここまでにしておきます。

BackWPupがDropBoxへのバックアップに失敗していた理由

確認してみたところ、「Missing function "curl_exec".」というエラーが出ていました。

確かに「curl」コマンドをインストールしていなかったので、「$ sudo apt-get install curl」をしてみました(その後再起動)。

しかし、それではダメだったので、「$ sudo apt-get install php5-curl」をしてから再起動($ sudo service apache2 graceful)してみたところ、エラーが出なくなりました。

メール設定

WordPressからのコメントお知らせメールが届いていないことに気が付いたので、sendmailをインストールしました。

参考資料

ubuntu 14.04 での標準的なサーバインストール手順(公式):

コメント(0)

新しいコメントを投稿