――使用Tcmalloc优化Nginx
注意:如果是系统是64位的话 需要安装libunwind
1)下载libunwind
[root@Nginx ~]# cd /Linux [root@Nginx ~]# wgethttp://ftp.twaren.net/Unix/NonGNU//libunwind/libunwind-1.1.tar.gz
2)安装libunwind
[root@Nginx Linux]# tar fzvx libunwind-1.1.tar.gz [root@Nginx Linux]# cd libunwind-1.1 [root@Nginx libunwind-1.1]# CFLAGS=-fPIC ./configure [root@Nginx libunwind-1.1]# make CFLAGS=-fPIC [root@Nginx libunwind-1.1]# make CFLAGS=-fPIC install
3)安装perftools
[root@Nginx libunwind-1.1]# cd .. [root@Nginx Linux]# wget https://gperftools.googlecode.com/files/gperftools-2.1.tar.gz [root@Nginx Linux]# tar zfvx gperftools-2.1.tar.gz [root@Nginx Linux]# cd gperftools-2.1 [root@Nginx gperftools-2.1]# ./configure --prefix=/usr/local/gperftools [root@Nginx gperftools-2.1]# make && make install [root@Nginx gperftools-2.1]# cp -r /usr/local/gperftools/lib/* /usr/local/lib/ [root@Nginx gperftools-2.1]# mkdir /tmp/tcmalloc [root@Nginx gperftools-2.1]# chmod 0777 /tmp/tcmalloc/
4)安装pcre
[root@Nginx gperftools-2.1]# cd .. [root@Nginx Linux]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.bz2 [root@Nginx Linux]# tar jfvx pcre-8.33.tar.bz2 [root@Nginx Linux]# cd pcre-8.33 [root@Nginx pcre-8.33]# ./configure --prefix=/usr/local/pcre-8.33 \ --enable-utf8 --enable-unicode-properties
――安装编译Nginx
1)下载Nginx
[root@Nginx pcre-8.33]# cd .. [root@Nginx Linux]# useradd -M -s /sbin/nologin nginx [root@Nginx Linux]# wget http://nginx.org/download/nginx-1.4.7.tar.gz
2)编译安装Nginx
[root@Nginx Linux]# tar zfvx nginx-1.4.7.tar.gz [root@Nginx Linux]# cd nginx-1.4.7 [root@Nginx nginx-1.4.7]# ./configure \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_gzip_static_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_stub_status_module \ --without-http_charset_module \ --with-google_perftools_module \ --with-pcre=/Linux/pcre-8.33 #注意 是pcre的源码包路径、不是安装路径 [root@Nginx nginx-1.4.7]# make && make install
3)配置Nginx
[root@Nginx nginx-1.4.7]# vi /usr/local/nginx/conf/nginx.conf #用户和组 user nginx nginx; #进程数【和CPU颗数相等】 worker_processes 8; #将每一个进程绑定到一个CPU [8颗CPU] worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; #错误日志 error_log logs/error.log; #进程PID pid logs/nginx.pid; events { #I/O模型 use epoll; #运行连接数 worker_connections 65535; } #Tcmalloc目录 google_perftools_profiles /tmp/tcmalloc; http { #文件类型 include mime.types; #虚拟主机配置目录 include /usr/local/nginx/conf/conf.d/*.conf; default_type application/octet-stream; #根据CUP的缓存设定 server_names_hash_bucket_size 128; #记录缓存 client_header_buffer_size 32k; #如果4k不够,用32k large_client_header_buffers 4 32k; #设置客户端能够上传文件大小的限制 client_max_body_size 300m; #是否调用sendfile函数 sendfile on; #允许使用socket的tcp_nopush tcp_nopush on; #超时时间/秒 keepalive_timeout 60; tcp_nodelay on; #关闭错误时Nginx版本显示 server_tokens off; fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m; #FastCGI的指令集与服务器的连接超时时间 fastcgi_connect_timeout 300; #等待一个FastCGI进程发送数据的最长时间 fastcgi_send_timeout 300; #等待后端应用程序返回给nginx的最长时间 fastcgi_read_timeout 300; #从FastCGI服务器到来的应答,本地将用多少和多大的缓冲区读取 fastcgi_buffer_size 64k; #默认值 fastcgi_buffers 4 64k; #默认值是fastcgi_buffers的两倍 fastcgi_busy_buffers_size 128k; #默认值是fastcgi_buffers的两倍 fastcgi_temp_file_write_size 128k; #开启FastCGI缓存并且为其制定一个名称,可以有效降低CPU负载,并且防止502错误 fastcgi_cache TEST; #将200,302应答缓存1小时 fastcgi_cache_valid 200 302 1h; #将301应答缓存1天 fastcgi_cache_valid 301 1d; #其他应答还我为1分钟 fastcgi_cache_valid any 1m; #开启gzip压缩 gzip on; #允许压缩最小字节,0表示多大都压缩 gzip_min_length 1k; #系统获取几个单位的缓存 gzip_buffers 4 16k; #判断http协议版本,默认即可 gzip_http_version 1.1; #gzip压缩比例,1压缩比最小处理速度最快,9压缩比最大处理速度最慢 gzip_comp_level 2; #默认nginx是开启gzip压缩,但是只针对html文件押送 gzip_types text/plain application/x-javascript text/css application/xml; #根据客户端的HTTP头来判断,是否需要压缩 gzip_vary on; #日志级别 limit_req_log_level warn; #限制来自单个IP地址的请求处理频率 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; }
4)创建虚拟主机
[root@Nginx nginx-1.4.7]# mkdir /usr/local/nginx/conf/conf.d [root@Nginx nginx-1.4.7]# cd /usr/local/nginx/conf/conf.d/ [root@Nginx conf.d]# vi www.nagix.com.conf server { #端口 listen 80; #域名 server_name www.nginx.com; #存放路径 root /usr/local/nginx/html; #首页类型 index index.html index.htm index.php #错误日志 error_log logs/www.nginx.com_error.log; location ~ \.php$ { root /usr/local/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
5)添加启动脚本
[root@Nginx conf.d]# vi /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemin # # chkconfig: 35 86 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
6)设置开机启动
[root@Nginx conf.d]# chmod +x /etc/init.d/nginx [root@Nginx conf.d]# chkconfig --add nginx [root@Nginx conf.d]# chkconfig nginx on
7)启动nginx
[root@Nginx conf.d]# service nginx start 正在启动nginx:/usr/local/nginx/sbin/nginx: error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory [失败]
正在启动nginx:/usr/local/nginx/sbin/nginx: error while loading shared libraries: libprofiler.so.0: cannot open shared object file: No such file or directory
[失败]
解决办法:
[root@Nginx conf.d]# ln -s /usr/local/lib/libunwind.so /lib64/libunwind.so.8 [root@Nginx conf.d]# ln -s /usr/local/lib/libprofiler.so.0 /lib64 [root@Nginx conf.d]# service nginx start 正在启动 nginx:[确定] [root@Nginx conf.d]# netstat -antup |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32981/nginx
――编译安装PHP
1)安装软件依赖包
[root@Nginx Linux]# yum install libxml2-devel bzip2-devel curl curl-devel libjpeg-devel freetype-devel gmp-devel mysql-devel unixODBC-devel libpng-devel libxslt-devel
2)下载PHP
[root@Nginx Linux]# wget http://at2.php.net/distributions/php-5.5.4.tar.gz
3)编译安装
[root@Nginx Linux]# tar zfxv php-5.5.4.tar.gz [root@Nginx Linux]# cd php-5.5.4 [root@Nginx php-5.5.4]# ./configure --prefix=/usr/local/php-5.5 \ --enable-safe-mode \ --enable-zip \ --with-bz2 \ --with-jpeg-dir=/usr/include \ --with-png-dir=/usr/include/libpng12 \ --with-freetype-dir=/usr/include/freetype2/freetype \ --with-libxml-dir=/usr/include/libxml2/libxml \ --with-xmlrpc \ --with-zlib \ --with-gd \ --with-curl \ --with-xsl \ --with-gettext \ --with-pear \ --enable-mbstring \ --enable-bcmath \ --enable-sockets \ --enable-exif \ --enable-fpm \ --with-openssl \ --with-pcre \ --with-mysql=/usr/local/mysql-5.5 \ --with-mysqli=/usr/local/mysql-5.5/bin/mysql_config \ --with-config-file-path=/usr/local/php-5.5/etc/ \ [root@Nginx php-5.5.4]# make && make install [root@Nginx php-5.5.4]# cp php.ini-production /usr/local/php-5.5/etc/php.ini [root@Nginx php-5.5.4]# cp /usr/local/php-5.5/etc/php-fpm.conf.default \ > /usr/local/php-5.5/etc/php-fpm.conf
4)配置PHP
[root@Nginx php-5.5.4]# vi /etc/profile PATH=/usr/local/php-5.5/bin:$PATH PATH=/usr/local/php-5.5/sbin:$PATH [root@Nginx php-5.5.4]# vi /usr/local/php-5.5/etc/php-fpm.conf ;pid = run/php-fpm.pid pid = run/php-fpm.pid#取消注释
5)设置php-fpm开机启动
[root@Nginx php-5.5.4]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@Nginx php-5.5.4]# chmod +x /etc/init.d/php-fpm [root@Nginx php-5.5.4]# chkconfig --add php-fpm [root@Nginx php-5.5.4]# chkconfig php-fpm on [root@Nginx php-5.5.4]# service php-fpm start [root@Nginx php-5.5.4]# netstat -antup |grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 25686/php-fpm
6)添加测试页面
[root@Nginx php-5.5.4]# vi /usr/local/nginx/html/index.php <?php phpinfo(); ?>
[root@Nginx conf.d]# curl http://www.nginx.com/index.php
如果解析没有问题的话、说明安装成功。环境有限,我在这里使用的是命令行访问WEB、大家使用客户端网页访问:http://www.nginx.com/index.php