From 0e160c2b9a784371a4dff1e74ce5c4c04101974a Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu@ip-172-31-95-148.ec2.internal>
Date: Wed, 30 Apr 2025 17:04:42 +0000
Subject: [PATCH 01/13] Update from EC@

---
 Dockerfile         | 20 ++++++++++++++++++++
 docker-compose.yml | 26 +++++++++++++++++++++-----
 2 files changed, 41 insertions(+), 5 deletions(-)
 create mode 100644 Dockerfile

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 000000000..09dd5219a
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,20 @@
+FROM php:7.4-apache
+
+# Install PHP extensions for Yii2
+RUN docker-php-ext-install pdo pdo_mysql
+
+# Enable mod_rewrite for Yii2
+RUN a2enmod rewrite
+
+# Set DocumentRoot to Yii2's web directory
+ENV APACHE_DOCUMENT_ROOT /var/www/html/web
+
+# Update Apache configuration
+RUN sed -ri 's|/var/www/html|${APACHE_DOCUMENT_ROOT}|g' /etc/apache2/sites-available/000-default.conf \
+    && sed -ri 's|/var/www/|${APACHE_DOCUMENT_ROOT}|g' /etc/apache2/apache2.conf
+
+# Set permissions for the Yii2 app
+WORKDIR /var/www/html
+COPY . /var/www/html
+RUN chown -R www-data:www-data /var/www/html && chmod -R 755 /var/www/html
+
diff --git a/docker-compose.yml b/docker-compose.yml
index 86be3bd0d..61150bd53 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,9 +1,25 @@
-version: '2'
+version: '3.8'
+
 services:
   php:
-    image: yiisoftware/yii2-php:7.4-apache
-    volumes:
-      - ~/.composer-docker/cache:/root/.composer/cache:delegated
-      - ./:/app:delegated
+    build:
+      context: .
+    image: yii2app-custom
     ports:
       - '8000:80'
+    networks:
+      - app-net
+    deploy:
+      replicas: 1
+      restart_policy:
+        condition: on-failure
+    volumes:
+      - yii2app-data:/var/www/html  # Named volume, safe in Swarm
+
+volumes:
+  yii2app-data:
+
+networks:
+  app-net:
+    driver: overlay
+

From 1f6994bff1ad92921149f565e0e095e54d0942bc Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Wed, 30 Apr 2025 23:13:43 +0530
Subject: [PATCH 02/13] Create deploy.yml

---
 .github/workflows/deploy.yml | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 .github/workflows/deploy.yml

diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml
new file mode 100644
index 000000000..fe2d05341
--- /dev/null
+++ b/.github/workflows/deploy.yml
@@ -0,0 +1,32 @@
+name: Deploy to EC2
+
+on:
+  push:
+    branches:
+      - master
+
+jobs:
+  deploy:
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: Checkout code
+        uses: actions/checkout@v3
+
+      - name: Log in to Docker Hub
+        run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
+
+      - name: Set up SSH
+        run: |
+          mkdir -p ~/.ssh
+          echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
+          chmod 600 ~/.ssh/id_rsa
+          ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
+
+      - name: Deploy to EC2 via SSH
+        run: |
+          ssh -o StrictHostKeyChecking=no ${{ secrets.EC2_USERNAME }}@${{ secrets.EC2_HOST }} << 'EOF'
+            docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
+            docker pull kubemanan/yii2app-custom:latest
+            docker service update --image kubemanan/yii2app-custom:latest yii2app_php
+          EOF

From dd7b4e63547c78ebebc885e2948e1368fba222bf Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Wed, 30 Apr 2025 23:38:10 +0530
Subject: [PATCH 03/13] Update docker-compose.yml

---
 docker-compose.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/docker-compose.yml b/docker-compose.yml
index 61150bd53..f4dcd17a6 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -4,7 +4,7 @@ services:
   php:
     build:
       context: .
-    image: yii2app-custom
+    image: kubemanan/yii2app-custom:latest
     ports:
       - '8000:80'
     networks:

From b613d25aecbb80a6037e2f82b647f83892e93e9c Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Thu, 1 May 2025 00:03:52 +0530
Subject: [PATCH 04/13] Create docker.yml

---
 ansible/docker.yml | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 ansible/docker.yml

