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依赖
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 [email protected]:niezhe/DjangoBlog.git
cd DjangoBlog/
pip install -r requirements.txt
#安装后默认是没有密码的
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';
flush privileges;
vim /etc/mysql/mysql.conf.d/mysqld.cnf
#注释下面这行代码
bind-address = 127.0.0.1
vim /etc/mysql/conf.d/mysql.cnf
#删除该文件的[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 */;
#数据库配置文件
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ.get('DJANGO_MYSQL_DATABASE') or 'djangoblog',#数据库名字
'USER': os.environ.get('DJANGO_MYSQL_USER') or 'root',#数据库用户名
'PASSWORD': os.environ.get('DJANGO_MYSQL_PASSWORD') or 'root',#数据库密码
'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'},
}}
#配置写入.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
./manage.py runserver
#出现如下表示启动成功,可以使用浏览器打开http://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 http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
#打开文件,粘贴如下内容
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
#没有报错说明安装成功
# 删除默认配置
rm /etc/nginx/sites-enabled/default
vim /etc/nginx/sites-enabled/djangoblog.com.conf
#粘贴下面内容
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 http://127.0.0.1:8000;
break;
}
}
}
#重启nginx
/etc/init.d/nginx restart
vim /etc/supervisor/conf.d/djangoblog.conf
[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/log/djangoblog.err
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
至此,全部完成。你可以通过你的浏览器访问http://www.jenny.fit
user root;#这个修改为root权限
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
server {
listen 443;
server_name www.jenny.fit;
#新增ssl
ssl on;
ssl_certificate /etc/nginx/cert/4921042_jenny.fit.pem;
ssl_certificate_key /etc/nginx/cert/4921042_jenny.fit.key;
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 http://127.0.0.1:8000;
break;
}
}
}
server {
listen 80;
server_name www.jenny.fit; #你的域名
rewrite ^(.*)$ https://$host$1 permanent; #把http的域名请求转成https
}