顯示具有 dockerfile 標籤的文章。 顯示所有文章
顯示具有 dockerfile 標籤的文章。 顯示所有文章

2024年3月27日 星期三

PHP 開發筆記 - 使用 Docker 在 Ubuntu 22.04 和 PHP 8.1 架設 osTicket v1.18.1 環境,研究 Mail Parser 流程

接續上篇追蹤 osTicket 信件處理流程的筆記,這次用 Docker 包裝可運行和測試的獨立環境,主要採用 Ubuntu 22.04 與 PHP 8.1,並且規劃方式,建構出備份原始信件的架構,以及可反覆解析信件的流程。

首先是 Dockerfile ,主要設計從 GitHub.com 取出 v1.18.1.zip 並解壓縮 /var/www/osticket-v1.18.1,其餘是安裝相關套件和資料庫的帳號建立等等,最後再把 MySQL, PHP-FPM 和 nginx 運行起來,並預設把 nginx logs 寫到 stdout 觀看:

RUN wget https://github.com/osTicket/osTicket/releases/download/v1.18.1/osTicket-v1.18.1.zip -O /tmp/osticket.zip \
    && unzip /tmp/osticket.zip -d /var/www/ \
    && mv /var/www/upload /var/www/osticket-v1.18.1 \
    && cp /var/www/osticket-v1.18.1/include/ost-sampleconfig.php /var/www/osticket-v1.18.1/include/ost-config.php \
    && chown -R www-data:www-data /var/www/osticket-v1.18.1 \
    && rm /tmp/osticket.zip

...

RUN service mysql start && \
    mysql -e "CREATE USER 'developer'@'%' IDENTIFIED BY '12345678';" && \
    mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'developer'@'%' WITH GRANT OPTION;" && \
    mysql -e "CREATE DATABASE osticket_dev CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" && \
    mysql -e "GRANT ALL PRIVILEGES ON osticket_dev.* TO 'developer'@'%';" && \
    mysql -e "FLUSH PRIVILEGES;" 

...

CMD service mysql start && service php8.1-fpm start && service nginx start && tail -f /var/log/nginx/access.log /var/log/nginx/error.log;

接下來更動 include/class.mailfetch.php 檔案,讓他下載信件時,可以存一份在 /tmp 方便後續使用:

@file_put_contents("/tmp/debug-mail.$i", $this->mbox->getRawEmail($i));

最後,弄一隻 api/cron-dev.php 檔案,可以指定 RawMail 的格式路徑,從指定位置讀進來解析信件,如此靠 api/cron-dev.php 就可以輕鬆不斷實驗 MIMEDecode 流程:

# php api/cron-dev.php 
[INFO] Input: /tmp/debug-mail
[INFO] Input File not found

# php api/cron-dev.php /tmp/debug-mail.1
[INFO] Input: /tmp/debug-mail.1
Ticket Object
(
    [ht] => Array
        (
            [ticket_id] => 2
            [ticket_pid] => 
...
            [lastupdate] => 2024-03-27 21:35:52
            [created] => 2024-03-27 21:35:52
            [updated] => 2024-03-27 21:35:52
            [topic] => 
            [staff] => 
            [user] => User Object
                (
                    [ht] => Array
                        (
                            [id] => 2
                            [org_id] => 0
                            [default_email_id] => 2
                            [status] => 0
                            [name] => UserName
                            [created] => 2024-03-27 21:35:52
                            [updated] => 2024-03-27 21:35:52
                            [default_email] => UserEmailModel Object
                                (
                                    [ht] => Array
                                        (
                                            [id] => 2
                                            [user_id] => 2
                                            [flags] => 0
                                            [address] => user@example.com
...

2015年7月27日 星期一

AWS 筆記 - 使用 AWS Elastic Beanstalk 發佈 Docker 維護的專案 @ Mac OS X 10.10.4

用 Docker 是有用過,但統一流程發佈出去倒沒幾次,現在就抽空碰一下 AWS Elastic Beanstalk 吧!目標就是只需提供 Docker 維護的 Project 直接交遞到 AWS 上,接著就要生出一台 app service 就是了,過程中可不是要自己先開台 EC2 、裝 Docker 環境等。

參考文件:
連續動作:

$ mkdir project && cd project
$ sudo pip install awsebcli
$ eb init
You have not yet set up your credentials or your credentials are incorrect
You must provide your credentials.
(aws-access-id): XXXXXXXXX
(aws-secret-key): XXXXXXXXX

Enter Application Name
(default is "project"):


結果發生忘了設定 IAM 權限,立馬添加 AWSElasticBeanstalkFullAccess 啦,再重新執行一次

$ eb init
Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-southeast-1 : Asia Pacific (Singapore)
7) ap-southeast-2 : Asia Pacific (Sydney)
8) ap-northeast-1 : Asia Pacific (Tokyo)
9) sa-east-1 : South America (Sao Paulo)

Enter Application Name
(default is "project"):

Select a platform.
1) Node.js
2) PHP
3) Python
4) Ruby
5) Tomcat
6) IIS
7) Docker
8) Multi-container Docker
9) GlassFish
10) Go
(default is 1): 7

