设为首页 收藏本站
查看: 1363|回复: 0

[经验分享] CentOS6.5 Nginx优化编译配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-6-9 10:02:40 | 显示全部楼层 |阅读模式
    epoll是Linux下 多路复用IO接口select/poll的增强版本,它能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率,因为它会复用文件描述符 集合来传递结果而不用迫使开发者每次等待事件之前都必须重新准备要被侦听的文件描述符集合,另一点原因就是获取事件的时候,它无须遍历整个被侦听的描述符 集,只要遍历那些被内核IO事件异步唤醒而加入Ready队列的描述符集合就行了。epoll除了提供select/poll那种IO事件的电平触发 (Level Triggered)外,还提供了边沿触发(Edge Triggered),这就使得用户空间程序有可能缓存IO状态,减少epoll_wait/epoll_pwait的调用,提高应用程序效率。


安装Nginx,这里我略过使用包管理器而使用编译的方式让Nginx运行起来:

1、安装Zlib函数库

####Gcc编译环境是必须条件#####
[iyunv@i-it ~]# yum install gcc-c++ make autoconf aotomake
[iyunv@i-it ~]# tar zxf zlib-1.2.8.tar.gz
[iyunv@i-it ~]# cd zlib-1.2.8
[iyunv@i-it zlib-1.2.8]# ./configure --prefix=/software/zlib
[iyunv@i-it zlib-1.2.8]# make && make install



2、添加对rewrite的支持,编译参数中启用了对utf8字符的支持,以便nginx支持中文的正则表达式

[iyunv@i-it ~]# tar zxf pcre-8.35.tar.gz
[iyunv@i-it ~]# cd pcre-8.35
[iyunv@i-it pcre-8.35]# ./configure --prefix=/software/pcre --enable-utf8 --enable-unicode-properties
[iyunv@i-it pcre-8.35]# make && make install



3、添加对https 的支持

[iyunv@i-it ~]# tar zxf openssl-1.0.1h.tar.gz
[iyunv@i-it ~]# cd openssl-1.0.1h
[iyunv@i-it openssl-1.0.1h]# ./config --prefix=/software/openssl
[iyunv@i-it openssl-1.0.1h]# make && make install



    编译TCMalloc,它是由Google公司发开的一款开源工具,goole- perftools中的一成员,TCMalloc在内存的分配效率和速度要比标准的glibc库好得多,它不但可以用来优化高并发下的Mysql,从而降 低系统的负载,还可以用于Nginx实现同样的功能,因此,对于高并发的Nginx来说无疑是如虎添翼。



4、安装libunwind库

[iyunv@i-it ~]# tar zxf libunwind-1.1.tar.gz
[iyunv@i-it ~]# cd libunwind-1.1
[iyunv@i-it libunwind-1.1]# CFLAGS=-fPIC ./configure --prefix=/software/google-libunwind
[iyunv@i-it libunwind-1.1]# make CFLAGS=-fPIC && make CFLAGS=-fPIC install



5、安装google-perftools

[iyunv@i-it ~]# tar zxf gperftools-2.2.tar.gz
[iyunv@i-it ~]# cd gperftools-2.2
[iyunv@i-it gperftools-2.2]# LDFLAGS="-L/software/google-libunwind/lib" CPPFLAGS="-I/software/google-libunwind/include" ./configure --prefix=/software/google-perftools
[iyunv@i-it gperftools-2.2]# make && make install



6、添加共享库路径

[iyunv@i-it ~]# echo "/software/google-libunwind/lib/" >> /etc/ld.so.conf
[iyunv@i-it ~]# echo "/software/google-perftools/lib/" >> /etc/ld.so.conf
[iyunv@i-it ~]# echo "/software/zlib/lib/" >> /etc/ld.so.conf
[iyunv@i-it ~]# echo "/software/pcre/lib/" >> /etc/ld.so.conf
[iyunv@i-it ~]# ldconfig -v



7、编译Nginx

[iyunv@i-it ~]# groupadd -g 1500 nginx
[iyunv@i-it ~]# useradd -M -u 1500 -g nginx -s /sbin/nologin nginx
[iyunv@i-it ~]# mkdir /var/tmp/nginx
[iyunv@i-it ~]# chown nginx:nginx /var/tmp/nginx/
[iyunv@i-it ~]# tar zxf nginx-1.7.1.tar.gz
[iyunv@i-it ~]# cd nginx-1.7.1

#############注释该文件的174行取消debug模式##############
[iyunv@i-it nginx-1.7.1]# vi auto/cc/gcc
    173 # debug
    174 # CFLAGS="$CFLAGS -g"
######因为google-perftools库的安装路径并非默认,所以这里又要修改一次源码####
[iyunv@i-it nginx-1.7.1]# sed -i "s#/usr/local#/software/google-perftools#" auto/lib/google-perftools/conf
[iyunv@i-it nginx-1.7.1]# ./configure --prefix=/software/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-debug --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre=/root/pcre-8.35 --with-openssl=/root/openssl-1.0.1h --with-zlib=/root/zlib-1.2.8
[iyunv@i-it nginx-1.7.1]# make && make install



8、先添加一个启动脚本,让Nginx run起来

[iyunv@i-it ~]# vi /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /software/nginx/logs/nginx.pid
# config: /software/nginx/conf/nginx.conf
nginxd=/software/nginx/sbin/nginx
nginx_config=/software/nginx/conf/nginx.conf
nginx_pid=/software/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL

#########赋权让其run起来#########
[iyunv@i-it ~]# chmod 755 /etc/init.d/nginx && chkconfig nginx on
[iyunv@i-it ~]# service nginx start
Starting nginx:                                            [  OK  ]
[iyunv@i-it ~]# netstat -pant | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      37831/nginx



9、Nginx run起来了,配置前面的google-perftools库

#####创建线程目录#####
[iyunv@i-it ~]# mkdir /tmp/tcmalloc
[iyunv@i-it ~]# chmod 0777 /tmp/tcmalloc/

####修改Nginx配置文件#####
[iyunv@i-it ~]# vi /software/nginx/conf/nginx.conf
pid        logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;

###查看google-perftools是否加载###
[iyunv@i-it ~]# service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[iyunv@i-it ~]#  lsof -n | grep tcmalloc  
nginx     37882   nginx    9w      REG              253,0        0    1179654 /tmp/tcmalloc.37882
nginx     37883   nginx   11w      REG              253,0        0    1179655 /tmp/tcmalloc.37883
####每一行输出的数据表示Nginx主配置文件中worker_processes的值,其每个线程文件后面的数值为Nginx启动的PID####
[iyunv@i-it ~]# ps aux | grep nginx
root     37880  0.0  0.2  30200   892 ?        Ss   00:27   0:00 nginx: master process /software/nginx/sbin/nginx -c /software/nginx/conf/nginx.conf
nginx    37882  0.0  1.1  34804  3844 ?        S    00:27   0:00 nginx: worker process                                       
nginx    37883  0.0  1.1  34804  3772 ?        S    00:27   0:00 nginx: worker process         
####iptables 开80端口####
[iyunv@i-it ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT



访问Nginx,还有些细节处理本来想一并写在本文中,但是不想一篇文章太长篇了,所以决定分成上下....
wKiom1OS83XjBdx-AAF917TFtfc508.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-20436-1-1.html 上篇帖子: Nginx禁止非sever_name指定域名访问 下篇帖子: nginx error日志报错
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表