プロジェクト

全般

プロフィール

Wiki » 履歴 » バージョン 2

本間 紀史, 2023/11/28 18:41

1 1 本間 紀史
# line_connect サーバー構築
2
3
## 本番サーバー
4
IPアドレス: 57.180.36.199
5
6
### 環境
7
Lightsail
8
https://lightsail.aws.amazon.com/ls/webapp/home/instances
9
https://lightsail.aws.amazon.com/ls/webapp/ap-northeast-1/instances/stg-techscore/networking
10
11
### サイズ
12
$20/month
13
14
### 構築手順(コマンド)
15
16
#### ユーザー作成
17
インスタンス構築時に指定した公開鍵に対応する秘密鍵を用いて ubuntu ユーザーでログインして以下を実行
18
19
``` shell
20
$ sudo su -
21
# useradd -u 10011 -d /home/norifumi -s /bin/bash -m norifumi -G admin
22
# mkdir /home/norifumi/.ssh
23
# echo 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtob29I4kjea0AHaA03nE3RJzIQYkOu4rAjNOU+nAaaDYCMczQSsxAbarV4XUbqaXwSCf0vTZL6n9eQVcYTLA5GefzD+gXTxLvOY08a/FOk2GUz+UxrKq1wLbGKE34bS87DTqLc8ScXzS4aCOxjZzOk67fiS28GyejR8ir/Do/bDA+zeVdEzWLyxEmbJwlKRioUqocErpSlxP4zhwMoCT6zdrerXBTT+plukPWeMYUMZkXfeQiX1ufWtkn2jnuqKlhBfNdjXq/ReMA91WiDvf0Wc7OAmN0mqPv6W63fZZ0EP7E5VE1b6MCI2Ij4yYqG5j1E5L5skxElE2Um3XUbJfcQ== [email protected]' > /home/norifumi/.ssh/authorized_keys
24
# chown -R norifumi:norifumi /home/norifumi/.ssh
25
# chmod 700 -R /home/norifumi/.ssh
26
# chmod 600 -R /home/norifumi/.ssh/*
27
# sed -i '/^norifumi:.*$/d' /etc/shadow
28
# echo 'norifumi:$6$xbAF8nuJMDDGo.n4$VYMiRTn1gQoliNU8nniUjd8vqaOb9Vqn5RTWBk9.MOvetl0oHbl0N.2EQ2UJ7nTxotLrHLfP.wXQam2atDpO01:19142:0:99999:7:::' >> /etc/shadow
29
```
30
31
以降、上記で作成した norifumi ユーザーで作業を行う
32
ubuntu ユーザーをログアウトし、 norifumi ユーザーで ssh 経由でログインする
33
34
#### NTP設定
35
36
```shell
37
# apt update
38
# apt install -y ntp net-tools
39
```
40
41
#### アプリ用アカウント作成
42
43
```shell
44
# groupadd dev -g 2000
45
# adduser --gid 2000 --uid 2100 deploy
46
```
47
48
途中の質問は全て Enter を押下してデフォルト値を利用する
49
```shell
50
Adding user `deploy' ...
51
Adding new user `deploy' (2100) with group `dev' ...
52
Creating home directory `/home/deploy' ...
53
Copying files from `/etc/skel' ...
54
New password: 
55
Retype new password: 
56
No password supplied
57
New password: 
58
Retype new password: 
59
No password supplied
60
New password: 
61
Retype new password: 
62
No password supplied
63
passwd: Authentication token manipulation error
64
passwd: password unchanged
65
Try again? [y/N] 
66
Changing the user information for deploy
67
Enter the new value, or press ENTER for the default
68
	Full Name []: 
69
	Room Number []: 
70
	Work Phone []: 
71
	Home Phone []: 
72
	Other []: 
