さくらのVPS導入手順 8.MySQLの利用準備

MySQLのインストール

さくらのVPSでは初期状態でMySQLもインストールされていません
MySQLをyumコマンドでインストールします

$ sudo yum -y install mysql-server

2014年1月の段階ではインストールされるのは5.1.71になるようです
こちらも最新は5.5系なのでapache同様一つ前のものになりますが、特殊なものを入れなければ特に問題は起きないでしょう
そこまで深く使いこなす人は、初期設定にここの記事など読んでいないと思いますので……

$ mysql --version
mysql  Ver 14.14 Distrib 5.1.71, for redhat-linux-gnu (x86_64) using readline 5.1

設定ファイルの編集

では、こちらも同じようにまずは設定ファイルのバックアップを取った後で編集を行います

$ sudo cp /etc/my.cnf /etc/my.cnf.original
$ sudo vi /etc/my.cnf

ハイライトされている行を追加してください

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

character-set-server=utf8
skip-character-set-client-handshake

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqldump]
default-character-set=utf8

サービスの起動

ではサービスを起動してみましょう
いっぱい説明文が出ますが、[Warning]とかが特に出ず、最後がOKになればほぼ大丈夫

$ sudo service mysqld start
MySQL データベースを初期化中:  Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h www4380ue.sakura.ne.jp password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

                                                           [  OK  ]
mysqld を起動中:                                           [  OK  ]

他のサイトを参考にして設定すると、以下のような警告が出るケースもありますが、[mysqld]中の”–default-character-set”を”–character-set-server”に変更してください

140127 00:00:00 [Warning] '--default-character-set' is deprecated and will be removed in a future release. Please use '--character-set-server' instead.
OK

サービスの登録

では、ここでも同じようにサービスを登録します

$ sudo chkconfig mysqld on
$ chkconfig --list mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

MySQL用rootユーザーのパスワードの設定

次に、起動したときの説明文でも注意を受けていましたが、このままではMySQL用のrootユーザーにパスワードが設定されていない状態なので、それを設定しておきましょう
以下の”で囲まれた部分に任意のパスワードを設定してください

$ sudo mysqladmin -u root password '任意のパスワード'

成功したら、早速そのパスワードを使ってログインしてみます

$ mysql -u root -p
Enter password:(先ほど設定したパスワード)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, 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>

このような表示になればログイン成功です

不要なユーザーの削除

最初に自動的に作られるユーザーとデータベースに不要なものが含まれていますので、それもここで削除しておきましょう
ハイライトしている行がコマンドの入力になります
1行目でmysqlクライアントを起動します
15行目で使用するデータベースを指定しています
20行目で、今登録されているユーザーを表示してみています。パスワードは皆さんそれぞれ、またhostのwww0000xの部分は皆さんのサーバー名になっていると思います
32行目で、そのうちの不要なものを削除し
35行目で再表示して不要なものが消えたことを確認しています

$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.1.71 Source distribution

Copyright (c) 2000, 2013, 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> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SELECT user, password, host FROM user;
+------+-------------------------------------------+-----------------------+
| user | password                                  | host                  |
+------+-------------------------------------------+-----------------------+
| root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | localhost             |
| root |                                           | www0000x.sakura.ne.jp |
| root |                                           | 127.0.0.1             |
|      |                                           | localhost             |
|      |                                           | www0000x.sakura.ne.jp |
+------+-------------------------------------------+-----------------------+
5 rows in set (0.00 sec)

mysql> DELETE FROM user WHERE user = '' OR ( user = 'root' AND host != 'localhost' );
Query OK, 4 rows affected (0.00 sec)

mysql> SELECT user, password, host FROM user;
+------+-------------------------------------------+-----------+
| user | password                                  | host      |
+------+-------------------------------------------+-----------+
| root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | localhost |
+------+-------------------------------------------+-----------+
1 row in set (0.00 sec)

不要なデータベースの削除

続けて不要なデータベースを削除しておきます
データベースの一覧を表示させると、初期状態では3つのデータベースが用意されていることが分かります

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

このうちtestデータベースは使うことはないので、削除してしまいましょう

mysql> DROP DATABASE test;
Query OK, 0 rows affected (0.02 sec)

これで消えました

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)

次は さくらのVPS導入手順 9.WordPressの利用準備

はじめに戻る さくらのVPS導入手順 0.はじめに

さくらのVPS導入手順 7.Apacheの利用準備

はじめに

