LoginSignup
35
35

More than 5 years have passed since last update.

AmazonLinuxにnginx+PHP5.6+PHP-fpm+PHP-redis環境を作る

Posted at

AmazonLinuxにnginx+PHP5.6+PHP-fpm+PHP-redis環境を作ったのでその時のメモ。なお、php-fpmはsocket接続で、PHP-redisはAmazonLinuxリポジトリになかったのでremiリポジトリからダウンロードした

環境

  • AmazonLinux
  • nginx(AmazonLinuxリポジトリ)
  • PHP5.6(AmazonLinuxリポジトリ)
  • PHP-fpm(AmazonLinuxリポジトリ)
  • PHP-redis(remiリポジトリ)

参考

nginx

ここはざっくり記載

# install
$sudo yum install nginx -y
# start service
$sudo service nginx start
# confirm web page can be downloaded
$wget http://localhsot
# set run level
$sudo chkconfig nginx on
$sudo chkconfig --list nginx

PHP5.6のインストール及びPHP-fpmでの接続

参考にさせて頂いたサイトとほぼ同じ記載ですが、パスが微妙に違うので記載します。なお、ドキュメントルートは/var/www/とする設定です

PHP5.6及びphp-fpmのインストール

$sudo yum install php56 php56-fpm -y

PHPの情報を取得するためにのphpを作成

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

php-fpmの設定変更。userとgroupをapacheからnginxに変更

/etc/php-fpm-5.6.d/www.conf
- user = apache
+ user = nginx

- group = apache
+ group = nginx

nginxの設定変更

/etc/nginx/nginx.conf
  http {
-   index index.html index.htm;
+   index index.php
      server {
-       root /usr/share/nginx/html; 
+       root /var/www;

-       #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           /var/www;
+       fastcgi_pass   127.0.0.1:9000;
+       fastcgi_index  index.php;
+       fastcgi_param  SCRIPT_FILENAME /var/www$fastcgi_script_name;
+       include        fastcgi_params;
+     }

php-fpmの再起動、及び自動起動設定

$sudo service php-fpm start
$sudo chkconfig php-fpm on
$sudo chkconfig php-fpm --list php-fpm

nginxの再起動

$sudo service nginx restart

設定の確認

$curl -s http://localhost/phpinfo.php |grep "PHP Version"
<tr><td class="e">PHP Version </td><td class="v">5.6.8 </td></tr>

PHP Version 5.6.8となっており、PHP5.6系が利用されている事が確認できます。

nginxとphp-fpmの接続をsocketにする

こちらも参考に記載したものとほぼ同じ内容です。nginxとphp-fpmの接続をsocketで実施します

php-fpmの設定変更

/etc/php-fpm-5.6.d/www.conf
-   listen = 127.0.0.1:9000
+   listen =  /var/run/php-fpm/php-fpm.sock

-   ;listen.owner = nobody
-   ;listen.group = nobody
+   listen.owner = nginx  
+   listen.group = nginx

nginxの設定変更

/etc/nginx/nginx.conf
-   fastcgi_pass   127.0.0.1:9000;
+   fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

サービス再起動

$sudo service php-fpm restart
$sudo service nginx restart

設定の確認

$curl -s http://localhost/phpinfo.php |grep "PHP Version"

先ほどと同じように取得できればOKです

PHP-redisのインストール

2015/5/28現在、AmazonLinuxリポジトリにPHP56-redisのパッケージがありませんでした。(PHP55はありますが。。。)

上記より、AmazonLinuxリポジトリ以外のものを利用する必要があり、本例ではremiリポジトリを利用します。

まず、パッケージインストール時に必要となるSCL-Utilというパッケージをrpmでインストールします。

$wget http://mirror.centos.org/centos/6/SCL/x86_64/scl-utils/scl-utils-20120927-11.el6.centos.alt.x86_64.rpm
$sudo rpm -ivh scl-utils-20120927-11.el6.centos.alt.x86_64.rpm

また、その他必要なパッケージを事前にインストールしておきます。

$sudo yum install php56-pecl-igbinary

次にremiリポジトリを追加します。

$wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
$sudo rpm -ivh remi-release-6.rpm

これでremiリポジトリが利用できるようになったのでphp-redisをインストールを行います。

$sudo yum --enablerepo=remi,remi-php56 install php56-php-pecl-redis.x86_64

インスールされると以下にsoファイルが存在するかと思います。

$ls /opt/remi/php56/root/usr/lib64/php/modules/redis.so

AmaozonLinux経由でインストールしたモジュール群は/usr/lib64/php/5.6/modules/に存在するのでこちらにシンボリックリンクを貼ります。

$sudo ln -s /opt/remi/php56/root/usr/lib64/php/modules/redis.so /usr/lib64/php/5.6/modules/redis.so

最後に以下のようなredisのiniファイルを配置します。

/etc/php-5.6.d/50-redis.ini
; Enable redis extension module
extension = redis.so

; phpredis can be used to store PHP sessions.
; To do this, uncomment and configure below

; RPM note : save_handler and save_path are defined
; for mod_php, in /etc/httpd/conf.d/php.conf
; for php-fpm, in /opt/remi/php56/root/etc/php-fpm.d/*conf

;session.save_handler = redis
;session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeout=2.5, tcp://host3:6379?weight=2"

準備が完了したので、サービスを再起動して確認します。

$sudo service php-fpm restart
$sudo service nginx restart

phpinfoを確認してredisのモジュールが読み込まれているか確認します。

$curl -s http://localhost/phpinfo.php |grep redis
/etc/php-5.6.d/50-redis.ini,
<h2><a name="module_redis">redis</a></h2>
<tr><td class="e">Registered save handlers </td><td class="v">files user redis  </td></tr>
This program is free software; you can redistribute it and/or modify it under the terms of the PHP License as published by the PHP Group and included in the distribution in the file:  LICENSE

redisのモジュールが呼ばれている事が確認できました。

php-redisをビルドする場合

今回はremiリポジトリを使いしましたが、ビルドする方法も記載します。

ビルド時に必要なものを事前にインストールします。

$sudo yum groupinstall "Development Tools"
$sudo yum install php56-devel

ソースコードをダウンロード

$git clone https://github.com/phpredis/phpredis.git

Githubページに記載してあるようにビルドしてみます

$phpize
$./configure
$make
$sudo make install
Installing shared extensions:     /usr/lib64/php/5.6/modules/

ビルドが終わると/usr/lib64/php/5.6/modules/にsoファイルが配置されます。あとはredis用のiniファイルを配置すればOKです。

35
35
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
35
35