☆☆ 新着記事 ☆☆

2018年11月29日木曜日

ねこでもわかる 「さくら VPS」 を試してみる 第四回 -PHP, MariaDBインストール

サーバー・レンタルの大手 さくらのVPSが、ネコでもわかる!らしいので、初心者だけど試してみる。の第四回目。  参考にするのは、さくら公式の導入ガイド「ネコでもわかる!さくらのVPS講座」
ネコに分かって、人間様に分からない筈はないので、早速、やってみる。 の四回目。

前回はApacheを入れて、Firewallの設定をして、htmlが表示できるようにした。 
今回は、phpとMaria DB (MySQLの派生版)をインストールして、環境構築をしてみる。

が、さっそくやってみる。

目次
  1. phpをインストールする
  2. MariaDBをインストールする

1. phpをインストールする
「CentOS7標準のphpはバージョン5.4ですが、現時点ではphp7.1がリリースされており今後はこちらが主流になります。手順は若干増えてしまいますが、ここではCentOS標準のphp5.4ではなく最新のphp7.1をインストールする手順を説明します。

php7.1はCentOS公式リポジトリ(アプリケーションの配布元サーバー)には置かれていません。phpの最新アプリケーションパッケージを置いているremiという別のリポジトリ(またはレポジトリとも言います)から取得します。VPS上でそのための設定を先に行います。

なお、remiを使えるようにするためには、本来ならばEPEL(イーペル)という別のリポジトリを先に使用可能にしておかなければならないのですが、さくらのVPSは初めからEPELリポジトリが使用可能になっていますのでEPELは設定不要です。」

(1) remi用の設定パッケージをインストール
suコマンドでrootユーザーになってから

# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
を実行。 

というが、どのディレクトリーで実行すれば良いのか?
不安を覚えつつも、SSH接続のdefault directory の 'root' directoryで実行してみる。
(この辺りは、Linuxのdirectory構成を理解しないと不安ではある。)

ふむ、一瞬で完了。 次に、

(2)php (とパッケージも) のインストール

# yum install --enablerepo=remi,remi-php71 php php-devel php-mbstring php-mysqlnd php-pdo php-gd

お、暫く無反応で、動いたと思ったら、

Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
12: Timeout on http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
epel/x86_64/metalink                                                 | 6.7 kB  00:00:00

というメッセージが出たので、失敗したかと思ったが、すぐに、それぞれのファイルのダウンロードが始まって、無事にインストールできた('complete'のメッセージが表示された)。 びっくりした。

(3) Apacheの再起動で使用可能にする。

# systemctl restart httpd

以下のphpファイルをvar/wwwにアップロードして、挙動確認。

<html>
  <body>
    <?php echo "Hello World! php" ?>
  </body>
</html>


phpが動くようになりました!!

(4) phpのアップロード・ファイルの上限を変更する
基本のインストールは上記で終わりですが、今後の事を考えて、アップロードできるファイルの上限サイズの変更をしておくらしい。 こういうガイドは素晴らしい!!

# cd /etc
# cp php.ini php.ini.old


を実行。 それぞれのコマンドは、第二回ですでに実施しているので解説なし。

ls -l で確認すると、コピーされたファイルが出来ていますね。
-rw-r--r--   1 root root    62394 Nov  8 02:54 php.ini
-rw-r--r--   1 root root    62394 Nov 29 20:36 php.ini.old

# vim php.ini
ファイルを開いて、2か所を変更。
(変更前)
post_max_size = 8M
・・・
upload_max_filesize = 2M

(変更後)
post_max_size = 128M
・・・
upload_max_filesize = 128M

該当箇所の検索は、
vimで ‘/’ をタイプしてください。検索モードになり、画面の一番下にフォーカスが移りますので、検索語句”post_max_size“を入力してEnterキーを押します。

# systemctl restart httpd
を、実行して、変更を有効に!!

これでおしまい。 

今回は、以前に使ったコマンドばかりでしたから、簡単でしたね。



2. MariaDBをインストールする

(1) パッケージのダウンロード
「CentOS6ではデータベースアプリケーションとしてMySQLが用意されていましたが、CentOS7ではMariaDBが標準となりました。」

ということで、MySQL派生のMariaDBをインストールする。

# yum install mariadb-server

の実行。 

上から連続してやっていると、Current Directoryは 'etc' になっているので、指示はないけど、一応、
#cd /
で、root directoryに戻ってから実行してみる。

すぐに’Complete!‘が表示されたのでダウンロード終了。


これでDiskの使用量は、
[root@ik1-xxx-xxxxx ~]# df -h
Filesystem      Size      Used     Avail   Use%  Mounted on    第二回終了時
/dev/vda4        16G       2.2G     14G    14%        /                     used 2.0G    
devtmpfs        233M       0       233M    0%         /dev                           0
tmpfs             244M        0       244M    0%        /dev/shm                    0
tmpfs             244M     8.6M   236M    4%       /run                           4.5M
tmpfs             244M        0      244M    0%       /sys/fs/cgroup              0
/dev/vda2      497M    222M   276M   45%     /boot                          222M
tmpfs              49M        0         49M     0%     /run/user/1000              0


だから、ほとんど変わりませんね。

(2) サーバー再起動時にMariaDBを自動的に起動する設定に変更。

systemctl enable mariadb

実行すると
Created symlink from /etc/systemd/system/multi-user....
のメッセージで完了

# systemctl start mariadb
特にメッセージは出ないけど、よしとしよう。

(3)MariaDBにログインするためのパスワードを設定
# mysql_secure_installation

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so
you should just press enter here.

Enter current password for root (enter for none):

こういうメッセージ。 なので、enterキーを押下。

Set root password? [Y/n]

Y で

'MariaDBの' root passwordを設定するらしい。

設定すると、また質問。

「その後、いくつかの質問が続きます。この先はすべて’Y’で答えておけばよいです。」
とのガイドだが、聞かれたことだけメモしておく。

1) By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]

2) Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]

3)By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] ^C

Aborting!
Cleaning up...
[root@ik1-339-29895 ~]#


うわ、コピーするつもりでctl+cを押してしまったら、abortしてしまった。

Remove test database and access to it?
Reload privilege tables now? [Y/n]

の2つが出来なかったみたい。

まあ、問題なさそうなので、これでMariaDBの初期設定が完了として、
次に進む。

(4)MariaDBにログインできるかどうかを確認

# mysql -u root -p

実行すると、
[root@ik1-xxx-xxxxx ~]# mysql -u root -p
Enter password: (さっき設定したパスワード)
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>

という事で接続ができた。

「DBにログインするには、まずVPSにLinuxユーザーとしてログイン済みでなければなりません。つまり、DBにログインするには、 「Linuxユーザーとしてログイン後、DBユーザーとしてログインする」という2回のログイン操作が必要になります。
また、VPSの外から直接DBにログインすることはできません。」

という説明。 まあ、DBですからね。


(5)データベースの確認


MariaDB [(none)]> show databases;  (最後のセミコロンまで入力要)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.01 sec)


「データを保存する「テーブル」の集合がデータベースであり、MariaDBインストール時には最初から3つのデータベース
・information_schema |
・mysql        
・performance_schema |
が存在します。MariaDBそのものも、「データベース」と呼ばれます。」

うーむ。test.dbが残っちゃってますね。

DBが表示され、MariaDBに接続できている事が確認されたので
exit()
で、MariaDBからログオフする。


これで第四回はお終いです。


0 件のコメント:

コメントを投稿