基于CentOS 5.4搭建nginx+php+spawn-fcgi+mysql高性能php平台
作者:pkfeiyang 主页:pkfeiyang.cublog.cn
首先介绍下支持php的cgi有php-fpm和spawn-fcgi 目前比较常用对于php-fpm支持cgi的安装lemp安装详解:http://blog.chinaunix.net/u3/103362/showart_2301020.html下面主要介绍nginx+php+spawn-fcgi+mysql 什么是spawn-fcgi?
spawn-fcgi是一个通用的FastCGI管理服务器 她是lighttpd中的一部份,但目前已经单独成为一个项目,最新的lighttpd没有这一块,但可以在以前版本中找到她在lighttpd-1.4.15( )中就有她Note注:最新的spawn-fcgi可以到lighttpd.net网站搜索“spawn-fcgi”找到她的最新版本发布地址目前她的下载地址是 最新版本是
下载软件包:
下载软件包: pcre 下载 wget nginx 下载 wget gd库下载 wget lib库下载 wget wget 算法需要的 wget wget wget wget wget wget wget wget wget
添加用户名 71 useradd www 72 groupadd www 73 usermod -a -G www www 74 mkdir -p /website/web001 75 chmod +w /website/web001/ -R 76 chown www:www /website/web001/ -R 77 useradd web001 78 usermod -g web001 www 79 chown -R web001:www /website/web001/调节时间crontab -e加入:15 3 * * * /usr/sbin/ntpdate 210.72.145.44 >/dev/null 2>&1安装需要的软件包: yum -y install ntp vim-enhanced gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel ncurses-devel openssl-devel libtool* zlib-devel libxml2-devel libjpeg-devel libpng-devel libtiff-devel fontconfig-devel freetype-devel libXpm-devel gettext-devel curl curl-devel pam-devel e2fsprogs-devel krb5-devel libidn libidn-devel libevent-devel libevent
安装mysql
157 CFLAGS="-march=nocona -O3 -pipe" 158 CXXFLAGS="{CFLAGS}" 161 tar -xzf mysql-5.1.48.tar.gz 162 cd mysql-5.1.48 ./configure --prefix=/usr/local/mysql5 --localstatedir=/data/mysql/data --with-comment=Source --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=gbk --with-extra-charsets=all --with-pthread --enable-static --with-collation=gbk_chinese_ci --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-plugins=all --without-ndb-debug make
make install
useradd mysql -d /data/mysql -s/sbin/nologin
/usr/local/mysql/bin/mysql_install_db--user=mysql/usr/local/mysql5/bin/mysqld_safe --user=mysql &(注意)
cd /usr/local/mysql
chown -R root:mysql .
mkdir -p /data/mysql/data
chown -R mysql /data/mysql/data
cp share/mysql/my-huge.cnf /etc/my.cnf
cp share/mysql/mysql.server/etc/rc.d/init.d/mysqld
chmod 755 /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
/etc/rc.d/init.d/mysqld start
安装php
tar -xjf gd-2.0.35.tar.bz2 106 107 cd gd-2.0.35 108 109 ./configure --prefix=/usr/local/gd2 110 make && make install 111 cd .. 112 113 tar -xjf libmcrypt-2.5.8.tar.bz2 114 cd libmcrypt-2.5.8 115 116 ./configure --prefix=/usr 117 118 make && make install 119 122 tar -xzf libiconv-1.13.tar.gz 123 cd libiconv-1.13 124 ls 125 ./configure --prefix=/usr 126 make && make install 127 128 129 tar -xzf mhash-0.9.9.9.tar.gz 130 cd mhash-0.9.9.9 131 ./configure --prefix=/usr 132 make && make install 133 vi /etc/ld.so.conf 加上/usr/lib ldconfigtar zxvf php-5.2.5.tar.gz(注意php-cgi 需要指定--enable-fastcgi和
--enable-force-cgi-redirect,并且好要注意php-cgi的生成和版本有关,5.2.x 的版本是可以的,5.3.x 以上集成了php-fpm 对于生成php-cgi没有多大效果)cd php-5.2.5/./configure
--prefix=/usr/local/php-fcgi
--with-config-file-path=/usr/loca/php-fcgi/etc
--with-mysql=/usr/local/mysql5 --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-mcrypt--enable-pdo --with-pdo-mysql=shared --with-pdo-sqlite=shared (这个需要加上 如果你用pdo 来连接数据库的话)sed -i 's#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#' Makefilemakemake installcp php.ini-dist /usr/local/webserver/php/etc/php.ini编辑php.ini sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php-fcgi/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\nextension = "gd.so"\n#' /usr/local/php-fcgi/etc/php.ini
安装memachetar -xzf memcache-2.2.5.tgzcd memcache-2.2.5/usr/local/php-fcgi/bin/phpize./configure --with-php-config=/usr/local/php-fcgi/bin/php-configmake && make install
安装eaccelerator
tar -xjf eaccelerator-0.9.6.1.tar.bz2/usr/local/php-fcgi/bin/phpizecd eaccelerator-0.9.6.1/usr/local/php-fcgi/bin/phpize./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php-fcgi/bin/php-config安装PDO_MYSQLtar -xzf PDO_MYSQL-1.0.2.tgzcd PDO_MYSQL-1.0.2/usr/local/php-fcgi/bin/phpize./configure --with-php-config=/usr/local/php-fcgi/bin/php-config --with-pdo-mysql=/usr/local/mysql5make && make install安装ZendOptimizer 这个版本高,不用安装了tar -xzf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
安装spawn-fcgi
#tar zvxf spawn-fcgi-1.6.3.tar.gz
#cd spawn-fcgi-1.6.3
#./configure --prefix=/usr/local/spawn-fcgi make && make install/usr/local/spawn/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 64 -u www -f /usr/bin/php-cgi启动php-cgi进程,监听127.0.0.1的10080端口,进程数为64(如果服务器内存小于3GB,可以只开启25个进程),用户为www:参数含义如下:
-f 指定调用FastCGI的进程的执行程序位置,根据系统上所装的PHP的情况具体设置。
-a 绑定到地址addr。
-p 绑定到端口port。
-s 绑定到unix socket的路径path。
-C 指定产生的FastCGI的进程数,默认为5。(仅用于PHP)
-P 指定产生的进程的PID文件路径。
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,CentOS下可以使用apache用户,其他的根据情况配置,如nobody、www-data等。
4. 将这行代码加入到/etc/rc.local文件底部,这样系统启动的时候就可以同时启动PHP的FastCGI进程
安装nginx安装 pcre
tar -xzf pcre-8.10.tar.gz
cd pcre-8.10
./configure
make && make install
wget tar zxvf ngx_cache_purge-1.0.tar.gztar -xzf nginx-0.8.49.tar.gzcd nginx-0.8.49./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-openssl=/usr/lib --add-module=../ngx_cache_purge-1.0
--add-module=/Users/mikewest/Repositories/nginx-static-etags (当nginx需要使用etags 进行页面缓
存的设置时就需要加载这个模块 然后在nginx。conf 添加这个参数
--add-module=../ngx_cache_purge-1.0 编译时
location / {
...
FileETag on;
...
}
)
make && make install
编辑nginx.conf文件
user www www;worker_processes 50;#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;pid /var/run/nginx.pid;# [ debug | info | notice | warn | error | crit ]#error_log /dev/null;error_log /var/www/logs/nginx_error.log error;#error_log logs/error.log notice;#error_log logs/error.log info;#Specifies the value for maximum file descriptors that can be opened by this process.worker_rlimit_nofile 6553;events{ use epoll; #maxclient = worker_processes * worker_connections / cpu_number worker_connections 6553;}http{ include mime.types; default_type application/octet-stream; charset utf-8; log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent "$http_referer" ' #access_log /dev/null; #General Options server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 64k; ignore_invalid_headers on; recursive_error_pages on; server_name_in_redirect off; sendfile on; #timeouts keepalive_timeout 60; #TCP Options tcp_nopush on; tcp_nodelay on; #fastcgi options fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_temp_path /var/www/tmp/fastcgi_temp_path; fastcgi_cache_methods GET HEAD; fastcgi_cache_min_uses 3; #fastcgi_cache fastcgi_cache; fastcgi_cache_valid 200 302 20m; fastcgi_cache_valid 301 1h; fastcgi_cache_valid any 20m; fastcgi_cache_key 127.0.0.1:9000$request_uri; fastcgi_buffer_size 128k; fastcgi_buffers 4 128k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_intercept_errors on; ###limit limit_zone one $binary_remote_addr 10m; limit_conn one 5; # limit_req_zone $binary_remote_addr zone=one2:10m rate=5r/s; # limit_req zone=one2 burst=5; #size limits client_max_body_size 50m; client_body_buffer_size 256k; #gzip compression gzip on; gzip_min_length 0; gzip_buffers 16 8k; gzip_http_version 1.0; gzip_comp_level 9; gzip_types text/plain text/css image/x-icon image/png; gzip_vary on; #temp files proxy_temp_path /var/www/tmp/proxy_temp_path;# fastcgi_temp_path /var/www/tmp/fastcgi_temp; client_body_temp_path /var/www/tmp/client_body_temp; #client_body_temp_path /usr/local/nginx8/client_body_temp 1 2; #proxy_temp_path /usr/local/nginx8/proxy_temp 1 2; #fastcgi_temp_path /usr/local/nginx8/fastcgi_temp 1 2; #proxy 麓煤?铆禄潞麓忙?猫?? #proxy proxy_buffering on; proxy_cache_min_uses 3; proxy_cache_path /var/www/tmp/proxy_cache_path levels=1:2 keys_zone=cache:500m inactive=30m max_size=10000M; proxy_cache_valid any 10m; proxy_cache_methods GET HEAD; proxy_cache_key "$host:$server_port$uri$is_args$args"; proxy_ignore_client_abort off; proxy_intercept_errors on; proxy_next_upstream error timeout invalid_header; proxy_redirect off; proxy_set_header X-Forwarded-For $remote_addr; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; #upstream ?枚录炉?潞?? upstream spawn { # ip_hash; server 127.0.0.1:10080; #server 127.0.0.1:9002; # include vhosts/tv.feiyang.com.conf; } # The following includes are specified for virtual hosts include vhosts/tv.feiyang.com.conf;}
编辑tv.feiyang.com.confserver { listen 80; server_name tv.feiyang.com; index index.html index.htm index.php; root /website/web001; error_page 404 500 502 503 504 ##servername chong ding xiang # rewrite ^/(.*) last; # rewrite ^/(.*) /test.php last; location / { proxy_next_upsteam http_502 http_504 error timeout invalid_header; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; proxy_ignore_headers Expires Cache-Control; } location ~ .*\.(php|php5)?$ { # fastcgi_pass spawn; fastcgi_pass 127.0.0.1:10080; include spawn_php5.conf; fastcgi_index index.php; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { expires 3d; } log_format wwwlogs '$http_x_forwarded_for - $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /var/www/logs/access.feiyang.log wwwlogs; }
编辑spawn_php5.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;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param REDIRECT_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 = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.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
阅读(1977) | 评论(1) | 转发(0) |