プロジェクト

全般

プロフィール

Wiki » 履歴 » バージョン 1

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

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
#### 試しに起動する
195
以下、 deploy ユーザー
196
```shell
197
$ cd ~/line_connect
198
$ LINE_CONNECT_DATABASE_PASSWORD=AkcJn2xqbsPY RAILS_ENV=production bin/rails assets:clean assets:precompile
199
$ LINE_CONNECT_DATABASE_PASSWORD=AkcJn2xqbsPY RAILS_LOG_TO_STDOUT=1 RAILS_ENV=production bin/rails s
200
```
201
```shell
202
=> Booting Puma
203
=> Rails 7.1.2 application starting in production 
204
=> Run `bin/rails server --help` for more startup options
205
Puma starting in single mode...
206
* Puma version: 6.4.0 (ruby 3.2.2-p53) ("The Eagle of Durango")
207
*  Min threads: 5
208
*  Max threads: 5
209
*  Environment: production
210
*          PID: 73011
211
* Listening on http://0.0.0.0:3000
212
Use Ctrl-C to stop
213
```
214
ブラウザで `http://[サーバーのIPアドレス]:3000/` にアクセス
215
ページが表示されればOK
216
217
#### sudoers の調整
218
```shell
219
# visudo
220
```
221
以下を最後に追記
222
```shell
223
deploy ALL=(ALL) NOPASSWD: /bin/mv /tmp/*.service /etc/systemd/system/
224
deploy ALL=(ALL) NOPASSWD: /bin/systemctl daemon-reload
225
deploy ALL=(ALL) NOPASSWD: /bin/systemctl enable *
226
deploy ALL=(ALL) NOPASSWD: /bin/systemctl restart *
227
```
228
229
####  デプロイ
230
```shell
231
$ sudo su - deploy
232
$ cd ~/line_connect
233
$ bundle exec cap production puma:systemd:config
234
$ bundle exec cap production puma:systemd:enable 
235
$ bundle exec cap production deploy
236
```
237
238
#### nginxの設定
239
```shell
240
# vi /etc/nginx/sites-available/line_connect.conf
241
```
242
以下のように記載
243
```shell
244
upstream puma {
245
  server unix:///usr/local/rails_apps/yakuzaiko/shared/tmp/sockets/puma.sock;
246
}
247
248
server {
249
  server_name prescription.yakuzaiko.com;
250
  listen 80 default_server;
251
  root /usr/local/rails_apps/yakuzaiko/current/public;
252
253
  location ^~ /assets/ {
254
    gzip_static on;
255
    expires max;
256
    add_header Cache-Control public;
257
  }
258
259
  location @puma {
260
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
261
    proxy_set_header Host $http_host;
262
    proxy_set_header X-Forwarded-SSL on; 
263
    proxy_redirect off;
264
    proxy_pass http://puma;
265
  }
266
267
  try_files $uri/index.html $uri @puma;
268
  error_page 500 502 503 504 /500.html;
269
}
270
```
271
272
```shelll
273
# ln -s /etc/nginx/sites-available/yakuzaiko.conf /etc/nginx/sites-enabled/
274
# rm /etc/nginx/sites-enabled/default
275
# chown deploy:dev /usr/local/rails_apps/yakuzaiko/shared/log/*
276
# sudo systemctl restart nginx
277
```
278
279
http://prescription.yakuzaiko.com/ にアクセス
280
281
282
#### SSLの設定
283
```shell
284
# apt install -y certbot python3-certbot-nginx
285
```
286
```shell
287
# certbot --nginx -d prescription.yakuzaiko.com
288
```
289
```
290
Saving debug log to /var/log/letsencrypt/letsencrypt.log
291
Plugins selected: Authenticator nginx, Installer nginx
292
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
293
cancel): [email protected]
294
295
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
296
Please read the Terms of Service at
297
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
298
agree in order to register with the ACME server. Do you agree?
299
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
300
(Y)es/(N)o: Y
301
302
303
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
304
Would you be willing, once your first certificate is successfully issued, to
305
share your email address with the Electronic Frontier Foundation, a founding
306
partner of the Let's Encrypt project and the non-profit organization that
307
develops Certbot? We'd like to send you email about our work encrypting the web,
308
EFF news, campaigns, and ways to support digital freedom.
309
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
310
(Y)es/(N)o: N
311
Account registered.
312
Requesting a certificate for prescription.yakuzaiko.com
313
314
Successfully received certificate.
315
Certificate is saved at: /etc/letsencrypt/live/prescription.yakuzaiko.com/fullchain.pem
316
Key is saved at:         /etc/letsencrypt/live/prescription.yakuzaiko.com/privkey.pem
317
This certificate expires on 2023-09-04.
318
These files will be updated when the certificate renews.
319
Certbot has set up a scheduled task to automatically renew this certificate in the background.
320
321
Deploying certificate
322
Successfully deployed certificate for prescription.yakuzaiko.com to /etc/nginx/sites-enabled/yakuzaiko.conf
323
Congratulations! You have successfully enabled HTTPS on https://prescription.yakuzaiko.com
324
325
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
326
If you like Certbot, please consider supporting our work by:
327
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
328
 * Donating to EFF:                    https://eff.org/donate-le
329
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
330
```
331
332
```
333
# systemctl restart nginx
334
```
335
336
https://prescription.yakuzaiko.com/ にアクセス