LoginSignup
37
51

More than 5 years have passed since last update.

CentOS7+nginx+php7+MariaDBの環境構築

Last updated at Posted at 2016-05-31

今までの開発環境がCentOS6+Apache2.2+php5.x+mysql5.xでした。php7もリリースされてから情報が増えてきたのと、nginxも触ってみたかったので、CentOS7にphp7とnginxを導入してみました。

自分へのメモ代わりと出来るだけシンプルかつ失敗がなさそうな手順にしてみました。

リポジトリの追加

nginxやphp7はCentOS7のリポジトリに無いのでepelを追加しました。

epelリポジトリ

インストール後に明示的に指定しないとepelが使用出来ないようにしました。

console
$ sudo yum install epel-release

「enabled=1」を「enabled=0」にしました。

console
# vi /etc/yum.repos.d/epel.repo

webサーバインストール

nginxをインストールします。

nginx

インストール

console
$ sudo yum --enablerepo=epel install nginx

設定

  • 設定ファイルを作成しました。
/etc/nginx/conf.d/centos7.conf
server {
  listen 80;
  server_name centos7;
  root /var/www;
  index index.php index.html index.htm;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    include fastcgi_params;
  }
}

有効化

console
$ sudo systemctl enable nginx

サービス起動

console
$ sudo systemctl start nginx
$ sudo systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2016-05-26 16:25:32 JST; 6min ago
  Process: 1141 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1098 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1090 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 1170 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1170 nginx: master process /usr/sbin/nginx
           └─1173 nginx: worker process

May 26 16:25:31 centos70 systemd[1]: Starting The nginx HTTP and reverse proxy server...
May 26 16:25:32 centos70 nginx[1098]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
May 26 16:25:32 centos70 nginx[1098]: nginx: configuration file /etc/nginx/nginx.conf test is successful
May 26 16:25:32 centos70 systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
May 26 16:25:32 centos70 systemd[1]: Started The nginx HTTP and reverse proxy server.

稼働確認

ブラウザで確認してnginxのwelcomeページが表示されることなどで、確認をします。

php

php7をインストールします。

インストール

console
# yum install --enablerepo=epel,remi-php70 php php-mbstring php-pear php-fpm php-mcrypt php-mysql

php-fpm

nginxにてphpが使用出来るようにするためにphp-fpmをインストールします。phpのextensionなのでその他のextensionと一緒にインストールしても問題ありません。

インストール

console
$ sudo yum --enablerepo=epel,remi,remi-php70 install php70-php-fpm

設定

/etc/php-fpm.d/www.conf
# vi /etc/php-fpm.d/www.conf

user = nginx
group = nginx

listen = /var/run/php-fpm.sock
listen.owner = nginx
listen.group = nginx

サービス有効化

console
$ sudo systemctl enable php-fpm

サービス起動

console
$ sudo systemctl start php-fpm
$ sudo systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2016-05-26 16:44:29 JST; 9s ago
 Main PID: 4343 (php-fpm)
   Status: "Ready to handle connections"
   CGroup: /system.slice/php-fpm.service
           ├─4343 php-fpm: master process (/etc/php-fpm.conf)
           ├─4345 php-fpm: pool www
           ├─4346 php-fpm: pool www
           ├─4347 php-fpm: pool www
           ├─4348 php-fpm: pool www
           └─4349 php-fpm: pool www

May 26 16:44:28 centos70 systemd[1]: Starting The PHP FastCGI Process Manager...
May 26 16:44:29 centos70 php-fpm[4343]: [26-May-2016 16:44:29] NOTICE: PHP message: PHP Warning:  PHP Sta...ine 0
May 26 16:44:29 centos70 php-fpm[4343]: [26-May-2016 16:44:29] NOTICE: PHP message: PHP Warning:  PHP Sta...ine 0
May 26 16:44:29 centos70 systemd[1]: Started The PHP FastCGI Process Manager.
Hint: Some lines were ellipsized, use -l to show in full.

稼働確認

  • phpのバージョンが7であることを確認します。
console
$ php -v
PHP 7.0.7 (cli) (built: May 25 2016 17:27:34) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
  • phpinfo()を使ってnginxでphpが稼働するか確認します。
phpinfo.php
<?php
    phpinfo();

php-mysqlnd

MariaDBとphpが使用出来るようにするためにphp-mysqlndをインストールします。phpのextensionなのでその他のextensionと一緒にインストールしても問題ありません。

インストール

console
$ sudo yum --enablerepo=epel,remi,remi-php70 install php70-php-mysqlnd

サービス再起動

console
$ sudo systemctl restart php-fpm
$ sudo systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2016-05-30 16:25:08 JST; 6s ago
 Main PID: 5233 (php-fpm)
   Status: "Ready to handle connections"
   CGroup: /system.slice/php-fpm.service
           ├─5233 php-fpm: master process (/etc/php-fpm.conf)
           ├─5234 php-fpm: pool www
           ├─5235 php-fpm: pool www
           ├─5236 php-fpm: pool www
           ├─5237 php-fpm: pool www
           └─5238 php-fpm: pool www

May 30 16:25:08 centos70 systemd[1]: Starting The PHP FastCGI Process Manager...
May 30 16:25:08 centos70 systemd[1]: Started The PHP FastCGI Process Manager.

php extension

インストール

  • extensionの検索をして必要そうなものを見つけます。
console
$ yum --enablerepo=epel,remi,remi-php70 search php70
  • extensionのインストール
console
$ sudo yum --enablerepo=epel,remi,remi-php70 install php70-php-mcrypt php70-php-mbstring php70-php-gd

MariaDB

インストール

console
$ sudo yum install mariadb mariadb-server

サービス有効化

console
$ sudo systemctl enable mariadb

サービス起動

console
$ sudo systemctl start mariadb
$ sudo systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2016-05-30 13:37:20 JST; 32min ago
 Main PID: 1228 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─1228 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─1619 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin...

May 30 13:37:16 centos70 systemd[1]: Starting MariaDB database server...
May 30 13:37:18 centos70 mysqld_safe[1228]: 160530 13:37:18 mysqld_safe Logging to '/var/log/mariadb/maria...og'.
May 30 13:37:18 centos70 mysqld_safe[1228]: 160530 13:37:18 mysqld_safe Starting mysqld daemon with databa...ysql
May 30 13:37:20 centos70 systemd[1]: Started MariaDB database server.
May 30 14:09:40 centos70 systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.

adminerのインストール

  • adminerを入れてみました。phpMyAdminより動作が機敏なこと、1ファイルだけで稼働することが特徴です。
  • ファイルをダウンロードしてサーバのドキュメントルート配下にアップします。 無事にデータベースの内部が参照できたらnginxもMariaDBもphpも動いています…。
37
51
2

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
37
51