diff --git a/ansible/docker.yml b/ansible/docker.yml
new file mode 100644
index 000000000..005ebc9e6
--- /dev/null
+++ b/ansible/docker.yml
@@ -0,0 +1,44 @@
+---
+- name: Install Docker and Docker Compose
+  hosts: all
+  become: true
+
+  tasks:
+    - name: Install required packages for Docker
+      apt:
+        name:
+          - apt-transport-https
+          - ca-certificates
+          - curl
+          - gnupg-agent
+          - lsb-release
+          - software-properties-common
+        state: present
+        update_cache: yes
+
+    - name: Add Docker GPG key
+      apt_key:
+        url: https://download.docker.com/linux/ubuntu/gpg
+        state: present
+
+    - name: Add Docker repository
+      apt_repository:
+        repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
+        state: present
+
+    - name: Install Docker CE
+      apt:
+        name: docker-ce
+        state: latest
+
+    - name: Install Docker Compose
+      curl:
+        url: https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)
+        dest: /usr/local/bin/docker-compose
+        mode: 'u+x'
+    
+    - name: Start and enable Docker service
+      systemd:
+        name: docker
+        enabled: yes
+        state: started

From 67067c494376af73763575a4aef3407ae29745cc Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Thu, 1 May 2025 00:05:58 +0530
Subject: [PATCH 05/13] Create nginx_php.yml

---
 ansible/nginx_php.yml | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 ansible/nginx_php.yml

diff --git a/ansible/nginx_php.yml b/ansible/nginx_php.yml
new file mode 100644
index 000000000..b890e1b24
--- /dev/null
+++ b/ansible/nginx_php.yml
@@ -0,0 +1,32 @@
+---
+- name: Install NGINX, Git, PHP dependencies
+  hosts: all
+  become: true
+
+  tasks:
+    - name: Install NGINX
+      apt:
+        name: nginx
+        state: latest
+
+    - name: Install Git
+      apt:
+        name: git
+        state: latest
+
+    - name: Install PHP dependencies
+      apt:
+        name:
+          - php
+          - php-fpm
+          - php-mysql
+          - php-cli
+          - php-xml
+          - php-mbstring
+        state: latest
+
+    - name: Start and enable NGINX service
+      systemd:
+        name: nginx
+        enabled: yes
+        state: started

From 80ffd084adae8b9947f6dd4f99c05c69fb8d64df Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Thu, 1 May 2025 00:06:16 +0530
Subject: [PATCH 06/13] Create swarm.yml

---
 ansible/swarm.yml | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 ansible/swarm.yml

diff --git a/ansible/swarm.yml b/ansible/swarm.yml
new file mode 100644
index 000000000..4bd2c08ab
--- /dev/null
+++ b/ansible/swarm.yml
@@ -0,0 +1,20 @@
+---
+- name: Initialize Docker Swarm
+  hosts: all
+  become: true
+
+  tasks:
+    - name: Initialize Docker Swarm
+      shell: docker swarm init
+      when: ansible_facts['distribution'] == 'Ubuntu'
+      ignore_errors: yes
+
+    - name: Get join token for workers
+      shell: docker swarm join-token worker -q
+      register: worker_token
+      when: ansible_facts['distribution'] == 'Ubuntu'
+      ignore_errors: yes
+
+    - name: Display join token for workers
+      debug:
+        var: worker_token.stdout

From 14dd3ffe13ddc0670795f0a30f4f5a5dd89c36f4 Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Thu, 1 May 2025 00:06:40 +0530
Subject: [PATCH 07/13] Create nginx_config.yml

---
 ansible/nginx_config.yml | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 ansible/nginx_config.yml

diff --git a/ansible/nginx_config.yml b/ansible/nginx_config.yml
new file mode 100644
index 000000000..3214f670e
--- /dev/null
+++ b/ansible/nginx_config.yml
@@ -0,0 +1,27 @@
+---
+- name: Configure NGINX Reverse Proxy for Yii2 App
+  hosts: all
+  become: true
+
+  tasks:
+    - name: Create reverse proxy NGINX config
+      copy:
+        dest: /etc/nginx/sites-available/default
+        content: |
+          server {
+              listen 80;
+              server_name  localhost;
+
+              location / {
+                  proxy_pass http://localhost:8000;
+                  proxy_set_header Host $host;
+                  proxy_set_header X-Real-IP $remote_addr;
+                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+                  proxy_set_header X-Forwarded-Proto $scheme;
+              }
+          }
+
+    - name: Reload NGINX
+      systemd:
+        name: nginx
+        state: reloaded

From e361c454e2c7019686ae30d85aef75c138fa27c8 Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Thu, 1 May 2025 00:07:11 +0530
Subject: [PATCH 08/13] Create deploy_service.yml

---
 ansible/deploy_service.yml | 8 ++++++++
 1 file changed, 8 insertions(+)
 create mode 100644 ansible/deploy_service.yml

