forked from postalsys/emailengine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
308 lines (244 loc) · 7.58 KB
/
install.sh
File metadata and controls
308 lines (244 loc) · 7.58 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
set -e
DOMAIN_NAME=$1
APP_URL="https://github.com/postalsys/emailengine/releases/latest/download/emailengine.tar.gz"
show_info () {
echo "Usage: $0 <domain-name>"
echo "Where"
echo " <domain-name> is the domain name for EmailEngine, eg. \"example.com\""
}
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
show_info
exit 1
fi
if [ "$DOMAIN_NAME" = "help" ]; then
show_info
exit
fi
if [ "$DOMAIN_NAME" = "-h" ]; then
show_info
exit
fi
echo
echo "NB! This install script works on public-facing servers, do not run it on local instances."
echo "The installer tries to provision an HTTPS certificate, and the process will fail if the server
is inaccessible from the public web.
"
read -n 1 -s -r -p "Press any key to continue..."
echo "
"
if ! [ -x `command -v curl` ]; then
apt-get update
apt-get install curl -q -y
echo ""
fi
if [[ -z $DOMAIN_NAME ]]; then
echo "Enter the domain name for your new EmailEngine installation."
echo "(ex. example.com or test.example.com)"
echo "Leave empty to autogenerate a domain name."
while [ -z "$DOMAIN_NAME" ]
do
#echo -en "\n"
read -p "Domain/Subdomain name: " DOMAIN_NAME
if [ -z "$DOMAIN_NAME" ]
then
DOMAIN_NAME=$(curl --silent --fail -XPOST "https://api.nodemailer.com/autoassign" -H "Content-Type: application/json" -d '{
"prefix": "engine",
"version": "1",
"requestor": "install"
}')
fi
done
fi
echo "Using the domain name \"${DOMAIN_NAME}\" for this installation."
# Prepare Caddy
apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
# Install Redis and Caddy and required tools
apt-get update
apt-get install redis-server caddy wget openssl -q -y
# Generate secure credentials
REDIS_PASSWORD=$(openssl rand -hex 32)
EENGINE_SECRET=$(openssl rand -hex 32)
# Configure Redis for production use
systemctl stop redis-server
# Backup original Redis config
cp /etc/redis/redis.conf /etc/redis/redis.conf.backup
# Configure Redis with authentication and production settings
cat >> /etc/redis/redis.conf <<EOF
# EmailEngine production configuration
requirepass $REDIS_PASSWORD
maxmemory-policy noeviction
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
EOF
# Start Redis with new configuration
systemctl start redis-server
systemctl enable redis-server
# Just in case the installation does not start Caddy already
systemctl enable caddy
systemctl start caddy
# Download and extract EmailEngine executable
TMPDIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'ee')
cd $TMPDIR
if ! [ -x `command -v wget` ]; then
if ! [ -x `command -v curl` ]; then
echo "Can not download application"
exit 1
else
curl "$APP_URL" -L -o emailengine.tar.gz
fi
else
# use wget do download EmailEngine
wget "$APP_URL"
fi
tar xzf emailengine.tar.gz
rm -rf emailengine.tar.gz
mv emailengine /opt
chmod +x /opt/emailengine
rm -rf $TMPDIR
# Create dedicated user for EmailEngine
if ! id -u emailengine >/dev/null 2>&1; then
useradd -r -s /bin/false -d /opt/emailengine -m emailengine
fi
# Set proper ownership
chown emailengine:emailengine /opt/emailengine
# Create unit file for EmailEngine
echo "[Unit]
Description=EmailEngine
After=redis-server
[Service]
# Configure environment variables
Environment=\"EENGINE_REDIS=redis://:${REDIS_PASSWORD}@127.0.0.1:6379/8\"
Environment=\"EENGINE_PORT=3000\"
Environment=\"EENGINE_SECRET=${EENGINE_SECRET}\"
Environment=\"EENGINE_API_PROXY=true\"
Environment=\"EENGINE_WORKERS=8\"
Environment=\"EENGINE_LOG_LEVEL=info\"
# Triggers install script specific upgrade instructions
Environment=\"EENGINE_INSTALL_SCRIPT=true\"
# Folder where EmailEngine executable is located
WorkingDirectory=/opt
# EmailEngine runs as dedicated user
User=emailengine
Group=emailengine
# Run the EmailEngine executable
ExecStart=/opt/emailengine
Type=simple
Restart=always
SyslogIdentifier=emailengine
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/emailengine.service
systemctl daemon-reload
systemctl enable emailengine
systemctl restart emailengine
# Create Caddyfile with security headers
echo "
:80 {
redir https://${DOMAIN_NAME}{uri}
}
${DOMAIN_NAME} {
reverse_proxy localhost:3000 {
header_up X-Real-IP {remote_host}
header_up X-Forwarded-For {remote_host}
header_up X-Forwarded-Proto {scheme}
}
header {
Strict-Transport-Security \"max-age=31536000; includeSubDomains\"
X-Frame-Options \"SAMEORIGIN\"
X-Content-Type-Options \"nosniff\"
X-XSS-Protection \"1; mode=block\"
Referrer-Policy \"strict-origin-when-cross-origin\"
}
# Request size limit for email operations
request_body {
max_size 100MB
}
}" > /etc/caddy/Caddyfile
# Create upgrade script
cat > '/opt/upgrade-emailengine.sh' <<'EOL'
#!/bin/bash
set -e
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
OLD_VERSION=`/opt/emailengine -v`
TMPDIR=$(mktemp -d -t ci-XXXXXXXXXX)
cd "$TMPDIR"
wget https://github.com/postalsys/emailengine/releases/latest/download/emailengine.tar.gz
tar xzf emailengine.tar.gz
rm -rf emailengine.tar.gz
NEW_VERSION=`./emailengine -v`
if [ "$OLD_VERSION" = "$NEW_VERSION" ]; then
rm -rf "$TMPDIR"
echo "Nothing to upgrade, already running $NEW_VERSION"
else
mv emailengine /opt
rm -rf "$TMPDIR"
chmod +x /opt/emailengine
chown emailengine:emailengine /opt/emailengine
systemctl restart emailengine
echo "Upgraded EmailEngine"
echo " - was: $OLD_VERSION"
echo " - now: $NEW_VERSION"
fi
EOL
chmod +x /opt/upgrade-emailengine.sh
systemctl reload caddy
printf "Waiting for the web server to start up.."
until $(curl --output /dev/null --silent --fail https://${DOMAIN_NAME}/); do
printf '.'
sleep 2
done
echo "."
# Save credentials to a secure file
cat > /root/emailengine-credentials.txt <<EOF
===========================================
EmailEngine Installation Credentials
===========================================
Domain: https://${DOMAIN_NAME}/
Redis Password: ${REDIS_PASSWORD}
Encryption Secret: ${EENGINE_SECRET}
IMPORTANT: Save these credentials securely!
They cannot be recovered if lost.
Systemd service: emailengine
Service user: emailengine
Redis database: 8
Commands:
Status: systemctl status emailengine
Logs: journalctl -u emailengine -f
Restart: systemctl restart emailengine
Upgrade: /opt/upgrade-emailengine.sh
Monitoring:
Redis memory: redis-cli -a '${REDIS_PASSWORD}' INFO memory
Service health: curl http://localhost:3000/health
===========================================
EOF
chmod 600 /root/emailengine-credentials.txt
echo ""
echo "========================================="
echo "Installation complete!"
echo "========================================="
echo ""
echo "Access your EmailEngine installation at:"
echo " https://${DOMAIN_NAME}/"
echo ""
echo "IMPORTANT: Credentials have been saved to:"
echo " /root/emailengine-credentials.txt"
echo ""
echo "This file contains your Redis password and encryption secret."
echo "Store these credentials securely - they cannot be recovered!"
echo ""
echo "To upgrade EmailEngine in the future:"
echo " /opt/upgrade-emailengine.sh"
echo ""
echo "To monitor Redis memory usage:"
echo " redis-cli -a 'YOUR_REDIS_PASSWORD' INFO memory"
echo ""