ubuntu部署python后端fastapi

ubuntu云服务器初始化环境

  1. update、upgrade
apt update
apt upgrade -y
apt autoremove -y
  1. Install pipx
apt install -y python-is-python3
apt install -y pipx
  1. Add user
adduser ubuntu
  1. Conf sudo
chmod +w /etc/sudoers
echo 'ubuntu  ALL=(ALL:ALL) NOPASSWD: ALL' >> /etc/sudoers
chmod -w /etc/sudoers
  1. Switch current user
su ubuntu
  1. Change pip mirror
cd
mkdir archives
cd archives
git clone https://github.com/waketzheng/carstino
cd carstino
python pip_conf.py --pip
  1. Use oh-my-zsh
sudo apt install -y zsh
./ohmyzsh
  1. Install uv
pipx install uv
pipx ensurepath
source ~/.zshrc
uv --version
  1. Set uv mirror
python ~/archives/carstino/pip_conf.py --uv
  1. Prepare tools
uv tool install --with pdm-plugin-i pdm
uv tool install rust-just
uv tool install ruff
uv tool install ty
uv tool install fast-dev-cli
uv tool install bumpversion2
  1. Set pdm mirror
pdm config check_update false
python ~/archives/carstino/pip_conf.py --pdm
  1. Install supervisor
sudo apt install supervisor
  1. Clone code
cd
mkdir coding
cd coding
git clone yyy/xxx
cd yyyy
  1. Create virtual environment and install deps
pdm venv create --with uv --with-pip
uv sync --all-extras --all-groups
  1. Deployment
mkdir deployment
echo '
PORT = 8080
bind = f"0.0.0.0:{PORT}"
workers = 2
worker_class = "uvicorn.workers.UvicornWorker"
' > deployment/gunicorn_config.py
mkdir logs
echo logs/ >> .gitignore
echo '
[program:xxx]
command=uv run --no-config --no-cache --no-sync gunicorn app.main:app -c deployment/gunicorn_config.py
directory=/home/ubuntu/coding/xxx
stdout_logfile=/home/ubuntu/coding/xxx/logs/supervisor.log

numprocs=1
autostart=true
startsecs=10
autorestart=true
startretried=3
redirect_stderr=true
stdout_logfile_maxbytes=20MB
stdout_logfile_backups=20
' > deployment/supervisor.conf
cd /etc/supervisor/conf.d
sudo chown -R $USER:$USER ..
ln -s /home/ubuntu/coding/xxx/deployment/supervisor.conf xxx.conf
sudo supervisorctl update
sudo supervisorctl status
posted @ 2026-06-12 12:02  waketzheng  阅读(9)  评论(0)    收藏  举报