linux下安裝nginx與nginx調優

來源:互聯網
上載者:User

標籤: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連接埠沒有被佔用等先前工作。

    操作過程:

    [[email protected] ~]# yum install gcc gcc-c++ openssl-devel -y   ###準備工作

    [[email protected] ~]# mkdir nginx        ###將nginx模組包放在這個目錄中

    [[email protected] nginx]# tar zxf agentzh-encrypted-session-nginx-module-v0.02-0-gc752861.tar.gz

    [[email protected] nginx]# mv agentzh-encrypted-session-nginx-module-c752861/ encrypted-session-nginx-module    ###該模組僅解壓即可,不需要編譯安裝,需要在編譯nginx時將其加進去

    [[email protected] nginx]# tar zxf chunkin-nginx-module-0.23rc2.tar.gz

    [[email protected] nginx]#  mv chunkin-nginx-module-0.23rc2 chunkin-nginx-module    ###該模組僅解壓即可,不需要編譯安裝,需要在編譯nginx時將其加進去

    [[email protected] nginx]# tar zxf google-perftools-1.8.3.tar.gz

    [[email protected] nginx]#  mv google-perftools-1.8.3 google-perftools   ###該模組僅解壓即可,不需要編譯安裝,需要在編譯nginx時將其加進去

    [[email protected] nginx]# cd google-perftools

    [[email protected] google-perftools]# ./configure --enable-frame-pointers  ####如果是64位系統要加--enable-frame-pointers參數

    [[email protected] google-perftools]# make &&make install    ###該模組編譯安裝後還要在nginx編譯時間將其添加進去

    [[email protected] nginx]# tar zxf libunwind-0.99.tar.gz

    [[email protected] nginx]# mv libunwind-0.99 libunwind    

    [[email protected] nginx]# cd libunwind

    [[email protected] libunwind]# ./configure CFLAGS=-fPIC

    [[email protected] libunwind]# make CFLAGS=-fPIC

    [[email protected] libunwind]# make install CFLAGS=-fPIC    ####該模組編譯安裝要加CFLAGS=-fPIC

    [[email protected] nginx]# tar zxf pcre-8.11.tar.gz

    [[email protected] nginx]# cd pcre-8.11

    [[email protected] pcre-8.11]# ./configure

    [[email protected] pcre-8.11]# make

    [[email protected] pcre-8.11]# make install    ####安裝nginx需要的Regex包,該軟體作用是nginx的rewrite功能需要它

    [[email protected] nginx]# tar zxf simpl-ngx_devel_kit-v0.2.17-10-g4192ba6.tar.gz

    [[email protected] nginx]# mv simpl-ngx_devel_kit-4192ba6/ simpl-ngx_devel_kit  ###該模組僅解壓即可,不需要編譯安裝,需要在編譯nginx時將其加進去  

    [[email protected] nginx]# ln -s /usr/local/lib/libprofiler.so.0 /lib/ 

    [[email protected] nginx]# ln -s /usr/local/lib/libprofiler.so.0 /usr/lib/

    [[email protected] nginx]# ln -s /usr/local/lib/libunwind.so.7 /lib/

    [[email protected] nginx]# ln -s /usr/local/lib/libunwind.so.7 /usr/lib/     ####這4步的軟連結需要做,我這裡是32位系統,如果是64位系統,可以做下面幾步:

        [[email protected] nginx]# ln -s /usr/local/lib/libprofiler.so.0 /lib/

        [[email protected] nginx]# ln -s /usr/local/lib/libprofiler.so.0 /usr/lib/

        [[email protected] nginx]# ln -s /usr/local/lib/libprofiler.so.0 /lib64/

        [[email protected] nginx]# ln -s /usr/local/lib/libprofiler.so.0 /usr/lib64/

        [[email protected] nginx]# ln -s /usr/local/lib/libunwind.so.7 /lib/

        [[email protected] nginx]# ln -s /usr/local/lib/libunwind.so.7 /usr/lib/

        [[email protected] nginx]# ln -s /usr/local/lib/libunwind.so.7 /lib64/

        [[email protected] nginx]# ln -s /usr/local/lib/libunwind.so.7 /usr/lib64/

    [[email protected] nginx]# tar zxf nginx-1.1.6.tar.gz

    [[email protected] nginx]# cd nginx-1.1.6

    [[email protected] 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 ####將上面解壓或者安裝好的各模組軟體添加進去

    [[email protected] nginx-1.1.6]# make

    [[email protected] nginx-1.1.6]# make install

    [[email protected] nginx]# useradd -M www   ####建立不帶使用者目錄www使用者

    [[email protected] nginx]# /usr/local/nginx/sbin/nginx   ####啟動nginx服務

    

    [[email protected] ~]# 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

    

    [[email protected] ~]# cat /usr/local/nginx/logs/nginx.pid   ####這兩步都發現有nginx服務進程,說明nginx啟動成功

        3324

    走到這裡,nginx安裝完成並且可以正常運行,下面對nginx最佳化,這裡對nginx的設定檔裡的參數最佳化,將nginx的原設定檔備份成nginx.conf_bak,下面是最佳化後的設定檔:

    [[email protected] ~]# mv nginx_laoshi.conf /usr/local/nginx/conf/nginx.conf

    [[email protected] ~]# 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 {

        listen8080;

        server_name stats.vfast.com.cn;

        location /status {

        stub_status on;

        access_log off;

        auth_basic "status";

        }

        }

        }

    這裡的設定檔參數調優是針對某台機器做的,不同機器的參數調優也略有不同,應按實際情況的做,調優完成後,要重啟nginx服務:

    [[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload  ####nginx服務平滑的重啟,或者也可以照下面的做也可以實現nginx平滑重啟:  

        [[email protected] ~]# ps -ef |grep nginx |grep master |awk ‘{print $2}‘   ###只尋找nginx主進程號

            3324

        [[email protected] ~]# kill -HUP 3324   ###nginx實現了平滑重啟

    ok,到這裡,nginx的編譯安裝以及調優完成。


本文出自 “個人感受” 部落格,謝絕轉載!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.