Skip to content

Deployment

Manato Kato edited this page Dec 17, 2021 · 2 revisions

setup

nginx

sudo apt install nginx

/etc/nginx/sites-available/barifac

server {
    listen 443 ssl;
    server_name api.barifac.tk;

    ssl_certificate_key     /etc/ssl/private/private.key;       # Key
    ssl_certificate         /etc/ssl/certificate.crt;     # Cert
    ssl_protocols           TLSv1.1 TLSv1.2 TLSv1.3;    # TLS1.1以上
    ssl_ciphers             HIGH:!aNULL:!MD5;           # 暗号化方式。HIGH=128bit長以上とし、MD5は不許可
    ssl_prefer_server_ciphers   on;                     # サーバの設定を優先

    proxy_set_header    Host    $host;
    proxy_set_header    X-Forwarded-Host    $host;
    proxy_set_header    X-Forwarded-Proto   $scheme;
    proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
    proxy_set_header    X-Real-IP           $remote_addr;

    location / {
        proxy_pass http://127.0.0.1:8000/;
    }
}

シンボリックリンクを貼り、nginxを再起動

sudo ln -s /etc/nginx/sites-available/barifac /etc/nginx/sites-enabled/
sudo systemctl restart nginx

gunicorn

poetry add gunicorn

/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/barifac/fastapi
ExecStart=/home/ubuntu/barifac/fastapi/.venv/bin/gunicorn app.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000

[Install]
WantedBy=multi-user.target

gunicorn起動

sudo systemctl start gunicorn.service
sudo systemctl enable gunicorn

migrate

export PYTHONPATH=/home/ubuntu/barifac/fastapi
cd fastapi/app/db/
poetry run alembic upgrade head
Clone this wiki locally