From 75e6d73ee3082e24fc9d65e4bff75fea054bba3f Mon Sep 17 00:00:00 2001 From: ppnm Date: Tue, 19 Nov 2024 19:50:03 +0630 Subject: [PATCH 01/13] github --- ecosystem.config.js | 19 ++++++++++++++++ main.yml | 54 +++++++++++++++++++++++++++++++++++++++++++++ next.config.mjs | 2 +- 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 ecosystem.config.js create mode 100644 main.yml diff --git a/ecosystem.config.js b/ecosystem.config.js new file mode 100644 index 0000000..7547047 --- /dev/null +++ b/ecosystem.config.js @@ -0,0 +1,19 @@ +// example ecosystem.config.js +// ecosystem.config.js +module.exports = { + apps: [ + { + name: "next-app", + script: "npm", + args: "start -p 3000", + watch: true, + + instances: 4, + exec_mode: "cluster", + + env: { + NODE_ENV: "production", + }, + }, + ], + }; \ No newline at end of file diff --git a/main.yml b/main.yml new file mode 100644 index 0000000..490d8c0 --- /dev/null +++ b/main.yml @@ -0,0 +1,54 @@ +name: Nextjs-App Deploy with GitHub Actions + +on: + push: + branches: + - master + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout Code + uses: actions/checkout@v2 + + - name: SSH into AWS Lightsail and Deploy + uses: appleboy/ssh-action@v1.1.0 + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + key: ${{ secrets.SSHKEY }} + script: | + + # Install dependencies + echo "Installing dependencies..." + sudo apt update + curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - + sudo apt install -y nodejs npm nginx + + # Clone repository + echo "Cloning repository..." + cd /var/www + git clone https://github.com/devktops/nextjs-app.git /var/www/nextjs-app || true + sudo chown -R $USER:www-data /var/www/nextjs-app + + # Install project dependencies and build + echo "Installing project dependencies and building the project..." + npm install + npm audit fix --force + npm run build + sudo chown -R $USER:www-data /var/www/nextjs-app + + # Start application with PM2 + echo "Starting application with PM2..." + sudo npm install -g pm2 + pm2 start next-app + pm2 save + sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u $USER --hp /home/$USER + + # Verify setup + echo "Verifying setup..." + node -v + npm -v + pm2 list diff --git a/next.config.mjs b/next.config.mjs index c34317f..1a5e515 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,6 +1,6 @@ /** @type {import('next').NextConfig} */ const nextConfig = { - output: 'export', + //output: 'export', distDir: 'out', trailingSlash: true, }; From f0a2b8b8c16d72c7a0e75277e9db796d391ac1ae Mon Sep 17 00:00:00 2001 From: ppnm Date: Tue, 19 Nov 2024 19:56:33 +0630 Subject: [PATCH 02/13] update --- main.yml => .github/workflows/main.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename main.yml => .github/workflows/main.yml (100%) diff --git a/main.yml b/.github/workflows/main.yml similarity index 100% rename from main.yml rename to .github/workflows/main.yml From 3c66449605540e8b785e1471be469f8065e2ca1a Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:03:47 +0630 Subject: [PATCH 03/13] fix 00 --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 490d8c0..33a5d25 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,14 +13,13 @@ jobs: - name: Checkout Code uses: actions/checkout@v2 - - name: SSH into AWS Lightsail and Deploy + - name: SSH into AWS Lightsail and Deploy uses: appleboy/ssh-action@v1.1.0 with: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} key: ${{ secrets.SSHKEY }} script: | - # Install dependencies echo "Installing dependencies..." sudo apt update @@ -35,6 +34,7 @@ jobs: # Install project dependencies and build echo "Installing project dependencies and building the project..." + cd /var/www/nextjs-app npm install npm audit fix --force npm run build From 38e5e36d5a36f6fcece09887946c656457c581f8 Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:15:55 +0630 Subject: [PATCH 04/13] fixed 01 --- .github/workflows/main.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 33a5d25..1a9fe99 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,15 +22,15 @@ jobs: script: | # Install dependencies echo "Installing dependencies..." - sudo apt update + apt update curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - - sudo apt install -y nodejs npm nginx + apt install -y nodejs npm nginx # Clone repository echo "Cloning repository..." cd /var/www git clone https://github.com/devktops/nextjs-app.git /var/www/nextjs-app || true - sudo chown -R $USER:www-data /var/www/nextjs-app + chown -R $USER:www-data /var/www/nextjs-app # Install project dependencies and build echo "Installing project dependencies and building the project..." @@ -38,11 +38,11 @@ jobs: npm install npm audit fix --force npm run build - sudo chown -R $USER:www-data /var/www/nextjs-app + chown -R $USER:www-data /var/www/nextjs-app # Start application with PM2 echo "Starting application with PM2..." - sudo npm install -g pm2 + npm install -g pm2 pm2 start next-app pm2 save sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u $USER --hp /home/$USER From caaf9d1a52a19b4c0642189d61afcdcd5d11a7cd Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:29:23 +0630 Subject: [PATCH 05/13] fixed 03 --- .github/workflows/main.yml | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1a9fe99..7c9b146 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,18 +20,13 @@ jobs: username: ${{ secrets.USERNAME }} key: ${{ secrets.SSHKEY }} script: | - # Install dependencies - echo "Installing dependencies..." - apt update - curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - - apt install -y nodejs npm nginx - + # Clone repository echo "Cloning repository..." cd /var/www git clone https://github.com/devktops/nextjs-app.git /var/www/nextjs-app || true - chown -R $USER:www-data /var/www/nextjs-app - + sudo chown -R $USER:www-data /var/www/nextjs-app + # Install project dependencies and build echo "Installing project dependencies and building the project..." cd /var/www/nextjs-app From db01465661807600d5a49c685e710c2bf34b655f Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:32:35 +0630 Subject: [PATCH 06/13] fixed 04 --- .github/workflows/main.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7c9b146..a23f946 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -21,12 +21,7 @@ jobs: key: ${{ secrets.SSHKEY }} script: | - # Clone repository - echo "Cloning repository..." - cd /var/www - git clone https://github.com/devktops/nextjs-app.git /var/www/nextjs-app || true - sudo chown -R $USER:www-data /var/www/nextjs-app - + # Install project dependencies and build echo "Installing project dependencies and building the project..." cd /var/www/nextjs-app From 371aff7a5b8a6ebb4c77f616aa37b0c4897ad5d5 Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:33:28 +0630 Subject: [PATCH 07/13] finxed 05 --- .github/workflows/main.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a23f946..49b5c79 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,8 +20,7 @@ jobs: username: ${{ secrets.USERNAME }} key: ${{ secrets.SSHKEY }} script: | - - + # Install project dependencies and build echo "Installing project dependencies and building the project..." cd /var/www/nextjs-app From 5908bd4dc535f0272f50898964c821a159bde542 Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:36:53 +0630 Subject: [PATCH 08/13] fixed 05 --- .github/workflows/main.yml | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 49b5c79..992cad5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,6 +13,11 @@ jobs: - name: Checkout Code uses: actions/checkout@v2 + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: '16' # Specify the Node.js version you're using + - name: SSH into AWS Lightsail and Deploy uses: appleboy/ssh-action@v1.1.0 with: @@ -20,24 +25,33 @@ jobs: username: ${{ secrets.USERNAME }} key: ${{ secrets.SSHKEY }} script: | - - # Install project dependencies and build + # Ensure the server is up-to-date + echo "Updating and upgrading system packages..." + sudo apt-get update -y + sudo apt-get upgrade -y + + # Install project dependencies echo "Installing project dependencies and building the project..." cd /var/www/nextjs-app npm install npm audit fix --force npm run build - chown -R $USER:www-data /var/www/nextjs-app - - # Start application with PM2 + + # Install and use PM2 to run the app + echo "Installing PM2..." + sudo npm install -g pm2 + + # Start the application with PM2 echo "Starting application with PM2..." - npm install -g pm2 - pm2 start next-app + pm2 start npm --name "nextjs-app" -- run start pm2 save + + # Set PM2 to start on system boot + echo "Configuring PM2 to start on system boot..." sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u $USER --hp /home/$USER - + # Verify setup - echo "Verifying setup..." + echo "Verifying Node.js, npm, and PM2 versions..." node -v npm -v pm2 list From dbfd25ee23ddc6f255a535d6f6b8a8a958ed1b29 Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Tue, 19 Nov 2024 20:45:47 +0630 Subject: [PATCH 09/13] Create me --- me | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 me diff --git a/me b/me new file mode 100644 index 0000000..490d8c0 --- /dev/null +++ b/me @@ -0,0 +1,54 @@ +name: Nextjs-App Deploy with GitHub Actions + +on: + push: + branches: + - master + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout Code + uses: actions/checkout@v2 + + - name: SSH into AWS Lightsail and Deploy + uses: appleboy/ssh-action@v1.1.0 + with: + host: ${{ secrets.HOST }} + username: ${{ secrets.USERNAME }} + key: ${{ secrets.SSHKEY }} + script: | + + # Install dependencies + echo "Installing dependencies..." + sudo apt update + curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - + sudo apt install -y nodejs npm nginx + + # Clone repository + echo "Cloning repository..." + cd /var/www + git clone https://github.com/devktops/nextjs-app.git /var/www/nextjs-app || true + sudo chown -R $USER:www-data /var/www/nextjs-app + + # Install project dependencies and build + echo "Installing project dependencies and building the project..." + npm install + npm audit fix --force + npm run build + sudo chown -R $USER:www-data /var/www/nextjs-app + + # Start application with PM2 + echo "Starting application with PM2..." + sudo npm install -g pm2 + pm2 start next-app + pm2 save + sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u $USER --hp /home/$USER + + # Verify setup + echo "Verifying setup..." + node -v + npm -v + pm2 list From f212362acce479187eb764ee2a7356e6982d9c16 Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:07:19 +0630 Subject: [PATCH 10/13] fixed 06 --- .github/workflows/main.yml | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 992cad5..3fb4923 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,11 +13,6 @@ jobs: - name: Checkout Code uses: actions/checkout@v2 - - name: Set up Node.js - uses: actions/setup-node@v2 - with: - node-version: '16' # Specify the Node.js version you're using - - name: SSH into AWS Lightsail and Deploy uses: appleboy/ssh-action@v1.1.0 with: @@ -25,33 +20,12 @@ jobs: username: ${{ secrets.USERNAME }} key: ${{ secrets.SSHKEY }} script: | - # Ensure the server is up-to-date - echo "Updating and upgrading system packages..." - sudo apt-get update -y - sudo apt-get upgrade -y - - # Install project dependencies - echo "Installing project dependencies and building the project..." - cd /var/www/nextjs-app + cd /path/to/your/app npm install npm audit fix --force npm run build - - # Install and use PM2 to run the app - echo "Installing PM2..." - sudo npm install -g pm2 - - # Start the application with PM2 - echo "Starting application with PM2..." - pm2 start npm --name "nextjs-app" -- run start + pm2 start next-app pm2 save - - # Set PM2 to start on system boot - echo "Configuring PM2 to start on system boot..." - sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u $USER --hp /home/$USER - - # Verify setup - echo "Verifying Node.js, npm, and PM2 versions..." node -v npm -v pm2 list From 4298c7b215db43441e98f5f8316fd0cdeac5399a Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:09:30 +0630 Subject: [PATCH 11/13] fixed 07 --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3fb4923..04bd13f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,8 +20,8 @@ jobs: username: ${{ secrets.USERNAME }} key: ${{ secrets.SSHKEY }} script: | - cd /path/to/your/app - npm install + cd /var/www/nextjs-app + npm i -y npm audit fix --force npm run build pm2 start next-app From d805bd0a497e19203075d31c9f79788aa313e683 Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:30:45 +0630 Subject: [PATCH 12/13] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 04bd13f..f74ac3b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,8 +19,8 @@ jobs: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} key: ${{ secrets.SSHKEY }} + workdir: /var/www/nextjs-app script: | - cd /var/www/nextjs-app npm i -y npm audit fix --force npm run build From a18f6d8bedbf305e8cba9353b9e7d502616b8368 Mon Sep 17 00:00:00 2001 From: PPNM <125627160+PPNM@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:50:33 +0630 Subject: [PATCH 13/13] Update main.yml --- .github/workflows/main.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f74ac3b..a331c16 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -4,11 +4,9 @@ on: push: branches: - master - jobs: deploy: runs-on: ubuntu-latest - steps: - name: Checkout Code uses: actions/checkout@v2 @@ -19,12 +17,15 @@ jobs: host: ${{ secrets.HOST }} username: ${{ secrets.USERNAME }} key: ${{ secrets.SSHKEY }} - workdir: /var/www/nextjs-app script: | + set -e + cd /var/www/nextjs-app || exit 1 + ls -la npm i -y npm audit fix --force npm run build - pm2 start next-app + pm2 stop next-app || true + pm2 start npm --name "next-app" -- run start pm2 save node -v npm -v