很大程度上参考了张宴老师的文章,但针对最新版本的nginx做了修改,同时做了一些方便的脚本。
张宴老师原文: http://blog.s135.com/nginx_php_v6/
好吧,其实64位、32位都一样....
所有包都在code.google.com或者sourceforge之类的地方寻找尽可能新的版本,名称以及md5如下:
- eaccelerator-0.9.6.1.tar.bz2 32ccd838e06ef5613c2610c1c65ed228
- libiconv-1.13.1.tar.gz 7ab33ebd26687c744a37264a330bbe9a
- libmcrypt-2.5.8.tar.gz 0821830d930a86a5c69110837c55b7da
- mcrypt-2.6.8.tar.gz 97639f8821b10f80943fa17da302607e
- mhash-0.9.9.9.tar.bz2 f91c74f9ccab2b574a98be5bc31eb280
- mysql-5.1.58.tar.gz ae5aef506088e521e4b1cc4f668e96d2
- nginx-1.0.11.tar.gz a41a01d7cd46e13ea926d7c9ca283a95
- pcre-8.10.tar.gz 9524f0ff50b9093c02c81f911e41b066
- php-5.3.8.tar.gz f4ce40d5d156ca66a996dbb8a0e7666a
同时还需要事先保存如下几个文件
#fcgi.conf
#用来进行cgi的解析,也可以参考nginx/conf/fastcgi.conf里面的配置...
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REMOTE_USER $remote_user;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
#nginx.conf
#nginx的主配置文件,当然,不想用这个,也可以用nginx的sample,差不多的
user web web;
worker_processes 10;
error_log /home/web/logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server
{
listen 80;
server_name 127.0.0.1;
index index.html index.htm index.php;
root /home/web/cgi-bin;
#limit_conn crawler 20;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location = /logs/ {
return 404;
}
location ~ .*\.(log|sh|txt|gz|tgz)?$ {
return 404;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /home/web/logs/nginx-access.log access;
}
}
#fastcgi_nginx_ctl.sh
#用来控制nginx和php-fpm启动、停止、重启的脚本,简单理解就是放在/etc/init.d里的东西,呵呵
#!/bin/sh
source /etc/profile
cmd=$1
php_fpm_bin_path="/usr/local/php/sbin/php-fpm"
php_fpm_conf_path="/usr/local/php/lib"
nginx_bin_path="/usr/local/nginx/sbin/nginx"
nginx_pid_path="/usr/local/nginx/nginx.pid"
case "$cmd" in
'start')
ulimit -SHn 65535
$php_fpm_bin_path -c $php_fpm_conf_path/php.ini -y $php_fpm_conf_path/php-fpm.conf -t
if [ "$?" -eq "0" ]
then
$php_fpm_bin_path -c $php_fpm_conf_path/php.ini -y $php_fpm_conf_path/php-fpm.conf
else
echo "php-fpm start test failed!"
fi
$nginx_bin_path -t
if [ "$?" -eq "0" ]
then
$nginx_bin_path
else
echo "nginx start test failed!"
fi
;;
'stop')
kill -QUIT `cat /usr/local/php/var/run/php-fpm.pid`
kill -QUIT `cat $nginx_pid_path`
;;
'reload')
$php_fpm_bin_path -c $php_fpm_conf_path/php.ini -y $php_fpm_conf_path/php-fpm.conf -t
if [ "$?" -eq "0" ]
then
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
else
echo "php-fpm reload test failed!"
fi
$nginx_bin_path -t
if [ "$?" -eq "0" ]
then
$nginx_bin_path -s reload
else
echo "nginx reload test failed!"
fi
;;
*)
# usage
echo "Usage: $0 {start|stop|reload}"
exit 1
;;
esac
#cut_log.sh
#用来每天切割nginx访问日志的,没有也无所谓,只是nginx不像apache那样有自己的切割工具而已。
#而且需要注意,nginx有个特点是,如果不reload,日志就算改名也没有用哦,不管改成什么,都会继续写在旧文件里写日志,所以要reload一下。
#!/bin/sh
cd /home/web/logs/;
mv -f nginx-access.log `date +%u -d "1 day ago"`.nginx-access.log;
/usr/local/nginx/sbin/nginx -s reload
#php.ini
#这个比较烦,大家可以等到php安装完成后,复制php的sample过去,这里我们只说一下需要新增或修改的内容
cgi.fix_pathinfo=0
error_log = /home/web/logs/php_errors.log
[eaccelerator]
zend_extension="/usr/lib/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/tmp"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
#php-fpm.conf
#这个也比较烦,同上,大家可以等安装完后,复制php的sample过去,这里给出一下需要修改的部分,特别是pm那几项,要根据自己的实际压力情况修改
#注意,旧版本的php-fpm配置文件是xml格式的,新版本的改为conf格式,这里与张宴老师的文章里没有提到
error_log = /home/web/logs/php-fpm.log
[global]
user = web
group = web
listen = 127.0.0.1:9000
pid = run/php-fpm.pid
pm = dynamic
pm.max_children = 8
pm.max_spare_servers = 4
pm.min_spare_servers = 2
pm.start_servers = 4
slowlog = /home/web/logs/$pool.log.slow
准备完成的话,就可以开工了
######################## START ##########################
#下面部分保存为install_nginx.sh
#!/bin/sh
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../
tar -jxvf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9/
./configure
make
make install
cd ../
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ../
groupadd mysql
useradd -g mysql -s "/sbin/nologin" mysql
tar zxvf mysql-5.1.58.tar.gz
cd mysql-5.1.58
./configure --prefix=/usr/local/mysql --localstatedir=/home/mysql --with-charset=gbk --with-extra-charsets=all --enable-thread-safe-client --with-plugins=partition,innobase,myisam,innodb_plugin,myisammrg,heap
make
make install
chown mysql:mysql -R /usr/local/mysql/
cd ../
tar zxvf php-5.3.8.tar.gz
cd php-5.3.8/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/lib --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --enable-fpm --enable-mbstring --with-mcrypt --with-mhash --enable-pcntl --enable-soap --with-gd=/usr/local/libgd --enable-gd-native-ttf --enable-gd-jis-conv --with-freetype-dir=/usr/local/freetype --with-jpeg-dir=/usr/local/libjpeg --with-png-dir=/usr --with-zlib-dir=/usr/local/lib --enable-xml --enable-sockets --with-pdo-mysql=/usr/local/mysql
make ZEND_EXTRA_LIBS='-liconv'
make install
cd ../
tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
cp /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so /usr/lib/
cd ../
mv php.ini /usr/local/php/lib/
groupadd web
useradd -g web -s "/sbin/nologin" web
mv php-fpm.conf /usr/local/php/lib/
tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
make && make install
cd ../
tar -zxvf nginx-1.0.11.tar.gz
cd nginx-1.0.11
./configure --user=web --group=web --prefix=/usr/local/nginx --with-http_stub_status_module
make && make install
cd ../
mv nginx.conf /usr/local/nginx/conf/
mv fcgi.conf /usr/local/nginx/conf/
#验证:
cat /usr/local/mysql/bin/mysqlbug | grep CONFIGURE_LINE
/usr/local/php/bin/php -i | grep configure
/usr/local/nginx/sbin/nginx -V
mv fastcgi_nginx_ctl.sh /home/
chmod +x /home/fastcgi_nginx_ctl.sh
mkdir /home/web/logs
chown web.web /home/web/logs
mv cut_log.sh /home/web/logs/
chmod +x /home/web/logs/cut_log.sh
######################## END ##########################
然后将所有的tar.gz,以及刚才保存的文件,修改的php的配置文件放在一起,例如/root/nginx里
执行:
chmod u+x install_nginx.sh
./install_nginx.sh > test.log
估计要等10分钟左右,出去放放风,可能会看到很多warning,反正只要没有严重报错就行了。
脚本运行完了,就算是装完了,但还有个小地方建议改一下。
nginx有个特点,会有大量的TIME_WAIT,建议修改内核参数来调整
vi /etc/sysctl.conf
添加
#if lots of TIME_WAIT , set tcp_timestamps = 1 , But be careful!
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.netfilter.ip_conntrack_max = 6553600
注意tcp_timestamps一定要等于1,recycle和reuse才会生效,这点网上的很多文章都没有提到,张宴老师也是.....
然后没啥了
<?php
phpinfo();
?>
保存成index.php,扔到/home/web/cgi-bin里
chown web.web /home/web -R
chmod u+x /home/web -R
/home/fastcgi_nginx_ctl.sh start
然后访问你的http://ip 去看看成功否吧,如果不行,去/home/web/logs 里看看error的日志,根据里面的信息调整。