Wiki » 履歴 » バージョン 3
本間 紀史, 2023/11/28 19:26
| 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 | 3 | 本間 紀史 | #### systemd |
| 245 | ```shell |
||
| 246 | # vi /etc/systemd/system/line_connect_puma_production.service |
||
| 247 | ``` |
||
| 248 | 以下のように記載 |
||
| 249 | ``` |
||
| 250 | [Unit] |
||
| 251 | Description=Puma HTTP Server for line_connect (production) |
||
| 252 | After=network.target |
||
| 253 | |||
| 254 | [Service] |
||
| 255 | Type=simple |
||
| 256 | User=deploy |
||
| 257 | WorkingDirectory=/usr/local/rails_apps/line_connect/current |
||
| 258 | # Support older bundler versions where file descriptors weren't kept |
||
| 259 | # See https://github.com/rubygems/rubygems/issues/3254 |
||
| 260 | ExecStart=/home/deploy/.rbenv/bin/rbenv exec bundle exec --keep-file-descriptors puma -C /usr/local/rails_apps/line_connect/shared/puma.rb |
||
| 261 | ExecReload=/bin/kill -USR1 $MAINPID |
||
| 262 | StandardOutput=append:/usr/local/rails_apps/line_connect/shared/log/puma_access.log |
||
| 263 | StandardError=append:/usr/local/rails_apps/line_connect/shared/log/puma_error.log |
||
| 264 | |||
| 265 | Restart=always |
||
| 266 | RestartSec=1 |
||
| 267 | |||
| 268 | SyslogIdentifier=puma |
||
| 269 | |||
| 270 | [Install] |
||
| 271 | WantedBy=multi-user.target |
||
| 272 | ``` |
||
| 273 | ```shell |
||
| 274 | # systemctl enable line_connect_puma_production.service |
||
| 275 | ``` |
||
| 276 | |||
| 277 | ```shell |
||
| 278 | $ vi /usr/local/rails_apps/line_connect/shared/puma.rb |
||
| 279 | `` |
||
| 280 | 以下のように記載 |
||
| 281 | ``` |
||
| 282 | #!/usr/bin/env puma |
||
| 283 | |||
| 284 | directory '/usr/local/rails_apps/line_connect/current' |
||
| 285 | rackup "/usr/local/rails_apps/line_connect/current/config.ru" |
||
| 286 | environment 'production' |
||
| 287 | |||
| 288 | tag '' |
||
| 289 | |||
| 290 | pidfile "/usr/local/rails_apps/line_connect/shared/tmp/pids/puma.pid" |
||
| 291 | state_path "/usr/local/rails_apps/line_connect/shared/tmp/pids/puma.state" |
||
| 292 | stdout_redirect '/usr/local/rails_apps/line_connect/shared/log/puma_access.log', '/usr/local/rails_apps/line_connect/shared/log/puma_error.log', true |
||
| 293 | |||
| 294 | threads 0,16 |
||
| 295 | |||
| 296 | bind 'unix:///usr/local/rails_apps/line_connect/shared/tmp/sockets/puma.sock' |
||
| 297 | |||
| 298 | workers 0 |
||
| 299 | |||
| 300 | restart_command 'bundle exec puma' |
||
| 301 | |||
| 302 | prune_bundler |
||
| 303 | |||
| 304 | on_restart do |
||
| 305 | puts 'Refreshing Gemfile' |
||
| 306 | ENV["BUNDLE_GEMFILE"] = "" |
||
| 307 | end |
||
| 308 | ``` |
||
| 309 | |||
| 310 | 1 | 本間 紀史 | #### デプロイ |
| 311 | ```shell |
||
| 312 | $ sudo su - deploy |
||
| 313 | $ cd ~/line_connect |
||
| 314 | $ bundle |
||
| 315 | $ bundle exec cap production deploy |
||
| 316 | ``` |
||
| 317 | |||
| 318 | #### nginxの設定 |
||
| 319 | ```shell |
||
| 320 | # vi /etc/nginx/sites-available/line_connect.conf |
||
| 321 | ``` |
||
| 322 | 以下のように記載 |
||
| 323 | ```shell |
||
| 324 | upstream puma { |
||
| 325 | 3 | 本間 紀史 | server unix:///usr/local/rails_apps/line_connect/shared/tmp/sockets/puma.sock; |
| 326 | 1 | 本間 紀史 | } |
| 327 | |||
| 328 | server { |
||
| 329 | 3 | 本間 紀史 | server_name mrlc.init6.co.jp; |
| 330 | 1 | 本間 紀史 | listen 80 default_server; |
| 331 | 3 | 本間 紀史 | root /usr/local/rails_apps/line_connect/current/public; |
| 332 | 1 | 本間 紀史 | |
| 333 | location ^~ /assets/ { |
||
| 334 | gzip_static on; |
||
| 335 | expires max; |
||
| 336 | add_header Cache-Control public; |
||
| 337 | } |
||
| 338 | |||
| 339 | location @puma { |
||
| 340 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
||
| 341 | proxy_set_header Host $http_host; |
||
| 342 | 3 | 本間 紀史 | proxy_set_header X-Forwarded-SSL on; |
| 343 | 1 | 本間 紀史 | proxy_redirect off; |
| 344 | proxy_pass http://puma; |
||
| 345 | } |
||
| 346 | |||
| 347 | try_files $uri/index.html $uri @puma; |
||
| 348 | error_page 500 502 503 504 /500.html; |
||
| 349 | } |
||
| 350 | ``` |
||
| 351 | |||
| 352 | ```shelll |
||
| 353 | 3 | 本間 紀史 | # ln -s /etc/nginx/sites-available/line_connect.conf /etc/nginx/sites-enabled/ |
| 354 | 1 | 本間 紀史 | # rm /etc/nginx/sites-enabled/default |
| 355 | 3 | 本間 紀史 | # chown deploy:dev /usr/local/rails_apps/line_connect/shared/log/* |
| 356 | 1 | 本間 紀史 | # sudo systemctl restart nginx |
| 357 | ``` |
||
| 358 | |||
| 359 | 3 | 本間 紀史 | http://mrlc.init6.co.jp/ にアクセス |
| 360 | 1 | 本間 紀史 | |
| 361 | #### SSLの設定 |
||
| 362 | ```shell |
||
| 363 | # apt install -y certbot python3-certbot-nginx |
||
| 364 | ``` |
||
| 365 | ```shell |
||
| 366 | 3 | 本間 紀史 | # certbot --nginx -d mrlc.init6.co.jp |
| 367 | 1 | 本間 紀史 | ``` |
| 368 | ``` |
||
| 369 | Saving debug log to /var/log/letsencrypt/letsencrypt.log |
||
| 370 | 3 | 本間 紀史 | Enter email address (used for urgent renewal and security notices) |
| 371 | (Enter 'c' to cancel): [email protected] |
||
| 372 | 1 | 本間 紀史 | |
| 373 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 374 | Please read the Terms of Service at |
||
| 375 | https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must |
||
| 376 | agree in order to register with the ACME server. Do you agree? |
||
| 377 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 378 | (Y)es/(N)o: Y |
||
| 379 | |||
| 380 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 381 | Would you be willing, once your first certificate is successfully issued, to |
||
| 382 | share your email address with the Electronic Frontier Foundation, a founding |
||
| 383 | partner of the Let's Encrypt project and the non-profit organization that |
||
| 384 | develops Certbot? We'd like to send you email about our work encrypting the web, |
||
| 385 | EFF news, campaigns, and ways to support digital freedom. |
||
| 386 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 387 | (Y)es/(N)o: N |
||
| 388 | Account registered. |
||
| 389 | 3 | 本間 紀史 | Requesting a certificate for mrlc.init6.co.jp |
| 390 | 1 | 本間 紀史 | |
| 391 | Successfully received certificate. |
||
| 392 | 3 | 本間 紀史 | Certificate is saved at: /etc/letsencrypt/live/mrlc.init6.co.jp/fullchain.pem |
| 393 | Key is saved at: /etc/letsencrypt/live/mrlc.init6.co.jp/privkey.pem |
||
| 394 | This certificate expires on 2024-02-26. |
||
| 395 | 1 | 本間 紀史 | These files will be updated when the certificate renews. |
| 396 | Certbot has set up a scheduled task to automatically renew this certificate in the background. |
||
| 397 | |||
| 398 | Deploying certificate |
||
| 399 | 3 | 本間 紀史 | Successfully deployed certificate for mrlc.init6.co.jp to /etc/nginx/sites-enabled/line_connect.conf |
| 400 | Congratulations! You have successfully enabled HTTPS on https://mrlc.init6.co.jp |
||
| 401 | 1 | 本間 紀史 | |
| 402 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 403 | If you like Certbot, please consider supporting our work by: |
||
| 404 | * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate |
||
| 405 | * Donating to EFF: https://eff.org/donate-le |
||
| 406 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 407 | ``` |
||
| 408 | |||
| 409 | ``` |
||
| 410 | # systemctl restart nginx |
||
| 411 | ``` |
||
| 412 | |||
| 413 | 3 | 本間 紀史 | https://mrlc.init6.co.jp/ にアクセス |