Django项⽬部署详细步骤DjangoBlog部署教程
环境
系统是ubuntu 18.04 LTS(阿⾥云主机)
假设你的域名是www.jenny.fit
python虚拟环境⽬录在 root/python/env
djangoblog源码位置在 root/python/DjangoBlog
登⼊⽤户root
安装软件
apt install mysql-server -y
apt install python3-pip -y
apt install memcached -y #⾃由开源的,⾼性能,分布式内存对象缓存系统,减少数据库的访问次数
apt install supervisor -y#Python开发的⼀套通⽤的进程管理程序,能将⼀个普通的命令⾏进程变为后台
apt install nginx -y
apt-get install git
apt-get install python-dev default-libmysqlclient-dev #安装mysqlclient依赖
配置python环境
pip3 install virtualenv#python虚拟环境建⽴
pip3 install gunicorn#UNIX 下的 WSGI HTTP 服务器
mkdir -p python/env &&cd python/env
virtualenv -p /usr/bin/python3 djangoblog#创建虚拟环境
source djangoblog/bin/activate#进⼊虚拟环境
(djangoblog) root@jenny:~/python/env#执⾏完后的结果
cd /root/python/
git clone git@github:niezhe/DjangoBlog.git
cd DjangoBlog/
pip install -
配置mysql数据库
设置密码
#安装后默认是没有密码的
mysql -u root
use mysql;
update user set authentication_string=password("你的密码")where user='root';#(⽆password字段的版本,也就是版本<=5.7的) update user set password=password('你的密码')where user='root';#(有password字段的版本,版本>5.7的)
update user set plugin="mysql_native_password";
flush privileges;
如果出现报错:
#Your password does not satisfy the current policy requirements
解决办法:
1、查看 mysql 初始的密码策略,
#查看密码策略
SHOW VARIABLES LIKE 'validate_password%';
2 、需要设置密码的验证强度等级
set global validate_password_policy=LOW;
3、 设置密码长度
set global validate_password_length=4;
设置远程访问
SELECT User, Host FROM mysql.user;
update user set host ='%'where user='root';
python新建项目教程flush privileges;
vim /etc/f.d/mysqldf
#注释下⾯这⾏代码
bind-address =127.0.0.1
修改mysql默认字符集
vim /etc/mysql/conf.d/mysqlf
#删除该⽂件的[mysql]⾏并贴⼊如下内容
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
#保存后退出重启mysql
/etc/init.d/mysql restart
建⽴数据库
#登录mysql建⽴数据库
mysql -u root -p
CREATE DATABASE`djangoblog`/*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
修改djangoblog配置
修改DjangoBlog/settings.py中的DATABASES配置,如下所⽰,可以将其中的配置写⼊.bashrc(环境⽩变量)中#数据库配置⽂件
DATABASES ={
'default':{
'ENGINE':'django.sql',
'NAME': ('DJANGO_MYSQL_DATABASE')or'djangoblog',#数据库名字
'USER': ('DJANGO_MYSQL_USER')or'root',#数据库⽤户名
'PASSWORD': ('DJANGO_MYSQL_PASSWORD')or'root',#数据库密码
'HOST': ('DJANGO_MYSQL_HOST')or'127.0.0.1',#数据库连接地址
'PORT':int(
<('DJANGO_MYSQL_PORT')or3306),#数据库端⼝
'OPTIONS':{
'charset':'utf8mb4'},
}}
#配置写⼊.bashrc
vim ~/.bashrc
export DJANGO_MYSQL_PASSWORD=jenny#数据库密码
export DJANGO_MYSQL_PORT=3308#数据库端⼝
#后⾯会⽤到
export DJANGO_DEBUG=False#django DEBUG开关
source ~/.bashrc#使环境变量⽣效
开始执⾏数据库迁移
./manage.py makemigrations
./manage.py migrate
./manage.py createsuperuser #创建⽤户
#收集static⽬录下的静态⽂件到collectstatic ⽬录
./manage.py collectstatic --no-input
#压缩静态⽂件
./manage.py compress --force
启动django项⽬
.
/manage.py runserver
#出现如下表⽰启动成功,可以使⽤浏览器打开127.0.0.1:8000/ 来查看
System check identified no issues (0 silenced).
December 15, 2020 - 21:37:36
Django version 3.1.1, using settings 'DjangoBlog.settings'
Starting development server at 127.0.0.1:8000/
Quit the server with CTRL-BREAK.
gunicorn 配置
#打开⽂件,粘贴如下内容
vim root/python/gunicorn_start.sh
#!/bin/bash
NAME="DjangoBlog"
DJANGODIR=/root/python/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 /root/python/env/djangoblog/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 /root/python/env/djangoblog/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--log-level=debug \
--log-file=-
把DjangoBlog/settings.py中的DEBUG = True修改为DEBUG = False
DEBUG关闭后,不加载配⽹⽂件,通过nginx来加载配置⽂件
#增加执⾏权限
chmod 777 gunicorn_start.sh
./gunicorn_start.sh
#没有报错说明安装成功
nginx配置
# 删除默认配置
rm /etc/nginx/sites-enabled/default
vim /etc/nginx/f
#粘贴下⾯内容
server {
listen 80;
server_name www.jenny.fit;#修改为你⾃⼰的域名
root /root/python/DjangoBlog/;
access_log /var/log/nginx/django_access.log;
error_log /var/log/nginx/django_error.log;
location /static/ {
alias /root/python/DjangoBlog/collectedstatic/;
expires max;
access_log off;
log_not_found off;
}
location /media {
# 静态⽂件配置
alias /root/python/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 127.0.0.1:8000;
break;
}
}
}
#重启nginx
/etc/init.d/nginx restart
配置Supervisor
vim /etc/supervisor/conf.f
[program:djangoblog]
command= /root/python//gunicorn_start.sh
user = root
autostart=true
autorestart=true
redirect_stderr =true
stdout_logfile = /var/log/djangoblog.log
stderr_logfile=/var/
supervisorctl update
supervisorctl reload
#备注Supervisor的常⽤命令
supervisorctl status //查看所有进程的状态
supervisorctl stop all //停⽌所有任务
supervisorctl start all //启动所有任务
supervisorctl restart //重启所有任务
supervisorctl update //配置⽂件修改后使⽤该命令加载新的配置
supervisorctl reload //重新启动配置中的所有程序
启动项⽬
/etc/init.d/memcached restart
/etc/init.d/nginx restart
注意事项
1. 项⽬启动后没有css和js
修改nginx权限,赋予root权限
user root;#这个修改为root权限
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
2. nginx修改为https
在nginx⽬录下新建cert放证书⽂件,然后修改配置如下etc/nginx/sites-enabled下配置如下:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论