TandoorRecipes项目手动安装与配置指南
前言
TandoorRecipes是一个基于Django框架开发的现代化食谱管理系统,它提供了完整的食谱管理、食材跟踪和菜单规划功能。本文将详细介绍如何在Linux系统上手动安装和配置TandoorRecipes项目,包括后端服务、数据库和前端构建的完整流程。
系统要求
在开始安装前,请确保您的系统满足以下最低要求:
- 操作系统:Ubuntu/Debian或兼容的Linux发行版
- Python版本:3.10或更高(推荐3.12)
- 内存:至少2GB(前端构建过程需要较多内存)
- 磁盘空间:建议预留至少1GB可用空间
准备工作
1. 创建专用用户
为安全考虑,我们首先创建一个专用用户来运行应用:
sudo useradd recipes
2. 系统更新与基础软件安装
更新系统并安装必要的软件包:
sudo apt update && sudo apt upgrade -y
sudo apt install -y git curl python3 python3-pip python3-venv nginx
3. 获取项目代码
将项目代码克隆到系统目录:
git clone https://github.com/vabene1111/recipes.git -b master
sudo mv recipes /var/www
cd /var/www/recipes
sudo chown -R recipes:www-data /var/www/recipes
4. 创建Python虚拟环境
python3 -m venv /var/www/recipes
source /var/www/recipes/bin/activate
依赖安装
1. Node.js和Yarn安装
前端构建需要Node.js环境:
# 对于Ubuntu系统
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
sudo npm install --global yarn
2. 数据库支持包
安装PostgreSQL开发库:
sudo apt install -y libpq-dev postgresql
3. LDAP支持(可选)
如需LDAP集成,安装以下包:
sudo apt install -y libsasl2-dev python3-dev libldap2-dev libssl-dev
4. Python依赖安装
/var/www/recipes/bin/pip3 install -r requirements.txt
5. 前端构建
cd ./vue
yarn install
yarn build
数据库配置
1. PostgreSQL设置
sudo -u postgres psql
在PostgreSQL控制台中执行:
CREATE DATABASE djangodb;
CREATE USER djangouser WITH PASSWORD 'your_secure_password';
GRANT ALL PRIVILEGES ON DATABASE djangodb TO djangouser;
ALTER DATABASE djangodb OWNER TO djangouser;
-- 优化设置
ALTER ROLE djangouser SET client_encoding TO 'utf8';
ALTER ROLE djangouser SET default_transaction_isolation TO 'read committed';
ALTER ROLE djangouser SET timezone TO 'UTC';
-- 临时授予超级用户权限(后续会移除)
ALTER USER djangouser WITH SUPERUSER;
2. 环境变量配置
下载模板配置文件:
wget https://raw.githubusercontent.com/vabene1111/recipes/develop/.env.template -O /var/www/recipes/.env
编辑.env
文件,重点关注以下配置项:
SECRET_KEY
:使用base64 /dev/urandom | head -c50
生成安全密钥POSTGRES_HOST
:通常为127.0.0.1POSTGRES_PASSWORD
:与数据库设置一致STATIC_URL
和MEDIA_URL
:指向/var/www/recipes
下的相应目录
应用初始化
1. 加载环境变量并迁移数据库
export $(cat /var/www/recipes/.env |grep "^[^#]" | xargs)
/var/www/recipes/bin/python3 manage.py migrate
2. 移除数据库超级用户权限
sudo -u postgres psql
ALTER USER djangouser WITH NOSUPERUSER;
exit
3. 收集静态文件
/var/www/recipes/bin/python3 manage.py collectstatic --no-input
/var/www/recipes/bin/python3 manage.py collectstatic_js_reverse
服务配置
1. Gunicorn服务
创建服务文件/etc/systemd/system/gunicorn_recipes.service
:
[Unit]
Description=gunicorn daemon for recipes
After=network.target
[Service]
Type=simple
Restart=always
RestartSec=3
User=recipes
Group=www-data
WorkingDirectory=/var/www/recipes
EnvironmentFile=/var/www/recipes/.env
ExecStart=/var/www/recipes/bin/gunicorn --error-logfile /tmp/gunicorn_err.log --log-level debug --capture-output --bind unix:/var/www/recipes/recipes.sock recipes.wsgi:application
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl enable --now gunicorn_recipes
sudo systemctl status gunicorn_recipes # 验证服务状态
2. Nginx配置
创建配置文件/etc/nginx/conf.d/recipes.conf
:
server {
listen 8002;
location /static/ {
alias /var/www/recipes/staticfiles;
}
location /media/ {
alias /var/www/recipes/mediafiles;
}
location / {
proxy_set_header Host $http_host;
proxy_pass http://unix:/var/www/recipes/recipes.sock;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
重新加载Nginx配置:
sudo systemctl reload nginx
更新流程
当需要更新应用时,执行以下步骤:
cd /var/www/recipes
git pull
export $(cat /var/www/recipes/.env |grep "^[^#]" | xargs)
bin/pip3 install -r requirements.txt
bin/python3 manage.py migrate
bin/python3 manage.py collectstatic --no-input
bin/python3 manage.py collectstatic_js_reverse
cd vue
yarn install
yarn build
sudo systemctl restart gunicorn_recipes
常见问题解决
-
前端构建内存不足:
- 确保系统有足够内存(至少2GB)
- 可尝试设置Node.js内存限制:
export NODE_OPTIONS=--max_old_space_size=4096
-
数据库连接问题:
- 检查
.env
中的数据库配置 - 验证PostgreSQL服务是否运行
- 检查
-
权限问题:
- 确保
/var/www/recipes
目录权限正确 - 检查Gunicorn服务使用的用户权限
- 确保
通过以上步骤,您应该已经成功安装并配置了TandoorRecipes项目。现在可以通过浏览器访问服务器IP和配置的端口(如8002)来使用您的食谱管理系统了。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考