#!/bin/bash

apt-get install -y zlib1g-dev libgd-dev

mkdir /data/download

wget -P /data/download/ https://openresty.org/download/openresty-1.21.4.1.tar.gz
wget -P /data/download/ wget https://github.com/openssl/openssl/archive/refs/tags/OpenSSL_1_1_1q.zip
wget -P /data/download/ https://jaist.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.zip
wget -O /data/download/geoip2.zip https://github.com/leev/ngx_http_geoip2_module/archive/refs/heads/master.zip
wget -O /data/download/upstream_check.zip https://github.com/yaoweibin/nginx_upstream_check_module/archive/refs/heads/master.zip

tar zxf /data/download/openresty-1.21.4.1.tar.gz -C /data/download/
unzip /data/download/OpenSSL_1_1_1q.zip -d /data/download/
unzip /data/download/pcre-8.45.zip -d /data/download/
unzip /data/download/geoip2.zip -d /data/download/
unzip /data/download/upstream_check.zip  -d /data/download/

cd /data/download/openresty-1.21.4.1/bundle/nginx-1.21.4
patch -p1 < /data/download/nginx_upstream_check_module-master/check_1.20.1+.patch

cd /data/download/openresty-1.21.4.1

./configure --prefix=/data/openresty \
            --with-threads \
            --with-http_auth_request_module \
            --with-http_gunzip_module \
            --with-stream_ssl_preread_module \
            --with-http_stub_status_module \
            --with-http_v2_module \
            --with-select_module \
            --with-http_realip_module \
            --with-stream_realip_module \
            --with-http_secure_link_module \
            --with-http_flv_module \
            --with-http_mp4_module \
            --with-file-aio \
            --with-http_slice_module \
            --with-http_gzip_static_module \
            --with-pcre=/data/download/pcre-8.45 \
            --with-openssl=/data/download/openssl-OpenSSL_1_1_1q \
            --add-module=/data/download/nginx_upstream_check_module-master \
            --add-module=/data/download/ngx_http_geoip2_module-master \
            --with-http_image_filter_module \
            --with-stream \
            --with-stream_ssl_module \
            --with-http_ssl_module \
|> make && make install

cat >> /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
User=root
Group=root
ExecStartPre=/data/openresty/nginx/sbin/nginx -t
ExecStart=/data/openresty/nginx/sbin/nginx
ExecReload=/data/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable nginx --now

cat >> /root/.ssh/authorized_keys << EOF
ssh-rsa [payload removed: 544 characters] root@vn-vdc-192-168-100-132
EOF

