この記事は以下の記事の続きです。
#3 さくらインターネットVPS で CentOS7 + nginx + PHP7 + MySQL 「nginxインストール&設定編」
目次
1. PHP7のインストール
PHP7をインストールする前にリポジトリを追加する必要があります。少しややこしいのですがRemiリポジトリからPHP7をインストールする必要がありますが、Remiリポジトリを追加するにはEPELリポジトリが必要となるので2つのリポジトリを追加します。
yum install epel-release
このままだとyum updateを行った場合に公式パッケージがEPELリポジトリのパッケージに上書きされてしまいます。非公式なので一応、EPELは必要なときのみ利用するほうが安全です。EPELを無効にします。
vim /etc/yum.repos.d/epel.repo
[epel] enabled=0
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
ちなみにRemiリポジトリはデフォルトで自動的に使用されないように設定されているのでEpelと違い変更の必要はありません。
yum -y install --enablerepo=epel,remi,remi-php73 php php-cli php-common php-gd php-imap php-mbstring php-mysql php-odbc php-pdo php-pear php-pgsql php-process php-snmp php-xml php-xmlrpc php-fpm php-mcrypt
cp /etc/php.ini /etc/php.ini.org
php.iniを編集していきます。
vim /etc/php.ini
;date.timezone = ↓変更後 date.timezone = "Asia/Tokyo" ;mbstring.language = Japanese ↓変更後(コメントアウトをはずすだけ) mbstring.language = Japanese ;mbstring.internal_encoding = ↓変更後 mbstring.internal_encoding = UTF-8 ;mbstring.http_input = ↓変更後 mbstring.http_input = pass ;mbstring.http_output = ↓変更後 mbstring.http_output = pass ;mbstring.encoding_translation = Off ↓変更後(コメントアウトをはずすだけ) mbstring.encoding_translation = Off ;mbstring.detect_order = auto ↓変更後(コメントアウトをはずすだけ) mbstring.detect_order = auto
[任意] セキュリティを高めるため、PHPのバージョン情報を吐くのを停止します。
expose_php = On ↓変更後 expose_php = Off
[任意] 開発時にPHPのエラーを画面に吐かせるようにしたい場合は以下も行います。
display_errors = Off ↓変更後 display_errors = On
[任意] PHPプログラムからファイルのアップロードそたりPOSTした場合の最大サイズを指定します。任意の値に変更します。PHPからファイルをアップロードするつもりがなければPOSTでそこまでのサイズが必要となることも滅多にないと思いますのでデフォルトで構いません。「memory_limit ≧ post_max_size ≧ upload_max_filesize」というサイズに設定するのが通常のようです。
memory_limit = 8M ↓変更後 memory_limit = 32M post_max_size = 8M ↓変更後 post_max_size = 32M upload_max_filesize = 2M ↓変更後 upload_max_filesize = 32M
[任意] 1リクエスト当たりの最大実行時間(秒)を必要なサイズに設定します。
max_execution_time = 30 ↓変更後 max_execution_time = 60
今後MySQLをブラウザから直接、閲覧編集できるphpMyAdminを利用する場合にはphpMyAdminが利用するためのセッションを保存するディレクトリ(/var/lib/php/session)を設定する必要があります。phpMyAdminを利用する予定がない場合には不要です。php.iniの修正はこれで終了です。
session.save_path = "/tmp" ↓変更後 session.save_path = "/var/lib/php/session"
vim /etc/php-fpm.d/www.conf
user = apache ↓変更後 user nginx group = apache ↓変更後 group = nginx listen = 127.0.0.1:9000 ↓変更後 listen = /var/run/php-fpm/php-fpm.sock ;listen.owner = nobody ↓変更後 listen.owner = nginx ;listen.group = nginx ↓変更後 listen.group = nginx
vim /etc/nginx/conf.d/default.conf
#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; #} 変更後↓ 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; }
systemctl enable php-fpm
systemctl start php-fpm
systemctl restart nginx
今後MySQLをブラウザから直接、閲覧編集できるphpMyAdminを利用する場合にはphpMyAdminが利用するためのセッションを保存するディレクトリ(/var/lib/php/session)を設定する必要があります。phpMyAdminを利用する予定がない場合には1-15まで飛ばしてOKです。
chown -R root.nginx /var/lib/php/session
setsebool -P httpd_can_network_connect_db on
2. MySQLのインストール
yum remove mariadb-libs
rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
sudo yum install -y mysql-community-server
mysql --version
[[email protected] ~]$ mysql --version mysql Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using EditLine wrapper
systemctl start mysqld.service
MySQLは初回の起動時にrootユーザーのパスワードをランダムに生成します。
そのため、パスワードを確認する必要があります。
cat /var/log/mysqld.log | grep 'temporary password'
[[email protected] chinpui]# sudo cat /var/log/mysqld.log | grep 'temporary password' 2017-01-05T16:19:13.356212Z 1 [Note] A temporary password is generated for [email protected]: .Krdeoh+
上記の場合は最後の「.Krdeoh+」がパスワードになります。
mysql_secure_installation
[[email protected] chinpui]# mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:
パスワードを聞かれたら2-6で表示したパスワードを入力します。
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
mysql -u root -p
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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>
ログインができました。
以上でさくらインターネットVPS で CentOS7 + nginx + PHP7 + MySQL の環境が整いました。
以降はCUIによる基本的なMySQLの操作です。
特に読む必要はありませんが、MySQLを操作したことがない方は参考にしてください。
SHOW DATABASES;
mysql> show databases -> ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
CREATE DATABASE test_db;
mysql> CREATE DATABASE test_db; Query OK, 1 row affected (0.00 sec)
SHOW DATABASES;
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test_db | +--------------------+ 5 rows in set (0.00 sec)
CREATE USER 'chinpui'@'localhost' IDENTIFIED BY 'MyPassword-9089778';
SELECT HOST, USER FROM mysql.user;
mysql> SELECT HOST, USER FROM mysql.user; +-----------+-----------+ | HOST | USER | +-----------+-----------+ | localhost | chinpui | | localhost | mysql.sys | | localhost | root | +-----------+-----------+ 3 rows in set (0.00 sec)
正常に作成されていることがわかります。
DROP DATABASE test_db;
mysql> DROP DATABASE test_db; Query OK, 0 rows affected (0.00 sec)
SHOW DATABASES;
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)