科技常识:Nginx0.5.33+PHP5.2.5(FastCGI)搭建胜过Apache10倍的Web服务器

2021-04-12 18:20:06
导读 今天小编跟大家讲解下有关科技常识:Nginx0 5 33+PHP5 2 5(FastCGI)搭建胜过Apache10倍的Web服务器,相信小伙伴们对这个话题应该也很关注吧

今天小编跟大家讲解下有关科技常识:Nginx0.5.33+PHP5.2.5(FastCGI)搭建胜过Apache10倍的Web服务器,相信小伙伴们对这个话题应该也很关注吧,小编也收集到了有关科技常识:Nginx0.5.33+PHP5.2.5(FastCGI)搭建胜过Apache10倍的Web服务器的相关资料,希望小伙伴会喜欢也能够帮助大家。

修正了PHP iconv和gd库冲突的BUG,增加了PHP mcrypt、memcache扩展,修改了PHP和Nginx编译参数,优化了Nginx配置文件,添加了部分功能。Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAPPOP3SMTP 代理服务器。 Nginx 是由 Igo Sysoev 为俄罗斯访问量第二的 ambe. 站点开发的,它已经在该站点运行超过两年半了。Igo 将源代码以类BSD许可证的形式发布。   Nginx 的中文维基: http:wiki.codemonges.comNginxChs   在高并发连接的情况下,Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。根据我的测试结果,Nginx 0.5.33 + PHP 5.2.5 (FastCGI) 可以承受3万以上的并发连接数,相当于同等环境下Apache的10倍。   根据我的经验,4GB内存的服务器+Apache(pefok模式)一般只能处理3000个并发连接,因为它们将占用3GB以上的内存,还得为系统预留1GB的内存。我曾经就有两台Apache服务器,因为在配置文件中设置的MaxCients为4000,当Apache并发连接数达到3800 时,导致服务器内存和Swap空间用满而崩溃。   而这台 Nginx 0.5.33 + PHP 5.2.5 (FastCGI) 服务器在3万并发连接下,开启的10个Nginx进程消耗150M内存(15M*10=150M),开启的64个php-cgi进程消耗1280M内存(20M*64=1280M),加上系统自身消耗的内存,总共消耗不到2GB内存。如果服务器内存较小,完全可以只开启25个php-cgi进程,这样 php-cgi消耗的总内存数才500M。   在3万并发连接下,访问Nginx 0.5.33 + PHP 5.2.5 (FastCGI) 服务器的PHP程序,仍然速度飞快。下图为Nginx的状态监控页面,显示的活动连接数为28457(关于Nginx的监控页配置,会在本文接下来所给出的Nginx配置文件中写明): 以下为 Nginx 0.5.33 + PHP 5.2.5 (FastCGI) 服务器在3万并发连接下,开启的10个Nginx进程和250个php-cgi进程时的系统负载情况:    安装步骤:  (系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为CentOS 4.4)  一、获取相关开源程序:  1、下载程序源码包到当前目录:  本文中提到的所有开源软件为截止到2007年11月25日的最新稳定版。我将它们打了两个压缩包。  第一个压缩包:nginx-php-1.zip  下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2746375  第二个压缩包:nginx-php-2.zip  下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2746370  附:各开源软件单独下载地址(如果下载了以上两个压缩包,则无需下载以下软件):  http://down.s135.com/linux/nginx-php/(登录用户名、密码均为blog.s135.com)  2、解压缩:mkdir -p /data0/softwarecd /data0/softwareunzip nginx-php-1.zipunzip nginx-php-2.zipcd /data0/software/nginx-php  二、安装PHP 5.2.5(FastCGI模式)  1、编译安装PHP 5.2.5所需的支持库:tar zxvf libiconv-1.11.tar.gzcd libiconv-1.11/./configure --prefix=/usr/localmakemake installcd ../tar zxvf freetype-2.3.5.tar.gzcd freetype-2.3.5/./configuremakemake installcd ../tar zxvf libpng-1.2.20.tar.gzcd libpng-1.2.20/./configuremakemake installcd ../tar zxvf jpegsrc.v6b.tar.gzcd jpeg-6b/./configure --enable-static --enable-sharedmakemake installmake install-libcd ../tar zxvf libxml2-2.6.30.tar.gzcd libxml2-2.6.30/./configuremakeinstallcd ../tar zxvf libmcrypt-2.5.8.tar.gz cd libmcrypt-2.5.8/./configuremakemake install/sbin/ldconfigcd libltdl/./configure --enable-ltdl-installmakemake installcd ../../tar zxvf mhash-0.9.9.tar.gzcd mhash-0.9.9/./configuremakemake installcd ../cp /usr/local/lib/libmcrypt.* /usr/lib ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2tar zxvf mcrypt-2.6.6.tar.gzcd mcrypt-2.6.6/./configuremakemake installcd ../  2、编译安装MySQL 5.0.45/usr/sbin/groupadd mysql/usr/sbin/useradd -g mysql mysqltar zxvf mysql-5.0.45.tar.gzcd mysql-5.0.45./configure --prefix=/usr/local/webserver/mysql/ --without-debug --with-unix-socket-path=/tmp/mysql.sock --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-clientmake && make installchmod +w /usr/local/webserver/mysqlchown -R mysql:mysql /usr/local/webserver/mysqlcp support-files/my-medium.cnf /usr/local/webserver/mysql/my.cnfcd ../  附:以下为附加步骤,如果你想在这台服务器上运行MySQL数据库,则执行以下两步。如果你只是希望让PHP支持MySQL扩展库,能够连接其他服务器上的MySQL数据库,那么,以下两步无需执行。  ①、以mysql用户帐号的身份建立数据表:/usr/local/webserver/mysql/bin/mysql_install_db --defaults-file=/usr/local/webserver/mysql/my.cnf --basedir=/usr/local/webserver/mysql --datadir=/usr/local/webserver/mysql/data --user=mysql --pid-file=/usr/local/webserver/mysql/mysql.pid --skip-locking --port=3306 --socket=/tmp/mysql.sock  ②、启动MySQL(最后的&表示在后台运行)/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe --defaults-file=/usr/local/webserver/mysql/my.cnf &  3、编译安装PHP(FastCGI模式)tar zxvf php-5.2.5.tar.gzcd php-5.2.5/./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-debug --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-force-cgi-redirect --enable-mbstring --with-mcryptsed -i 's#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#' Makefilemakemake installcp php.ini-dist /usr/local/webserver/php/etc/php.inicd ../  4、编译安装PHP5扩展模块tar zxvf memcache-2.2.1.tgzcd memcache-2.2.1//usr/local/webserver/php/bin/phpize./configure --with-php-config=/usr/local/webserver/php/bin/php-configmakemake installcd ../cd php-5.2.5/ext/gd//usr/local/webserver/php/bin/phpize./configure --with-jpeg-dir --with-png-dir --with-zlib-dir --with-ttf --with-freetype-dir --with-php-config=/usr/local/webserver/php/bin/php-configmakemake installcd ../../../  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 ="memcache.so"  extension ="gd.so"  自动修改:若嫌手工修改麻烦,可执行以下shell命令,自动完成对php.ini文件的修改:sed -i 's#extension_dir ="./"#extension_dir ="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension ="memcache.so"\nextension ="gd.so"\n#' /usr/local/webserver/php/etc/php.ini  6、创建www用户和组,以及其使用的目录:/usr/sbin/groupadd www -g 48/usr/sbin/useradd -u 48 -g www wwwmkdir -p /data0/vshare/htdocschmod +w /data0/vshare/htdocschown -R www:www /data0/vshare/htdocs  7、安装lighttpd中附带的spawn-fcgi,用来启动php-cgi  注:压缩包中的spawn-fcgi程序为已经编译成二进制的版本。cp spawn-fcgi /usr/local/webserver/php/binchmod +x /usr/local/webserver/php/bin/spawn-fcgi  8、启动php-cgi进程,监听127.0.0.1的10080端口,进程数为64(如果服务器内存小于3GB,可以只开启25个进程),用户为www:/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u www -f /usr/local/webserver/php/bin/php-cgi  三、安装Nginx 0.5.33  1、安装Nginx所需的pcre库:tar zxvf pcre-7.2.tar.gzcd pcre-7.2/./configuremake && make installcd ../  2、安装Nginxtar zxvf nginx-0.5.33.tar.gzcd nginx-0.5.33/./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_modulemake && make installcd ../  3、创建Nginx日志目录mkdir -p /data1/logschmod +w /data1/logschown -R www:www /data1/logs  4、创建Nginx配置文件  ①、在/usr/local/webserver/nginx/conf/目录中创建nginx.conf文件:rm -f /usr/local/webserver/nginx/conf/nginx.confvi /usr/local/webserver/nginx/conf/nginx.conf  输入以下内容:引用userwww www;worker_processes 10;error_log/data1/logs/nginx_error.logcrit;#pid logs/nginx.pid;#Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200;events { use epoll; worker_connections 51200;}http { include conf/mime.types; default_typeapplication/octet-stream; charsetgb2312; server_names_hash_bucket_size 128; #sendfile on; #tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; gzip on; gzip_min_length1k; gzip_buffers 4 8k; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/html application/xml; server { listen 80; server_nameblog.s135.com; index index.html index.htm index.php; root/data0/vshare/htdocs; if (-d $request_filename) { rewrite ^/(.*)([^/])$ http://$host/$1$2/permanent; } location ~ .*\.php?$ { include conf/fcgi.conf; fastcgi_pass127.0.0.1:10080; fastcgi_index index.php; } log_formataccess'$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.logaccess; } server { listen80; server_namestatus.blog.s135.com; location / { stub_status on; access_log off; } } }  ②、在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件:vi /usr/local/webserver/nginx/conf/fcgi.conf  输入以下内容:引用fastcgi_paramGATEWAY_INTERFACECGI/1.1;fastcgi_paramSERVER_SOFTWARE nginx;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;# PHP only, required if PHP was built with --enable-force-cgi-redirect#fastcgi_paramREDIRECT_STATUS 200;  5、启动Nginxulimit -SHn 51200/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf  四、配置开机自动启动Nginx + PHPvi /etc/rc.local  在末尾增加以下内容:引用ulimit -SHn 51200/usr/local/webserver/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u www -f /usr/local/webserver/php/bin/php-cgi/usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf  五、优化Linux内核参数vi /etc/sysctl.conf  在末尾增加以下内容:引用net.ipv4.tcp_fin_timeout = 30net.ipv4.tcp_keepalive_time = 300net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.ip_local_port_range = 5000 65000  使配置立即生效:/sbin/sysctl -p  六、在不停止Nginx服务的情况下平滑变更Nginx配置  (1)、修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:/usr/local/webserver/nginx/sbin/nginx -t  如果屏幕显示以下两行信息,说明配置文件正确:  the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok  the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully  (2)、这时,输入以下命令查看Nginx主进程号:ps -ef | grep"nginx: master process"| grep -v"grep"| awk -F ' ' '{print $2}'  屏幕显示的即为Nginx主进程号,例如:  6302  这时,执行以下命令即可使修改过的Nginx配置文件生效:kill -HUP 6302

来源:爱蒂网

免责声明:本文由用户上传,如有侵权请联系删除!

猜你喜欢

最新文章