Project

General

Profile

Wiki » History » Version 4

關山 和丈, 07/30/2024 02:47 AM

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