進階的AWS Beanstalk佈署laravel用法
本篇紀錄更進階的AWS EB操作
主要為透過設定檔建立整個EB相關服務
基本EB應用程式設定
在執行EB CLI的目錄下建立.elasticbeanstalk/config.yml
同長為EB CLI執行eb init建立後才會建立
且預設EB CLI會將其加入.gitignore中
如果希望進版控或是自動化的話
不希望透過AWS後台設定的話
就可以實做這一步驟
另外這邊也設定EC2 isntance的key pair name
branch-defaults:
default:
environment: apiserver-stage1
global:
application_name: apiserver
branch: null
default_ec2_keyname: {your-key-pair-name}
default_platform: PHP 7.2 running on 64bit Amazon Linux 2
default_region: ap-southeast-1
include_git_submodules: true
instance_profile: null
platform_name: null
platform_version: null
profile: eb-cli
repository: null
sc: null
workspace_type: Application
.ebextension設定環境
參考連結:https://docs.aws.amazon.com/zh_tw/elasticbeanstalk/latest/dg/ebextensions.html
在執行EB CLI的目錄下建立.ebextension資料夾
並且在該資料夾內建立.conf為附檔名的檔案
設定Proxy Server為Nginx且設定document_root
可參考官方文件
option_settings:
aws:elasticbeanstalk:environment:proxy:
ProxyServer: nginx
aws:elasticbeanstalk:container:php:phpini:
document_root: /public
memory_limit: 128M
zlib.output_compression: "Off"
allow_url_fopen: "On"
display_errors: "Off"
max_execution_time: 60
安裝額外套件
一樣在.ebextension中
使用packages屬性
packages:
yum:
nodejs: 10
pm2: 4.2
執行額外指令
一樣在.ebextension中
使用commands屬性
commands:
01_install_node:
command:
- curl --location https://rpm.nodesource.com/setup_10.x | sudo bash -
- yum -y install nodejs
ignoreErrors: true
02_install_yarn:
test: '[ ! -f /usr/bin/yarn ] && echo "Yarn not found, installing..."'
command:
- wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
- yum -y install yarn
ignoreErrors: true
hooks
參考連結:https://docs.aws.amazon.com/zh_tw/elasticbeanstalk/latest/dg/platforms-linux-extend.html
這邊可以在EC2的不同生命週期中
使用特殊的hook做些不同的事情
例如要在所有原始碼預備好解壓縮後且在發佈之前做一些事情
可以在.platform/hooks/postdeploy/01_do_something.sh中做一些事情
另外這些hook設定
將會發佈到ec2的/var/app/current/.platform/hooks中
功能我一開測試一直有問題
EB建立每次都在instance crash掉
後來查了很久發現是開頭沒加#!/bin/bash
就因為這個問題搞了很久
#!/bin/bash
timedatectl set-timezone Asia/Taipei \
&& curl --silent --location https://rpm.nodesource.com/setup_10.x | bash \
&& yum install -y nodejs \
&& wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo \
&& yum install -y yarn \
&& yarn global add pm2
Auto Scaling設定
參考連結
- https://docs.aws.amazon.com/zh_tw/elasticbeanstalk/latest/dg/environments-cfg-autoscaling-triggers.html
- https://docs.aws.amazon.com/zh_tw/elasticbeanstalk/latest/dg/command-options-general.html#command-options-general-autoscalingtrigger
option_settings:
aws:autoscaling:asg:
MaxSize: 4
MinSize: 2
aws:autoscaling:trigger:
LowerBreachScaleIncrement: '-1'
UpperBreachScaleIncrement: '1'
UpperThreshold: '6000000'
BreachDuration: '5'
EvaluationPeriods: '1'
LowerThreshold: '2000000'
MeasureName: NetworkOut
Period: '5'
Statistic: Average
Unit: Bytes
額外設定nginx
可參考官方文件的Reverse proxy configuration(反向代理組態)設定
這是laravel環境需要做的調整
作法是在EB CLI執行的目錄下
建立.platform/nginx/conf/elasticbeanstalk/下建立*.conf的nginx設定
root /var/www/html/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
location ~ \.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/run/php-fpm/www.sock;
}
發佈後EB將會將該檔案複製到instance上的/var/proxy/staging/nginx/conf.d/elasticbeanstalk/內
系統也會自動在/etc/nginx/nginx.conf的預設nginx設定中加入
include conf.d/elasticbeanstalk/*.conf;
讓nginx/conf.d/elasticbeanstalk中的自動nginx設定起作用
透過.ebextension設定RDS資源
.ebextension可額外設定各種AWS資源
參考連結:https://docs.aws.amazon.com/zh_tw/elasticbeanstalk/latest/dg/environment-resources.html
所有資源可依照Cloud Formation的資訊設定
Resources:
AWSEBRDSDatabase:
Type: AWS::RDS::DBInstance
Properties:
AllocatedStorage: 5
DBInstanceClass: db.t2.small
DBClusterIdentifier: {cluster-name}
DBInstanceIdentifier: {instance-name}
DBName: {db-name}
Engine: mysql
EngineVersion: 5.7
MasterUsername: {username}
MasterUserPassword: {password}
啟動服務
# 建立應用程式碼
eb init
# 建立環境(服務)
eb create laravel-app
佈署流程
參考:https://docs.aws.amazon.com/zh_tw/elasticbeanstalk/latest/dg/platforms-linux-extend.html
透過EB CLI使用zip佈署
如果有自動化工具可以產生zip檔案
可使用EB CLI artifact來指定佈署的zip
即可透過EB CLI佈署
另外要注意的是.ebextensions跟.platform這類的EB設定目錄
也要打包進zip交給EB CLI發佈
才會有作用
# .elasticbeanstalk/config.yml
deploy:
artifact: path/to/buildartifact.zip
參考連結
其他注意事項
.elasticbeanstalk/config.yml中
global.sc如果設定為git
因為laravel本身.env是加入.gitignore中
所以使用EB CLI發佈後.env是的不會被發佈出去
這樣會造成instance上沒.env
所以如果global.sc設定為git後要使用其他方式設定.env到instance上