73
Is the information correct? [Y/n] 
74
```
75
76
```shell
77
# visudo
78
```
79
最後に1行追加
80
```shell
81
%dev ALL=(ALL) ALL
82
```
83
84
#### パッケージのインストール
85
```shell
86
# apt -y install patch curl build-essential openssl libreadline-dev libreadline-dev git zlib1g zlib1g-dev libssl-dev libyaml-dev libxml2-dev libxslt1-dev autoconf libc6-dev libncurses-dev automake libtool bison subversion libmysqlclient-dev nginx monit nodejs fonts-takao-mincho fonts-takao
87
```
88
89
#### DBサーバーのインストール・設定
90
```shell
91
# apt -y install mysql-server
92
# mysql -u root
93
```
94
95
```sql
96
mysql> CREATE USER 'line_connect'@'%' IDENTIFIED WITH mysql_native_password BY 'AkcJn2xqbsPY';
97
mysql> GRANT ALL PRIVILEGES ON line_connect.* TO 'line_connect'@'%';
98
mysql> FLUSH PRIVILEGES;
99
```
100
101
#### Ruby のインストール
102
```shell
103
# sudo su - deploy
104
```
105
以下、 deploy ユーザー
106
```shell
107
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
108
$ echo 'export PATH="~/.rbenv/bin:$PATH"' >> ~/.bashrc
109
$ ~/.rbenv/bin/rbenv init >> ~/.bashrc
110
$ echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc
111
$ source ~/.bashrc
112
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
113
$ rbenv  install 3.2.2
114
```
115
116
#### アプリケーション用ディレクトリの作成
117
```shell
118
# mkdir -p /usr/local/rails_apps/line_connect/shared/tmp/sockets
119
# mkdir -p /usr/local/rails_apps/line_connect/shared/tmp/pids
120
# mkdir -p /usr/local/rails_apps/line_connect/shared/config
121
# mkdir -p /usr/local/rails_apps/line_connect/shared/log
122
# chown -R www-data:www-data /usr/local/rails_apps/
123
# chmod -R 2775 /usr/local/rails_apps/
124
# chown -R deploy /usr/local/rails_apps/line_connect/
125
```
126
127
#### デプロイユーザーの調整
128
```shell
129
# sudo su - deploy
130
```
131
以下、 deploy ユーザー
132
```shell
133
$ ssh-keygen -C ''
134
```
135
入力は全てデフォルトで enter キーを押下していく
136
```shell
137
Generating public/private rsa key pair.
138
Enter file in which to save the key (/home/deploy/.ssh/id_rsa): 
139
Created directory '/home/deploy/.ssh'.
140
Enter passphrase (empty for no passphrase): 
141
Enter same passphrase again: 
142
Your identification has been saved in /home/deploy/.ssh/id_rsa
143
Your public key has been saved in /home/deploy/.ssh/id_rsa.pub
144
The key fingerprint is:
145
SHA256:679JHZ5l9D1RC1kPy9GOdnnE6GS7xVBXV4l/+lGmnQI 
146
The key's randomart image is:
147
+---[RSA 3072]----+
148
|             .**@|
149
|             +*=X|
150
|             +=Xo|
151
|           E .=+%|
152
|        S   o.oXB|
153
|         . o *+oo|
154
|        . . + ...|
155
|       . . .    .|
156
|        ..+.     |
157
+----[SHA256]-----+
158
```
159
自分自身にSSHできるようにする
160
```shell
161
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
162
```
163
164
#### ソースコードを展開する
165
※事前に /home/deploy/.ssh/id_rsa.pub の公開鍵を git に登録してソースコードを取得できるようにする
166
以下、 deploy ユーザー
167
```shell
168
$ git clone [email protected]:init6/line_connect.git /home/deploy/line_connect
169
$ cd ~/line_connect
170
$ vi config/master.key
171
```
172
以下の内容を記載して保存
173
```shell
174
2f70de59a1afb1b8fe18f672d9aebea6
175
```
176
```shell
177
$ vi /usr/local/rails_apps/line_connect/shared/config/master.key
178
```
179
以下の内容を記載して保存
180
```shell
181
2f70de59a1afb1b8fe18f672d9aebea6
182
```
183
```shell
184
$ bundle
185
```
186
187
#### DBを用意する
188
以下、 deploy ユーザー
189
```shell
190
$ cd ~/line_connect
191
$ LINE_CONNECT_DATABASE_PASSWORD=AkcJn2xqbsPY RAILS_ENV=production bin/rails db:create db:migrate db:seed
192
```
193
194 2 本間 紀史
```shell
195
$ vi /usr/local/rails_apps/line_connect/shared/config/database.yml
196
```
197
以下の内容を記載して保存
198
```shell
199
production:
200
  adapter: mysql2
201
  encoding: utf8mb4
202
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
203
  host: localhost
204
  database: line_connect
205
  username: line_connect
206
  password: AkcJn2xqbsPY
