LoginSignup
16
27

More than 5 years have passed since last update.

[さくらVPS]CentOS7にphp7 + php-fpm + nginxをインストールする

Last updated at Posted at 2018-04-02

はじめに

さくらVPSを新しく借りたのでphp7 + php-fpm + nginxの環境をつくる

環境

OS: CentOS Linux release 7.4
WebServer: nginx 1.13.9
DocumentRoot: /var/www/html
Domain: www.example.com

まず

# yum update

EPELリポジトリの追加

# yum -y install epel-release

yum コマンド実行時は自動的にEPELリポジトリが使用されるようになる。
--enablerepo=repo名を指定した時のみ使用するようにしたい場合は
以下を編集する。

/etc/yum.repos.d/epel.repo
[epel]
enabled=1
↓ 変更
enabled=0

remiリポジトリの追加

# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

yum コマンド実行時は自動的にEPELリポジトリが使用されるようになる。
--enablerepo=repo名を指定した時のみ使用するようにしたい場合は
以下を編集する。

/etc/yum.repos.d/remi-safe.repo
[epel]
enabled=1
↓ 変更
enabled=0

Nginx

/etc/yum.repos.d/nginx.repoを作成

/etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
enabled=0
gpgcheck=0

Install

# yum -y --enablerepo=nginx install nginx
# nginx -v 
nginx version nginx/1.13.9

起動設定

# systemctl start nginx
# systemctl enable nginx
# systemctl status nginx

設定ファイルの編集

初期設定ファイルのバックアップをとる

cd /etc/nginx
# cp nginx.conf nginx.conf.org
cd /etc/nginx/conf.d
# cp default.conf default.conf.org

設定ファイルを編集

/etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;
    root   /var/www/html;
    charset UTF-8;
    access_log  /var/log/nginx/www.example.com.access.log  main;
    error_log  /var/log/nginx/www.example.com.error.log;

    location / {
        index index.php index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        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 restart nginx

php, php-fpm

Install

# yum -y install --enablerepo=epel,remi,remi-php72  php php-mbstring php-pdo php-mysqlnd php-fpm php-gd
php -v
PHP 7.2.3

最初にインストールした際に下記エラーが発生したので--enablerpoにepelリポジトリを追加。

エラー: パッケージ: php-7.2.1-1.el7.remi.x86_64 (remi-php72)
要求: libargon2.so.0()(64bit)

初期設定ファイルのバックアップをとる

cd /etc/php-fpm.d
cp www.conf www.conf.org

設定ファイルを編集

/etc/php-fpm.d/www.conf
- user 
+ user nginx
- group
+ group nginx
- listen = 127.0.0.1:9000
+ /var/run/php-fpm/php-fpm.sock
- ;listen.owner = nobody
+ listen.owner = nginx
- ;listen.group = nobody 
+ listen.owner = nginx

php-fpmを再起動

# systemctl restart php-fpm

確認

とりあえずphpinfoを表示させる

/var/www/html/index.php
<?php phpinfo(); ?>

ブラウザでhttp://Domain名
でアクセスしてphpinfoが表示されればOK

参考

CentOS7+nginx+php7+MariaDBの環境構築
sanosoft@ウィキ PHP7.2のインストール

16
27
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
16
27