LoginSignup
82

More than 3 years have passed since last update.

CentOSにnginx, php-fpm, mysqlの環境を構築する1

Last updated at Posted at 2014-05-19

centosのバージョン

# cat /etc/redhat-release
CentOS release 6.5 (Final)

nginxのインストール

公式ページ:公式ページ

nginxのためのレポジトリの登録

# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm を取得中
警告: /var/tmp/rpm-tmp.96TgxU: ヘッダ V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
準備中...                ########################################### [100%]
   1:nginx-release-centos   ########################################### [100%]

/etc/yum.repos.dにnginx.repoができていることを確認する。

インストール

# yum install nginx
# nginx -v
  nginx version: nginx/1.6.0

nginxでbasic認証を設定する

■参考
http://heartbeats.jp/hbblog/2012/02/nginx03.html

 htpasswd -cm .htpasswd username

nginx.confの設定

基本的な設定は/etc/nginx/nginx.congに記載する。
今回のサーバ設定は/etc/nginx/conf.dに個別におく。
デフォルトでドキュメントルートなどは設定してあるので、
ポートのみ変更してアクセスしてみる。
/usr/share/nginx/html配下のindex.html(nginxのwelcomeページ)
が表示されればOK。

つぎー。

php、 php-fpm, mysqlのインストール

これだけインストール済みだった

#yum list installed | grep php

php-cli.x86_64          5.3.3-27.el6_5  @updates
php-common.x86_64       5.3.3-27.el6_5  @updates
php-domxml-php4-php5.noarch
php-jsonlint.noarch     1.1.2-1.el6     @epel
php-mcrypt.x86_64       5.3.3-3.el6     @epel
php-pdo.x86_64          5.3.3-27.el6_5  @updates
php-pear.noarch         1:1.9.4-4.el6   @base
php-xml.x86_64          5.3.3-27.el6_5  @updates

念のため一旦全部削除

yum remove 'php-*'

消えた。怖い。

remi リポジトリ追加

rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
vim /etc/yum.repos.d/remi.repo

remiの設定を新規で追加する

/etc/yum.repos.d/remi.repo
[remi]
name=Les RPM de remi pour Enterprise Linux 5 - $basearch
baseurl=http://rpms.famillecollet.com/el5.$basearch/
http://iut-info.univ-reims.fr/remirpms/el5.$basearch/
enabled=0
priority=1

インストールする(いつかソースからしてみたい)

yum --enablerepo=remi install mysql mysql-server  php php-fpm php-devel php-cli php-xml php-mysql php-mbstring php-gd

続いてphp-fpmの設定

php-fpmの設定

/etc/php-fpm.d/www.confが設定ファイルになっている。

起動ユーザをnginxにする

user = nginx
group = nginx

待ち受けポートを変更

listen = 127.0.0.1:9050

netstat -taponで確認

tcp        0      0 127.0.0.1:9050              0.0.0.0:*                   LISTEN      1402/php-fpm        off (0.00/0/0)

おお、いけてる。
ドキュメントルート配下にindex.phpを置いて

index.php
<?php
phpinfo();

http://XXXX/index.phpにアクセスしてみる。
エラー。
nginxのコンフィグを確認する。
pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
というコメント配下のコメントアウトを外してみる。

    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9050;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
    }

エラ-。なぜに。
ログを見る。

nginx/error.log
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

参考URL:http://tonby.sakura.ne.jp/?p=953
参考URL:http://nginx.org/ja/docs/http/request_processing.html

rootとfastcgi_paramの設定が悪かったみたい。

fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;となっていたが、
/scriptsフォルダなんてないw

ドキュメントルート配下の.phpをみてほしいので、
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
に変更。再度アクセス。エラー。なんでじゃい。

今度はドキュメントルートの設定が悪いみたい。

location / {}にてrootを設定していたので、
location ~ \.php$ {}にて、$document_rootを参照できると思っていたが違った。
試しにログに$document_rootを吐くようにしてみたら、/etc/nginx/htmlと吐かれていた。
これは意図しないドキュメントルート。

各ロケーションで参照するための$document_rootはserver{}直下でする必要があるみたい。
よって、以下のように変更。

nginx.confg
server {
   root   /usr/share/nginx/html;

   location ~ \.php$ {
        #root           $document_root;
        fastcgi_pass   127.0.0.1:9050;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

やっとphpInfoがみれたw
今日はここまで。次は、mysqlを設定をしてみる。

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
82