Nginx安裝

1.下載相關組件
yum install -y gcc gcc-c++                                   #安裝C/C++編譯器
yum -y install gd-devel geoip-devel perl-ExtUtils-Embed
wget http://125.39.35.133/files/40450000042A3380/zlib.net/zlib-1.2.8.tar.gz
wget http://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz
wget https://www.openssl.org/source/openssl-1.0.2h.tar.gz
wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
wget http://nginx.org/download/nginx-1.6.2.tar.gz
 
2.順次解壓安裝zlib/pcre/openssl/nginx並安裝
zlib/pcre這些庫文件直接 ./configure &&make &&make install
tar xf openssl-1.0.2h.tar.gz -C /root
cd /root/openssl-1.0.2h
./config --prefix=/usr/local --openssldir=/usr/local/openssl
make
make install

 

 

 

 


cd /root/nginx-1.6.2

./configure --add-module=/root/ngx_cache_purge-2.1 --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'  --with-http_degradation_module --with-http_perl_module --with-ld-opt=-Wl,-E --with-mail_ssl_module --with-http_image_filter_module --with-http_geoip_module --with-http_ssl_module --with-openssl=/root/openssl-1.0.2h 

makemake install

錯誤信息

/usr/local/nginx/sbin/nginx: error whileloading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解決方法:
cd /lib64
ln -s libpcre.so.0.0.libpcre.so.1
 
 
啓動方法:
啓動:nginx
停止:nginx -s stop
重載:nginx reload
 
3.虛擬主機
基於域名的虛擬主機:
     server {
          listen 80;
          server_name www1.zyg.com;
          root /etc/nginx/www1;                            #創建目錄
          index index.html;
     }
     server {
          listen 80;
          server_name www2.zyg.com;
          root /etc/nginx/www2;
          index index.html;
     }
基於IP的虛擬主機:
     server {
          listen 192.168.122.11:80;
          root /etc/nginx/www1;
          index index.html;
     }
     server {
          listen 2.2.2.1:80;
          root /etc/nginx/www2;
          index index.html;
     }
基於端口的虛擬主機:
     server {
          listen 8001;
          root /etc/nginx/www1;
          index index.html;
     }
     server {
          listen 8002;
          root /etc/nginx/www2;
          index index.html;
     }
 
4.訪問控制
    server {
        listen 8001;
        root www1;
        index index.html;
        auth_basic "test";                #一個提示
        auth_basic_user_file /usr/local/nginx/passwd.db;
    }

    server {
        listen 8002;
        root www2;
        index index.html;
        allow 192.168.122.0/24;
        deny all;
    }
[root@node1 nginx]# htpasswd -c /usr/local/nginx/passwd.db power     #給power設置密碼
 
5.平滑升級
• 使用新的可執行程序替換舊的可執行程序,對於編譯安裝的Nginx,可以將新版本編譯安裝到舊版本的nginx安裝路徑中.替換之前,最好備份一下舊的可執行程序
• 發送以下指令:
Kill USR2 <舊版本的nginx主進程號>
• 舊版本的主進程將重命名它的pid文件爲.oldbin (例如:/usr/local/nginx/logs/nginx.pid.oldbin),然後執行新版本的nginx可執行程序,依次啓動新的主進程和新的工作進程.
• 此時,新舊版本的nginx實例會同時運行,共同處理輸入的請求.要逐步停止舊版本的nginx實例,你必須發送WINCH信號給舊的主進程,然後,它的工作進程就將開始從容關閉:
    kill WINCH <舊版本的Nginx主進程號>
• 一段時間後,舊的工作進程(worker process)處理了所有已連接的請求後退出,僅由新的工作進程來處理輸入的請求了.
• 這時候,我們可以決定是使用新版本,還是回覆到舊的版本;
    Kill HUP <舊的主進程號>:Nginx將在不重載配置文件的情況下啓動它的工作進程;
    Kill QUIT <新的主進程號>:正常關閉其它工作進程(woker process);
    如果此時報錯, 提示還有進程沒有結束就用下面命令先關閉舊工作進程,再關閉主進程號:
    Kill TERM <新的工作進程號>:強制退出工作進程;
    Kill <新的主進程號或舊的主進程號>:如果因爲某些原因新的工作進程不能退出,則向其發送kill信號.
  新的主進程退出後,舊的主進程會移除.oldbin前綴,恢復爲他的.pid文件,這樣,一切就都恢復到升級之前了,如果嘗試升級成功,而你也希望保留新的服務器時,可發送QUIT信號給舊的主進程,使其退出而只留下新的服務器運行
 
6.日誌切割
  mv /data1/logs/access.log /data1/logs/20160520.log
  kill -USR1 'cat /var/run/nginx/nginx.pid'  #這樣的話就不用ps查找Nginx主進程號了.

  如果要讓它每天定時切割日誌,可以編寫shell腳本.並且利用crontab來每天定時運行.

  #!/bin/bash
  #定義Nginx日誌文件的存放路徑
  logs_path = "/var/logs/nginx/"

  mkdir -p ${logs_path}$(date -d "yesterday" + "%Y")/$(date -d "yesterday" +"%m")/
  mv ${logs_path}access.log  ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday"+"%m")/access_$(date -d "yesterday" + "%Y%m%d").log
  kill -USR1 'cat /var/run/nginx/nginx.pid'

  再配置crontab,輸入crontab -e
  輸入:
  00 00 * * * /bin/bash /usr/loca/webserver/nginx/sbin/cut_nginx_log.sh
發佈了31 篇原創文章 · 獲贊 30 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章