码迷,mamicode.com
首页 > 系统相关 > 详细

linux下安装nginx与nginx调优

时间:2014-07-03 15:23:26      阅读:732      评论:0      收藏:0      [点我收藏+]

标签:nginx安装 nginx调优

    linux系统为rhel5.6,nginx版本为nginx-1.1.6.tar.gz,可以到网上下载最新的安装,由于nginx是基于很多模块实现强大的功能,所以要安装并编译其他模块软件包,这里安装的模块软件包有:agentzh-encrypted-session-nginx-module-v0.02-0-gc752861.tar.gz、chunkin-nginx-module-0.23rc2.tar.gz、google-perftools-1.8.3.tar.gz、libunwind-0.99.tar.gz、pcre-8.11.tar.gz、simpl-ngx_devel_kit-v0.2.17-10-g4192ba6.tar.gz,如果有需要还可以下载更多的包进行编译安装,可以到wiki.nginx.org里面下载需要的软件与文档等。安装nginx要先从安装各模块包开始,各模块包的安装没有先后顺序限制,当然在开始安装这些包前要先安装gcc、gcc-c++等工具、先确保系统的80端口没有被占用等先前工作。

    操作过程:

    [root@localhost ~]# yum install gcc gcc-c++ openssl-devel -y   ###准备工作

    [root@localhost ~]# mkdir nginx        ###将nginx模块包放在这个目录中

    [root@localhost nginx]# tar zxf agentzh-encrypted-session-nginx-module-v0.02-0-gc752861.tar.gz

    [root@localhost nginx]# mv agentzh-encrypted-session-nginx-module-c752861/ encrypted-session-nginx-module    ###该模块仅解压即可,不需要编译安装,需要在编译nginx时将其加进去

    [root@localhost nginx]# tar zxf chunkin-nginx-module-0.23rc2.tar.gz

    [root@localhost nginx]#  mv chunkin-nginx-module-0.23rc2 chunkin-nginx-module    ###该模块仅解压即可,不需要编译安装,需要在编译nginx时将其加进去

    [root@localhost nginx]# tar zxf google-perftools-1.8.3.tar.gz

    [root@localhost nginx]#  mv google-perftools-1.8.3 google-perftools   ###该模块仅解压即可,不需要编译安装,需要在编译nginx时将其加进去

    [root@localhost nginx]# cd google-perftools

    [root@localhost google-perftools]# ./configure --enable-frame-pointers  ####如果是64位系统要加--enable-frame-pointers参数

    [root@localhost google-perftools]# make &&make install    ###该模块编译安装后还要在nginx编译时将其添加进去

    [root@localhost nginx]# tar zxf libunwind-0.99.tar.gz

    [root@localhost nginx]# mv libunwind-0.99 libunwind    

    [root@localhost nginx]# cd libunwind

    [root@localhost libunwind]# ./configure CFLAGS=-fPIC

    [root@localhost libunwind]# make CFLAGS=-fPIC

    [root@localhost libunwind]# make install CFLAGS=-fPIC    ####该模块编译安装要加CFLAGS=-fPIC

    [root@localhost nginx]# tar zxf pcre-8.11.tar.gz

    [root@localhost nginx]# cd pcre-8.11

    [root@localhost pcre-8.11]# ./configure

    [root@localhost pcre-8.11]# make

    [root@localhost pcre-8.11]# make install    ####安装nginx需要的正则表达式包,该软件作用是nginx的rewrite功能需要它

    [root@localhost nginx]# tar zxf simpl-ngx_devel_kit-v0.2.17-10-g4192ba6.tar.gz

    [root@localhost nginx]# mv simpl-ngx_devel_kit-4192ba6/ simpl-ngx_devel_kit  ###该模块仅解压即可,不需要编译安装,需要在编译nginx时将其加进去  

    [root@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /lib/ 

    [root@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /usr/lib/

    [root@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /lib/

    [root@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /usr/lib/     ####这4步的软链接需要做,我这里是32位系统,如果是64位系统,可以做下面几步:

        [root@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /lib/

        [root@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /usr/lib/

        [root@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /lib64/

        [root@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /usr/lib64/

        [root@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /lib/

        [root@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /usr/lib/

        [root@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /lib64/

        [root@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /usr/lib64/

    [root@localhost nginx]# tar zxf nginx-1.1.6.tar.gz

    [root@localhost nginx]# cd nginx-1.1.6

    [root@localhost nginx-1.1.6]#  ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-file-aio --with-http_stub_status_module --with-http_sub_module --with-http_addition_module --with-http_random_index_module --with-http_secure_link_module --with-http_dav_module --with-http_mp4_module --with-http_degradation_module --with-http_gzip_static_module --with-google_perftools_module --with-http_flv_module --add-module=/root/nginx/chunkin-nginx-module --add-module=/root/nginx/simpl-ngx_devel_kit --add-module=/root/nginx/encrypted-session-nginx-module ####将上面解压或者安装好的各模块软件添加进去

    [root@localhost nginx-1.1.6]# make

    [root@localhost nginx-1.1.6]# make install

    [root@localhost nginx]# useradd -M www   ####创建不带用户目录www用户

    [root@localhost nginx]# /usr/local/nginx/sbin/nginx   ####启动nginx服务

    

    [root@localhost ~]# ps -ef |grep nginx  ####如果配置文件中的 user nobody没有划掉注释,这里的worker process使用者会是nobody,去掉注释后会变成nobody

        root      3324     1  0 10:18 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx

        nobody    3325  3324  0 10:18 ?        00:00:00 nginx: worker process      

        nobody    3326  3324  0 10:18 ?        00:00:00 nginx: worker process      

        nobody    3327  3324  0 10:18 ?        00:00:00 nginx: worker process      

        nobody    3328  3324  0 10:18 ?        00:00:00 nginx: worker process      

        root      4035 12365  0 10:50 pts/3    00:00:00 grep nginx

    

    [root@localhost ~]# cat /usr/local/nginx/logs/nginx.pid   ####这两步都发现有nginx服务进程,说明nginx启动成功

        3324

    走到这里,nginx安装完成并且可以正常运行,下面对nginx优化,这里对nginx的配置文件里的参数优化,将nginx的原配置文件备份成nginx.conf_bak,下面是优化后的配置文件:

    [root@localhost ~]# mv nginx_laoshi.conf /usr/local/nginx/conf/nginx.conf

    [root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf

        user  nobody;

        worker_processes  4;

        worker_cpu_affinity 00000001 00000010 00000100 00001000 

        

        error_log  logs/error.log error;

        pid        logs/nginx.pid;

        

        events {

            worker_connections  102400;

         use epoll;

        }

        

        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  logs/access.log  main;

         client_header_buffer_size 4k;

         large_client_header_buffer 4 32k;

         open_file_cache max=102400 inactive=20s;

         open_file_cache_valid 30s;

         open_file_cache_min_uses 1;

         #client_max_body_size 8m;

         server_names_hash_bucket_size 128;

            sendfile        on;

         #autoindex on;

            tcp_nopush     on;

         tcp_nodelay   on;

            keepalive_timeout  120;

        

            gzip  on;

         gzip_static  on;

         gzip_min_length  1k;

         gzip_buffer 4 16k;

         gzip_types text/css text/javascript application/xml text/plain application/x-javascript;

         gzip_vary  on;

         gzip_comp_level  5;

        

         upstream www.vfast.com.cn {

         #ip_hash;

         #fair;

         #url_hash;

         server 10.255.254.132:80 max_fails=5;

         server 10.255.254.134:80 weight=10 ;

         }

            server {

                listen       80;

                server_name  www.vfast.com.cn;

                location / {

         proxy_pass http://www.vfast.com.cn;

                }

         }

         server {

         listen 8080;

         server_name stats.vfast.com.cn;

         location /status {

         stub_status on;

         access_log off;

         auth_basic "status";

         }

         }

        }

    这里的配置文件参数调优是针对某台机器做的,不同机器的参数调优也略有不同,应按实际情况的做,调优完成后,要重启nginx服务:

    [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload  ####nginx服务平滑的重启,或者也可以照下面的做也可以实现nginx平滑重启:  

        [root@localhost ~]# ps -ef |grep nginx |grep master |awk ‘{print $2}‘   ###只查找nginx主进程号

            3324

        [root@localhost ~]# kill -HUP 3324   ###nginx实现了平滑重启

    ok,到这里,nginx的编译安装以及调优完成。


本文出自 “个人感受” 博客,谢绝转载!

linux下安装nginx与nginx调优,布布扣,bubuko.com

linux下安装nginx与nginx调优

标签:nginx安装 nginx调优

原文地址:http://4593973.blog.51cto.com/4583973/1433855

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!