Last active
April 11, 2021 19:37
-
-
Save cretudorin/196c7c605ac2787e237fd64a7fde0bea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ "$EUID" -ne 0 ] | |
then echo "Me wants sudo!" | |
exit | |
fi | |
echo " | |
************************************************************************************** | |
This script will try to compile and setup nginx as a dns load balancer. | |
Will do the following: | |
* check if deb-src is enabled for the main distro repository | |
* check which nginx version can be installed from the debian | |
* install nginx build dependencies | |
* download the nginx source with the same version and compile with stream module. | |
* offer to overwrite the config for systemd and nginx.conf | |
************************************************************************************** | |
" | |
read -p "Are you sure you want to continue? y/N " -n 1 -r | |
if [[ !($REPLY =~ ^[Yy]$) ]] | |
then | |
exit | |
fi | |
distro_codename=$(dpkg --status tzdata | grep Provides | cut -f2 -d'-') | |
prefix="/etc/nginx" | |
nginx_conf_path="/etc/nginx/nginx.conf" | |
systemd_unit_path="/etc/systemd/system/nginx.service" | |
temp_dir=$(mktemp) | |
debian_src_repo="/etc/apt/sources.list.d/$distro_codename-src.list" | |
update_systemd_unit () { | |
echo "[Unit]" >> $systemd_unit_path | |
echo "Description=nginx - high performance web server" >> $systemd_unit_path | |
echo "Documentation=https://nginx.org/en/docs/" >> $systemd_unit_path | |
echo "After=network-online.target remote-fs.target nss-lookup.target" >> $systemd_unit_path | |
echo "Wants=network-online.target" >> $systemd_unit_path | |
echo "" >> $systemd_unit_path | |
echo "[Service]" >> $systemd_unit_path | |
echo "Type=forking" >> $systemd_unit_path | |
echo "PIDFile=/var/run/nginx.pid" >> $systemd_unit_path | |
echo "ExecStartPre=/usr/sbin/nginx -t -c $nginx_conf_path" >> $systemd_unit_path | |
echo "ExecStart=/usr/sbin/nginx -c $nginx_conf_path" >> $systemd_unit_path | |
echo "ExecReload=/bin/kill -s HUP \$MAINPID" >> $systemd_unit_path | |
echo "ExecStop=/bin/kill -s TERM \$MAINPID" >> $systemd_unit_path | |
echo "" >> $systemd_unit_path | |
echo "[Install]" >> $systemd_unit_path | |
echo "WantedBy=multi-user.target" >> $systemd_unit_path | |
} | |
update_nginx_config () { | |
echo "user www-data;" >> $nginx_conf_path | |
echo "worker_processes $(nproc --all);" >> $nginx_conf_path | |
echo "" >> $nginx_conf_path | |
echo "error_log logs/error.log;" >> $nginx_conf_path | |
echo "error_log off;" >> $nginx_conf_path | |
echo "" >> $nginx_conf_path | |
echo "events {" >> $nginx_conf_path | |
echo " worker_connections 1024;" >> $nginx_conf_path | |
echo "}" >> $nginx_conf_path | |
echo "" >> $nginx_conf_path | |
echo "stream {" >> $nginx_conf_path | |
echo " upstream dns_servers {" >> $nginx_conf_path | |
echo " server 192.168.999.999:53;" >> $nginx_conf_path | |
echo " server 1.1.1.1:53 backup;" >> $nginx_conf_path | |
echo " server 8.8.8.8:53 backup;" >> $nginx_conf_path | |
echo " }" >> $nginx_conf_path | |
echo "" >> $nginx_conf_path | |
echo " server {" >> $nginx_conf_path | |
echo " listen 53 udp;" >> $nginx_conf_path | |
echo " listen 53; #tcp" >> $nginx_conf_path | |
echo " proxy_pass dns_servers;" >> $nginx_conf_path | |
echo " proxy_responses 1;" >> $nginx_conf_path | |
echo " proxy_timeout 1s;" >> $nginx_conf_path | |
echo " # enable for debugging only" >> $nginx_conf_path | |
echo " #error_log /var/log/nginx.dns.log info;" >> $nginx_conf_path | |
echo " }" >> $nginx_conf_path | |
echo "}" >> $nginx_conf_path | |
} | |
# distro main src repo not found | |
if [[ ! -f $debian_src_repo && -z $(cat /etc/apt/sources.list | grep "deb-src http.* $distro_codename main") ]] | |
then | |
# search for the dist main repo | |
if [[ -n $(cat /etc/apt/sources.list | grep "deb http.* $distro_codename main") ]] | |
then | |
echo $(cat /etc/apt/sources.list | grep "$distro_codename main" | sed 's/deb http/deb-src http/g') >> $debian_src_repo | |
# not found, will exit now | |
else | |
echo -e "\e[31m Error: Can't find the main repository for $distro_codename. Bye Bye" | |
exit | |
fi | |
fi | |
apt update | |
# get version | |
ngx_version=$(apt-cache policy nginx | grep Candidate | awk -F ':' '{print $2}' | awk -F '-' '{print $1}' | awk '{ gsub(/ /,""); print }') | |
# install dependencies | |
apt build-dep nginx | |
# get nginx source | |
cd $temp_dir | |
wget "https://nginx.org/download/nginx-$ngx_version.tar.gz" | |
tar zxvf nginx-$ngx_version.tar.gz | |
cd nginx-$ngx_version | |
# configure, make and install | |
./configure --prefix=$prefix \ | |
--sbin-path=/usr/sbin/nginx \ | |
--conf-path=$nginx_conf_path \ | |
--error-log-path=/var/log/nginx/error.log \ | |
--pid-path=/var/run/nginx.pid \ | |
--lock-path=/var/run/nginx.lock \ | |
--user=nginx \ | |
--group=nginx \ | |
--build=Debian \ | |
--with-stream | |
make | |
make install | |
if [[ ! -f $nginx_conf_path ]] | |
then | |
mkdir $prefix | |
update_nginx_config | |
else | |
echo "" | |
echo "nginx already exists in $nginx_conf_path" | |
read -p "Overwrite? y/N" -n 1 -r | |
echo "" | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
echo "" > $nginx_conf_path | |
update_nginx_config | |
fi | |
fi | |
if [ ! -f $systemd_unit_path ]; | |
then | |
update_systemd_unit | |
else | |
echo "" | |
echo "Systemd unit for nginx already exists" | |
read -p "Overwrite? y/N" -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
echo "" > $nginx_conf_path | |
update_nginx_config | |
fi | |
fi | |
## enable and start | |
systemctl enable 'nginx.service' | |
systemctl restart 'nginx.service' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment