VagrantのCentOS7でPHP7、Nginxを構築するまで。
Macローカルで環境確認
$ vagrant -v vagrant 2.1.1
$ VBoxManage -v 5.2.12r122591
VagrantでのCentOS/php72のinstallからhelloworldまで
$ vagrant init centos/7
vagrantfileの中身
Vagrant.configure("2") do |config| config.vm.box = "centos/7" # config.vm.network "forwarded_port", guest: 80, host: 8080 # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" config.vm.network "forwarded_port", guest: 22, host: 12222, id: "ssh" config.vm.network "private_network", ip: "192.168.33.11" config.vm.synced_folder "/test/php-test", "/home/vagrant/php_test" // 予め作っておいてsyncさせる
syncがうまくいかない場合はでguestaddition
みたいなエラーやwarning出ていたら、一番下に記載しているvagrant-vbguest
のpluginを入れる必要ある。
vagrantを立ち上げてCentOSのバージョン確認
$ vagrant up $ vagrant ssh [vagrant@localhost ~]$ sudo su - [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core)
sshログインできるようにする。
$ vagrant ssh-config >> ~/.ssh/config $ ssh vagrant@192.168.33.11 [vagrant@localhost ~]$
php7.2のインストール
[vagrant@localhost ~]$ sudo su - [root@localhost ~]# yum update -y [root@localhost ~]# yum -y install epel-release [root@localhost ~]# yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm [root@localhost ~]# ls -l /etc/yum.repos.d/ // EPEL, Remi のリポジトリが追加されていることを確認。 -rw-r--r--. 1 root root 951 Oct 2 2017 epel.repo -rw-r--r--. 1 root root 2605 Jun 19 08:28 remi.repo -rw-r--r--. 1 root root 1314 Jun 19 08:28 remi-php72.repo [root@localhost ~]# yum -y install --enablerepo=remi,remi-php72 php php-devel php-mbstring php-pdo php-gd php-xml php-mcrypt [root@localhost ~]# php -v PHP 7.2.9 (cli) (built: Aug 15 2018 09:19:33) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies root@localhost ~]# yum -y install --enablerepo=remi,remi-php72 php-mysql // mysql入れ忘れ [root@localhost ~]# yum list installed | grep php72 php.x86_64 7.2.9-1.el7.remi @remi-php72 php-cli.x86_64 7.2.9-1.el7.remi @remi-php72 php-common.x86_64 7.2.9-1.el7.remi @remi-php72 php-devel.x86_64 7.2.9-1.el7.remi @remi-php72 php-gd.x86_64 7.2.9-1.el7.remi @remi-php72 php-json.x86_64 7.2.9-1.el7.remi @remi-php72 php-mbstring.x86_64 7.2.9-1.el7.remi @remi-php72 php-mysqlnd.x86_64 7.2.9-1.el7.remi @remi-php72 php-pdo.x86_64 7.2.9-1.el7.remi @remi-php72 php-pecl-mcrypt.x86_64 1.0.1-6.el7.remi.7.2 @remi-php72 php-xml.x86_64 7.2.9-1.el7.remi @remi-php72
nginxで立ち上げるならあとでphp-fpmも必要かな?
とりあえずhelloworld
$ cd php-test $ vi index.php // <?php echo "Hello World;" ?>と記入。 [root@localhost ~]# php -S 192.168.33.11:8000
ブラウザに192.168.33.11:8000と入れて、画面上に「Hello World」と出てくればOK!
MySQLを使えるようにする。(以下よりターミナルはvagrant上の表示。)
こちらを参考に。 CentOS 7 に MySQL 5.7 を yum インストールして初期設定までやってみた - enomotodev’s blog
# rpm -qa | grep -i mariadb mariadb-libs-5.5.56-2.el7.x86_64 # yum -y remove mariadb-libs # rm -rf /var/lib/mysql # rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm # yum -y install mysql-community-server # mysqld --version mysqld Ver 5.7.23 for Linux on x86_64 (MySQL Community Server (GPL))
できた。
MySQL
# systemctl start mysqld.service // 起動 # systemctl enable mysqld.service // 自動起動 # cat /var/log/mysqld.log | grep password // 初期パスの確認 2018-08-18T04:16:15.511041Z 1 [Note] A temporary password is generated for root@localhost: ,ji)&uHrH1Pe # mysql_secure_installation // MySQLの初期設定もろもろ # vi /etc/my.cnf // 以下の2行を追加。 character-set-server = utf8 default_password_lifetime = 0 # systemctl start mysqld.service // 再起動
Databaseの設定 ~ table作成
# mysql -u root -p mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ mysql> create database test_db; mysql> grant all on test_db.* to dbuser@localhost identified by 'G&hsj934jf'; // user追加/権限付与してパスワード設定 mysql> quit # mysql -u dbuser -p // [Enter]してパスワード入力 mysql> use test_db; mysql> create table users ( id int not null auto_increment primary key, name varchar(255), age int ); mysql> show tables; +-------------------+ | Tables_in_test_db | +-------------------+ | users | +-------------------+ mysql> desc users; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | YES | | NULL | | | score | int(11) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec)
ちゃんと接続できるか確認。
index.phpへの記述
index.php
<?php define('DB_DATABASE', 'test_db'); define('DB_USERNAME', 'dbuser'); define('DB_PASSWORD', 'G&hsj934jf'); define('PDO_DSN', 'mysql:host=localhost;dbname=' . DB_DATABASE); try { $db = new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->exec("insert into users (name, age) values ('hoge', 30)"); echo "ユーザーを追加しました。"; $db = null; } catch (PDOException $e) { echo $e->getMessage(); exit; }
ビルトインサーバーを立ち上げてブラウザで確認。
# php -S 192.168.33.11:8000
ブラウザに「192.168.33.11:8000」と入力して、「ユーザーを追加しました。」と出ればOK
Nginxのインストール
Nginxが入っていないの確認。
# yum -y --enablerepo=nginx install nginx Loaded plugins: fastestmirror Error getting repository data for nginx, repository not found # nginx -v -bash: nginx: command not found
Nginxのインストール準備
# vi /etc/yum.repos.d/nginx.repo // 先ファイルを作成して、nginxのinstallに関する設定を記述。 [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
記入したら「:wq!」で保存。 これを記入するとさっきエラーになったyumのコマンドも通るように。
# yum -y --enablerepo=nginx install nginx (略) Complete! # nginx -v nginx version: nginx/1.14.0
できた。ついでに起動。
# systemctl start nginx # systemctl enable nginx
php-fpmのinstall
# yum -y install --enablerepo=remi,remi-php72 php-fpm
※php7.2関連のinstallは常にyum -y install --enablerepo=remi,remi-php72
がつく。でないと(yum install php-fpm
など)初期設定のレポジトリからinstallするので、古いバージョンのphp関連のinstallしかできないので、依存関係がおかしくなりエラーになる。
# systemctl enable php-fpm
で常に起動。
nginxの設定
/etc/php-fpm.d/www.confの書き換え
書き換える。
# vi /etc/php-fpm.d/www.conf ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache user chosen to provide access to the same directories as httpd user = apache ; RPM: Keep a group allowed to write in log dir. group = apache
ここを
; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; RPM: apache user chosen to provide access to the same directories as httpd ;user = apache // コメントアウト user = nginx // 追記 ; RPM: Keep a group allowed to write in log dir. ;group = apache // コメントアウト group = nginx // 追記
/etc/nginx/conf.d/default.confの書き換え
# vi /etc/nginx/conf.d/default.conf (略) # 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; #}
ここを以下に書き換え。(デフォルト部分は#で残しておく。)
(略) # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { # root html; root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; }
できたら保存して# systemctl restart nginx
# vi /usr/share/nginx/html/info.php <?php phpinfo(); ?>
ブラウザに192.168.33.11/info.phpと入力。 するとエラーの記述がでた。 調べてみると初期でerror.logは出力されないみたいなので、できるように設定する。
error.log出力設定
;catch_workers_output = yes
をコメントアウトになっているので、外す(もしくはコピーして外す。)
# vi /etc/php-fpm.d/www.conf ; Redirect worker stdout and stderr into main error log. If not set, stdout and ; stderr will be redirected to /dev/null according to FastCGI specs. ; Default Value: no ;catch_workers_output = yes catch_workers_output = yes
# systemctl restart php-fpm
で再起動。
確認。
# tail -f /var/log/php-fpm/error.log [18-Aug-2018 06:09:13] NOTICE: fpm is running, pid 22342 [18-Aug-2018 06:09:13] NOTICE: ready to handle connections [18-Aug-2018 06:09:13] NOTICE: systemd monitor interval set to 10000ms [18-Aug-2018 06:09:13] WARNING: [pool www] child 22346 said into stderr: "ERROR: Unable to set php_value 'soap.wsdl_cache_dir'"
こんなの出てた。
調べたらphp-soap
というものをインストールせよと。
# yum -y install --enablerepo=remi,remi-php72 php-soap
エラーログこれ以上でなくなった。
http://192.168.33.11/info.php
info.phpの画面出力。
nginxのdocumentrootを変更して作業ディレクトリからphpの表示ができるようにする。
# vi /etc/nginx/conf.d/default.conf location / { # root /usr/share/nginx/html; root /home/vagrant/php-test; // Document_rootを変更。 index index.html index.htm index.php; // index.phpを追記。 } (略) # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { # root html; # root /usr/share/nginx/html; root /home/vagrant/php-test; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; }
再起動 + SELinuxの設定を解除。
# systemctl restart nginx # getenforce Enforcing # setenforce 0 # systemctl restart nginx # getenforce Permissive
/home/vagrantの権限がひっかかっているので700→755に。
# chmod 755 vagrant
ここでかなりどはまりした。。。 vagrantのディレクトリの権限とSELinuxというものでつっかかっていたみたい。。。 とりあえず解決した。
ここまでやってブラウザで「http://192.168.33.11/index.php」と入れると「Hello」と出てきたので、一安心。。。
丸一日かかってしまった。笑 余分だったところもあるかもだけど、テストでもう一度上記の手順を踏むとブラウザで動くようになった。めでたし。 vagrantでsyncもさせているので、ローカルのエディタでガリガリ編集しながらPHPを動かせる!!!!
NginxのconfやSELinuxの知識もつけないと。。。
最初にyum update
でSELinuxの設定も色々動いていたからそのへんの知識も必要。
SELinuxを無効にする。
SELINUX=enforcing
からSELINUX=permissive
に変える。
# vi /etc/selinux/config (略) SELINUX=permissive (略)
備考
vagrant(VirtualBox)のバージョンの関係でうまくsyncができなかったので、vagrantのvagrant-vbguestというプラグインで解決。
vagrantfileのsyncにも,type: "virtualbox"
と追記してreloadする。
追記(20180911)
再度PHPの環境構築に手惑いなんとかできた。 nginxのconfファイルとphp-fpm間でのfastcgiのpathが違うことがエラーの原因とのこと
エラーメッセージ
var/log/nginx/error.log
2018/09/11 23:54:39 [error] 4489#4489: *8 connect() failed (111: Connection refused) while connecting to upstream , client: 192.168.33.1, server: default_server, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0. 1:9000", host: "192.168.33.33"
# vi /etc/php-fpm.d/www.conf
; Set permissions for unix socket, if one is used. In Linux, read/write ; permissions must be set in order to allow connections from a web server. ; Default Values: user and group are set as the running user ; mode is set to 0660 ;listen.owner = nginx ;listen.group = nginx ;listen.mode = 9000 // 追記する。 listen.owner = nginx listen.group = nginx listen.mode = 9000