您的位置:首页 > 运维架构 > Nginx

Nginx与PHP(fastCGI)安装、配置与优化

2013-08-30 10:13 435 查看
Nginx与PHP(fastCGI)安装、配置与优化一、安装环境1、centos5.5-32二、利用centos自带的Yum安装、升级抽需的程序库1、yum–yinstallgccgcc-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.gzWgethttp://blog.s135.com/soft/linux/nginx_php/php/php-5.2.10.tar.gzWgethttp://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.10-fpm-0.5.11.diff.gzWgethttp://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.1.38.tar.gzWgethttp://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.tar.gzWgethttp://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gzWgethttp://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gzWgethttp://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgzWgethttp://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gzWgethttp://blog.s135.com/soft/linux/nginx_php/pcre/pcre-7.9.tar.gzWgethttp://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.5.3.tar.bz2Wgethttp://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgzWgethttp://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gzWgethttp://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.2.2.tgz1、nginx-0.8.15.tar.gz2、php-5.2.10.tar.gz3、php-5.2.10-fpm-0.5.11.diff.gz4、mysql-5.1.38.tar.gz5、libiconv-1.13.tar.gz6、mcrypt-2.5.8.tar.gz7、mhash-0.9.9.9.tar.gz8、pcre-7.9.tar.gz9、eaccelerator-0.9.5.3.tar.bz210、PDO_MYSQL-1.0.2.tgz11、ImageMagick.tar.gz12、Imagick-2.2.2.tgz四、安装php5.2.10(fastcgi模式)1、tar–zxflibiconv-1.13.tar.gzcdlibiconv-1.13/./configure–prefix=/usr/localMakeMakeinstallCd../2、tar–zxflibmcrypt-2.5.8.tar.gzcdlibmcrypt-2.5.8/./configureMakeMakeinstall/sbin/ldconfigCdlibltdl/./configure–enable-ltdl-installMakeMakeinstallCd../../3、tar–zxfmhash-0.9.9.9.tar.gzcdmhash-0.9.9.9/./configureMake&&makeinstallCd../Ln–s/usr/local/lib/libmcrypt.la/usr/lib/libmcrypt.laLn–s/usr/local/lib/libmcrypt.so/usr/lib/libmcrypt.soLn–s/usr/local/lib/libmcrypt.so.4/usr/lib/libmcrypt.so.4Ln–s/usr/local/lib/libmcrypt.so.4.4.8/usr/lib/libmcrypt.so.4.4.8Ln–s/usr/local/lib/libmhash.a/usr/lib/libmhash.aLn–s/usr/local/lib/libmhash.la/usr/lib/libmhash.laLn–s/usr/local/lib/libmhash.so/usr/lib/libmhash.soLn–s/usr/local/lib/libmhash.so.2/usr/lib/libmhash.so.2Ln–s/usr/local/lib/libmhash.so.2.0.1/usr/lib/libmhash.so.2.0.14、tar–zxfmcrypt-2.6.8.tar.gzcdmcrypt-2.6.8//sbin/ldconfig./configureMake&&makeinstallCd../5、编译安装mysql5.1.381、groupaddmysqluseradd–gmysqlmysqltar–zxfmysql-5.1.38.tar.gzcdmysql-5.1.38/./configure–prefix=/usr/local/webserver/mysql–enable-assembler–with-extra-charsets=complex–enable-thread-safe-client–with-big-tables–with-readline–with-ssl–with-embedded-server–enable-local-infile--with-plugins=innobase–with-mysqld-ldflags=-all-static–with-client-ldflags=-all-staticMake&&makeinstallChown–Rmysql:mysql/usr/local/webserver/mysqlChmod+w/usr/local/webserver/mysql/usr/local/mysql/bin/mysql_install_db–user=mysqlEcho“exportPATH=$PATH:/usr/local/webserver/mysql/bin”>>/etc/profileSource/etc/profileCpsupport-files/mysql.server/etc/init.d/mysqldChmod700/etc/init.d/mysqld6、编译安装php5(fastcig模式)1、tar–zxfphp-5.2.10.tar.gzgzip–cdphp-5.2.10-fpm-0.5.11.diff.gz|patch–dphp-5.2.10–p1cdphp-5.2.10/./configure–prefix=/usr/local/webserver/php–with-config-file-path=/usr/local/webserver/php/etc–with-mysql=/usr/local/webserver/mysql–with-mysqli=/usr/local/webserver/mysql/bin/mysql_config–with-iconv-dir=/usr/local–with-freetype-dir–with-jpeg-dir–with-png-dir–with-zlib–with-libxml-dir=/usr–enable-xml–disable-rpath–enable-discard-path–enable-safe-mode–enable-bcmath–enable-shmop–enable-sysvsem–enable-inline-optimization–with-curl–with-curlwrappers–enable-mbregex–enable-fastcgi–enable-fpm–enable-force-cgi-redirect–enable-mbstring–with-mcrypt–with-gd–enable-gd-native-ttf–with-openssl–with-mhash–enable-pcntl–enable-sockets–with-ldap–with-ldap-sasl–with-xmlrpc–enable-zip–enable-soap–without-pearMakeZEND_EXTRA_LIBS=’-liconv’MakeinstallCpphp.ini-dist/usr/local/webserver/php/etc/php.iniCd../7、编译安装php5扩展模块1、tar–jxfeaccelerator-0.9.5.3.tar.bz2Cdeaccelerator-0.9.5.3//usr/local/webserver/php/bin/phpize./configure–enable-eaccelerator=shared–with-php-config=/usr/local/webserver/php/bin/php-configMake&&makeinstallCd../2、tar–zxfPDO_MYSQL-1.0.2.tgzcdPDO_MYSQL-1.0.2//usr/local/webserver/php/bin/phpize./configure–with-php-config=/usr/local/webserver/php/bin/php-config–with-pdo-mysql=/usr/local/webserver/mysqlMake&&makeinstallCd../3、tar–zxfImageMagick.tar.gzcdImageMagick-6.5.1-2/./configureMake&&makeinstallCd../4、tar–zxfimagick-2.2.2.tgzcdimagick-2.2.2//usr/local/webserver/php/bin/phpize./configure–with-php-config=/usr/local/webserver/php/bin/php-configMake&&makeinstallCd../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=on6、配置eaccelerator加速phpMkdir–p/usr/local/webserver/eaccelerator_cacheVim/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.comwww.domain.com两个虚拟主机使用的目录GroupaddwwwUseradd–gwwwwwwMkdir–p/data0/htdocs/blogChmod+w/data0/htdocs/blogChown–Rwww:www/data0/htdocs/blogMkdir–p/data0/htdocs/wwwChmod+w/data0/htdocs/wwwChown–Rwww:www/data0/htdocs/www8、创建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.151、tar–zxfpcre-7.9.tar.gzcdpcre-7.9/./configureMake&&makeinstallCd../2、tar–zxfnginx-0.8.15.tar.gzcdnginx-0.8.15/./configure–user=wwwgroup=www–prefix=/usr/local/webserver/nginx–with-hhtp_stub_status_module–with-http_ssl_moduleMake&&makeinstall3、创建nginx.conf配置文件,把默认的配置文件给保存一份Vim/usr/local/webserver/nginx/conf/nginx.confUserwwwwww;#运行的用户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.conffastcgi_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–SHn65535/usr/local/webserver/nginx/sbin/ngins####-sreload重新加载配置文件4、配置开机自动启动nginx+phpVi/etc/rc.localUlimit–SHn65535/usr/local/webserver/php/sbin/php-fpmstart/usr/local/webserver/nginx/sbin/ngins
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Nginx