207
```
208
209 1 本間 紀史
#### 試しに起動する
210
以下、 deploy ユーザー
211
```shell
212
$ cd ~/line_connect
213
$ LINE_CONNECT_DATABASE_PASSWORD=AkcJn2xqbsPY RAILS_ENV=production bin/rails assets:clean assets:precompile
214
$ LINE_CONNECT_DATABASE_PASSWORD=AkcJn2xqbsPY RAILS_LOG_TO_STDOUT=1 RAILS_ENV=production bin/rails s
215
```
216
```shell
217
=> Booting Puma
218
=> Rails 7.1.2 application starting in production 
219
=> Run `bin/rails server --help` for more startup options
220
Puma starting in single mode...
221
* Puma version: 6.4.0 (ruby 3.2.2-p53) ("The Eagle of Durango")
222
*  Min threads: 5
223
*  Max threads: 5
224
*  Environment: production
225
*          PID: 73011
226
* Listening on http://0.0.0.0:3000
227
Use Ctrl-C to stop
228
```
229
ブラウザで `http://[サーバーのIPアドレス]:3000/` にアクセス
230
ページが表示されればOK
231
232
#### sudoers の調整
233
```shell
234
# visudo
235
```
236
以下を最後に追記
237
```shell
238
deploy ALL=(ALL) NOPASSWD: /bin/mv /tmp/*.service /etc/systemd/system/
239
deploy ALL=(ALL) NOPASSWD: /bin/systemctl daemon-reload
240
deploy ALL=(ALL) NOPASSWD: /bin/systemctl enable *
241
deploy ALL=(ALL) NOPASSWD: /bin/systemctl restart *
242
```
243
244
####  デプロイ
245
```shell
246
$ sudo su - deploy
247
$ cd ~/line_connect
248 2 本間 紀史
$ bundle
249
###################ここから
250
251 1 本間 紀史
$ bundle exec cap production puma:systemd:config
252
$ bundle exec cap production puma:systemd:enable 
253
$ bundle exec cap production deploy
254
```
255
256
#### nginxの設定
257
```shell
258
# vi /etc/nginx/sites-available/line_connect.conf
259
```
260
以下のように記載
261
```shell
262
upstream puma {
263
  server unix:///usr/local/rails_apps/yakuzaiko/shared/tmp/sockets/puma.sock;
264
}
265
266
server {
267
  server_name prescription.yakuzaiko.com;
268
  listen 80 default_server;
269
  root /usr/local/rails_apps/yakuzaiko/current/public;
270
271
  location ^~ /assets/ {
272
    gzip_static on;
273
    expires max;
274
    add_header Cache-Control public;
275
  }
276
277
  location @puma {
278
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
279
    proxy_set_header Host $http_host;
280
    proxy_set_header X-Forwarded-SSL on; 
281
    proxy_redirect off;
282
    proxy_pass http://puma;
283
  }
284
285
  try_files $uri/index.html $uri @puma;
286
  error_page 500 502 503 504 /500.html;
287
}
288
```
289
290
```shelll
291
# ln -s /etc/nginx/sites-available/yakuzaiko.conf /etc/nginx/sites-enabled/
292
# rm /etc/nginx/sites-enabled/default
293
# chown deploy:dev /usr/local/rails_apps/yakuzaiko/shared/log/*
294
# sudo systemctl restart nginx
295
```
296
297
http://prescription.yakuzaiko.com/ にアクセス
298
299
300
#### SSLの設定
301
```shell
302
# apt install -y certbot python3-certbot-nginx
303
```
304
```shell
305
# certbot --nginx -d prescription.yakuzaiko.com
306
```
307
```
308
Saving debug log to /var/log/letsencrypt/letsencrypt.log
309
Plugins selected: Authenticator nginx, Installer nginx
310
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
311
cancel): [email protected]
312
313
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
314
Please read the Terms of Service at
315
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
316
agree in order to register with the ACME server. Do you agree?
317
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
318
(Y)es/(N)o: Y
319
320
321
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
322
Would you be willing, once your first certificate is successfully issued, to
323
share your email address with the Electronic Frontier Foundation, a founding
324
partner of the Let's Encrypt project and the non-profit organization that
325
develops Certbot? We'd like to send you email about our work encrypting the web,
326
EFF news, campaigns, and ways to support digital freedom.
327
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
328
(Y)es/(N)o: N
329
Account registered.
330
Requesting a certificate for prescription.yakuzaiko.com
331
332
Successfully received certificate.
333
Certificate is saved at: /etc/letsencrypt/live/prescription.yakuzaiko.com/fullchain.pem
334
Key is saved at:         /etc/letsencrypt/live/prescription.yakuzaiko.com/privkey.pem
335
This certificate expires on 2023-09-04.
336
These files will be updated when the certificate renews.
337
Certbot has set up a scheduled task to automatically renew this certificate in the background.
338
339
Deploying certificate
340
Successfully deployed certificate for prescription.yakuzaiko.com to /etc/nginx/sites-enabled/yakuzaiko.conf
341
Congratulations! You have successfully enabled HTTPS on https://prescription.yakuzaiko.com
342
343
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
344
If you like Certbot, please consider supporting our work by:
345
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
346
 * Donating to EFF:                    https://eff.org/donate-le
347
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
348
```
349
350
```
351
# systemctl restart nginx
352
```
353
354
https://prescription.yakuzaiko.com/ にアクセス