Skip to content

Latest commit

 

History

History
198 lines (160 loc) · 4.43 KB

File metadata and controls

198 lines (160 loc) · 4.43 KB

Docker Work

Dockerfile

# The Dockerfile acts as a blueprint that tells Docker how to build the image,
# including setting up the base environment, installing dependencies, and
# defining the command to start your application.

# Use an official Python runtime as the base image
FROM python:3.13.2-slim

# Prevent Python from writing pyc files and buffer output
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set the working directory in the container to /app
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt /app/

# Install the Python dependencies
RUN pip install -r requirements.txt

# Copy the Django project code into the container
COPY . /app

# Collect static files for the Django application
RUN python manage.py collectstatic --noinput

# Expose port 8000 for the Django development server
EXPOSE 8000

# Run the Django development server
CMD ["gunicorn", "student_management.wsgi:application", "--bind", "0.0.0.0:8000"]

Docker Compose

docker-compose.yml

version: '3.9'  # Docker Compose version

services:
  web:  # Name of the service (like a container)
    build: .  # Build the Docker image using the Dockerfile in the current directory
    ports:
      - "8000:8000"  # Map port 8000 on your VPS to port 8000 inside the container
    volumes:
      - .:/app  # Sync current folder (host) with /app folder inside the container (for live code changes)
    command: python manage.py runserver 0.0.0.0:8000  # Command to run the Django server inside the container

Build The Image

This reads your Dockerfile and creates a Docker image.

docker-compose build

Run the container

The -d flag runs containers in the background (detached mode).

docker-compose up -d

Create Digital Ocean Droplet / Or Other VPS

If VPS IP Address is 134.209.159.172, Go to terminal and type

ssh root@134.209.159.172

Hosting On VPS

Login Via SSH

ssh root@youripaddress

Update System

sudo apt update && sudo apt upgrade -y

Install Docker and Docker Compose

sudo apt install docker.io -y
sudo systemctl enable --now docker
sudo apt install docker-compose -y

Verify Installation

docker --version
docker-compose --version

Go to Directory & Clone the Repo

cd /var/www
git clone https://github.com/yourusername/yourproject.git
cd yourproject

Navigate to the Project Directory

cd yourproject

Build the Docker Image

docker-compose build

Run the Container

docker-compose up -d

Nginx

sudo apt install nginx -y

Add Projects To Site Available

nano /etc/nginx/sites-available/mydjango

Paste It

server {
    listen 80;
    server_name hindivoicetyping.com www.hindivoicetyping.com;

    location /static/ {
        alias /var/www/DjangoStudentManagement/staticfiles/;
    }

    location /media/ {
        alias /var/www/DjangoStudentManagement/media/;
    }

    location / {
        proxy_pass http://127.0.0.1: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;
    }
}

To Save Press Ctrl + X, Enter

Enable Configuration

sudo ln -s /etc/nginx/sites-available/mydjango /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Access the Website

http://your_vps_ip:8000

For Security

SECURITY WARNING: don't run with debug turned on in production!

DEBUG = False

In settings.py update the following:

DEBUG = False

ALLOWED_HOSTS = ['localhost', '127.0.0.1','134.209.159.172','hindivoicetyping.com']

# Static files settings
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

# Media files (for uploaded images)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

For Https

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d hindivoicetyping.com -d www.hindivoicetyping.com

Reload

sudo nginx -t
sudo systemctl reload nginx

For Admin Login

CSRF_TRUSTED_ORIGINS = ['https://hindivoicetyping.com']