这是在网上搜索的一篇文章,其中有很多地方做了调整,之前一直在fedora下开发,今天给公司电脑安装了ubuntu11.10,实在受不了windows7,用于提高系统性能的很多软件没有windows版本。
首先安装这些,都是需要的,即使你现在不用,未来也要用.
sudo apt-get install build-essential libpcre3-dev libssl-dev libxslt-dev libgd2-xpm-dev libgeoip-dev zlib1g-dev
下载源码包,没办法,debian包目前还不能随意安装扩展
wget http://www.nginx.org/download/nginx-0.8.35.tar.gz
解压之后,进入目录运行以下命令,有一些是多余的,自己调整吧.我觉得以后要用,就懒得弄了
注意一下–with-cpu-opt 参数
./configure --conf-path=/etc/nginx/nginx.conf \--error-log-path=/var/log/nginx/error.log \--pid-path=/var/run/nginx.pid \--lock-path=/var/lock/nginx.lock \--http-log-path=/var/log/nginx/access.log \--http-client-body-temp-path=/var/lib/nginx/body \--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \--with-http_stub_status_module \--with-http_ssl_module \--with-http_gzip_static_module \--with-http_image_filter_module \--with-sha1=/usr/include/openssl \--with-md5=/usr/include/openssl \--with-cpu-opt=intel32 make && make install
手工建立一下目录
建立shell 脚本 /etc/init.d/nginx
#! /bin/sh ### BEGIN INIT INFO# Provides: nginx# Required-Start: $all# Required-Stop: $all# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: starts the nginx web server# Description: starts nginx using start-stop-daemon### END INIT INFO PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDAEMON=/usr/local/nginx/sbin/nginxNAME=nginxDESC=nginx test -x $DAEMON || exit 0 # Include nginx defaults if availableif [ -f /etc/default/nginx ] ; then . /etc/default/nginxfi set -e case "$1" in start) echo -n "Starting $DESC: " start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON -- $DAEMON_OPTS echo "$NAME." ;; stop) echo -n "Stopping $DESC: " start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON echo "$NAME." ;; restart|force-reload) echo -n "Restarting $DESC: " start-stop-daemon --stop --quiet --pidfile \ /var/run/$NAME.pid --exec $DAEMON sleep 1 start-stop-daemon --start --quiet --pidfile \ /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS echo "$NAME." ;; reload) echo -n "Reloading $DESC configuration: " start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON echo "$NAME." ;; *) N=/etc/init.d/$NAME echo "Usage: $N {start|stop|restart|force-reload}" >& 2 exit 1 ;;esac exit 0
执行命令:
sudo update-rc.d -f nginx defaults
配置nginx.conf
比较关键的下面的几个,自己查文档,根据自己情况来调整,需要计算一下,因为用了epoll,要调整文件描述符的地方也不止这里一处,下面还有几处,需要留意了.
worker_processes
worker_rlimit_nofile
worker_cpu_affinity
worker_connections
通常来说,worker_connections* worker_processes = worker_rlimit_nofile ,worker_cpu_affinity根据worker_processes来调整.
若你要做大文件上传,可以参考这篇文章 点击这里
其他参数也要根据自己的需要进行调整.
#user www-data www-data;worker_processes 4; error_log /var/log/nginx/error.log info; pid /var/run/nginx.pid; worker_rlimit_nofile 32768; worker_cpu_affinity 0001 0010 0100 1000; events { use epoll; worker_connections 8192;} http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 75 20; client_max_body_size 8m; large_client_header_buffers 4 8k; gzip on; gzip_comp_level 3; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; postpone_output 1460; output_buffers 4 32k; # fastcgi_cache_path /usr/local/nginx/fastcgi_cache # levels=1:2 # keys_zone=WEB:10m # inactive=5m; open_file_cache max=5000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; server { listen 80; server_name www.example.com; root /var/www; #charset koi8-r; location / { index index.html index.htm index.php; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ { expires 30d; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # #error_page 500 502 503 504 /50x.html; #location = /50x.html { # root html; #} location ~ \.php { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE web-server; 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; #support PATH_INFO fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; # fastcgi_cache WEB; # fastcgi_cache_valid 200 302 1h; # fastcgi_cache_valid 301 1d; # fastcgi_cache_valid any 1m; # fastcgi_cache_min_uses 1; # fastcgi_cache_use_stale error timeout invalid_header http_500; # 因为header过大造成502错误可以把下面两个参数设置大一点 # fastcgi_buffer_size 128k; # fastcgi_buffers 4 256k; } } server { listen 80; server_name status.example.com; location / { stub_status on; access_log off; } } }
下载 php-5.2.14.tar.gz和 php-5.2.14-fpm-0.5.14.diff.gz,一定要下载0.5.14这个版本以及以上版本,这个版本我的补丁已经提交到官方合并了,打了补丁就不用弄limit.conf配置或者ulimit
然后安装PHP,configure这里也要根据自己的需要调整一下.
sudo ln -s /usr/lib/i386-linux-gnu/libpng* /usr/lib/ sudo ln -s /usr/lib/i386-linux-gnu/libjpeg* /usr/lib/
tar zxvf php-5.2.14.tar.gzgzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1 ./configure --prefix=/usr/local --enable-fastcgi --enable-fpm --with-zlib --with-gd --enable-mbstring --enable-sockets --with-pear --with-freetype-dir --with-jpeg-dir --with-png-dir --with-ttf --enable-force-cgi-redirect make && make install
配置php-fpm.conf,配置下面几个参数就可以了,其他的默认
max_requests即是说每个进程若超过这个数目(跟php进程有一点点关系,关系不大),就自动杀死..我这里应该设置512的,不过懒得压力测试了,设置大一点,不过也不要设置过大,是个结构体,没测试过,接近8K到9K大小.网上动辄设置100k,有点浪费内存了.一个进程浪费大小接近1M.按照网上常用配置的128个进程,大概浪费100M左右.好吧,我承认100M是白菜价,但也别这样浪费..= =
max_children基本就是进程数,跟nginx的进程没有想象中的那么大,因为FPM会自己管理进程(有待考证,起码我简单浏览了一下源码,认为是这个意思).参数不宜设置过大,很占内存,进程的消耗就不用我多说了.
max_children较好的设置方式根据req/s来设置,若程序是 100 req/s的处理能力..最大并发是10K,那么就设置 100比较好,这是动态来调整的.
不过你若用php 5.3,也可以把style设置为apache-like,那么设置start_servers,min_spare_servers,max_spare_servers三个参数就可以自动调整
很简单,具体看配置文件,这样的设置之后,在高负载和复杂的php程序会省事一点,毕竟测试req/s是可恶的体力活.
<value name="user">nobody</value> <value name="group">nogroup</value> <value name="max_children">64</value> <value name="rlimit_files">32768</value> <value name="max_requests">8192</value>
copy 源码包的php.ini到 /usr/local/lib,用php –ini可以验证是否加载通过.
修改php.ini几个地方:
你也可以这么做的,这样做可以避免硬盘IO
apc.shm_segments 可以设置的跟php-fpm的max_children相等,也不一定了,自己用apc.php查看着调整,主要是为了降低lock的时间.
apc.shm_size 自己考虑设置设置多少了,记住你用的APC内存是 shm_segments * shm_size ,比如 shm_segments为32 ,shm_size是默认的30,那么就是32*30,大概是960M..好大啊…= =
顺便说一下,可以调整一下kernel.shmmax的大小,php-fpm也用了不少shm..寒
output_buffering = Onextension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613/" extension = "memcached.so"extension = "apc.so"extension = "mongo.so"
执行pecl命令安装上面的扩展:
pecl install apcpecl install memcachedpecl install mongo
调整内核参数,打开/etc/sysctl.conf,在末位加上.
详细的请看这里,本人还有不少参数没调整,自己研究了…我不是运维人员..= =
#Disabling the TCP options reduces the overhead of each TCP packet and might help to get the #These ensure that TIME_WAIT ports either get reused or closed fast. #The tcp_fin_timeout variable tells kernel how long to keep sockets in the state FIN-WAIT-2 if you were the one closing the socket.net.ipv4.tcp_fin_timeout = 1 #This variable enables the fast recycling function of TIME-WAIT sockets.net.ipv4.tcp_tw_recycle = 1 #this allows reusing sockets in TIME_WAIT state for new connections when it is safe from protocol viewpoint.net.ipv4.tcp_tw_reuse = 1 #TCP memory #Maximum TCP Receive Windownet.core.rmem_max = 16777216net.core.rmem_default = 16777216 #Maximum TCP Send Windownet.core.wmem_max = 16777216net.core.wmem_default = 16777216 net.core.netdev_max_backlog = 30000 net.core.somaxconn = 262144 # Enable TCP SYN Cookie Protectionnet.ipv4.tcp_syncookies = 1 #Maximal number of TCP sockets not attached to any user file handle, held by system.net.ipv4.tcp_max_orphans = 262144 #Maximal number of remembered connection requests, which still did not receive an acknowledgment from connecting client.net.ipv4.tcp_max_syn_backlog = 262144 #To open the other side of the connection, the kernel sends a SYN with a piggybacked ACK on it, to acknowledge the earlier received SYN. net.ipv4.tcp_synack_retries = 2 #Number of SYN packets the kernel will send before giving up on the new connection.net.ipv4.tcp_syn_retries = 2 # increase Linux autotuning TCP buffer limits# min, default, and max number of bytes to use# set max to at least 4MB, or higher if you use very high BDP paths net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 net.ipv4.tcp_window_scaling = 1net.ipv4.tcp_timestamps = 1net.ipv4.tcp_sack = 1 # pluggable congestion control algorithmsnet.ipv4.tcp_congestion_control=cubic # Maximum size of shared memory segment(bytes)kernel.shmmax = 524288000# Total amount of shared memory available (pages)# see command: getconf PAGESIZE# kernel.shmall = 2097152 # msgmnb specifies the maximum total size, in bytes, # of all messages that can be queued simultaneously on a message queue.kernel.msgmnb = 65536# This specifies the maximum size of a message # that can be sent from one process to another process. kernel.msgmax = 65536
最后还有几个命令要执行一下:
sysctl -p cp /usr/local/sbin/php-fpm /etc/init.d/php-fpmsudo update-rc.d -f php-fpm defaults
这样就做完了,然后就是启动 php-fpm和nginx就可以了.
另外,我没用mysql,所以就不贴MYSQL如何配置了..基本按照张宴的来安装就可以了.只是mysql的配置参数需要修改一下..因为他配的有点粗糙.
如果你跟我一样用mongodb,基本不需要怎么配置了,解压就能用,所以就不贴了
ps:还有很多优化参数,我没有加上..哥们我不是运维人员,所以各位自己研究了.= =..另外我以后也会补充的,等负载高了再说.当然,若有错误,请指正了