Nginx与PHP(fastCGI)安装、配置与优化
一、安装环境
1、centos5.5-32
二、利用centos自带的Yum安装、升级抽需的程序库
1、yum�Cyinstallgccgcc-c++autoconflibjpeglibjpeg-devellibpnglibpng-develfreetypefreetype-devellibxml2libxml2-develzlibzlib-develglibcglibc-develglib2glib2-develbzip2bzip2-develncursesncurses-develcurlcurl-devele2fsprogse2fsprogs-develkrb5krb5-devellibidnlibidn-developensslopenssl-developenldapopenldap-develnss_ldapopenldap-clientsopenldap-servers
三、从blog.s135.com下载所需要的软件(注:比较稳定,只允许在本站,或者在linux/unix下通过wget、curl等命令下载以下软件)
Wgethttp://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.15.tar.gz
Wgethttp://blog.s135.com/soft/linux/nginx_php/php/php-5.2.10.tar.gz
Wgethttp://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.10-fpm-0.5.11.diff.gz
Wgethttp://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.1.38.tar.gz
Wgethttp://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.tar.gz
Wgethttp://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
Wgethttp://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
Wgethttp://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
Wgethttp://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
Wgethttp://blog.s135.com/soft/linux/nginx_php/pcre/pcre-7.9.tar.gz
Wgethttp://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.5.3.tar.bz2
Wgethttp://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz
Wgethttp://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
Wgethttp://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.2.2.tgz
1、nginx-0.8.15.tar.gz
2、php-5.2.10.tar.gz
3、php-5.2.10-fpm-0.5.11.diff.gz
4、mysql-5.1.38.tar.gz
5、libiconv-1.13.tar.gz
6、mcrypt-2.5.8.tar.gz
7、mhash-0.9.9.9.tar.gz
8、pcre-7.9.tar.gz
9、eaccelerator-0.9.5.3.tar.bz2
10、PDO_MYSQL-1.0.2.tgz
11、ImageMagick.tar.gz
12、Imagick-2.2.2.tgz
四、安装php5.2.10(fastcgi模式)
1、tar�Czxflibiconv-1.13.tar.gz
cdlibiconv-1.13/
./configure�Cprefix=/usr/local
Make
Makeinstall
Cd../
2、tar�Czxflibmcrypt-2.5.8.tar.gz
cdlibmcrypt-2.5.8/
./configure
Make
Makeinstall
/sbin/ldconfig
Cdlibltdl/
./configure�Cenable-ltdl-install
Make
Makeinstall
Cd../../
3、tar�Czxfmhash-0.9.9.9.tar.gz
cdmhash-0.9.9.9/
./configure
Make&&makeinstall
Cd../
Ln�Cs/usr/local/lib/libmcrypt.la/usr/lib/libmcrypt.la
Ln�Cs/usr/local/lib/libmcrypt.so/usr/lib/libmcrypt.so
Ln�Cs/usr/local/lib/libmcrypt.so.4/usr/lib/libmcrypt.so.4
Ln�Cs/usr/local/lib/libmcrypt.so.4.4.8/usr/lib/libmcrypt.so.4.4.8
Ln�Cs/usr/local/lib/libmhash.a/usr/lib/libmhash.a
Ln�Cs/usr/local/lib/libmhash.la/usr/lib/libmhash.la
Ln�Cs/usr/local/lib/libmhash.so/usr/lib/libmhash.so
Ln�Cs/usr/local/lib/libmhash.so.2/usr/lib/libmhash.so.2
Ln�Cs/usr/local/lib/libmhash.so.2.0.1/usr/lib/libmhash.so.2.0.1
4、tar�Czxfmcrypt-2.6.8.tar.gz
cdmcrypt-2.6.8/
/sbin/ldconfig
./configure
Make&&makeinstall
Cd../
5、编译安装mysql5.1.38
1、groupaddmysql
useradd�Cgmysqlmysql
tar�Czxfmysql-5.1.38.tar.gz
cdmysql-5.1.38/
./configure�Cprefix=/usr/local/webserver/mysql�Cenable-assembler�Cwith-extra-charsets=complex�Cenable-thread-safe-client�Cwith-big-tables�Cwith-readline�Cwith-ssl�Cwith-embedded-server�Cenable-local-infile
--with-plugins=innobase�Cwith-mysqld-ldflags=-all-static�Cwith-client-ldflags=-all-static
Make&&makeinstall
Chown�CRmysql:mysql/usr/local/webserver/mysql
Chmod+w/usr/local/webserver/mysql
/usr/local/mysql/bin/mysql_install_db�Cuser=mysql
Echo“exportPATH=$PATH:/usr/local/webserver/mysql/bin”>>/etc/profile
Source/etc/profile
Cpsupport-files/mysql.server/etc/init.d/mysqld
Chmod700/etc/init.d/mysqld
6、编译安装php5(fastcig模式)
1、tar�Czxfphp-5.2.10.tar.gz
gzip�Ccdphp-5.2.10-fpm-0.5.11.diff.gz|patch�Cdphp-5.2.10�Cp1
cdphp-5.2.10/
./configure�Cprefix=/usr/local/webserver/php�Cwith-config-file-path=/usr/local/webserver/php/etc�Cwith-mysql=/usr/local/webserver/mysql�Cwith-mysqli=/usr/local/webserver/mysql/bin/mysql_config�Cwith-iconv-dir=/usr/local�Cwith-freetype-dir�Cwith-jpeg-dir�Cwith-png-dir�Cwith-zlib�Cwith-libxml-dir=/usr�Cenable-xml�Cdisable-rpath�Cenable-discard-path�Cenable-safe-mode�Cenable-bcmath�Cenable-shmop�Cenable-sysvsem�Cenable-inline-optimization�Cwith-curl�Cwith-curlwrappers�Cenable-mbregex�Cenable-fastcgi�Cenable-fpm�Cenable-force-cgi-redirect�Cenable-mbstring�Cwith-mcrypt�Cwith-gd�Cenable-gd-native-ttf�Cwith-openssl�Cwith-mhash�Cenable-pcntl�Cenable-sockets�Cwith-ldap�Cwith-ldap-sasl�Cwith-xmlrpc�Cenable-zip�Cenable-soap�Cwithout-pear
MakeZEND_EXTRA_LIBS=’-liconv’
Makeinstall
Cpphp.ini-dist/usr/local/webserver/php/etc/php.ini
Cd../
7、编译安装php5扩展模块
1、tar�Cjxfeaccelerator-0.9.5.3.tar.bz2
Cdeaccelerator-0.9.5.3/
/usr/local/webserver/php/bin/phpize
./configure�Cenable-eaccelerator=shared�Cwith-php-config=/usr/local/webserver/php/bin/php-config
Make&&makeinstall
Cd../
2、tar�CzxfPDO_MYSQL-1.0.2.tgz
cdPDO_MYSQL-1.0.2/
/usr/local/webserver/php/bin/phpize
./configure�Cwith-php-config=/usr/local/webserver/php/bin/php-config�Cwith-pdo-mysql=/usr/local/webserver/mysql
Make&&makeinstall
Cd../
3、tar�CzxfImageMagick.tar.gz
cdImageMagick-6.5.1-2/
./configure
Make&&makeinstall
Cd../
4、tar�Czxfimagick-2.2.2.tgz
cdimagick-2.2.2/
/usr/local/webserver/php/bin/phpize
./configure�Cwith-php-config=/usr/local/webserver/php/bin/php-config
Make&&makeinstall
Cd../
5、修改php.ini文件:
手工修改:查找/usr/local/webserver/php/etc/php.ini中的extension_dir=”./”
修改为extension_dir=”/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613”
并在此行后增加以下几行,保存
Extension=”pdo_mysql.so”
Extension=”imagick.so”
再查找output_buffering=off
修改为output_buffering=on
6、配置eaccelerator加速php:
Mkdir�Cp/usr/local/webserver/eaccelerator_cache
Vim/usr/local/webserver/php/etc/php.ini
[eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"#模块的具体路径
eaccelerator.shm_size="64"#共享内存大小
eaccelerator.cache_dir="/usr/local/webserver/eaccelerator_cache"#缓存目录
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"
7、创建www用户和组以供blog.domain.com和www.domain.com两个虚拟主机使用的目录
Groupaddwww
Useradd�Cgwwwwww
Mkdir�Cp/data0/htdocs/blog
Chmod+w/data0/htdocs/blog
Chown�CRwww:www/data0/htdocs/blog
Mkdir�Cp/data0/htdocs/www
Chmod+w/data0/htdocs/www
Chown�CRwww:www/data0/htdocs/www
8、创建php-fpm配置文件(php-fpm是为php打的一个fastcgi管理补丁,其实是不需要,但为了方便,可以平滑变更php.ini配置而无须重启php-cgi)
在原有的配置文件里修改/usr/local/webserver/php/etc/php-fpm.conf
注:如果您安装nginx+php用于程序调试,请将以下的<valuename=”display_errors”>0</value>修改为<valuename=”display_errors”>1</value>,以便显示PHP错误信息,否则,NGINX会报状态为500的空白错误页。
Php-fpm.conf这个配置文件里主要关注一下:
Unixuserofprocesses
<valuename=”user”>www</value>
Unixgroupofprocesses
<valuename=”group”>www</value>
上面的内容需要修改一下用户与组就可以了,这个用户与组跟nginx配置文件里的一样。
<valuename=”max_children”>128</value>
上面的一行内容就用来生成多少子php-cgi进程。
启动php-cgi进程(如果服务器的内存小于3GB,可以只开启64个进程),用户为www:
Ulimit-SHn
/usr/local/webserver/php/sbin/php-fpmstart
注:/usr/local/webserver/php/sbin/php-fpm还有其它参数,包括:start|stop|restart|reload|logrotate,修改php.ini后不重启Php-cgi,重新加载配置文件使用reload
五、安装nginx-0.8.15
1、tar�Czxfpcre-7.9.tar.gz
cdpcre-7.9/
./configure
Make&&makeinstall
Cd../
2、tar�Czxfnginx-0.8.15.tar.gz
cdnginx-0.8.15/
./configure�Cuser=wwwgroup=www�Cprefix=/usr/local/webserver/nginx�Cwith-hhtp_stub_status_module�Cwith-http_ssl_module
Make&&makeinstall
3、创建nginx.conf配置文件,把默认的配置文件给保存一份
Vim/usr/local/webserver/nginx/conf/nginx.conf
Userwwwwww;#运行的用户
Worker_processes1;#启动进程,通常设置成和CPU的数量相等
########全局日志及PID文件
Error_log/data1/logs/nginx_error.logcrit;
Pid/usr/local/webserver/nginx/nginx.pid;
Worker_rlimit_nofile65535;
Events
{
Useepoll;#epoll是多路复用IO(I/OMultiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
Worker_connections65535;#单个后台workerprocess进程的最大并发链接数
}
http
{
Includemime.types;#设定mime类型,类型由mime.type来定义
Default_typeapplication/octet-stream;
Server_names_hash_bucket_size128;
Client_header_buffer_size32k;
Large_client_header_buffers432k;
Client_max_body_size8m;
Sendfileon;
Tcp_nopushon;
Keepalive_timeout60;
Tcp_nodelayon;
Fastcgi_connect_timeout300;
Fastcgi_send_timeout300;
Fastcgi_read_timeout300;
Fastcgi_buffer_size64k;
Fastcgi_buffers464k;
Fastcgi_busy_buffers_size128k;
Fastcgi_temp_file_write_size128k;
Gzipon;
Gzip_min_length1k;
Gzip_buffers416k;
Gzip_http_version1.0;
Gzip_comp_level2;
Gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
Gzip_varyon;
Server
{
Listen80;
Server_nameblog.domain.com;
Indexindex.htmlindex.htmindex.php;
Root/data0/htdocs/blog;
Location~.*\.(php|php5)?$
{
Fastcgi_pass127.0.0.1:9000;
Fastcgi_indexindex.php;
Includefcgi.conf;
}
Location~.*\.(gif|jpg|jpeg|png|tmp|swf)$
{
Expries3d;
}
Location~.*\.(js|css)$
{
1h;
}
log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"';
Access_log/data1/logs/access.logmain;
}
}
2、由于上nginx.conf配置文件里提到了fcgi.conf配置文件,所以需要新建一个,内容如下:
Vi/usr/local/webserver/nginx/conf/fcgi.conf
fastcgi_paramGATEWAY_INTERFACECGI/1.1;
fastcgi_paramSERVER_SOFTWAREnginx;
fastcgi_paramQUERY_STRING$query_string;
fastcgi_paramREQUEST_METHOD$request_method;
fastcgi_paramCONTENT_TYPE$content_type;
fastcgi_paramCONTENT_LENGTH$content_length;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
fastcgi_paramSCRIPT_NAME$fastcgi_script_name;
fastcgi_paramREQUEST_URI$request_uri;
fastcgi_paramDOCUMENT_URI$document_uri;
fastcgi_paramDOCUMENT_ROOT$document_root;
fastcgi_paramSERVER_PROTOCOL$server_protocol;
fastcgi_paramREMOTE_ADDR$remote_addr;
fastcgi_paramREMOTE_PORT$remote_port;
fastcgi_paramSERVER_ADDR$server_addr;
fastcgi_paramSERVER_PORT$server_port;
fastcgi_paramSERVER_NAME$server_name;
fastcgi_paramREDIRECT_STATUS200;
3、启动nginx:
Ulimit�CSHn65535
/usr/local/webserver/nginx/sbin/ngins####-sreload重新加载配置文件
4、配置开机自动启动nginx+php
Vi/etc/rc.local
Ulimit�CSHn65535
/usr/local/webserver/php/sbin/php-fpmstart
/usr/local/webserver/nginx/sbin/ngins