快速安裝nginx

一、建立nignx用戶

/usr/sbin/groupadd -f nginx
/usr/sbin/useradd -g nginx nginx

二、安裝依賴

yum install gcc  gcc-c++ -y #默認有的話無須安裝
yum install -y pcre-devel openssl-devel #依賴(支持Nginx服務訪問,以https方式訪問)

三、下載軟件包&編譯安裝

wget -q http://nginx.org/download/nginx-1.14.2.tar.gz #下載軟件包
tar xf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx-1.14.2 --user=nginx --group=nginx --with-http_stub_status_module  --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module #根據你的需求添加編譯時要帶的模塊
make && make install 

四、軟連接啓動

ln -s /usr/local/nginx-1.14.2 /usr/local/nginx
# 這條ln命令的意義十分深遠重大。這是生產環境的經驗。
# 將Nginx安裝路徑經過軟連接方式更改成/usr/local/nginx/,方便人員使用。
# 安裝時指定版本號路徑是爲了便於查看分區當前使用的版本,也方便之後升級。
/usr/local/nginx/sbin/nginx   #啓動nginx 若報錯說明沒有建立nginx用戶或者是
ln -s /usr/local/nginx-1.14.2/sbin/nginx /usr/local/sbin/  #作條軟鏈接直接用nginx啓動
在nginx.conf中 把user nobody的註釋去掉既可
netstat -lntup|grep 80 查看是否有80端口

五、查看nginx的編譯參數

#/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.14.2 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module

六、檢查語法&平滑重啓

nginx -t   #檢查語法是否正常
nginx -s reload  #平滑重啓  

七、精簡nginx配置文件

egrep -v "#|^$" nginx.conf.default >nginx.conf  #精簡化/最小化默認nginx.conf配置文件信息

八、配置nginx負載均衡

# vim conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream wang.com{  #服務器集羣的名字
        server 10.10.16.223:8080 weight=1;
        server 10.10.16.252:8080 weight=2;

 } 
    server {
        listen       80;
        server_name  localhost;
        location / {
        proxy_pass http://wang.com;  #以上面對應 
        proxy_redirect default;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

   標準的配置請寫在vhost下面:javascript

[root@linux-node1 conf]# cat nginx.conf
#user  www;
worker_processes  auto;
worker_rlimit_nofile 204800;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    log_format  main  '$remote_addr - $host [$time_local] "$request" '
                    '$status $body_bytes_sent  "$http_referer" '
                   '"$http_user_agent" "$http_x_forwarded_for" $request_time $upstream_addr $upstream_status $upstream_response_time $bytes_sent';
    
    gzip on ;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml application/json application/javascript text/javascript image/jpeg image/gif image/png;
    gzip_disable "MSIE [1-6].";
    gzip_vary on;
    #其餘的在着重添加優化參數

   include vhost/*.conf;

}

   處理邏輯的放在vhost下面:css

[root@shenzhen conf]# pwd
/usr/local/nginx/conf
[root@shenzhen conf]# cat vhost/gunicorn.conf 
server {
    listen  80;
    server_name xx.xxx.xxx.xxx;

    #location  / {
    #            root   html;
    #        index  index.html index.htm;
    #    }

    location / {
        proxy_pass http://127.0.0.1:5001;
        proxy_redirect default;
      }


    error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
           }

    access_log /data/logs/nginx/gunicorn_access.log main;
    error_log /data/logs/nginx/gunicorn_error.log;

}

九、終結版

  一鍵安裝openresty,配置優化等html

[root@linux-node1 ~]# cd /usr/local/src
[root@linux-node1 src]# git clone https://github.com/unixwang/linux-package.git
[root@linux-node1 src]# cd linux-package/
[root@linux-node1 linux-package]# yum localinstall -y jemalloc-4.5.0-1.x86_64.rpm openresty-1.15.8.1-1.x86_64.rpm
[root@linux-node1 vhost]# cd /usr/local/openresty/nginx/conf/vhos
[root@linux-node1 vhost]# /usr/local/openresty/nginx/sbin/nginx

相關文章
相關標籤/搜索