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:
FastAPI application with Uvicorn
PostgreSQL database
Redis for caching
Install all dependencies and run database migration:
$ make install
Setup nginx:
$ sudo apt install nginx
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;
}
}
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:
Install PostgreSQL and Redis:
sudo apt update sudo apt install postgresql redis-server
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;
Set up Python environment:
python -m venv venv source venv/bin/activate pip install -r requirements.txt
Configure environment variables:
cp .example.env .env # Edit .env with your configuration
Run migrations:
make migrate
Start the application with Uvicorn:
make run
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.