-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvds_setup.sh
executable file
·204 lines (150 loc) · 6.07 KB
/
vds_setup.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
# --------------------------------------------------------
# THIS SCRIPT AUTOMATICALLY CONFIGURES DOCKER ORIENTED VDS
# --------------------------------------------------------
set -ae
. ./.env
set +a
if [[ $(id -u) != 0 ]]; then
echo "Run script with superuser privileges!"
exit 1
fi
if [[ "$VDS_IS_REMOTE" = "y" ]] && [[ -z "$HOST_IP" ]]; then
HOST_IP=$(curl -s https://api.ipify.org)
fi
# update system
echo -e "\n *** Update system ***"
echo -e "-------------------------------------------\n"
apt update
apt dist-upgrade -y
apt full-upgrade -y
apt install -ym git openssl software-properties-common bc wget curl cron python3 mc tree make
apt autoremove -y
apt autoclean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
echo "kernel.exec-shield=1" >> /etc/sysctl.conf
echo "kernel.randomize_va_space=1" >> /etc/sysctl.conf
echo "net.ipv4.conf.all.rp_filter=1" >> /etc/sysctl.conf
echo "net.ipv4.conf.all.log_martians=1" >> /etc/sysctl.conf
# create main user
echo -e "\n *** Create main User ***"
echo -e "-------------------------------------------\n"
echo "Creating user $VDS_USER"
adduser ${VDS_USER}
usermod -a -G sudo,www-data ${VDS_USER}
# ssh
echo -e "\n *** Configure SSH ***"
echo -e "-------------------------------------------\n"
apt install -ym openssh-client openssh-server
if [[ -f "/etc/ssh/sshd_config" ]]; then
cp -av /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sed -e "s/PermitRootLogin prohibit-password/PermitRootLogin no/g; s/PermitRootLogin yes/PermitRootLogin no/g" /etc/ssh/sshd_config.bak > /etc/ssh/sshd_config
fi
mkdir -vp -m 700 /root/.ssh
touch /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
chown -R root:root /root/.ssh
echo -e "\n${ID_RSA_PUB}" >> /root/.ssh/authorized_keys
mkdir -vp -m 700 "/home/${VDS_USER}/.ssh"
touch "/home/${VDS_USER}/.ssh/authorized_keys"
chmod 600 "/home/${VDS_USER}/.ssh/authorized_keys"
echo -e "\n${ID_RSA_PUB}" >> "/home/${VDS_USER}/.ssh/authorized_keys"
ssh-keygen -t rsa -P "" -f "/home/${VDS_USER}/.ssh/id_rsa"
ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa
chown -R "${VDS_USER}:${VDS_USER}" "/home/${VDS_USER}/.ssh"
service ssh restart
# fail2ban
echo -e "\n *** Configure Fail2ban ***"
echo -e "-------------------------------------------\n"
add-apt-repository -y universe
apt update
apt install -ym fail2ban
if [[ -f "/etc/fail2ban/jail.d/defaults-debian.conf" ]]; then
echo -e "\n[sshd-ddos]\nenabled = true\n" >> /etc/fail2ban/jail.d/defaults-debian.conf
fi
service fail2ban start
# firewall
echo -e "\n *** Configure Firewall ***"
echo -e "-------------------------------------------\n"
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow http
ufw allow https
# docker ports
ufw allow 2377/tcp
ufw allow 7946
ufw allow 4789/udp
ufw enable
ufw status
# docker
echo -e "\n *** Install Docker ***"
echo -e "-------------------------------------------\n"
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
touch /etc/apt/sources.list.d/docker.list
echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" >> /etc/apt/sources.list.d/docker.list
echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) edge" >> /etc/apt/sources.list.d/docker.list
#echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) nightly" >> /etc/apt/sources.list.d/docker.list
apt update
apt install -ym docker-ce
curl -L https://github.com/docker/compose/releases/download/1.29.1/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
groupadd -f docker
usermod -a -G docker $VDS_USER
usermod -a -G docker root
systemctl enable docker
# self-signed certificates
echo -e "\n *** Create self-signed Certificate ***"
echo -e "-------------------------------------------\n"
mkdir -pv /etc/certs/self-signed
openssl dhparam -out /etc/certs/dhparam.pem -dsaparam 4096
openssl req -x509 -nodes -newkey rsa:4096 -days 36500 -keyout /etc/certs/self-signed/cert.key -out /etc/certs/self-signed/cert.crt -subj "/C=RU/ST=RU/L=Internet/O=$(hostname -s)/CN=${HOST_IP}"
# nginx proxy
echo -e "\n *** Install Web Server ***"
echo -e "-------------------------------------------\n"
add-apt-repository -y ppa:ondrej/nginx-mainline
apt update
apt install -y nginx
if [[ -f "$PWD/nginx/nginx.conf" ]]; then
mv -v /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
cp -v "$PWD/nginx/nginx.conf" /etc/nginx/nginx.conf
chown root:root /etc/nginx/nginx.conf
chmod 644 /etc/nginx/nginx.conf
mkdir -pv /tmp/nginx-proxy-cache
nginx -s reload
else
echo "Custom nginx.conf not found. Keep default."
fi
mkdir -pv -m 775 /var/www
chown -R "${VDS_USER}:www-data" /var/www
# let's encrypt
if [[ "$VDS_IS_REMOTE" = "y" ]]; then
git clone https://github.com/Neilpang/acme.sh.git
apt install -ym socat
cd "$PWD/acme.sh"
/bin/bash acme.sh --install --accountemail "${ADMIN_EMAIL}" --cert-home /etc/certs
cd "$PWD/.."
rm -r "$PWD/acme.sh"
source /root/.bashrc
/root/.acme.sh/acme.sh --upgrade --auto-upgrade
fi
# portainer
echo -e "\n *** Install Docker manager ***"
echo -e "-------------------------------------------\n"
docker run -d -p 9000:9000 -p 9443:9443 -v /etc/certs/self-signed:/certs -v /var/run/docker.sock:/var/run/docker.sock -v /opt/portainer:/data --restart always --name portainer portainer/portainer-ce --ssl --sslcert /certs/cert.crt --sslkey /certs/cert.key
sed "s/portainer/${PORTAINER_DOMAIN}/g" "$PWD/nginx/portainer.conf" > /etc/nginx/conf.d/portainer.conf
chmod 644 /etc/nginx/conf.d/portainer.conf
nginx -s reload
# result
echo -e "\n *** All Done! ***"
echo "-------------------------------------------"
echo "Connect to VDS: ssh ${VDS_USER}@${HOST_IP}"
echo "Docker manager: https://${PORTAINER_DOMAIN}"
if [[ "$VDS_IS_REMOTE" = "y" ]]; then
echo "An ACME Shell script: https://github.com/Neilpang/acme.sh"
else
echo "Don't forget to add '${HOST_IP} ${PORTAINER_DOMAIN}' to /etc/hosts"
fi
echo -e "Reboot VDS to complete installation: sudo reboot\n"
exit 0