Backup of blog/2010-11-04 (1) Back


Back to History list

#navi(../)
*nginx+php-fpm導入 [#qe87ac18]
移転してから自分でサーバーの中身をいじるようになったためか、ちょっとでも重くなるとすぐにWebminで負荷を見るようになった。まぁ、それ以前に設定が面倒だからとPHPをApacheモジュールで使っていたので重いのも当然である。で、CGIモードでPHPを動かす方法を調べていたのだが、そもそもApacheに拘る理由とはなんだろうか?と考えた。HTTPデーモンと聞くと無意識にApacheと考えてしまいそれ以外、見落としているプログラムがあるかもしれない。そういえば、昔Windowsでサーバーひらいている人はAN-HTTPD使ってた気がするし、MacはMacでOS標準のWeb共有というプログラムでWebサーバー開いていたような。

というわけで、他のHTTPDサーバーについて調べてみた。すると、lighttpdというのとnginxというのが浮かび上がった。どちらも共通しているのが、PHPを別プロセスで動かしているところである。どっちにするか迷ったが、開発背景と、コンフィグファイルの構文から後者のnginxを選ぶことにした。なんと、nginxはロシア製である。どうやら、ロシアの開発者が自社のポータルサイト向けに作ったHTTPDサーバーらしい。最初から設計方針がはっきりしている点がポイント高い。ちsourceForgeや最近話題のGitHubなどで使われているらしい。まぁ、弱小個人サイトに導入するほどのものではないが、どうせやるならってことで。

まずは、Ubuntuにインストール。例によってapt-getで。
 apt-get install nginx
すると、/etc/nginx以下に設定ファイルが置かれる。

まずは、nginx.confの設定。PukiWiki Adv.の開発でお世話になっている[[HTML5 ★ Boilerplate>http://html5boilerplate.com/]]の[[推奨nginx.conf>https://github.com/paulirish/html5-boilerplate/blob/master/nginx.conf]]を参考に設定を組む。この推奨設定は、Yslowのガイドラインに基づいた設定になっている。

nginx.conf
#sh(){{
user				www-data;
worker_processes	1;

error_log	/var/log/nginx/error.log;
pid			/var/run/nginx.pid;

events {
	# When you need > 8000 * cpu_cores connections, you start optimizing
	# your OS, and this is probably the point at where you hire people
	# who are smarter than you, this is *a lot* of requests.
	worker_connections 8000;

	# This sets up some smart queueing for accept(2)'ing requests
	# Set it to "on" if you have > worker_processes
	accept_mutex off;

	# These settings are OS specific, by defualt Nginx uses select(2),
	# however, for a large number of requests epoll(2) and kqueue(2)
	# are generally faster than the default (select(2))
	# use epoll; # enable for Linux 2.6+
	# use kqueue; # enable for *BSD (FreeBSD, OS X, ..)
}

http {
	# Set the mime-types
	include	  	/etc/nginx/mime.types;

	# And the fallback mime-type
	# default_type application/octet-stream;

	# Format for our log files
	log_format main '$remote_addr - $remote_user [$time_local] $status '
		'"$request" $body_bytes_sent "$http_referer" '
		'"$http_user_agent" "$http_x_forwarded_for"';

	# Click tracking!
	access_log	/var/log/nginx/access.log;

	# ~2 seconds is often enough for HTML/CSS, but connections in
	# Nginx are cheap, so generally it's safe to increase it
	keepalive_timeout 5;

	# You usually want to serve static files with Nginx
	sendfile on;

	tcp_nopush on; # off may be better for Comet/long-poll stuff
	tcp_nodelay off; # on may be better for Comet/long-poll stuff

	# Enable Gzip
	gzip on;
	gzip_http_version 1.0;
	gzip_comp_level 2;
	gzip_min_length 1100;
	gzip_buffers 4 8k;
	gzip_proxied any;
	gzip_types text/plain text/xml application/xml application/xml+rss text/css text/javascript application/javascript application/json;

	gzip_static on;

	gzip_proxied expired no-cache no-store private auth;
	gzip_disable "MSIE [1-6]\.";
	gzip_vary on;

	upstream backend {
#		server	127.0.0.1:9000;
		server	unix:/var/run/php-fpm.socket;
	}

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}
}}