nginxをインストールする | ヌキのさくらインターネットVPSでCentOSを構築

ヌキのさくらインターネットVPSでCentOSを構築

CA12年新卒のonukiが

さくらインターネットVPSで
CentOSを構築する手順をちまちまと更新していきたいなって思います。
切りのいいところでまた違うネタにしていこうかと思いますが
しばらくはVPSのお話です。

記事を書きながらの構築は結構時間がかかるw

今回はnginxという高速HTTPアクセラレータというものをインストールしました。
apacheの様にwebサーバとしても利用可能です。
プロキシサーバとしてもよく使われています。
pixivではフロントにnginxがいてnginxのキャッシュにあるデータはそのまま返してくれます。
フロント側からの順番は

nginx-squid-apache-nginxという順番だったと思います(記憶が曖昧ですいません)
squidもプロキシとして使用されています。
pixivの様な大量な画像を配信するシステムでは毎回DBへアクセスして画像を返すよりも
プロキシを利用してキャッシュで画像を返すことが大事だと思っています。
pixivのシステムの詳細はググれば勉強会資料が多くあるのでわかると思います。

私は絵を描くわけではないのでヘビーユーザではないですが、システムはとてもおもしろく
勉強会やSoftware Designなどに記事があれば必ず読みます。

そんな話は別のブログで話そうと思います。

インストールと簡単な設定までnginxについて書きたいと思います。

epelレポジトリにyumでインストールできる最新版のものがあるのでepelレポジトリを追加しておきましょう。
このブログの中にMySQLで5.5を入れた記事でレポジトリの追加方法を書いているのでそこを参考にしてください。

dagレポジトリにもありますが、ちょっと古いのでepelからインストールしましょう。

[root@www ~]#  yum install --enablerepo=remi --enablerepo=epel nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* epel: ftp.kddilabs.jp
* remi: remi-mirror.dedipower.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:0.8.54-1.el5 set to be updated
--> Processing Dependency: libgd.so.2()(64bit) for package: nginx
--> Processing Dependency: libGeoIP.so.1()(64bit) for package: nginx
--> Running transaction check
---> Package gd.x86_64 0:2.0.33-9.4.el5_4.2 set to be updated
---> Package geoip.x86_64 0:1.4.6-1.el5.rf set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
Package Arch Version Repository Size
============================================================================================================================================
Installing:
nginx x86_64 0.8.54-1.el5 epel 389 k
Installing for dependencies:
gd x86_64 2.0.33-9.4.el5_4.2 base 155 k
geoip x86_64 1.4.6-1.el5.rf dag 754 k

Transaction Summary
============================================================================================================================================
Install 3 Package(s)
Upgrade 0 Package(s)

Total download size: 1.3 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): gd-2.0.33-9.4.el5_4.2.x86_64.rpm | 155 kB 00:00
(2/3): nginx-0.8.54-1.el5.x86_64.rpm | 389 kB 00:00
(3/3): geoip-1.4.6-1.el5.rf.x86_64.rpm | 754 kB 00:00
--------------------------------------------------------------------------------------------------------------------------------------------
Total 1.5 MB/s | 1.3 MB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : geoip 1/3
Installing : gd 2/3
Installing : nginx 3/3

Installed:
nginx.x86_64 0:0.8.54-1.el5

Dependency Installed:
gd.x86_64 0:2.0.33-9.4.el5_4.2 geoip.x86_64 0:1.4.6-1.el5.rf

Complete!


単純にこれでインストールは完了です。

設定ファイルは
/etc/nginx/nginx.conf

にあります。

さくらではオリジナルの設定ファイルがデフォルトの設定に置かれています
下記パスに通常のデフォルトの設定ファイルが書かれています。
/etc/nginx/nginx.conf.default

さくらの設定だといろいろ削除されているファイルがあるので
他の機能を利用したい場合は上記ファイルを参考にしてみてください。

私は
/etc/nginx/conf.d/onuki.conf

にバーチャルホストとして設定ファイルを他に書いています。

中身は
server {
listen onukisample.com:80;
server_name onukisample.com onukisample.com;
access_log /var/log/nginx/onukisample.com.access.log main;
error_log /var/log/nginx/onukisample.com.error.log;

location / {
root /var/www/html/onuki/;
index index.php index.html index.htm;
}

location ~ \.php$ {
root /var/www/html/onukisample.com/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/blog.mayochiki.com/$fastcgi_script_name;
include fastcgi_params;
}
}


この様に書いています太字の部分はWordPressを利用するためにphp-fastcgiを使うように設定してあげています。

nginxはデフォルトではphpは使用できないので別でphp-fastcgiを一緒にインストールします。
apacheだとhttp-develとphpをインストールして設定ファイルに追記してあげればいいんですがね・・・
nginxは別プロセスでphp-fastcgiを起動してあげなければなりません。
とりあえず、インストールします

yum install --enablerepo=epel --enablerepo=remi --enablerepo=dag  spawn-fcgi


これでインストールができますが、yumでインストールした時は普通/etc/rc.d/init.d/配下に起動スクリプトがあるが、これは作成されません。
なので自分で起動スクリプトを作成しなければなりません。

 # vi /etc/rc.d/init.d/php-fastcgi
#!/bin/sh
#
# php-cgi - php-fastcgi swaping via spawn-fcgi
#
# chkconfig: - 85 15
# description: Run php-cgi as app server
# processname: php-cgi
# config: /etc/sysconfig/phpfastcgi (defaults RH style)
# pidfile: /var/run/php_cgi.pid
# Note: See how to use this script :
# http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/
# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

spawnfcgi="/usr/bin/spawn-fcgi"
php_cgi="/usr/bin/php-cgi"
prog=$(basename $php_cgi)
server_ip=127.0.0.1
server_port=9000
server_user=nginx
server_group=nginx
#server_childs=5
server_childs=20
pidfile="/var/run/php_cgi.pid"

# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi

start() {
[ -x $php_cgi ] || exit 1
[ -x $spawnfcgi ] || exit 2
echo -n $"Starting $prog: "
daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -f ${php_cgi}
retval=$?
echo
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} $prog -QUIT
retval=$?
echo
[ -f ${pidfile} ] && /bin/rm -f ${pidfile}
return $retval
}

restart(){
stop
sleep 2
start
}

rh_status(){
status -p ${pidfile} $prog
}

case "$1" in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
rh_status;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 3
esac


この通りにコピペして作成すれば起動スクリプトは完成です。

nginxとphp-fastcgiを起動してバーチャルホストの設定のドメインにアクセスすればOKです。
WordPressをDocumentRootに配置して設定できるようになれば完璧です。
私はこれでできました。


結局現在はphp-fastcgiではなくphp-fpmを利用していますがね・・・w
ハートビーツのCTOの馬場さんのブログに早いと書かれているのでやってみたのですが
あんまり差がわからなかったw
ちゃんとチューニングしないとダメですね

細かい設定などはこちらを参考にしてください。
ハイパフォーマンスHTTPサーバ Nginx入門/Clement Nedelcu

¥3,150
Amazon.co.jp