|
| 1 | +Step 1: Signup in AWS Account for Free Tier |
| 2 | +Step 1A: Create Docker Account Keep save your username and password for future use |
| 3 | +Step 2: Create EC2 Instance using Ubuntu Image |
| 4 | +Step 3: Create RDS for Database -> Select MySQL -> Security Group allow Inbound and Outbound Rules |
| 5 | +Step 4: Login to EC2 using Connect |
| 6 | +Step 5: Install Docker |
| 7 | +Step 6: Download mySql Workbench |
| 8 | + |
| 9 | +Check Docker Image : docker run -it your-dockerhub-username/ecommerce-backend:latest /bin/bash |
| 10 | +Check Docker Logs : docker logs <container_id> |
| 11 | +-----------------------------------Install Docker Code for EC2----------------------------- |
| 12 | + |
| 13 | +sudo apt update |
| 14 | +sudo apt install -y docker.io |
| 15 | +sudo systemctl start docker |
| 16 | +sudo systemctl enable docker |
| 17 | + |
| 18 | +# Install Docker Compose |
| 19 | +sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose |
| 20 | +sudo chmod +x /usr/local/bin/docker-compose |
| 21 | + |
| 22 | +# Add your user to the docker group |
| 23 | +sudo usermod -aG docker $USER |
| 24 | + |
| 25 | +-------------------------Then Open Port 80 and 8000 for Django in EC2 ------------------------- |
| 26 | +sudo ufw allow 8000 |
| 27 | +sudo ufw allow 80 |
| 28 | + |
| 29 | +-----------Install MySQL CLient Like Workbench or In VScode then Connect Database Server------- |
| 30 | +Create Database From Explore |
| 31 | + |
| 32 | +----------------------------Project Settings and Configurations------------------------------- |
| 33 | + |
| 34 | +.env -> Update Database Setting in .env File |
| 35 | + |
| 36 | +------------------------------------------------- |
| 37 | + |
| 38 | +settings.py |
| 39 | +ALLOWED_HOSTS = ["*"] |
| 40 | +STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') # Directory for collectstatic |
| 41 | +STATICFILES_DIRS = [ |
| 42 | + os.path.join(BASE_DIR, 'static'), # Directory for static files used in development |
| 43 | +] |
| 44 | + |
| 45 | +TEMPLATES = [ |
| 46 | + { ....... |
| 47 | + 'DIRS': ['EcommerceInventory/templates'], #Add this Template Directory For React |
| 48 | + } |
| 49 | + |
| 50 | +-------------------------------------------------- |
| 51 | +urls.py |
| 52 | +if settings.DEBUG: |
| 53 | + urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) |
| 54 | + |
| 55 | +# Add the catch-all pattern at the end |
| 56 | +urlpatterns += [ |
| 57 | + re_path(r'^(?:.*)/?$', index), |
| 58 | +] |
| 59 | + |
| 60 | +def index(request): |
| 61 | + return render(request, 'index.html') |
| 62 | + |
| 63 | +Create templates Directory in Project Folder |
| 64 | + |
| 65 | +--------------------Update API Url in React .env file--------------------------- |
| 66 | +REACT_APP_API_URL='API_URL' |
| 67 | + |
| 68 | +-------------------Updating index.html Content of React------------------------ |
| 69 | + |
| 70 | + |
| 71 | +-----------------Create Dockerfile in Parent Directory------------------------ |
| 72 | +# Stage 1: Build frontend |
| 73 | +FROM node:18 as build-stage |
| 74 | +WORKDIR /code |
| 75 | + |
| 76 | +# # Set working directory for frontend |
| 77 | +# WORKDIR /app/frontend |
| 78 | + |
| 79 | + |
| 80 | +# # Copy frontend package files |
| 81 | +COPY ./Frontend/ecommerce_inventory/ /code/Frontend/ecommerce_inventory/ |
| 82 | + |
| 83 | +WORKDIR /code/Frontend/ecommerce_inventory/ |
| 84 | +# # Install frontend dependencies |
| 85 | +RUN npm install |
| 86 | + |
| 87 | +# # Copy frontend source code |
| 88 | +# COPY ./Frontend/ecommerce_inventory ./ |
| 89 | + |
| 90 | +# # Build frontend (adjust this based on your React build process) |
| 91 | +RUN npm run build |
| 92 | + |
| 93 | +# Stage 2: Build Django backend |
| 94 | +FROM python:3.11.0 |
| 95 | + |
| 96 | +# Set environment variables |
| 97 | +ENV PYTHONDONTWRITEBYTECODE 1 |
| 98 | +ENV PYTHONUNBUFFERED 1 |
| 99 | +WORKDIR /code |
| 100 | + |
| 101 | +# Set working directory for backend |
| 102 | + |
| 103 | +# Copy backend requirements file |
| 104 | +# COPY ./Backend/EcommerceInventory/requirements.txt /code/ |
| 105 | + |
| 106 | +# Copy built frontend to Django static files directory |
| 107 | + |
| 108 | +# Copy Django project files |
| 109 | +COPY ./Backend/EcommerceInventory /code/Backend/EcommerceInventory/ |
| 110 | + |
| 111 | +RUN pip install -r ./Backend/EcommerceInventory/requirements.txt |
| 112 | +COPY --from=build-stage ./code/Frontend/ecommerce_inventory/build ./Backend/EcommerceInventory/static/ |
| 113 | +COPY --from=build-stage ./code/Frontend/ecommerce_inventory/build/static ./Backend/EcommerceInventory/static/ |
| 114 | +COPY --from=build-stage ./code/Frontend/ecommerce_inventory/build/index.html ./Backend/EcommerceInventory/EcommerceInventory/templates/index.html |
| 115 | + |
| 116 | +# Collect static files |
| 117 | +RUN python ./Backend/EcommerceInventory/manage.py migrate |
| 118 | +RUN python ./Backend/EcommerceInventory/manage.py collectstatic --no-input |
| 119 | + |
| 120 | +# Expose port 80 (adjust as necessary) |
| 121 | +EXPOSE 80 |
| 122 | +WORKDIR /code/Backend/EcommerceInventory |
| 123 | +# Command to run Django server |
| 124 | +CMD ["gunicorn", "EcommerceInventory.wsgi:application", "--bind", "0.0.0.0:8000"] |
| 125 | + |
| 126 | + |
| 127 | +-------------Create .github/workflows/deploy.yml file----------------------------- |
| 128 | +name: Deploy to AWS |
| 129 | + |
| 130 | +on: |
| 131 | + push: |
| 132 | + branches: |
| 133 | + - TEST |
| 134 | + |
| 135 | +jobs: |
| 136 | + build-and-deploy: |
| 137 | + runs-on: ubuntu-latest |
| 138 | + |
| 139 | + steps: |
| 140 | + - name: Checkout code |
| 141 | + uses: actions/checkout@v2 |
| 142 | + |
| 143 | + - name: Set up Docker Buildx |
| 144 | + uses: docker/setup-buildx-action@v1 |
| 145 | + |
| 146 | + - name: Log in to DockerHub |
| 147 | + uses: docker/login-action@v1 |
| 148 | + with: |
| 149 | + username: ${{ secrets.DOCKER_USERNAME }} |
| 150 | + password: ${{ secrets.DOCKER_PASSWORD }} |
| 151 | + |
| 152 | + - name: Build and push Docker images |
| 153 | + run: | |
| 154 | + docker build -t ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest . |
| 155 | + docker push ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest |
| 156 | + |
| 157 | + - name: Set up SSH |
| 158 | + run: | |
| 159 | + mkdir -p ~/.ssh |
| 160 | + echo "${{ secrets.AWS_EC2_KEY }}" > ~/.ssh/id_rsa |
| 161 | + chmod 600 ~/.ssh/id_rsa |
| 162 | + ssh-keyscan -H ${{ secrets.AWS_EC2_IP }} >> ~/.ssh/known_hosts |
| 163 | + |
| 164 | + # - name: Copy docker-compose.yml to EC2 |
| 165 | + # run: | |
| 166 | + # scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa docker-compose.yml ubuntu@${{ secrets.AWS_EC2_IP }}:/home/ubuntu/docker-compose.yml |
| 167 | + |
| 168 | + # - name: Copy Dockerfile to EC2 |
| 169 | + # run: | |
| 170 | + # scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa Dockerfile ubuntu@${{ secrets.AWS_EC2_IP }}:/home/ubuntu/Dockerfile |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + - name: Deploy to EC2 |
| 175 | + run: | |
| 176 | + ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ubuntu@${{ secrets.AWS_EC2_IP }} << 'EOF' |
| 177 | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} ${{ secrets.DOCKER_USERNAME }} |
| 178 | + docker pull ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest |
| 179 | + docker stop ecommerce-backend || true |
| 180 | + docker rm ecommerce-backend || true |
| 181 | + docker run -d --name ecommerce-backend -p 80:8000 ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest |
| 182 | + EOF |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | +-----------------Add Setting variables in Github--------------------------------- |
| 188 | +Repo -> Settings-> Security -> Secrets and variables -> Actions -> New Repository Secret |
| 189 | + |
| 190 | +DOCKER_USERNAME |
| 191 | +DOCKER_PASSWORD |
| 192 | +AWS_EC2_IP |
| 193 | +AWS_EC2_KEY -> key Pair Contents |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +--------------------Docker Commands ---------------------------------------- |
| 198 | + |
| 199 | +List all Running Container : |
| 200 | +docker ps |
| 201 | + |
| 202 | +List all Container: |
| 203 | +docker ps -a |
| 204 | + |
| 205 | +Enter Docker Image Shell |
| 206 | +docker run -it DOCKER_USENAME/REPO_NAME:latest /bin/bash |
| 207 | + |
| 208 | +Check Docker Logs |
| 209 | +docker logs CONTAINER_ID |
0 commit comments