Do you want to set up SSH for your instances?
(y/n): y

Select a keypair.
1) changyy
2) [ Create new KeyPair ]
(default is 2):


以上就收工了 XD 在本機端的 project 裡頭,會有 .elasticbeanstalk 跟 .gitignore 的產生,接著再把一些 Dockerfile 與相關程式碼資料都搬進來吧,此例以架設 web server 為例

$ wget https://raw.githubusercontent.com/changyy/docker-study/web-server/Dockerfile
$ eb create dev-web
Creating application version archive "app-1507##_######".
Uploading project/app-1507##_######.zip to S3. This may take a while.
Upload Complete.
Environment details for: dev-web
  Application name: project
  Region: us-west-2
  Deployed Version: app-1507##_######
  Environment ID: e-#########
  Platform: 64bit Amazon Linux 2015.03 v1.4.3 running Docker 1.6.2
  Tier: WebServer-Standard
  CNAME: UNKNOWN
  Updated: 2015-07-27 03:52:28.037000+00:00
Printing Status:
INFO: createEnvironment is starting.
INFO: Using elasticbeanstalk-us-west-2-######### as Amazon S3 storage bucket for environment data.
INFO: Created load balancer named: awseb-e-p-AWSEBLoa-##########
INFO: Created security group named: awseb-e-#########-stack-AWSEBSecurityGroup-########
INFO: Created Auto Scaling launch configuration named: awseb-e-#########-stack-AWSEBAutoScalingLaunchConfiguration-NU2G8F2JA2BT
INFO: Created Auto Scaling group named: awseb-e-#########-stack-AWSEBAutoScalingGroup-############
INFO: Waiting for EC2 instances to launch. This may take a few minutes.
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:############:scalingPolicy:####-####-####-####-#####:autoScalingGroupName/awseb-e-#########-stack-AWSEBAutoScalingGroup-############:policyName/awseb-e-#########-stack-AWSEBAutoScalingScaleUpPolicy-####
INFO: Created Auto Scaling group policy named: arn:aws:autoscaling:us-west-2:############:####-####-####-####-#####:autoScalingGroupName/awseb-e-#########-stack-AWSEBAutoScalingGroup-############:policyName/awseb-e-#########-stack-AWSEBAutoScalingScaleDownPolicy-####
INFO: Created CloudWatch alarm named: awseb-e-#########-stack-AWSEBCloudwatchAlarmHigh-############
INFO: Created CloudWatch alarm named: awseb-e-#########-stack-AWSEBCloudwatchAlarmLow-############
INFO: Successfully pulled ubuntu:14.04
INFO: Successfully built aws_beanstalk/staging-app
INFO: Docker container ############ is running aws_beanstalk/current-app.
INFO: Added EC2 instance 'i-############' to Auto Scaling Group 'awseb-e-#########-stack-AWSEBAutoScalingGroup-############'.
INFO: Adding instance 'i-############' to your environment.
INFO: Successfully launched environment: dev-web


如此一來,服務就跑起來了 :P 整個過程 AWS Elastic Beanstalk 使用了 S3 儲存開發環境、開了一台 EC2 機器、啟用一個 ELB 單位、添加兩個 cloudwatch 關注服務情況(開新機器或關機器)以及一個 Auto scaling group 來確保服務的不中斷。

如此一來,可以再用此指令開啟瀏覽位置

$ eb open

接著就會開啟 browser 瀏覽了,不需自行在找尋找位置。

另外,有興趣的可以用以下指令研究:

$ eb status
Environment details for: dev-web
  Application name: project
  Region: us-west-2
  Deployed Version: 4a06
  Environment ID: e-######
  Platform: 64bit Amazon Linux 2015.03 v1.4.3 running Docker 1.6.2
  Tier: WebServer-Standard
  CNAME: dev-web-######.elasticbeanstalk.com
  Updated: 2015-07-27 06:15:45.859000+00:00
  Status: Ready
  Health: Green


$ eb logs
...
-------------------------------------
/var/log/docker-ps.log
-------------------------------------
'docker ps' ran at Mon Jul 27 06:34:11 UTC 2015:
CONTAINER ID        IMAGE                              COMMAND               CREATED             STATUS              PORTS                     NAMES
#########        aws_beanstalk/staging-app:latest   "/usr/sbin/apache2ctl -D FOREGROUND"   19 minutes ago      Up 19 minutes       80/tcp, 443/tcp   furious_feynman  
...


$ eb config
...


$ eb terminate
The environment "dev-web" and all associated instances will be terminated.
To confirm, type the environment name:



以上就差不多了,若有興趣的可以試一下另一個 dockfile: https://github.com/changyy/docker-study/tree/web-ssh-server,除了要下載 Dockfile 外,還要下載 start_service.sh ,其中後者可以參考 Docker 官方文件,改用 Supervisor 也行(https://docs.docker.com/articles/using_supervisord/)。機器開啟後,多了 ssh server 在執行。這時先去 EC2 查詢是哪一台機器,用 ssh ec2-user@server_ip 登入看看。