LoginSignup
39
38

More than 5 years have passed since last update.

virtualbox(centos)でnginx+php-fpm設定メモ(virturalhostの設定まで)

Last updated at Posted at 2013-02-24

環境 centos6.3(ゲストOS)macosx10.8(ホストOS)

nginxを以前入れたのだが肝心のphpの設定をまだ指定なかったので設定のメモ。
※上記エントリーと関連性がありません。単独でphp-fpmをインストールした場合の設定メモです。
ついでに普段複数のサイトを開発する場合に必要なvirtualhostの設定まで

注意事項

  • nginxがインストール済みの状態であること。インストールをしていない場合はvirtualbox(centos)でnginxインストールメモを参照してください。
  • ここから先は全てroo(権限)ユーザーで実行しています
  • 開発環境構築の話です。本番環境などはもっと詳しいブログをみつけてください。

参考にしたサイト

php-fpmのインストール

centos標準のリポジトリにはないのでremiリポジトリから取得する
remiのリポジトリの設定がない場合は外部リポジトリの追加(remi,epel)を参照してください。

yum --enablerepo=remi install php-fpm


Loaded plugins: fastestmirror, priorities, security
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
135 packages excluded due to repository priority protections
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php-fpm.x86_64 0:5.4.12-1.el5.remi will be installed
--> Processing Dependency: php-common = 5.4.12-1.el5.remi for package: php-fpm-5.4.12-1.el5.remi.x86_64
--> Running transaction check
---> Package php-common.x86_64 0:5.4.12-1.el5.remi will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================
 Package                 Arch                Version                         Repository         Size
=====================================================================================================
Installing:
 php-fpm                 x86_64              5.4.12-1.el5.remi               remi              1.5 M
Installing for dependencies:
 php-common              x86_64              5.4.12-1.el5.remi               remi              1.0 M

Transaction Summary
=====================================================================================================
Install       2 Package(s)

Total download size: 2.5 M
Installed size: 10 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): php-common-5.4.12-1.el5.remi.x86_64.rpm                                | 1.0 MB     00:04     
(2/2): php-fpm-5.4.12-1.el5.remi.x86_64.rpm                                   | 1.5 MB     00:04     
-----------------------------------------------------------------------------------------------------
Total                                                                259 kB/s | 2.5 MB     00:09     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : php-common-5.4.12-1.el5.remi.x86_64                                               1/2 

WARNING : These php-* RPM are not official Fedora/Redhat build and
overrides the official ones. Don't file bugs on Fedora Project nor Redhat.

Use dedicated forums http://forums.famillecollet.com/

  Installing : php-fpm-5.4.12-1.el5.remi.x86_64                                                  2/2 
  Verifying  : php-common-5.4.12-1.el5.remi.x86_64                                               1/2 
  Verifying  : php-fpm-5.4.12-1.el5.remi.x86_64                                                  2/2 

Installed:
  php-fpm.x86_64 0:5.4.12-1.el5.remi                                                                 

Dependency Installed:
  php-common.x86_64 0:5.4.12-1.el5.remi                                                              

Complete!

入った。

php-fpmの確認

バージョン確認

php-fpm -v
PHP 5.4.12 (fpm-fcgi) (built: Feb 20 2013 14:06:12)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

php-fpmの起動

/etc/init.d/php-fpm start
php-fpm を起動中: 

nginxの設定

nginxにPHPの設定を追加しましょう

/etc/nginx/conf.d/default.conf
vim /etc/nginx/conf.d/default.conf



server {
    listen       80;
    server_name  lo.test.org;
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/web/lo.test.org/www$fastcgi_script_name;
        include        fastcgi_params;
    }
}

server {
    listen       80;
    server_name  lo.sample.org;
    root   /home/web/lo.sample.org/www;
    index  index.php index.html index.htm;
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /home/web/lo.sample.org/www$fastcgi_script_name;
        include        fastcgi_params;
    }
}

serverディレクティブにそれぞれ server_nameを追加してserverディレクティブのlocationの項目にroot(apacheで言うドキュメントルート)を追加する。
fastcgi_paramにはドキュメントルート+$fastcgi_script_nameを追加する

nginxの起動

apacheと一緒

/etc/init.d/nginx restart


httpd を停止中:                                            [  OK  ]
nginx を起動中:                                            [  OK  ]

動作確認用のphpファイルを設置

いつもどおりで

ドキュメントルート/phpinfo.php
<?php
phpinfo();

上記の設定の場合は
/home/web/lo.sample.org/www/phpinfo.php
/home/web/lo.test.org/www/phpinfo.php
の2つに設定してください。

動作確認

上記の設定の場合は
http://lo.sample.org/phpinfo.php
http://lo.test.org/phpinfo.php
でphpinfoの中身が見えればOK!!

一番php-fpmがイマイチピンとこなくてnginxの設定に億劫でしたがやってみたら意外と簡単でしたのでお試しあれ

39
38
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
39
38