└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # **How to Deploy Nest js & Nextjs to Hostinger Vps & enable github actions CI/CD** 2 | 3 | # link the vps with domain 4 | ``` 5 | 1- copy vps ip address 6 | 2- go to domain dns system 7 | 3- replace A record with 8 | @ vps ip address 3600(ttl) 9 | ``` 10 | # Link Any sub domain 11 | ``` 12 | #add a dns record for each subdomain 13 | for example api.domain.com 14 | name : api 15 | type : A 16 | ipe : vps ip 17 | ttl :3600 18 | ``` 19 | # setup git & node envs to server 20 | ``` 21 | # Update system packages 22 | sudo apt update && sudo apt upgrade -y 23 | 24 | # Install Node.js and npm (using Node.js 18.x as an example) 25 | curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - 26 | sudo apt install -y nodejs 27 | 28 | # Install build essentials 29 | sudo apt install -y build-essential 30 | 31 | # Install Git 32 | sudo apt install -y git 33 | 34 | # Install PM2 for process management 35 | sudo npm install -g pm2 36 | 37 | # Verify installations 38 | node -v 39 | npm -v 40 | git --version 41 | ``` 42 | 43 | # Next.js Application Setup 44 | 45 | ``` 46 | # Create directory for your app 47 | mkdir -p /var/www/nextjs-app 48 | cd /var/www/nextjs-app 49 | 50 | # Clone your Next.js repository (replace with your repo URL) 51 | git clone https://github.com/yourusername/your-nextjs-app.git . 52 | ** if private ** 53 | -username 54 | -password : get it from github deplovepers page (token) 55 | # Install dependencies 56 | npm install 57 | 58 | nano .env 59 | -> copy and paste then ctrl+x 60 | 61 | # Build the app 62 | npm run build 63 | 64 | # Start with PM2 65 | pm2 start npm --name "nextjs-app" -- start 66 | ``` 67 | # Nest.js Application Setup 68 | ``` 69 | # Create directory for your app 70 | mkdir -p /var/www/nestjs-app 71 | cd /var/www/nestjs-app 72 | 73 | # Clone your NestJS repository (replace with your repo URL) 74 | git clone https://github.com/yourusername/your-nestjs-app.git . 75 | 76 | # Install dependencies 77 | npm install 78 | - if u use pnpm 79 | -> install -g pnpm 80 | then pnpm install 81 | 82 | #add .env 83 | 84 | nano .env 85 | -> copy and paste then ctrl+x 86 | 87 | # Build the app 88 | npm run build 89 | 90 | # Start with PM2 91 | pm2 start dist/main.js --name "nestjs-app" 92 | ``` 93 | 94 | # Setup Nginx as Reverse Proxy 95 | 96 | ``` 97 | # Install Nginx 98 | sudo apt install -y nginx 99 | 100 | # Configure Nginx for Next.js 101 | sudo nano /etc/nginx/sites-available/nextjs 102 | 103 | # Add configuration 104 | server { 105 | listen 80; 106 | server_name yourdomain.com www.yourdomain.com; 107 | 108 | location / { 109 | proxy_pass http://localhost:3000; 110 | proxy_http_version 1.1; 111 | proxy_set_header Upgrade $http_upgrade; 112 | proxy_set_header Connection 'upgrade'; 113 | proxy_set_header Host $host; 114 | proxy_cache_bypass $http_upgrade; 115 | } 116 | } 117 | 118 | # Configure Nginx for NestJS 119 | sudo nano /etc/nginx/sites-available/nestjs 120 | 121 | # Add configuration 122 | server { 123 | listen 80; 124 | server_name www.api.yourdomain.com api.yourdomain.com; 125 | 126 | location / { 127 | proxy_pass http://localhost:3001; 128 | proxy_http_version 1.1; 129 | proxy_set_header Upgrade $http_upgrade; 130 | proxy_set_header Connection 'upgrade'; 131 | proxy_set_header Host $host; 132 | proxy_cache_bypass $http_upgrade; 133 | } 134 | } 135 | 136 | # Enable sites 137 | sudo ln -s /etc/nginx/sites-available/nextjs /etc/nginx/sites-enabled/ 138 | sudo ln -s /etc/nginx/sites-available/nestjs /etc/nginx/sites-enabled/ 139 | 140 | # Test and restart Nginx 141 | sudo nginx -t 142 | sudo systemctl restart nginx 143 | ``` 144 | 145 | # Add ssl certificates (https) 146 | ``` 147 | # Install Certbot 148 | sudo apt install -y certbot python3-certbot-nginx 149 | 150 | # Obtain SSL certificates 151 | sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com 152 | sudo certbot --nginx -d api.yourdomain.com 153 | 154 | # Auto-renewal is set up by default 155 | 156 | #open your nginx and test if ssl certificates is enabled 157 | sudo nano /etc/nginx/sites-available/nestjs 158 | sudo nano /etc/nginx/sites-available/nextjs 159 | #then if added 160 | ctrl+x 161 | ``` 162 | # Add A github Workflow 163 | Note : the secret keys u can add them to github repo 164 | from 165 | --> repo settings 166 | ---> secrets and keys 167 | ----> create secret repo 168 | * host : vps ip 169 | * user : root or (username) 170 | * ssh_key ---> cat ~/.ssh/id_rsa 171 | ``` 172 | # .github/workflows/nextjs-deploy.yml 173 | name: Deploy Next.js Application 174 | 175 | on: 176 | push: 177 | branches: [ main ] 178 | paths: 179 | - 'nextjs-app/**' 180 | 181 | jobs: 182 | deploy: 183 | runs-on: ubuntu-latest 184 | steps: 185 | - uses: actions/checkout@v3 186 | 187 | - name: Setup Node.js 188 | uses: actions/setup-node@v3 189 | with: 190 | node-version: '18' 191 | 192 | - name: Install dependencies 193 | working-directory: ./ 194 | run: npm ci 195 | 196 | - name: Build application 197 | working-directory: ./ 198 | run: npm run build 199 | 200 | - name: Deploy to VPS 201 | uses: appleboy/ssh-action@master 202 | with: 203 | host: ${{ secrets.VPS_HOST }} 204 | username: ${{ secrets.VPS_USERNAME }} 205 | key: ${{ secrets.VPS_SSH_KEY }} 206 | script: | 207 | cd /var/www/nextjs-app 208 | git pull git@github.com:username/repo-name.git 209 | 210 | npm ci 211 | npm run build 212 | pm2 restart nextjs-app 213 | 214 | # .github/workflows/nestjs-deploy.yml 215 | name: Deploy NestJS Application 216 | 217 | on: 218 | push: 219 | branches: [ main ] 220 | paths: 221 | - 'nestjs-app/**' 222 | 223 | jobs: 224 | deploy: 225 | runs-on: ubuntu-latest 226 | steps: 227 | - uses: actions/checkout@v3 228 | 229 | - name: Setup Node.js 230 | uses: actions/setup-node@v3 231 | with: 232 | node-version: '18' 233 | 234 | - name: Install dependencies 235 | working-directory: ./ 236 | run: npm ci 237 | 238 | - name: Build application 239 | working-directory: ./ 240 | run: npm run build 241 | 242 | - name: Deploy to VPS 243 | uses: appleboy/ssh-action@master 244 | with: 245 | host: ${{ secrets.VPS_HOST }} 246 | username: ${{ secrets.VPS_USERNAME }} 247 | key: ${{ secrets.VPS_SSH_KEY }} 248 | script: | 249 | cd /var/www/nestjs-app 250 | git pull 251 | npm ci 252 | npm run build 253 | pm2 restart nestjs-app 254 | ``` 255 | --------------------------------------------------------------------------------