diff --git a/ansible/deploy_service.yml b/ansible/deploy_service.yml
new file mode 100644
index 000000000..dc28d09ec
--- /dev/null
+++ b/ansible/deploy_service.yml
@@ -0,0 +1,8 @@
+---
+- name: Deploy Yii2 app as Swarm service
+  hosts: all
+  become: true
+
+  tasks:
+    - name: Deploy PHP service
+      shell: docker stack deploy -c /app/docker-compose.yml yii2app

From 820b5fbbf7a1950b844accd3af0fbf0da1c7a8a4 Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Thu, 1 May 2025 00:07:48 +0530
Subject: [PATCH 09/13] Create clone_repo.yml

---
 ansible/clone_repo.yml | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100644 ansible/clone_repo.yml

diff --git a/ansible/clone_repo.yml b/ansible/clone_repo.yml
new file mode 100644
index 000000000..819fe52e1
--- /dev/null
+++ b/ansible/clone_repo.yml
@@ -0,0 +1,11 @@
+---
+- name: Clone the Yii2 repo
+  hosts: all
+  become: true
+
+  tasks:
+    - name: Clone repository
+      git:
+        repo: 'https://github.com/croodycoder/yii2-app-basic.git'
+        dest: /app
+        version: master

From bc086ab233ca4befeda40579d4ba0872149fc176 Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Thu, 1 May 2025 00:15:23 +0530
Subject: [PATCH 10/13] Update build.yml

---
 .github/workflows/build.yml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index f1065f909..a88622956 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -38,7 +38,7 @@ jobs:
           key: ${{ env.key }}
 
       - name: Cache extensions
-        uses: actions/cache@v1
+        uses: actions/cache@v2  # Updated to v2
         with:
           path: ${{ steps.cache-env.outputs.dir }}
           key: ${{ steps.cache-env.outputs.key }}
@@ -62,7 +62,7 @@ jobs:
             echo "COMPOSER_CACHE_DIR=~\AppData\Local\Composer" >> $GITHUB_ENV
 
       - name: Cache dependencies installed with composer
-        uses: actions/cache@v1
+        uses: actions/cache@v2  # Updated to v2
         with:
           path: ${{ steps.cache-env.outputs.dir }}
           key: php${{ matrix.php }}-composer-${{ matrix.dependencies }}-${{ hashFiles('**/composer.json') }}

From 0d542f0bcea104ab5d5d46129b91bfe6a33cb60e Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Thu, 1 May 2025 00:19:08 +0530
Subject: [PATCH 11/13] Update build.yml

---
 .github/workflows/build.yml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index a88622956..def41f11a 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -31,14 +31,14 @@ jobs:
 
       - name: Setup cache environment
         id: cache-env
-        uses: shivammathur/cache-extensions@v1
+        uses: shivammathur/cache-extensions@v2.323.0
         with:
           php-version: ${{ matrix.php }}
           extensions: ${{ env.extensions }}
           key: ${{ env.key }}
 
       - name: Cache extensions
-        uses: actions/cache@v2  # Updated to v2
+        uses: actions/cache@v2.1.8  # Updated to v2
         with:
           path: ${{ steps.cache-env.outputs.dir }}
           key: ${{ steps.cache-env.outputs.key }}

From c47e3e36f8244789e386e2eab33b1c2dead43c36 Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Thu, 1 May 2025 00:20:46 +0530
Subject: [PATCH 12/13] Update build.yml

---
 .github/workflows/build.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index def41f11a..888d4ee0f 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -31,7 +31,7 @@ jobs:
 
       - name: Setup cache environment
         id: cache-env
-        uses: shivammathur/cache-extensions@v2.323.0
+        uses: shivammathur/cache-extensions@v2.1.8
         with:
           php-version: ${{ matrix.php }}
           extensions: ${{ env.extensions }}

From 1ad25973df640ffc1f0ab8ff71204be6387f3553 Mon Sep 17 00:00:00 2001
From: Crooder <153824801+croodycoder@users.noreply.github.com>
Date: Thu, 1 May 2025 00:22:04 +0530
Subject: [PATCH 13/13] Update build.yml

---
 .github/workflows/build.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 888d4ee0f..def41f11a 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -31,7 +31,7 @@ jobs:
 
       - name: Setup cache environment
         id: cache-env
-        uses: shivammathur/cache-extensions@v2.1.8
+        uses: shivammathur/cache-extensions@v2.323.0
         with:
           php-version: ${{ matrix.php }}
           extensions: ${{ env.extensions }}
