-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudinit.sh
180 lines (150 loc) · 4.2 KB
/
cloudinit.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
#
# cloudinit script to spin up and configure a bob
#
# Copyright (C) 2019 kevin olson <[email protected]>
#
SERVICE=bob
ENV=staging
[email protected]:fumeapp/bob.git
PORT=4000
MAXSIZE=25G
MAXMEMORY=1G
KEY="-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACCb3GUG+VJJ7IEuPOYCEOJCt4vnsj28RL4Z1uA3XyMajAAAAJhy42AHcuNg
BwAAAAtzc2gtZWQyNTUxOQAAACCb3GUG+VJJ7IEuPOYCEOJCt4vnsj28RL4Z1uA3XyMajA
AAAEAV6pQFtosbkNXfVL0hoRFEubwEb8/Ea/TKvyiozGixOJvcZQb5UknsgS485gIQ4kK3
i+eyPbxEvhnW4DdfIxqMAAAAEmFjaWRqYXp6QGdtYWlsLmNvbQECAw==
-----END OPENSSH PRIVATE KEY-----"
echo "$KEY" >/home/ec2-user/.ssh/id_rsa
chmod 0700 /home/ec2-user/.ssh/*
chown -R ec2-user:ec2-user /home/ec2-user/.ssh/
HOSTNAME=$SERVICE-$ENV
hostname $HOSTNAME
yum -y update
amazon-linux-extras install nginx1.12 php8.0
sudo systemctl enable amazon-ssm-agent
sudo systemctl start amazon-ssm-agent
yum -y install git docker php-fpm php-mbstring php-process php-xml php-devel gcc libyaml-devel
usermod -aG docker ec2-user
# nginx config
echo "
user nginx;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_disable "msie6";
client_max_body_size $MAXSIZE;
server {
listen $PORT;
root /home/ec2-user/$SERVICE/public;
index index.php;
charset utf-8;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
}
" > /etc/nginx/nginx.conf
# php-fpm config
echo '
[www]
user = ec2-user
group = ec2-user
listen = /run/php-fpm/www.sock
listen.acl_users = apache,nginx,ec2-user
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
' > /etc/php-fpm.d/www.conf
# php.ini updates
memory_limit=$MAXMEMORY
upload_max_filesize=$MAXSIZE
post_max_size=$MAXSIZE
max_execution_time=100
max_input_time=223
for key in memory_limit upload_max_filesize post_max_size max_execution_time max_input_time
do
sed -i "s/^\($key\).*/\1 $(eval echo = \${$key})/" /etc/php.ini
done
# Install php-redis (no available module)
git clone git://github.com/nicolasff/phpredis.git
cd phpredis
phpize
./configure
make
make install
echo "extension=redis.so" > /etc/php.d/20-redis.ini
git clone https://github.com/php/pecl-file_formats-yaml.git
cd pecl-file_formats-yaml
phpize
./configure
make
make install
echo "extension=yaml.so" > /etc/php.d/20-yaml.ini
systemctl restart php-fpm.service
systemctl restart nginx.service
# setup horizon with systemd
echo "
[Unit]
Description=Laravel Horizon Queue Manager
After=network.target auditd.service
[Service]
ExecStart=/bin/php /home/ec2-user/bob/artisan horizon
Restart=always
User=ec2-user
Group=ec2-user
[Install]
WantedBy=multi-user.target
" > /lib/systemd/system/horizon.service
# Install Composer
curl -sS https://getcomposer.org/installer | HOME="/home/ec2-user" php -- --install-dir=/usr/local/bin --filename=composer
su ec2-user -c "
scl enable php74 bash
cd ~/
source ~/.bashrc
ssh-keyscan github.com >> ~/.ssh/known_hosts
git clone $REPO
cd $SERVICE
git checkout $ENV
/usr/local/bin/composer $ENV
"
gpasswd -a nginx ec2-user
chmod g+x /home/ec2-user
chmod g+x /home/ec2-user/$SERVICE
service nginx restart
service docker start
systemctl enable horizon.service
systemctl start horizon.service