でぶ

とあるサイトの開発室

『さくらのVPS』にNginx、PHP-FPM、MariaDBな環境を構築したよ!

はじめに。

今回は前回、初期設定が終わった『さくらのVPS』にいろいろとインストール。 LAMPとは一味違ったナウなLEMP(Linux、Nginx、MariaDBPHP-FPM)サーバーを作ります。

最新版のNginxをyumでインストール。

リポジトリを追加。

$ sudoedit /etc/yum.repos.d/nginx.repo

リンク先のサイトを参考に。

Install - Nginx Community

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

$ sudo yum update
$ sudo yum install nginx

とりあえず、設定ファイルを見てみる。

$ sudo vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

Nginxのデフォルトのドキュメントルートは、 /usr/share/nginx/html らしい。

Nginxを起動する。

$ sudo systemctl start nginx.service

Nginxが自動で起動するように設定。

$ sudo systemctl enable nginx.service

ブラウザから自分のVPNIPアドレスを入力しNginxのスタートページが見れたら成功!

PHPをインストール。

PHP関係はそのまままとめてインストール。

$ sudo yum install php php-fpm php-devel php-gd php-mbstring

PHP-FPMを設定。

$ sudo cp /etc/php-fpm.d/{www.conf,www.conf.back}
$ sudoedit /etc/php-fpm.d/www.conf

; RPM: apache Choosed to be able to access some dir as httpd
user = nginx   # apacheから書き換える
; RPM: Keep a group allowed to write in log dir.
group = nginx  # apacheから書き換える

listen = /var/run/php-fpm/php-fpm.sock ←UNIXソケットを利用

nginxとPHP-FPMのやり取りはTCPより高速なUNIXソケットで設定。

PHP-FPMを起動する。

$ sudo systemctl start php-fpm.service

PHP-FPMが自動で起動するように設定。

$ sudo systemctl enable php-fpm.service

NginxでPHPを動かすための設定。

$ sudo cp /etc/nginx/conf.d/{default.conf,default.conf.back}
$ sudoedit /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

10行目のindexにindex.phpを追加。 30行目からのPHPのセクションを編集。

最新版のMariaDByumでインストール。

リポジトリを追加。

$ sudo vim /etc/yum.repos.d/MariaDB.repo

OSや32Bit、64Bitによってディレクトリが異なるので注意!下記のリンクを参考に。

MariaDB - Setting up MariaDB Repositories - MariaDB

# MariaDB 10.0 CentOS repository list - created 2014-10-16 12:59 UTC
# http://mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

$ sudo yum update
$ sudo yum install MariaDB-devel MariaDB-client MariaDB-server

設定ファイルをバックアップする。

$ sudo cp /etc/my.cnf.d/{server.cnf,server.cnf.back}

$ sudo vim /etc/my.cnf.d/server.cnf

下記を追加。

character_set_server=utf8
default-storage-engine=InnoDB
innodb_file_per_table
[mysql]
default-character-set=utf8
[mysqldump]
default-character-set=utf8

MariaDBを起動。

$ sudo systemctl start mysql.service

MariaDBが自動で起動するように設定。

$ sudo systemctl enable mysql.service

rootパスワードを設定します。

$ mysqladmin -u root password '新しいパスワード'

MariaDBサーバに接続する。

$ mysql -u root -p
Enter password:

ついでにストレージエンジンの確認。

MariaDB [(none)]> SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                                    | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| CSV                | YES     | CSV storage engine                                                         | NO           | NO   | NO         |
| MRG_MyISAM         | YES     | Collection of identical MyISAM tables                                      | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables                  | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears)             | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                                      | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| ARCHIVE            | YES     | Archive storage engine                                                     | NO           | NO   | NO         |
| FEDERATED          | YES     | FederatedX pluggable storage engine                                        | YES          | NO   | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                                         | NO           | NO   | NO         |
| Aria               | YES     | Crash-safe tables with MyISAM heritage                                     | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.00 sec)

ついでにステータスも確認。

MariaDB [(none)]> STATUS;
--------------
mysql  Ver 15.1 Distrib 10.0.11-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:          1937
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server:                 MariaDB
Server version:         10.0.11-MariaDB MariaDB Server
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 1 day 31 sec

Threads: 1  Questions: 77193  Slow queries: 0  Opens: 40  Flush tables: 1  Open tables: 102  Queries per second avg: 0.893
--------------

以上で終了です。お疲れ様でした!

まとめ。

ApacheよりもNginx! MySQLよりもMariaDB

比較するとさすがに情報はすくないけど・・・

参考サイト。

Install - Nginx Community MariaDB - Setting up MariaDB Repositories - MariaDB

[amazonjs asin="4048702270" locale="JP" title="ハイパフォーマンスHTTPサーバ Nginx入門"]