基于DjangoBlog框架的博客系统部署
本地部署验证
下载DjangoBlog框架
项目放在/srv路径下
$ cd /srv
$ git clone https://github.com/liangliangyy/DjangoBlog.git
配置Python虚拟环境
虚拟环境放在项目根目录下
$ cd DjangoBlog
$ pytyhon -m venv djangodev
安装项目依赖
激活虚拟环境,安装依赖
$ . djangodev/bin/activate
$ pip install -Ur requirements.txt
注意:如果mysqlclient安装失败,可以使用pymysql替代,后面说明相关改动点
配置数据库
使用MariaDB 10.5.8 Server,登录root用户,并创建新用户
$ mysql -uroot -p
$ create user 'django'@'localhost' identified by 'yourpassword';
$ GRANT ALL PRIVILEGES ON *.* TO 'django'@'localhost' WITH GRANT OPTION;
$ flush privileges;
$ quit
$ mysql -udjango -p
$ CREATE DATABASE `djangoblog` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
$ quit
修改项目配置settings.py
‘NAME’:数据库名称
‘USER’、'PASSWORD’分别为用户名、密码,与上一步数据库保持一致即可。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ.get('DJANGO_MYSQL_DATABASE') or 'djangoblog',
'USER': os.environ.get('DJANGO_MYSQL_USER') or 'django',
'PASSWORD': os.environ.get('DJANGO_MYSQL_PASSWORD') or 'yourpassword',
'HOST': os.environ.get('DJANGO_MYSQL_HOST') or '127.0.0.1',
'PORT': int(
os.environ.get('DJANGO_MYSQL_PORT') or 3306),
'OPTIONS': {
'charset': 'utf8mb4'},
}}
运行本地服务
$ python manage.py makemigrations
$ python manage.py migrate # 数据库迁移
$ python manage.py createsuperuser # 创建超级用户,即后台管理账号
$ python manage.py create_testdata # 创建测试数据,博客、标签等数据
$ python manage.py collectstatic --noinput # 收集静态文件
$ python manage.py compress --force
$ python manage.py runserver # 运行服务
浏览器打开: http://127.0.0.1:8000/ 即可显示博客首页。
服务器部署验证
gunicorn配置
Python虚拟环境安装gunicorn
$ cd DjangoBlog
$ . djangodev/bin/activate
$ pip install gunicorn
$ vim gunicorn_start.sh
配置如下内容:
#!/bin/bash
NAME="DjangoBlog"
DJANGODIR=/srv/DjangoBlog #Django project directory
USER=root # the user to run as
GROUP=root # the group to run as
NUM_WORKERS=1 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=DjangoBlog.settings # which settings file should Django use
DJANGO_WSGI_MODULE=DjangoBlog.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source $DJANGODIR/djangodev/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec $DJANGODIR/djangodev/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--log-level=debug \
--log-file=-
注意:项目目录和用户改为自己的目录、用户
脚本增加可执行权限:
$ chmod +x gunicorn_start.sh
$ ./gunicorn_start.sh
Ctrl + C终止运行
Nginx配置
编辑Nginx配置文件:
$ vim /etc/nginx/conf.d/default.conf
编辑内容如下:
server {
listen 80;
server_name www.xxx.top;
root /srv/DjangoBlog;
access_log /var/log/nginx/django_access.log;
error_log /var/log/nginx/django_error.log;
location /static/ {
alias /srv/DjangoBlog/collectedstatic/;
expires max;
access_log off;
log_not_found off;
}
location /media {
# 静态文件配置
alias /srv/DjangoBlog/uploads/;
expires max;
}
location ~ \.py$ {
return 403;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:8000;
break;
}
}
}
注意:server_name 改成自己的域名或者服务器IP地址
保存文件、退出,重启Nginx
$ nginx -s reload
Supervisor配置
创建项目配置文件
$ vim /etc/supervisor/conf.d/djangoblog.ini
编辑内容:
[program:djangoblog]
command = /srv/DjangoBlog/gunicorn_start.sh
user = root
autostart=true
autorestart=true
redirect_stderr = true
stdout_logfile = /var/log/djangoblog.log
stderr_logfile=/var/log/djangoblog.err
修改主配置文件:
$ vim /etc/supervisor/supervisord.conf
文件尾部如下:
[include]
files = ./conf.d/*.ini
启动supervisor
$ supervisord -c /etc/supervisor/supervisord.conf
查看服务状态
$ supervisorctl -c /etc/supervisor/supervisord.conf status
以上为整个部署过程,接下来通过浏览器访问www.xxx.com即可查看博客页面。
问题解决记录
- No module named ‘mistune’
下载 mistune.py文件,并放在项目根目录
https://github.com/dellzhui/DjangoBlog/blob/master/mistune.py - mysqlclient安装失败
用pymysql 替代mysqlclient,Mariadb替代MySQL
# 在 Django 站点的 __init__.py 文件中添加如下两行:
import pymysql
pymysql.install_as_MySQLdb()
- pickle错误:ValueError: unsupported pickle protocol: 5
需要Python3.8以上支持,本人安装Python3.9解决 - ssl错误:No module named ‘_ssl’
修改Setup文件
$ vim ~/soft/Python-3.9.0/Modules/Setup
取消注释下列4行,路径中ssl改为openssl
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/openssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
重新配置、编译、安装Python3.9
$ cd ~/soft/Python-3.9.0
$ ./configure
$ make && make install
- supervisor运行错误
$ ps -ef | grep supervisor
$ kill -9 <pid>
- 日志记录页面报错
错误提示:Database returned an invalid datetime value. Are time zone definitions for your database installed?
解决方法:
修改django project下的settings.py中时区配置信息:
# USE_TZ = True
# TIME_ZONE = 'UTC'
USE_TZ = False
TIME_ZONE = 'Asia/Shanghai'