-
Notifications
You must be signed in to change notification settings - Fork 7
/
brotli.sh
177 lines (143 loc) · 4.81 KB
/
brotli.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
# version 2.1
# version 2.1
# date: 2021-04-27
# changes:
# - fix minor issues
# version 2
# date: 2021-04-27
# changes:
# - migrate to Google repo
# - test with Ubuntu 20.04
# use it while developing / testing.
# set -o errexit -o pipefail -o noclobber -o nounset
# set -x
# compile Nginx from the official repo with brotli compression
[ ! -d ${HOME}/log ] && mkdir ${HOME}/log
G_DIR="$(pwd)"
# logging everything
log_file=${HOME}/log/brotli.log
exec > >(tee -a ${log_file} )
exec 2> >(tee -a ${log_file} >&2)
# Defining return code check function
check_result() {
if [ $1 -ne 0 ]; then
echo "Error: $2"
exit $1
fi
}
export DEBIAN_FRONTEND=noninteractive
printf '%-72s' "Updating apt repos..."
sudo apt-get -qq update
echo done.
printf '%-72s' "Installing pre-requisites..."
sudo apt-get -qq install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip
echo done.
codename=$(lsb_release -cs)
# function to add the official Nginx.org repo
nginx_repo_add() {
distro=$(gawk -F= '/^ID=/{print $2}' /etc/os-release)
if [ "$codename" == "juno" ] ; then
codename=bionic
fi
if [ "$distro" == "elementary" ] ; then
distro=ubuntu
fi
if [ "$distro" == "linuxmint" ] ; then
distro=ubuntu
fi
# Remove old key if it exists
curl -LSs -o /tmp/nginx_signing.key http://nginx.org/keys/nginx_signing.key
check_result $? 'Nginx key could not be downloaded!'
sudo apt-key add /tmp/nginx_signing.key &> /dev/null
check_result $? 'Nginx key could not be added!'
sudo mv /tmp/nginx_signing.key /etc/apt/trusted.gpg.d/nginx_signing.asc
check_result $? 'Nginx key could not be added to apt trusted key storage!'
# for updated info, please see https://nginx.org/en/linux_packages.html#stable
nginx_branch= # leave this empty to install stable version
# or nginx_branch="mainline"
if [ "$nginx_branch" == 'mainline' ]; then
nginx_src_url="https://nginx.org/packages/mainline/${distro}/"
else
nginx_src_url="https://nginx.org/packages/${distro}/"
fi
[ -f /etc/apt/sources.list.d/nginx.list ] && sudo rm /etc/apt/sources.list.d/nginx.list
echo "deb [arch=amd64] ${nginx_src_url} ${codename} nginx" | sudo tee /etc/apt/sources.list.d/nginx.list &> /dev/null
echo "deb-src [arch=amd64] ${nginx_src_url} ${codename} nginx" | sudo tee -a /etc/apt/sources.list.d/nginx.list &> /dev/null
# finally update the local apt cache
sudo apt-get update -qq
check_result $? 'Something went wrong while updating apt repos.'
}
case "$codename" in
"stretch")
nginx_repo_add
;;
"xenial")
nginx_repo_add
;;
"bionic")
nginx_repo_add
;;
"focal")
nginx_repo_add
;;
"juno")
codename=bionic
nginx_repo_add
;;
"tara")
codename=bionic
nginx_repo_add
;;
*)
echo "Distro: $codename"
echo 'Warning: Could not figure out the distribution codename. Continuing to install Nginx from the OS.'
;;
esac
sudo install -o ${UID} -g $(id -gn $USER) -d /usr/local/src/${USER}
cd /usr/local/src/${USER}
sudo apt-get source nginx
sudo apt-get build-dep nginx -y
if [ ! -d ngx_brotli ]; then
git clone -q --recursive https://github.com/google/ngx_brotli
else
git -C ngx_brotli pull -q origin master
fi
[ -L /usr/local/src/ngx_brotli ] && sudo rm /usr/local/src/ngx_brotli
sudo ln -s /usr/local/src/${USER}/ngx_brotli /usr/local/src/ngx_brotli
sudo chown ${USER}:$USER /usr/local/src/ngx_brotli
cd /usr/local/src/${USER}/nginx-*/
# modify the existing config
sed -i -e '/\.\/configure/ s:$: --add-module=/usr/local/src/ngx_brotli:' debian/rules
# if gcc 8 is installed add patch to nginx
if [ "$(gcc -dumpversion)" == "8" ]; then
mkdir -p debian/patches
cp ${G_DIR}/gcc-8_fix.diff debian/patches/gcc-8_fix
echo "gcc-8_fix" > debian/patches/series
fi
# build the updated pacakge
dpkg-buildpackage -b
# optional
# install the updated package in the current server
cd /usr/local/src/${USER}
sudo dpkg -i nginx_*.deb
# take a backup
[ ! -d ~/backups/nginx-$(date +%F) ] && mkdir -p ~/backups/nginx-$(date +%F)
cp nginx*.deb ~/backups/nginx-$(date +%F)/
# print info about remove all the sources and apt sources file
cd ~/
printf "
# To clean up after install You can run
rm -rf /usr/local/src/$(echo ${USER})/nginx*
rm -rf /usr/local/src/$(echo ${USER})/ngx_brotli
sudo rm /etc/apt/sources.list.d/nginx.list
sudo apt-get -qq update
"
# hold the package nginx from updating accidentally in the future by someone else!
sudo apt-mark hold nginx
sudo apt-mark hold nginx-dbg
# stop the previously running instance, if any
sudo nginx -t && sudo systemctl stop nginx
# start the new Nginx instance
sudo nginx -t && sudo systemctl start nginx
echo 'All done.'