Missing something?

Server-Side

A comprehensive cheat sheet for managing server-side deployments using Localweb, covering essential commands, configurations, and monitoring techniques. Includes Nginx and Systemd configurations.

Localweb Basics

Starting the Application

Starting the Uvicorn server with uv:

uv run uvicorn app.main:app --host 0.0.0.0 --port 8001

This command starts the server on port 8001, accessible from any IP address.

Enabling auto-reload:

uv run uvicorn app.main:app --host 0.0.0.0 --port 8001 --reload

The --reload flag automatically restarts the server on code changes.

Setting the log level:

uvicorn app.main:app --host 0.0.0.0 --port 8001 --reload --log-level info

Adjust the --log-level to control the verbosity of the logs (e.g., info, debug, error).

Stopping the Application

Gracefully stopping the server:

kill -15 <pid>

Sends a SIGTERM signal, allowing the server to shut down gracefully. Replace <pid> with the actual process ID.

Avoid using kill -9 <pid>:

This sends a SIGKILL signal, which immediately terminates the process without allowing it to clean up resources or finish ongoing tasks.

Accessing Documentation

Swagger UI:

https://staging.api.git-learn.com.br/docs

Interactive API documentation.

ReDoc:

https://staging.api.git-learn.com.br/redoc

Alternative API documentation viewer.

Nginx Configuration

Configuration File

Editing the Nginx configuration file:

sudo nano /etc/nginx/sites-available/gitcourse-api.conf

This opens the configuration file for the gitcourse-api site.

Example Configuration:

server {
    listen 80;
    server_name staging.api.git-learn.com.br;

    location / {
        proxy_pass http://localhost:8001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /docs {
        proxy_pass http://localhost:8001/docs;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location /redoc {
        proxy_pass http://localhost:8001/redoc;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Applying Changes

Testing the configuration:

sudo nginx -t

Checks the Nginx configuration file for syntax errors.

Reloading Nginx:

sudo systemctl reload nginx

Applies the changes without interrupting existing connections.

Systemd Service

Service File

Creating the Systemd service file:

sudo nano /etc/systemd/system/gitcourse-backend.service

This creates a service file to manage the backend application.

Example Service File Configuration:

[Unit]
Description=GitCourse Backend Service
After=network.target

[Service]
User=deploy
WorkingDirectory=/home/deploy/git-course-backend-staging
ExecStart=/home/deploy/.local/bin/uv run uvicorn app.main:app --host 0.0.0.0 --port 8001
Restart=on-failure

[Install]
WantedBy=multi-user.target

Managing the Service

Reloading Systemd daemon:

sudo systemctl daemon-reload

This reloads the systemd manager configuration.

Starting the service:

sudo systemctl start gitcourse-backend

Starts the gitcourse-backend service.

Restarting the service:

sudo systemctl restart gitcourse-backend

Restarts the gitcourse-backend service.

Checking the service status:

sudo systemctl status gitcourse-backend

Displays the current status and logs of the gitcourse-backend service.

Bash Aliases

Setting up Aliases

Defining the alias:

alias stage-backend='cd /home/deploy/git-course-backend-staging'

This creates an alias named stage-backend to quickly navigate to the staging directory.

To make it permanent, add this line to your ~/.bashrc or ~/.zshrc file.

Applying the alias:

source ~/.bashrc or source ~/.zshrc

Reloads the shell configuration to make the alias available in the current session.

Using the Alias

Navigating to the directory:

stage-backend

This command will change the current directory to /home/deploy/git-course-backend-staging.