fix: 支持从独立环境变量配置 PostgreSQL 数据库
- 添加 DB_ENGINE, DB_NAME, DB_USER 等环境变量 - 支持 PostgreSQL 和 SQLite 切换 - 云服务器默认使用 PostgreSQL
This commit is contained in:
9
TOOLS.md
9
TOOLS.md
@@ -43,13 +43,20 @@ Add whatever helps you do your job. This is your cheat sheet.
|
|||||||
|
|
||||||
## Git 配置
|
## Git 配置
|
||||||
|
|
||||||
### 城市手册项目
|
### 城市手册项目 - 内网仓库
|
||||||
|
|
||||||
- **仓库**: http://10.2.0.100:8989/mashen/chengshishouce.git
|
- **仓库**: http://10.2.0.100:8989/mashen/chengshishouce.git
|
||||||
- **用户名**: mashen
|
- **用户名**: mashen
|
||||||
- **密码**: 825670@MashenClaw
|
- **密码**: 825670@MashenClaw
|
||||||
- **邮箱**: mashen@datalibstar.com
|
- **邮箱**: mashen@datalibstar.com
|
||||||
|
|
||||||
|
### 城市手册项目 - 外网仓库
|
||||||
|
|
||||||
|
- **仓库**: https://xjp.datalibstar.com/mashen/chengshouse.git
|
||||||
|
- **用户名**: mashen
|
||||||
|
- **密码**: 825670@MashenClaw
|
||||||
|
- **邮箱**: mashen@datalibstar.com
|
||||||
|
|
||||||
## PostgreSQL 数据库
|
## PostgreSQL 数据库
|
||||||
|
|
||||||
### 城市手册项目
|
### 城市手册项目
|
||||||
|
|||||||
@@ -86,17 +86,18 @@ WSGI_APPLICATION = 'city_manual.wsgi.application'
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
DATABASE_URL = os.environ.get('DATABASE_URL', '')
|
# 数据库配置 - 支持 PostgreSQL 和 SQLite
|
||||||
|
DB_ENGINE = os.environ.get('DB_ENGINE', '')
|
||||||
|
|
||||||
if DATABASE_URL.startswith('postgres'):
|
if DB_ENGINE == 'django.db.backends.postgresql':
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.postgresql',
|
'ENGINE': 'django.db.backends.postgresql',
|
||||||
'NAME': DATABASE_URL.split('/')[-1].split('?')[0],
|
'NAME': os.environ.get('DB_NAME', 'cssc'),
|
||||||
'USER': DATABASE_URL.split('@')[0].split('/')[-1].split(':')[0],
|
'USER': os.environ.get('DB_USER', 'coder'),
|
||||||
'PASSWORD': DATABASE_URL.split('@')[0].split(':')[-1],
|
'PASSWORD': os.environ.get('DB_PASSWORD', ''),
|
||||||
'HOST': DATABASE_URL.split('@')[1].split(':')[0],
|
'HOST': os.environ.get('DB_HOST', 'localhost'),
|
||||||
'PORT': DATABASE_URL.split('@')[1].split(':')[-1].split('?')[0],
|
'PORT': os.environ.get('DB_PORT', '5432'),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ python manage.py migrate --noinput
|
|||||||
python manage.py collectstatic --noinput
|
python manage.py collectstatic --noinput
|
||||||
|
|
||||||
echo "⚙️ 创建 Gunicorn 服务..."
|
echo "⚙️ 创建 Gunicorn 服务..."
|
||||||
sudo cat > /etc/systemd/system/city-manual.service << 'EOF'
|
sudo bash -c 'cat > /etc/systemd/system/city-manual.service << EOF
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=City Manual Gunicorn Service
|
Description=City Manual Gunicorn Service
|
||||||
After=network.target
|
After=network.target
|
||||||
@@ -86,23 +86,19 @@ After=network.target
|
|||||||
User=ubuntu
|
User=ubuntu
|
||||||
Group=ubuntu
|
Group=ubuntu
|
||||||
WorkingDirectory=/home/ubuntu/city-manual/backend
|
WorkingDirectory=/home/ubuntu/city-manual/backend
|
||||||
ExecStart=/home/ubuntu/city-manual/backend/venv/bin/gunicorn \
|
ExecStart=/home/ubuntu/city-manual/backend/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/city-manual.sock city_manual.wsgi:application
|
||||||
--access-logfile - \
|
|
||||||
--workers 3 \
|
|
||||||
--bind unix:/run/city-manual.sock \
|
|
||||||
city_manual.wsgi:application
|
|
||||||
Restart=always
|
Restart=always
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
EOF
|
EOF'
|
||||||
|
|
||||||
sudo systemctl daemon-reload
|
sudo systemctl daemon-reload
|
||||||
sudo systemctl enable city-manual
|
sudo systemctl enable city-manual
|
||||||
sudo systemctl start city-manual
|
sudo systemctl start city-manual
|
||||||
|
|
||||||
echo "🌐 配置 Nginx..."
|
echo "🌐 配置 Nginx..."
|
||||||
sudo cat > /etc/nginx/sites-available/city-manual << 'EOF'
|
sudo bash -c 'cat > /etc/nginx/sites-available/city-manual << "EOF"
|
||||||
server {
|
server {
|
||||||
listen 80;
|
listen 80;
|
||||||
server_name cssc.datalibstar.com;
|
server_name cssc.datalibstar.com;
|
||||||
@@ -124,15 +120,15 @@ server {
|
|||||||
location / {
|
location / {
|
||||||
include proxy_params;
|
include proxy_params;
|
||||||
proxy_pass http://unix:/run/city-manual.sock;
|
proxy_pass http://unix:/run/city-manual.sock;
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host \$host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP \$remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||||
}
|
}
|
||||||
|
|
||||||
client_max_body_size 10M;
|
client_max_body_size 10M;
|
||||||
}
|
}
|
||||||
EOF
|
EOF'
|
||||||
|
|
||||||
sudo ln -sf /etc/nginx/sites-available/city-manual /etc/nginx/sites-enabled/
|
sudo ln -sf /etc/nginx/sites-available/city-manual /etc/nginx/sites-enabled/
|
||||||
sudo nginx -t
|
sudo nginx -t
|
||||||
|
|||||||
Reference in New Issue
Block a user