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

[经验分享] linux下安装nginx与nginx调优

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-7-3 18:04:04 | 显示全部楼层 |阅读模式
  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端口没有被占用等先前工作。    操作过程:

    [iyunv@localhost ~]# yum install gcc gcc-c++ openssl-devel -y   ###准备工作
    [iyunv@localhost ~]# mkdir nginx        ###将nginx模块包放在这个目录中
    [iyunv@localhost nginx]# tar zxf agentzh-encrypted-session-nginx-module-v0.02-0-gc752861.tar.gz
    [iyunv@localhost nginx]# mv agentzh-encrypted-session-nginx-module-c752861/ encrypted-session-nginx-module    ###该模块仅解压即可,不需要编译安装,需要在编译nginx时将其加进去
    [iyunv@localhost nginx]# tar zxf chunkin-nginx-module-0.23rc2.tar.gz
    [iyunv@localhost nginx]#  mv chunkin-nginx-module-0.23rc2 chunkin-nginx-module    ###该模块仅解压即可,不需要编译安装,需要在编译nginx时将其加进去
    [iyunv@localhost nginx]# tar zxf google-perftools-1.8.3.tar.gz
    [iyunv@localhost nginx]#  mv google-perftools-1.8.3 google-perftools   ###该模块仅解压即可,不需要编译安装,需要在编译nginx时将其加进去
    [iyunv@localhost nginx]# cd google-perftools
    [iyunv@localhost google-perftools]# ./configure --enable-frame-pointers  ####如果是64位系统要加--enable-frame-pointers参数
    [iyunv@localhost google-perftools]# make &&make install    ###该模块编译安装后还要在nginx编译时将其添加进去
    [iyunv@localhost nginx]# tar zxf libunwind-0.99.tar.gz
    [iyunv@localhost nginx]# mv libunwind-0.99 libunwind   
    [iyunv@localhost nginx]# cd libunwind
    [iyunv@localhost libunwind]# ./configure CFLAGS=-fPIC
    [iyunv@localhost libunwind]# make CFLAGS=-fPIC
    [iyunv@localhost libunwind]# make install CFLAGS=-fPIC    ####该模块编译安装要加CFLAGS=-fPIC
    [iyunv@localhost nginx]# tar zxf pcre-8.11.tar.gz
    [iyunv@localhost nginx]# cd pcre-8.11
    [iyunv@localhost pcre-8.11]# ./configure
    [iyunv@localhost pcre-8.11]# make
    [iyunv@localhost pcre-8.11]# make install    ####安装nginx需要的正则表达式包,该软件作用是nginx的rewrite功能需要它
    [iyunv@localhost nginx]# tar zxf simpl-ngx_devel_kit-v0.2.17-10-g4192ba6.tar.gz
    [iyunv@localhost nginx]# mv simpl-ngx_devel_kit-4192ba6/ simpl-ngx_devel_kit  ###该模块仅解压即可,不需要编译安装,需要在编译nginx时将其加进去  
    [iyunv@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /lib/
    [iyunv@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /usr/lib/
    [iyunv@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /lib/
    [iyunv@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /usr/lib/     ####这4步的软链接需要做,我这里是32位系统,如果是64位系统,可以做下面几步:
       [iyunv@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /lib/
        [iyunv@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /usr/lib/
        [iyunv@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /lib64/
        [iyunv@localhost nginx]# ln -s /usr/local/lib/libprofiler.so.0 /usr/lib64/
        [iyunv@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /lib/
        [iyunv@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /usr/lib/
        [iyunv@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /lib64/
        [iyunv@localhost nginx]# ln -s /usr/local/lib/libunwind.so.7 /usr/lib64/
    [iyunv@localhost nginx]# tar zxf nginx-1.1.6.tar.gz
    [iyunv@localhost nginx]# cd nginx-1.1.6
    [iyunv@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 ####将上面解压或者安装好的各模块软件添加进去
    [iyunv@localhost nginx-1.1.6]# make
    [iyunv@localhost nginx-1.1.6]# make install
    [iyunv@localhost nginx]# useradd -M www   ####创建不带用户目录www用户
    [iyunv@localhost nginx]# /usr/local/nginx/sbin/nginx   ####启动nginx服务

    [iyunv@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

    [iyunv@localhost ~]# cat /usr/local/nginx/logs/nginx.pid   ####这两步都发现有nginx服务进程,说明nginx启动成功
        3324
    走到这里,nginx安装完成并且可以正常运行,下面对nginx优化,这里对nginx的配置文件里的参数优化,将nginx的原配置文件备份成nginx.conf_bak,下面是优化后的配置文件:

    [iyunv@localhost ~]# mv nginx_laoshi.conf /usr/local/nginx/conf/nginx.conf
    [iyunv@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服务:

    [iyunv@localhost ~]# /usr/local/nginx/sbin/nginx -s reload  ####nginx服务平滑的重启,或者也可以照下面的做也可以实现nginx平滑重启:  
        [iyunv@localhost ~]# ps -ef |grep nginx |grep master |awk '{print $2}'   ###只查找nginx主进程号
            3324
        [iyunv@localhost ~]# kill -HUP 3324   ###nginx实现了平滑重启
    ok,到这里,nginx的编译安装以及调优完成。

运维网声明 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-21531-1-1.html 上篇帖子: nginx配置文件详解 下篇帖子: linux下nginx启动停止重启控制脚本 linux
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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