--- title: 部署筆記 tags: note --- > 此筆記為 AstroCamp 專案時期,部署的筆記。 :::success :bulb: 參考文件 :link: [五倍文章,部署工具,原來是這樣!(Nginx、Passenger、Capsitrano)](https://5xruby.tw/posts/rails-deploy/) :link: [reid 的筆記-不使用 rvm 或 rbenv](https://hackmd.io/@Reid/Sy0eizEDw) :::spoiler 蒼時弦也 - 部署你的第一個 Rails 網站(共八部) [Part.1](https://blog.frost.tw/posts/2018/03/20/Getting-started-deploy-your-Ruby-on-Rails-Part-1/#more) [Part.2](https://blog.frost.tw/posts/2018/03/23/Getting-started-deploy-your-Ruby-on-Rails-Part-2/#more) [Part.3](https://blog.frost.tw/posts/2018/03/27/Getting-started-deploy-your-Ruby-on-Rails-Part-3/#more) [Part.4](https://blog.frost.tw/posts/2018/04/10/Getting-started-deploy-your-Ruby-on-Rails-Part-4/#more) [Part.5](https://blog.frost.tw/posts/2018/04/15/Getting-started-deploy-your-Ruby-on-Rails-Part-5/#more) [Part.6](https://blog.frost.tw/posts/2018/05/07/Getting-started-deploy-your-Ruby-on-Rails-Part-6/#more) [Part.7](https://blog.frost.tw/posts/2018/05/20/Getting-started-deploy-your-Ruby-on-Rails-Part-7/#more) [Part.8](https://blog.frost.tw/posts/2018/05/28/Getting-started-deploy-your-Ruby-on-Rails-Part-8/#more) ::: ## AWS instance 申請 先去AWS 申請帳號,service -> EC2 ->launch instance ![](https://i.imgur.com/8xGpICN.png) 選擇CentOS 7 Minimal (CentOS 7.8.2003) ![](https://i.imgur.com/Dm7hc4Q.png) (不選8的原因:太新,可能有雷) > 自己真心試過,選 8 的話,會多很多額外的設定! 接下來會進入設定的畫面,可以選擇你要的核心跟記憶體大小(有免費的方案可以選,不過記憶體只有 1G,沒有辦法裝 `passenger` :cry: :money_with_wings: :money_with_wings: :money_with_wings: :money_with_wings: ) 全部設定完後,會生成一對SSH key給你,請保管好,此生只有這一把,不會補發給你! 接著會開始做初始化的動作 申請好後,開始連線! 終端機裡打: `ssh -i xxxx.pem centos@18.178.204.13 ` 意思為用本地的private_key 跟遠端的伺服器裡的public_KEY比對,連線。 :::spoiler `WARNING: UNPROTECTED PRIVATE KEY FILE!` 得到下列錯誤訊息: ``` @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVATE KEY FILE! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Permissions 0644 for 'aws_server.pem' are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored. Load key "aws_server.pem": bad permissions root@13.231.207.31: Permission denied (publickey,gssapi-keyex,gssapi-with-mic). ``` 說我的key是0644的權限太公開了! 何謂0644? 對終端機下: `ls -al` 得到下列訊息: ``` total 1352 drwx------+ 11 yuan staff 352 8 26 21:09 . drwxr-xr-x+ 51 yuan staff 1632 8 26 21:17 .. -rw-r--r--@ 1 yuan staff 10244 8 26 21:09 .DS_Store -rw-r--r-- 1 yuan staff 0 8 23 23:24 .localized drwxr-xr-x 12 yuan staff 384 8 26 12:10 AstroCamp -rw-r--r--@ 1 yuan staff 1692 8 26 21:09 aws_server.pem ``` 第一個字母不看,後面開始三個三個一組,第一組代表本人使用者,第二組代表「」,第三組代表其他使用者,每一個字母代表權限。 ``` r = read 讀取 4點 w = write 編輯 2點 x = 執行 1點 - = 不能做事 0點 ``` 以上面的aws_server.pem前面那串來說三個三個字一組 | rw- | r- - | r- - | | ---------- | ---------- | ------ | | 當前使用者 |群組使用者 |其他人 | | r(4)+w(2) | r(4) | r(4) | | 6 | 4 | 4 | 這就是0644的權限 更改權限為600 `chmod 0600 aws_server.pem` 變為 `-rw-------@ 1 yuan staff 1692 8 26 21:09 aws_server.pem` 即為 600 重新登入: `ssh -i aws_server.pem centos@13.231.207.31` 登入後: ``` Welcome to CentOS-7-x86_64-Minimal-8GiB-HVM-20200428_104347. * Use "sudo su -" command in order to become root. For your convenience, this AMI is provided by ProComputers.com. Visit http://www.procomputers.com for more information. If you don't want to see this message anymore, please remove the content of the /etc/motd text file. ``` ::: ## 切換使用者為root 更換成root在設定系統比較方便,但比較危險 `$ sudo su -` ## 建立使用者 ``` useradd gn2481 -G wheel ``` wheel 這個群組在 CentOS 7 通常會視為「有權限執行 sudo 指令」的使用者們,之後要去做設定 ## 設定新使用者的ssh key 複製本地端aws做的id_rsa_pub的內容 可以用`cat`指令看到內容 到遠端伺服器用root切換到新使用者 `mkdir ~/.ssh` `vi ~/.ssh/authorized_keys` :::info 進入 vim 模式了。 把內容用INSER模式(按`i`)貼上去,離開INSER模式(`esc`),存檔`:wq`離開 ::: 設定成只有自己能用的權限(700或600都可,不要後面是44) ``` chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys ``` 更改時區: ``` # timedatectl set-timezone Asia/Taipei ``` ## 安裝 epel repository (optional) 相當於 app store ``` $ sudo yum install epel-release.noarch ``` ## 安裝系統常用套件,wegt,curl,vim ``` $ yum install -y net-tools wget curl vim ``` ## 開始裝rvm ruby rails :::warning 正常來說,部署時,並不需要不同版本的 ruby ,應該採取自行編譯安裝的方式 ::: ### rvm `gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB` `\curl -sSL https://get.rvm.io | bash -s stable` 若第一行沒過,可以先下第二行,錯誤訊息會跟你說要打什麼! 接著讀取rvm環境變數,才有rvm的指令可以用 `source /etc/profile.d/rvm.sh` ### 裝完rvm,裝ruby `$ rvm install 2.6.5` ### 裝rails rake ### passenger vi /etc/profile.d/passenger.sh 貼上export PATH='/root/passenger-6.0.6/bin':$PATH 設定 /opt/nginx/conf 下的nginx.conf 網址 ### 看log cat 看檔案內容全部 head -2 看檔案前兩行(不打-2 看十行) tail -2 看檔案後兩行(不打-2 看十行) ### Nginx passenger-install-nginx-module [passenger & nginx 的關西](https://5xruby.tw/posts/rails-deploy/) ### pgSQL #### 伺服器端裝sql ``` # Install the repository RPM: yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm # Install PostgreSQL: yum install -y postgresql12-server ``` 做db初始化 `/usr/pgsql-12/bin/postgresql-12-setup initdb` #### 裝pg gem! 先裝 `yum install postgresql-devel` 看能不能 `gem install pg -- --with-pg-config=/usr/pgsql-12/bin/pg_config` 不然再去裝: 還要裝libpqxx.x86_64 ps : yum search 名稱 可以搜尋有這個名字的東西 ### 專案裝gem Capistrano 用ssh 部署到server上 [大神筆記](https://blog.frost.tw/posts/2018/04/15/Getting-started-deploy-your-Ruby-on-Rails-Part-5/#more) capfile 裡下面四行取消註解,並安裝相對應的gem ```ruby= #/capfile require "capistrano/bundler" require "capistrano/rails/assets" require "capistrano/rails/migrations" require "capistrano/passenger" ``` ```ruby= #/gem_file gem 'capistrano', '~> 3.10', require: false gem 'capistrano-bundler', '~> 2.0', '>= 2.0.1' gem 'capistrano-rails', '~> 1.6', '>= 1.6.1' gem 'capistrano-passenger', '~> 0.2.0' ``` deploy.rb內可以設定被git ignore的檔案路徑,先在sever建給他 有用figaro的話把application.yml, master.keys, secrets.yml丟到server的/home/deploy/staging/shared/config 裡 ### server 用git clone下來 先裝git ``` yum install git ``` 然後用deploy身份產生一對SSH_key ``` ssh-keygen ``` ### 裝node.js ``` # As root curl -sL https://rpm.nodesource.com/setup_14.x | bash - # No root privileges curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash - ``` 然後記得跑:不然都沒裝到! ``` sudo yum install -y nodejs ``` ### 讓專案在server可以跑起來 本地專案下 ``` cap staging deploy ``` 會噴錯,要檢查錯誤訊息 若卡在: `deploy:assets:precompile`:大部分是server端的node.js跟yarn的問題 `deploy:migrating`:pg 的database沒有建好,可以進server裡 ``` # sudo su - deploy # createdb 跟專案的db同名! ``` `deploy:symlink:release`:passenger或nginx的問題,可以去檢查路徑有沒有設定對。 `vi cd /etc/profile.d/passenger.sh` 或是server有沒有啟動 I18n: appliaction.rb : ```ruby= # I18n.default_locale = :'zh-TW' config.I18n.available_locales = [:fr, :en, "zh-CN", "zh-TW"] ``` environments/staging.rb or production.rb ```ruby= config.i18n.fallbacks = false ``` ps 聊天室使用redis ssh deploy@staging.bubble-gram.com action cable 會讓 passenger process 一直長出來,(預設最多只有6個很小。)要去/opt/nginx/conf/nginx.conf 檔裡,開大: `passenger_max_pool_size 40` demo_day 可以把記憶體開到 16 或 32 G 資料庫要 dump 出來