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