微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CentOS6.5 Nginx优化编译配置

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

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

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

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

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

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

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

[root@i-it ~]# groupadd -g 1500 Nginx [root@i-it ~]# useradd -M -u 1500 -g Nginx -s /sbin/nologin Nginx [root@i-it ~]# mkdir /var/tmp/Nginx [root@i-it ~]# chown Nginx:Nginx /var/tmp/Nginx/ [root@i-it ~]# tar zxf Nginx-1.7.1.tar.gz [root@i-it ~]# cd Nginx-1.7.1 #############注释该文件的174行取消debug模式############## [root@i-it Nginx-1.7.1]# vi auto/cc/gcc 173 # debug 174 # CFLAGS="$CFLAGS -g" ######因为google-perftools库的安装路径并非认,所以这里又要修改一次源码#### [root@i-it Nginx-1.7.1]# sed -i "s#/usr/local#/software/google-perftools#" auto/lib/google-perftools/conf [root@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-google_perftools_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 [root@i-it Nginx-1.7.1]# make && make install

[root@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起来######### [root@i-it ~]# chmod 755 /etc/init.d/Nginx && chkconfig Nginx on [root@i-it ~]# service Nginx start Starting Nginx: [ OK ] [root@i-it ~]# netstat -pant | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 37831/Nginx

#####创建线程目录##### [root@i-it ~]# mkdir /tmp/tcmalloc [root@i-it ~]# chmod 0777 /tmp/tcmalloc/ ####修改Nginx配置文件##### [root@i-it ~]# vi /software/Nginx/conf/Nginx.conf pid logs/Nginx.pid; google_perftools_profiles /tmp/tcmalloc; ###查看google-perftools是否加载### [root@i-it ~]# service Nginx restart Stopping Nginx: [ OK ] Starting Nginx: [ OK ] [root@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#### [root@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端口#### [root@i-it ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

###minimal 本来就是最小安装,这里我也就不提建议了,跟着自己的环境来### [root@i-it ~]# chkconfig --list auditd 0:off 1:off 2:on 3:on 4:on 5:on 6:off blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off ip6tables 0:off 1:off 2:on 3:on 4:on 5:on 6:off iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off iscsi 0:off 1:off 2:off 3:on 4:on 5:on 6:off iscsid 0:off 1:off 2:off 3:on 4:on 5:on 6:off lvm2-monitor 0:off 1:on 2:on 3:on 4:on 5:on 6:off mdmonitor 0:off 1:off 2:on 3:on 4:on 5:on 6:off multipathd 0:off 1:off 2:off 3:off 4:off 5:off 6:off netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off Nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off postfix 0:off 1:off 2:on 3:on 4:on 5:on 6:off rdisc 0:off 1:off 2:off 3:off 4:off 5:off 6:off restorecond 0:off 1:off 2:off 3:off 4:off 5:off 6:off rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off saslauthd 0:off 1:off 2:off 3:off 4:off 5:off 6:off sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off udev-post 0:off 1:on 2:on 3:on 4:on 5:on 6:off

###建议先备份一下### [root@i-it ~]# ll /software/Nginx/sbin/Nginx -h -rwxr-xr-x. 1 root root 2.9M Jun 8 00:02 /software/Nginx/sbin/Nginx [root@i-it ~]# strip /software/Nginx/sbin/Nginx [root@i-it ~]# ll /software/Nginx/sbin/Nginx -h -rwxr-xr-x. 1 root root 2.6M Jun 8 01:54 /software/Nginx/sbin/Nginx

####重新挂载当前分区,不记录Nginx文件访问的时间修改#### mount -o defaults,noatime,nodiratime -o remount /dev/sda1

####修改配置文件#### [root@i-it ~]# egrep -v "^#|^$" /etc/security/limits.conf * - nofile 65535 * - nproc 65535 [root@i-it ~]# su - [root@i-it ~]# ulimit -u 65535 [root@i-it ~]# ulimit -n 65535

 [root@i-it ~]# cat >/etc/sysctl.conf<

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。