LoginSignup
2
4

More than 5 years have passed since last update.

nginx サーバーに PHP, MySQL を入れる

Posted at

前回まで

  1. Vagrant に CentOS7 と nginx を入れて Transmit で接続する

目的

前回までに制作したサーバーにPHP, MySQL, Wordpressを入れてサイトらしくしたい.

手順 - 概略

  1. PHP をインストールし PHP と nginx をつなげる
  2. MySQL をインストールする
  3. Wordpress をインストールし MySQL で設定する (次回に)

1. PHP をインストールし PHP と nginx をつなげる

$ sudo yum install --assumeno php
$ sudo yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
$ yum repolist
$ sudo yum install --enablerepo=remi-php71 php php-cli php-common php-devel php-fpm php-gd php-mbstring php-mysqlnd php-pdo php-pear php-pecl-apcu php-soap php-xml php-xmlrp

説明することも特にないのですがインストールできるPHPのバージョンを調べて リポジトリ をいれてます.

$ sudo vi /etc/php.ini

display_errors = On
error_log = "/var/log/php_errors.log"
date.timezone = "Asia/Tokyo"
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
を追記


$ sudo vi /etc/php-fpm.d/www.conf

25行目くらいにある user と group を編集
user = nginx
group = nginx
に

このくらいで日付の修正していたのを忘れていたことを思い出しました.
PHP の設定が終わったらその方法を記載します.

$ sudo vi /etc/nginx/conf.d/default.conf

~~~ 省略 ~~~
location / {
        root   /usr/share/nginx/html;
    #   index  index.html index.htm;
        index  index.php;
}
~~~ 中略 ~~~
location ~ \.php$ {
    #    root           html;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
         fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$fastcgi_script_name;
         include        fastcgi_params;
}
~~~ 省略 ~~~

になるように編集

あとは nginx と php-fpm を再起動するだけです……と思ったのですが

$ sudo systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

( ゚д゚ )

設定するたびに毎回このエラーを見ているので原因もわかっているのですが一応指示に従ってみましょう.
一部分だけ抽出します.

$ sudo systemctl status nginx
~~~ 省略 ~~~
Active: failed (Result: exit-code) since Tue 2018-07-03 13:32:24 JST; 10s ago
~~~ 中略 ~~~
Jul 03 13:32:24 localhost.localdomain nginx[2605]: nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/default.conf:12
~~~ 省略 ~~~

純粋な記述ミスですね. 毎回やらかすので恥ずかしい.
default.conf を確認したところ index index.php のあとにセミコロンがありませんでした.
アホですね.

さてもう一回

$ sudo systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

( Д ) ゚ ゚

このエラーは初めてでした.

$ sudo systemctl status nginx
~~~ 省略 ~~~
Active: failed (Result: exit-code) since Tue 2018-07-03 13:39:04 JST; 7s ago
~~~ 中略 ~~~
Jul 03 13:39:04 localhost.localdomain nginx[2620]: nginx: [emerg] "fastcgi_pass" directive is not allowed here in /etc/nginx/conf.d/default.conf:33
~~~ 省略 ~~~

fastcgi_pass はここには置けないんだよ と……
ええ…

というわけで default.conf を再確認してみると

location ~ \.php$ {
    ~~~ 省略 ~~~
}

の先端に # が付きっぱなしでした. 馬鹿か俺は.

これでもう一回再スタートしてみました.

$ sudo systemctl restart nginx
$ sudo systemctl status nginx

無事に成功しました.

$ sudo systemctl restart php-fpm
$ sudo systemctl enable php-fpm

最後に index.php を作成して表示を確認します.
alias で ルートディレクトリまで移動するコマンドも合わせて作っちゃいましょう.

$ echo "alias mvserver='cd /usr/share/nginx/html'" >> .bashrc
$ source ~/.bashrc
$ mvserver
$ sudo vi index.php

<?php
    phpinfo();

ページを更新して PHP についての情報がずらーっと出てくればオッケーです.

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

まずMariaDBを削除してからインストールします.

$ rpm -qa | grep maria
$ sudo yum remove mariadb-libs
$ sudo rm -rf /var/lib/mysql/

$ sudo rpm -Uvh http://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
$ sudo yum -y install mysql-community-server
$ sudo systemctl start mysqld
$ sudo systemctl enable mysqld

インストール完了です.
MySQLのセキュリティを一応設定しておきます.

$ sudo cat /var/log/mysqld.log | grep 'temporary password'
A temporary password is generated for root@localhost: (Password)

$ sudo mysql_secure_installation

Enter password for user root: (sudo mysql_secure_installation で確認したパスワードを入力)
New password: (新しいパスワードを入力)
Re-enter new password: (もう一度新しいパスワードを入力)
Press y|Y for Yes, any other key for No: n
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
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


$ sudo vi /etc/my.cnf

default-storage-engine=InnoDB
innodb_file_per_table

character-set-server = utf8
collation-server = utf8_general_ci

[mysql]
default-character-set = utf8 

[client]
default-character-set = utf8

を文末に追記


$ sudo systemctl restart mysqld

3. Wordpress をインストールし MySQL で設定する

さてこの説明に入りたかったのですが……
エラー処理を入れたら長くなってしまったので別記事にしたいと思います.
代わりに vagrant の時刻設定の方法を書きたいと思います.

3.5. Vagrant サーバーの時刻をホストと合わせる

$ sudo rm /etc/localtime
$ sudo ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

これで時刻を日本時間にします……がこれでもまだずれるので次の処理をします.

$ exit
$ sudo vi Vagrantfile

config.vm.provider :virtualbox do |vb|
vb.customize ["setextradata", :id, "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled", 0]
end

を 下から2行目に追記

$ vagrant halt
$ vagrant up
$ vagrant ssh -c date

このときに現在時刻と合っていれば成功です.

次回予告

WordPress と Node.js を入れていきます.
WordPress は公式のドキュメントがわかりやすいので最高ですね.
Node.js は gulp を扱うのに入れます.

参考文献・サイト・資料

今回PHPとMySQLを設定するのに次の文献・サイト・資料などを参考にさせていただきました.
この場を借りて先の賢人方に多大なる感謝を申し上げます.

今回の設定の参考の中心になりました. ありがとうございます.
VagrantでCentOS7にnginx, php7を入れてindex.phpを表示させるまで

MySQLをインストールする際の参考にさせていただきました.
CentOS7.3にMySQL5.7をyumでインストールする

時刻合わせの方法が記載されています.
vagrantで時刻がおかしい場合の対処法

前回記載したサイトや資料などに関しては今回割愛させていただいております.
その他多くのサイトの情報を参考にしています. 本当にありがとうございました.

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4