-
Notifications
You must be signed in to change notification settings - Fork 13
/
server-setup.sh
129 lines (115 loc) · 3.19 KB
/
server-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
#!/usr/bin/env bash
set -x
set -e
BIFF_ENV=${1:-prod}
CLJ_VERSION=1.11.1.1105
TRENCH_VERSION=0.3.0
echo waiting for apt to finish
while (ps aux | grep [a]pt); do
sleep 3
done
# Dependencies
apt-get update
apt-get upgrade
apt-get -y install default-jre rlwrap
curl -O https://download.clojure.org/install/linux-install-$CLJ_VERSION.sh
chmod +x linux-install-$CLJ_VERSION.sh
./linux-install-$CLJ_VERSION.sh
rm linux-install-$CLJ_VERSION.sh
wget https://github.com/athos/trenchman/releases/download/v$TRENCH_VERSION/trenchman_${TRENCH_VERSION}_linux_amd64.tar.gz
tar -xf trenchman_${TRENCH_VERSION}_linux_amd64.tar.gz
mv trench /usr/local/bin/
bash < <(curl -s https://raw.githubusercontent.com/babashka/babashka/master/install)
# Non-root user
useradd -m app
mkdir -m 700 -p /home/app/.ssh
cp /root/.ssh/authorized_keys /home/app/.ssh
chown -R app:app /home/app/.ssh
# Git deploys
set_up_app () {
cd
mkdir repo.git
cd repo.git
git init --bare
cat > hooks/post-receive << EOD
#!/usr/bin/env bash
git --work-tree=/home/app --git-dir=/home/app/repo.git checkout -f
cd /home/app
bb --force -e nil
bb post-receive
EOD
chmod +x hooks/post-receive
}
sudo -u app bash -c "$(declare -f set_up_app); set_up_app"
# Systemd service
cat > /etc/systemd/system/app.service << EOD
[Unit]
Description=app
StartLimitIntervalSec=500
StartLimitBurst=5
[Service]
User=app
Restart=on-failure
RestartSec=5s
Environment="BIFF_ENV=$BIFF_ENV"
WorkingDirectory=/home/app
ExecStart=/bin/sh -c '\$\$(bb --force -e nil; bb run-cmd)'
[Install]
WantedBy=multi-user.target
EOD
systemctl enable app
cat > /etc/systemd/journald.conf << EOD
[Journal]
Storage=persistent
EOD
systemctl restart systemd-journald
cat > /etc/sudoers.d/restart-app << EOD
app ALL= NOPASSWD: /bin/systemctl reset-failed app.service
app ALL= NOPASSWD: /bin/systemctl restart app
EOD
chmod 440 /etc/sudoers.d/restart-app
# Firewall
ufw allow OpenSSH
ufw enable
# Web dependencies
apt-get -y install nginx
snap install core
snap refresh core
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot
# Nginx
rm /etc/nginx/sites-enabled/default
cat > /etc/nginx/sites-available/app << EOD
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
root /home/app/target/resources/public;
client_max_body_size 10M;
location / {
try_files \$uri \$uri/index.html @resources;
}
location @resources {
root /home/app/resources/public;
try_files \$uri \$uri/index.html @proxy;
}
location @proxy {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Host \$host;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP \$remote_addr;
}
}
EOD
ln -s /etc/nginx/sites-{available,enabled}/app
# Firewall
ufw allow "Nginx Full"
# Let's encrypt
certbot --nginx
# App dependencies
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
apt-get -y install nodejs
bash < <(curl -s https://raw.githubusercontent.com/babashka/babashka/master/install)