快速安装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

相关文章
相关标签/搜索