# Nginx 1.23.3 源码安装



**Nginx源码安装脚本**

> \*\*系统环境：CentOS 7.4 \*\* **软件版本：1.16.1**

**\[root@localhost \~]# vi auto\_install\_nginx.sh**

```bash
#!/usr/bin/env bash
# ==============================
# 2020-3-13 10:22:30
# Author: ^~^
# Auto Install Nginx Soft
# 适用于 Centos 6.X、7.X 
# 适用于 Ubuntu （未测试），
# ==============================


NGINX_PREFIX=/usr/local/nginx1.23
NGINX_USER=www-data




# if nginx command is exists
if [[ `which nginx >/dev/null 2>&1 ; echo $?` == 0 ]];then
    echo "Error: which nginx  is already exists, exit 1"
    exit 1
fi



# if nginx prifix dir is  exists
if [[ -d "${NGINX_PREFIX}" ]];then
    echo "Error: ${NGINX_PREFIX} is already exists, exit 1"
    exit 1
fi



# install lib
if [[ `which yum >/dev/null 2>&1 ; echo $?` == 0 ]];then
    yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ wget lrzsz 

elif [[ `which apt >/dev/null 2>&1 ; echo $?` == 0 ]];then
    apt-get install libpcre3 libpcre3-dev
    apt-get install zlib1g-dev
    apt-get install openssl libssl-dev

else
    echo "Error: Your system does not support, exit 1"
    exit 1
fi


# download to dir
cd /usr/local/src
wget -c https://nginx.org/download/nginx-1.23.3.tar.gz
useradd -s /sbin/nologin www-data


# building
tar zvfx nginx-1.23.3.tar.gz \
&& cd nginx-1.23.3 \
&& ./configure --prefix=${NGINX_PREFIX} \
--with-http_dav_module \
--with-http_stub_status_module \
--with-http_addition_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_v2_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-pcre \
--user=${NGINX_USER} \
--group=${NGINX_USER} \
&& make \
&& make install \
&& ln -sf ${NGINX_PREFIX}/sbin/nginx /usr/bin/ \
&& nginx -version



# back old nginx.conf
mv ${NGINX_PREFIX}/conf/nginx.conf ${NGINX_PREFIX}/conf/nginx.conf.bak


# make new nginx.conf
cat > ${NGINX_PREFIX}/conf/nginx.conf << EOF

user www-data;
worker_processes auto;
pid /run/nginx.pid;


events {
    worker_connections 65535;
    use epoll;
    multi_accept on;
}


http {
    include   mime.types;
    log_format main '{ "@timestamp": "\$time_iso8601", '
                                '"remote_addr": "\$remote_addr", '
                                '"request_method": "\$request_method", '
                                '"uri": "\$uri", '
                                '"body_bytes_sent": \$body_bytes_sent, '
                                '"request_time": [\$request_time], '
                                '"upstream_response_time": "\$upstream_response_time", '
                                '"status": "\$status", '
                                '"upstream_status": "\$upstream_status", '
                                '"request": "\$request", '
                                '"http_referrer": "\$http_referer", '
                                '"http_x_forwarded_for": "\$http_x_forwarded_for", '
                                '"http_user_agent": "\$http_user_agent", '
                            '"host": "\$host", '
                            '"server_port": "\$server_port", '
                            '"upstream_addr": "\$upstream_addr", '
                            '"scheme": "\$scheme" }';


    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   300;
    client_header_buffer_size 4k;
    open_file_cache max=102400 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;
    client_header_timeout 15;
    client_body_timeout 15;
    reset_timedout_connection on;
    send_timeout 150;
    server_tokens off;
    client_max_body_size 1024m;
    types_hash_max_size 4096;
    gzip on;
    gzip_vary  on;
    gzip_min_length  1k;
    gzip_buffers     16 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
    gzip_proxied   expired no-cache no-store private auth;
    gzip_disable   "MSIE [1-6]\.";
    fastcgi_connect_timeout     600;
    fastcgi_send_timeout 600;
    fastcgi_read_timeout 600;
    fastcgi_buffer_size 64k;
    fastcgi_buffers  4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    fastcgi_temp_path nginx_tmp;
    fastcgi_intercept_errors on;
    fastcgi_cache_path fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;

   
    include vhosts/*.conf;
}


EOF


# mkdir logs dir  ===>>> start nginx ===>>> enabled start nginx for reboot
mkdir /var/log/nginx \
&& nginx -t \
&& nginx \
&& echo "nginx" >> /etc/rc.local \
&& chmod +x /etc/rc.local \
&& ps -ef |grep -v grep |grep ${NGINX_USER} \
&& echo "Install nginx success"
```