では、ここからはWEBサーバーの設置を行っていきます
構成としては、Apache+MySQL+PHP+WordPressで進めていきます

Apacheのインストール

さくらのVPSでは初期状態でApacheはインストールされていません
まずはApacheをyumコマンドでインストールします

$ sudo yum -y install httpd

なお、現在Apacheには最新の2.4系も存在しているが、yumでインストールされるのは2.2.15のようです

$ httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Aug 13 2013 17:29:28

設定ファイルの編集

設定ファイルを編集して、Apacheの初期設定を行います
かなり複雑な設定ファイルなので、いざというときのために編集前にバックアップを取っておきましょう
バックアップの取り方は人それぞれですが、私は同じ場所にファイル名を変えてコピーしています

$ sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.original

バックアップしたら編集を開始しましょう

$ sudo vi /etc/httpd/conf/httpd.conf

はじめに ServerTokens の設定を変更します
エディタでコマンド入力状態のときに “/ServerTokens” を実行することで、キーワード”ServerTokens”を検索することができます
初期状態では攻撃者の参考になるOSの情報を返すようになっているので、サーバーがApacheであることだけを返すように変更します

# Don't give away too much information about all the subcomponents
# we are running.  Comment out this line if you don't mind remote sites
# finding out what major optional modules you are running
ServerTokens Prod

次に、ユーザーとグループがapacheになっていることを確認します

# User/Group: The name (or #number) of the user/group to run httpd as.
#  . On SCO (ODT 3) use "User nouser" and "Group nogroup".
#  . On HPUX you may not be able to use shared memory as nobody, and the
#    suggested workaround is to create a user www and use that user.
#  NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET)
#  when the value of (unsigned)Group is above 60000;
#  don't use Group #-1 on these systems!
#
User apache
Group apache

次はServerAdmin(サーバー管理者の連絡先)です
必要に応じて記述するかしないか決めればいいが、これからこのサーバーに設置するメールサーバーで受信するメールアドレスを書くことは避けましょう
なぜなら、同じサーバーに設置されているので、その時はメールも受け取れない可能性が高いからです

# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. admin@your-domain.com
#
ServerAdmin root@localhost

次はDocumentRoot(ドキュメントの配置場所)です
標準では以下のようになっていますので、ここでは特に変更する必要はありませんが、WordPressでサブドメインを使って複数のBLOGを作成したり、ドメインそのものを複数使ってサイトを運用したりする際には、ここの設定を変更します
それについてはまた別の記事で説明します

# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

次にオプション設定を変更します
先ほどのドキュメントルート”/var/www/html”に対応する以下の設定を変更します
標準ではファイルの一覧を表示する設定になってるので、表示しないようにします
ハイライトされている15行目のIndexesの前に”-“を付け、22行目のnoneをAllに変更します

<Directory "/var/www/html">

#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
    Options -Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride All

#
# Controls who can get stuff from this server.
#
    Order allow,deny
    Allow from all

</Directory>

次に、ServerSignature(サーバーの詳細情報の表示)をOffにする
これも悪意のある相手に余計な情報を与えないためです

# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory
# listings, mod_status and mod_info output etc., but not CGI generated
# documents or custom error documents).
# Set to "EMail" to also include a mailto: link to the ServerAdmin.
# Set to one of:  On | Off | EMail
#
ServerSignature Off

以上で編集完了なので、簡単なミスは用意されているツールで確認する
スペルミスや不可能な設定値などのエラーになるような記述はこれで確認できる

$ sudo apachectl configtest
Syntax OK

サービスの起動

ではサービスを起動してみよう

$ sudo service httpd start
httpd を起動中:                                            [  OK  ]

[OK]が出れば成功なので、早速先に登録してあったドメイン名、もしくはIPアドレスでブラウザでアクセスしてみよう
このようなテストページが表示されるはずである
sakura-vps-07-01

起動サービスの登録

次にサーバーを再起動した際にも自動的にサービスが開始されるよう、サービスの登録を行っておこう
1行目が登録のコマンドですが、結果は表示されないので、その次のコマンドで実行結果を確認しています

$ sudo chkconfig httpd on
$ chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

これでWEB(HTTP)サーバーの準備は完了です
自分で作成したコンテンツを”/var/www/html”以下に設置することで、登録したドメインを使ってコンテンツを公開可能になりました

次は さくらのVPS導入手順 8.MySQLの利用準備

はじめに戻る さくらのVPS導入手順 0.はじめに