読者です 読者をやめる 読者になる 読者になる

kagasu’s blog

備忘録

nginxとphp-fpmを使ってfastcgi_cacheする(PHP5.5)

環境

[root@ ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

手順

rpm -ivh http://rpms.famillecollet.com/enterprise/7/remi/x86_64/remi-release-7.1-3.el7.remi.noarch.rpm
yum -y install --enablerepo=remi --enablerepo=remi-php55 php php-fpm php-opcache php-devel php-mbstring php-mcrypt php-mysql php-cli php-xml php-gd
yum -y install nginx
vim /etc/php-fpm.d/www.conf

user = nginx
group = nginx
vim /etc/php-fpm.d/www.conf

pm = dynamic
dm.max_children = 100
pm.start_servers = 10
pm.min_spare_servers = 5
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --reload
vim /etc/php.ini

default_charset = "UTF-8"
date.timezone = "Asia/Tokyo"
[mbstring]
mbstring.language = Japanese
vim /etc/nginx/conf.d/hoge.conf

server {
  listen 80;
  server_name hoge.com;
  root /var/www/html/hoge;
  index index.php index.html;

  fastcgi_cache_key $scheme://$host$request_uri$is_args$args;
  fastcgi_cache CACHE;
  fastcgi_cache_valid 200 30m; # HTTPステータスコード200は30分間キャッシュする
  fastcgi_cache_valid any 1d; # その他は1日

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }
  
  location ~ \.php$ {
	try_files                $uri = 404;
	include                  /etc/nginx/fastcgi_params;
	fastcgi_pass            127.0.0.1:9000;
	#fastcgi_pass             unix:/var/run/php-fpm.sock;

	fastcgi_index            index.php;
	fastcgi_split_path_info  ^(.+\.php)(/.+)$;

	fastcgi_param            SCRIPT_FILENAME $document_root$fastcgi_script_name;
	fastcgi_param            PATHINFO        $fastcgi_path_info;
	fastcgi_param            PATH_TRANSLATED $document_root$fastcgi_path_info;

	fastcgi_buffers 8 128k;
	fastcgi_buffer_size 256k;
	fastcgi_read_timeout 60;

  } 
}
vim /etc/nginx/nginx.conf

http {
  ...
  
  fastcgi_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=CACHE:4m;
}

server { ... } を削除して以下を追加
server {
  listen 80 default_server;
  root /var/www/html;
  index index.php index.html;

  location ~ \.php$ {
     fastcgi_pass 127.0.0.1:9000;
     fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
     fastcgi_param PATH_INFO $fastcgi_script_name;
     include fastcgi_params;
  }
}
mkdir -p /usr/local/nginx/cache
chkconfig nginx on
chkconfig php-fpm on
systemctl start nginx
systemctl start php-fpm