forked from eduvpn/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_centos.sh
executable file
·251 lines (194 loc) · 8.73 KB
/
deploy_centos.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/bin/sh
#
# Deploy a single VPN machine
#
###############################################################################
# CONFIGURATION
###############################################################################
MACHINE_HOSTNAME=$(hostname -f)
# DNS name of the Web Server
printf "DNS name of the Web Server [%s]: " "${MACHINE_HOSTNAME}"; read -r WEB_FQDN
WEB_FQDN=${WEB_FQDN:-${MACHINE_HOSTNAME}}
VPN_STABLE_REPO=1
VPN_DEV_REPO=${VPN_DEV_REPO:-0}
if [ "${VPN_DEV_REPO}" = 1 ]
then
VPN_STABLE_REPO=0
fi
###############################################################################
# SYSTEM
###############################################################################
# SELinux enabled?
if ! /usr/sbin/selinuxenabled
then
echo "Please **ENABLE** SELinux before running this script!"
exit 1
fi
PACKAGE_MANAGER=/usr/bin/yum
###############################################################################
# SOFTWARE
###############################################################################
# disable and stop existing firewalling
systemctl disable --now firewalld >/dev/null 2>/dev/null || true
systemctl disable --now iptables >/dev/null 2>/dev/null || true
systemctl disable --now ip6tables >/dev/null 2>/dev/null || true
if grep -q "Red Hat" /etc/redhat-release
then
# RHEL
subscription-manager repos --enable=rhel-7-server-optional-rpms
subscription-manager repos --enable=rhel-7-server-extras-rpms
${PACKAGE_MANAGER} -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
else
# CentOS
${PACKAGE_MANAGER} -y install epel-release
fi
# import PGP key and add repository
rpm --import https://repo.letsconnect-vpn.org/2/rpm/RPM-GPG-KEY-LC
cat << EOF > /etc/yum.repos.d/LC-v2.repo
[LC-v2]
name=VPN Stable Packages (EL \$releasever)
baseurl=https://repo.letsconnect-vpn.org/2/rpm/epel-\$releasever-\$basearch
gpgcheck=1
enabled=${VPN_STABLE_REPO}
EOF
cat << EOF > /etc/yum.repos.d/LC-master.repo
[LC-master]
name=VPN Development Packages (EL \$releasever)
baseurl=https://vpn-builder.tuxed.net/repo/master/epel-\$releasever-\$basearch
gpgcheck=1
gpgkey=https://vpn-builder.tuxed.net/repo/master/RPM-GPG-KEY-LC
enabled=${VPN_DEV_REPO}
EOF
# install software (dependencies)
${PACKAGE_MANAGER} -y install mod_ssl php-opcache httpd iptables pwgen \
iptables-services php-fpm php-cli policycoreutils-python chrony
# install software (VPN packages)
${PACKAGE_MANAGER} -y install vpn-server-node vpn-server-api vpn-user-portal \
vpn-maint-scripts
###############################################################################
# SELINUX
###############################################################################
# allow Apache to connect to PHP-FPM
setsebool -P httpd_can_network_connect=1
# allow OpenVPN to bind to the management ports
semanage port -a -t openvpn_port_t -p tcp 11940-16036
# allow OpenVPN to bind to additional ports for client connections
semanage port -a -t openvpn_port_t -p tcp 1195-5290
semanage port -a -t openvpn_port_t -p udp 1195-5290
###############################################################################
# APACHE
###############################################################################
# Use a hardened ssl.conf instead of the default, gives A+ on
# https://www.ssllabs.com/ssltest/
cp resources/ssl.conf /etc/httpd/conf.d/ssl.conf
cp resources/localhost.centos.conf /etc/httpd/conf.d/localhost.conf
# Switch to MPM event (https://httpd.apache.org/docs/2.4/mod/event.html)
sed -i "s|^LoadModule mpm_prefork_module modules/mod_mpm_prefork.so$|#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so|" /etc/httpd/conf.modules.d/00-mpm.conf
sed -i "s|^#LoadModule mpm_event_module modules/mod_mpm_event.so$|LoadModule mpm_event_module modules/mod_mpm_event.so|" /etc/httpd/conf.modules.d/00-mpm.conf
# php-fpm configuration (taken from Fedora php-fpm package, only required on
# CentOS)
cp resources/php.conf /etc/httpd/conf.d/php.conf
# VirtualHost
cp resources/vpn.example.centos.conf "/etc/httpd/conf.d/${WEB_FQDN}.conf"
sed -i "s/vpn.example/${WEB_FQDN}/" "/etc/httpd/conf.d/${WEB_FQDN}.conf"
###############################################################################
# PHP
###############################################################################
# switch to unix socket and secure it, the default in newer PHP versions, but
# not on CentOS 7
sed -i "s|^listen = 127.0.0.1:9000$|listen = /run/php-fpm/www.sock|" /etc/php-fpm.d/www.conf
sed -i "s|;listen.mode = 0666|listen.mode = 0660|" /etc/php-fpm.d/www.conf
sed -i "s|;listen.group = nobody|listen.group = apache|" /etc/php-fpm.d/www.conf
# set timezone to UTC
cp resources/70-timezone.ini /etc/php.d/70-timezone.ini
# work around to create the session directory, otherwise we have to install
# the PHP package, this is only on CentOS
mkdir -p /var/lib/php/session
chown -R root.apache /var/lib/php/session
chmod 0770 /var/lib/php/session
restorecon -R /var/lib/php/session
###############################################################################
# VPN-SERVER-API
###############################################################################
# update the IPv4 CIDR and IPv6 prefix to random IP ranges
vpn-server-api-update-ip --profile internet --host "${WEB_FQDN}"
# initialize the CA
sudo -u apache vpn-server-api-init
###############################################################################
# VPN-USER-PORTAL
###############################################################################
# DB init
sudo -u apache vpn-user-portal-init
###############################################################################
# NETWORK
###############################################################################
cat << EOF > /etc/sysctl.d/70-vpn.conf
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
# allow RA for IPv6 on external interface, NOT for static IPv6!
#net.ipv6.conf.eth0.accept_ra = 2
EOF
sysctl --system
###############################################################################
# UPDATE SECRETS
###############################################################################
# update API secret
vpn-server-api-update-api-secrets
###############################################################################
# CERTIFICATE
###############################################################################
# generate self signed certificate and key
openssl req \
-nodes \
-subj "/CN=${WEB_FQDN}" \
-x509 \
-sha256 \
-newkey rsa:2048 \
-keyout "/etc/pki/tls/private/${WEB_FQDN}.key" \
-out "/etc/pki/tls/certs/${WEB_FQDN}.crt" \
-days 90
###############################################################################
# DAEMONS
###############################################################################
systemctl enable --now php-fpm
systemctl enable --now httpd
###############################################################################
# OPENVPN SERVER CONFIG
###############################################################################
# NOTE: the openvpn-server systemd unit file only allows 10 OpenVPN processes
# by default!
# generate the OpenVPN server configuration files and certificates
vpn-server-node-server-config
# enable and start OpenVPN
systemctl enable --now openvpn-server@internet-0
systemctl enable --now openvpn-server@internet-1
###############################################################################
# FIREWALL
###############################################################################
cp resources/firewall/iptables /etc/sysconfig/iptables
cp resources/firewall/ip6tables /etc/sysconfig/ip6tables
systemctl enable --now iptables
systemctl enable --now ip6tables
###############################################################################
# USERS
###############################################################################
REGULAR_USER="demo"
REGULAR_USER_PASS=$(pwgen 12 -n 1)
# the "admin" user is a special user, listed by ID to have access to "admin"
# functionality in /etc/vpn-user-portal/config.php (adminUserIdList)
ADMIN_USER="admin"
ADMIN_USER_PASS=$(pwgen 12 -n 1)
sudo -u apache vpn-user-portal-add-user --user "${REGULAR_USER}" --pass "${REGULAR_USER_PASS}"
sudo -u apache vpn-user-portal-add-user --user "${ADMIN_USER}" --pass "${ADMIN_USER_PASS}"
###############################################################################
# SHOW INFO
###############################################################################
echo "########################################################################"
echo "# Portal"
echo "# https://${WEB_FQDN}/"
echo "# Regular User: ${REGULAR_USER}"
echo "# Regular User Pass: ${REGULAR_USER_PASS}"
echo "#"
echo "# Admin User: ${ADMIN_USER}"
echo "# Admin User Pass: ${ADMIN_USER_PASS}"
echo "########################################################################"