This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userData.sh
180 lines (158 loc) · 5.58 KB
/
userData.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
#!/bin/bash
echo "Script begin" >> /var/log/userData.log
## Mount EBS Volume
# Make file system if necessary (only when the volume is new)
echo "file -s /dev/xvdh" >> /var/log/userData.log
OUTPUT="$(file -s /dev/xvdh)"
echo $OUTPUT >> /var/log/userData.log
if [[ $OUTPUT == *"/dev/xvdh: data"* ]]; then
mkfs -t xfs /dev/xvdh
NEWINSTALL=true
echo "Volume file system created" >> /var/log/userData.log
else
NEWINSTALL=false
echo "Volume file system found" >> /var/log/userData.log
fi
# Create mount point
mkdir /mnt/ebs
# Get UUID and create fstab entry
REGEX="\/dev\/xvdh: UUID=\"(.+?)\" "
OUTPUT="$(blkid)"
if [[ $OUTPUT =~ $REGEX ]]; then
echo "UUID=${BASH_REMATCH[1]} /mnt/ebs xfs defaults,nofail 0 2" >> /etc/fstab
echo "EBS UUID found" >> /var/log/userData.log
else
echo "Could not find EBS UUID" >> /var/log/userData.log
exit 1
fi
## Mount EBS tmp
# Make file system if necessary (only when the volume is new)
echo "file -s /dev/xvdi" >> /var/log/userData.log
OUTPUT="$(file -s /dev/xvdi)"
echo $OUTPUT >> /var/log/userData.log
if [[ $OUTPUT == *"/dev/xvdi: data"* ]]; then
mkfs -t xfs /dev/xvdi
echo "Temp file system created" >> /var/log/userData.log
else
echo "Temp file system found" >> /var/log/userData.log
fi
# Create mount point
mkdir /mnt/temp
# Get UUID and create fstab entry
REGEX="\/dev\/xvdi: UUID=\"(.+?)\" "
OUTPUT="$(blkid)"
if [[ $OUTPUT =~ $REGEX ]]; then
echo "UUID=${BASH_REMATCH[1]} /mnt/temp xfs defaults,nofail 0 2" >> /etc/fstab
echo "Tmp EBS UUID found" >> /var/log/userData.log
else
echo "Could not find Tmp EBS UUID" >> /var/log/userData.log
exit 1
fi
# Ensure the filesystems are using all volumes space (useful after increasing the size)
xfs_growfs -d /mnt/ebs
xfs_growfs -d /mnt/temp
# Mount
mount -a
# Install Mysql
echo "Installing Mysql..." >> /var/log/userData.log
apt update
apt install apache2 mysql-server -y
## MYSQL on EBS
# https://www.digitalocean.com/community/tutorials/how-to-move-a-mysql-data-directory-to-a-new-location-on-ubuntu-18-04
# Stop mysql
systemctl stop mysql
# Move Mysql data dir to EBS Volume (only on new install)
if [ "$NEWINSTALL" == true ]; then
rsync -av /var/lib/mysql /mnt/ebs
fi
# Backup the current mysql data dir
mv /var/lib/mysql /var/lib/mysql.bak
# Configure mysql to use ebs as data dir
sed -i -e '/datadir/s/\/var\/lib\/mysql/\/mnt\/ebs\/mysql/' /etc/mysql/mysql.conf.d/mysqld.cnf
# Configure AppArmor to allow access
echo "alias /var/lib/mysql/ -> /mnt/ebs/mysql/," >> /etc/apparmor.d/tunables/alias
systemctl restart apparmor
# Recreate default dir to trick mysql into starting
sudo mkdir /var/lib/mysql/mysql -p
# Mysql perf optimisations
echo "[server]" >> /etc/mysql/conf.d/mysql.cnf
echo "innodb_buffer_pool_size=768M" >> /etc/mysql/conf.d/mysql.cnf
echo "innodb_io_capacity=4000" >> /etc/mysql/conf.d/mysql.cnf
# Start mysql
systemctl start mysql
# Install other required packages
echo "Installing Packages..." >> /var/log/userData.log
apt install php zip libapache2-mod-php php-gd php-json php-mysql php-curl php-mbstring php-intl php-imagick php-xml php-zip php-mysql software-properties-common -y
add-apt-repository universe -y
add-apt-repository ppa:certbot/certbot -y
apt install certbot python-certbot-apache -y
# Stop Apache during install
systemctl stop apache2
## MYSQL
# Todo: mysql_secure_installation
if [ "$NEWINSTALL" == true ]; then
echo "Creating DB: 'nextcloud'@'localhost' IDENTIFIED BY '<MYSQL_NEXTCLOUD_PASSWORD>'" >> /var/log/userData.log
mysql --user="root" --execute="CREATE DATABASE nextcloud;
CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY '<MYSQL_NEXTCLOUD_PASSWORD>';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost';
FLUSH PRIVILEGES;"
else
echo "Not a new install, DB not created" >> /var/log/userData.log
fi
## Nextcloud Install on EBS
echo "Installing Nextcloud..." >> /var/log/userData.log
if [ "$NEWINSTALL" == true ]; then
wget https://download.nextcloud.com/server/releases/latest-18.zip
unzip latest*.zip
mv nextcloud /mnt/ebs/
chown -R www-data:www-data /mnt/ebs/nextcloud
chown www-data:www-data /mnt/temp
fi
ln -s /mnt/ebs/nextcloud /var/www/html/nextcloud
## Apache
APACHE_CONFIG=$(
cat <<'EOF'
Alias / '/var/www/html/nextcloud/'
<Directory /var/www/html/nextcloud/>
Options +FollowSymlinks
AllowOverride All
<IfModule mod_dav.c>
Dav off
</IfModule>
SetEnv HOME /var/www/html/nextcloud
SetEnv HTTP_HOME /var/www/html/nextcloud
</Directory>
EOF
)
echo "$APACHE_CONFIG" >/etc/apache2/sites-available/nextcloud.conf
# Apache conf
a2ensite nextcloud
a2enmod rewrite headers env dir mime socache_shmcb ssl
sed -i '/^memory_limit =/s/=.*/= 512M/' /etc/php/7.2/apache2/php.ini
echo "sys_temp_dir = \"/mnt/temp\"" >> /etc/php/7.2/apache2/php.ini
# systemctl restart apache2
# Connect to S3 (https://autoize.com/s3-compatible-storage-for-nextcloud/)
NEXTCLOUDS3CONFIG=$(
cat <<'EOF'
<?php
$CONFIG = [
'objectstore' => array(
'class' => 'OC\\Files\\ObjectStore\\S3',
'arguments' => array(
'bucket' => '<BUCKET_NAME>',
'key' => '<USER_KEY>',
'secret' => '<USER_SECRET>',
'autocreate' => false,
'use_ssl' => true,
'use_path_style'=>false,
'region' => '<AWS_REGION>',
),
),
];
EOF
)
echo "$NEXTCLOUDS3CONFIG" > /var/www/html/nextcloud/config/storage.config.php
chown -R www-data:www-data /var/www/html/nextcloud/config/storage.config.php
# Start Apache once conf done
systemctl start apache2
echo "Script execution complete!" >> /var/log/userData.log