Deployment

This guide covers different deployment options for the UContact REST API Service.

Docker Compose Deployment

The application can be deployed using Docker Compose, which sets up all required services:

  1. FastAPI application with Uvicorn

  2. PostgreSQL database

  3. Redis for caching

  1. Install all dependencies and run database migration:

$ make install

  1. Setup nginx:

$ sudo apt install nginx

  1. Create nginx config:

Example of nginx configuration file:

server {
    listen 80;
    server_name your_domain.com;

    location / {
        include proxy_params;
        proxy_pass http://localhost:3000;
    }
}
  1. Links sites and restart Nginx

$ sudo cp ucontact.conf /etc/nginx/sites-available/ucontact $ sudo ln -s /etc/nginx/sites-available/ucontact /etc/nginx/sites-enabled/ $ sudo nginx -t $ sudo systemctl restart nginx

Congrats! Now you can use your application.

You can also modify compose.yaml and Dockerfile to you needs:

Docker Compose Configuration

Use a compose.yaml file:

 1services:
 2  app:
 3    build: .
 4    ports:
 5      - "127.0.0.1:3000:8000"
 6    env_file:
 7      - .env
 8    depends_on:
 9      - db
10      - redis
11
12  db:
13    image:  postgres
14    env_file:
15      - .env
16  redis:
17    image: redis:8.0-rc1
18    ports:
19      - "6379:6379"
20  

Dockerfile

Example of Dockerfile:

1FROM python:3.10-slim
2ENV APP_HOME /app
3WORKDIR $APP_HOME
4COPY . .
5RUN pip install -r requirements.txt
6EXPOSE 3000
7ENTRYPOINT ["python", "main.py"]

Deployment Commands

The application includes several Makefile commands for easy deployment:

Database Management

# Create new database and run migrations
make newdb:
    docker-compose exec db createdb -U postgres contacts_db
    make updb

# Update database with latest migrations
make updb:
    alembic upgrade head

# Create new migration
make migration message="migration message":
    alembic revision --autogenerate -m "$(message)"

Service Management

# Start all services
make up:
    poetry export --without-hashes -f requirements.txt --output requirements.txt
    docker-compose up -d

# Start Redis instance only
make upredis:
    docker run --name redis-hw012 -p 6379:6379 -d redis:8.0-rc1

# Run migrations in Docker
make migr:
    @img=$$(docker ps -aqf "name=goit-pythonweb-hw-012_app") && \
    docker exec -it $$img sh -c "alembic upgrade head"

Manual Deployment

For manual deployment without Docker:

  1. Install PostgreSQL and Redis:

    sudo apt update
    sudo apt install postgresql redis-server
    
  2. Create database and user:

    sudo -u postgres psql
    CREATE DATABASE contacts_db;
    CREATE USER myuser WITH PASSWORD 'mypassword';
    GRANT ALL PRIVILEGES ON DATABASE contacts_db TO myuser;
    
  3. Set up Python environment:

    python -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
    
  4. Configure environment variables:

    cp .example.env .env
    # Edit .env with your configuration
    
  5. Run migrations:

    make migrate
    
  6. Start the application with Uvicorn:

    make run
    
  7. Configure Nginx as reverse proxy:

    sudo apt install nginx
    sudo cp ucontact.conf /etc/nginx/sites-available/ucontact
    sudo ln -s /etc/nginx/sites-available/ucontact /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl restart nginx
    

Congrats! Now you can use your application.