- 上线前检查项目还存在的问题:python manage.py check --deploy
配置文件(settings.py):
#所有关于debug的都注释掉
DEBUG = False
# 允许所有主机访问
ALLOWED_HOSTS = ['*']
#指定静态文件在服务器位置
STATIC_ROOT = '/root/project/stat/'
# HTTP和HTTPS相关配置
# # 保持HTTPS连接的时间
# SECURE_HSTS_SECONDS = 3600
# SECURE_HSTS_INCLUDE_SUBDOMAINS = True
# SECURE_HSTS_PRELOAD = True
#
# # 自动重定向到安全连接
# SECURE_SSL_REDIRECT = True
#
# # 避免浏览器自作聪明推断内容类型(避免跨站脚本攻击风险)
# SECURE_CONTENT_TYPE_NOSNIFF = True
#
# # 避免跨站脚本攻击
# SECURE_BROWSER_XSS_FILTER = True
#
# # COOKIE只能通过HTTPS进行传输
# SESSION_COOKIE_SECURE = True
# CSRF_COOKIE_SECURE = True
#
# # 防止点击劫持攻击手段 - 修改HTTP协议响应头
# # 当前网站是不允许使用<iframe>标签进行加载的
# X_FRAME_OPTIONS = 'DENY'
- 上线(打开阿里云服务器)
- 创建项目文件夹(配置文件、代码文件、日志文件、静态资源文件)
[root@izuf6hgznqeh3d0hgxzyrqz ~]# mkdir project_school
[root@izuf6hgznqeh3d0hgxzyrqz ~]# mkdir -p project_school/conf
[root@izuf6hgznqeh3d0hgxzyrqz ~]# mkdir -p project_school/code
[root@izuf6hgznqeh3d0hgxzyrqz ~]# mkdir -p project_school/logs
[root@izuf6hgznqeh3d0hgxzyrqz ~]# mkdir -p project_school/stat
[root@izuf6hgznqeh3d0hgxzyrqz project_school]# ls
code conf logs stat
- 克隆码云项目(code文件夹)
[root@izuf6hgznqeh3d0hgxzyrqz code]# git clone https://gitee.com/weejoo/school.git
[root@izuf6hgznqeh3d0hgxzyrqz code]# ls
school
- 创建环境,添加依赖项
[root@izuf6hgznqeh3d0hgxzyrqz project_school]# python3 -m venv venv
[root@izuf6hgznqeh3d0hgxzyrqz project_school]# source venv/bin/activate
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project_school]# ls
code conf logs stat venv
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project_school]# pip install -r code/school/requirements.txt
创建python3 -m venv venv
启动source venv/bin/activate
更新pip pip install -U pip
安装依赖项 pip install -r code/fangtx/requirements.txt
- uwsgi相关
- 配置uwsgi
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project]# vim conf/uwsgi.conf
1[uwsgi]
2 # 配置项目前导路径
3 base=/root/project
4 # 配置项目名称
5 name=fangtx
6 # 守护进程
7 master=true
8 # 进程个数(CPU个数)
9 processes=4
10 # 虚拟环境地址
11 pythonhome=%(base)/venv
12 # 项目地址
13 chdir=%(base)/code/fangtx
14 # 指定python解释器
15 pythonpath=%(pythonhome)/bin/python
16 # 指定uwsgi文件
17 module=%(name).wsgi
18 # 通信的地址和端口(自己服务器内网的IP地址和端口)
19 http=172.19.169.61:80
20 # 日志文件地址
21 # logto=%(base)/logs/uwsgi.log
- 安装uwsgi
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project]# pip install uwsgi
- 启动uwsgi
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project]# uwsgi --ini conf/uwsgi.conf
- 如果80端口被占用(nginx)
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project_school]# netstat -nap | grep 80
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project_school]# systemctl stop nginx
- 确认uwsgi可运行后,恢复配置文件中注释的日志,uwsgi用8000端口,nginx用80端口
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project_school]# vim conf/uwsgi.conf
19 通信的地址和端口(自己服务器的IP地址和端口)
socket=服务器内网IP:8000
20 # 日志文件地址
21 logto=%(base)/logs/uwsgi.log
- 配置Nginx
- 静态文件夹转移
(先在pycharm中指定静态资源位置)
STATIC_ROOT = '/root/project_school/stat/'
(venv) [root@izuf6hgznqeh3d0hgxzyrqz school]# git pull
(venv) [root@izuf6hgznqeh3d0hgxzyrqz fangtx]# python manage.py collectstatic
(venv) [root@izuf6hgznqeh3d0hgxzyrqz stat]# ls
admin css images js rest_framework
- 项目更新pull(不是必须的)
(venv) [root@izuf6hgznqeh3d0hgxzyrqz fangtx]# git pull
- 配置nginx(动静分离)
- 后台运行uwsgi
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project]# uwsgi --ini conf/uwsgi.conf &
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project]# jobs
[1]+ 运行中 uwsgi --ini conf/uwsgi.conf &
- 安装nginx(不是必须)
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project_school]# yum install nginx
- 配置nginx文件(nginx官方)-让官方的包含自己项目的nginx文件
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project]# vim /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
10 # Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
include /root/project_school/conf/nginx.conf;
}
- 配置nginx文件(nginx自己)(locatin动态走uwsgi,静态走nginx)
server{
2 listen 80;
3 server_name _;
4 access_log /root/project_school/logs/access.log;
5 error_log /root/project_school/logs/error.log;
6
7 location / {
8 include uwsgi_params;
9 uwsgi_pass 服务器内网IP:8000;
10 }
11
12 location /static/ {
13 alias /root/project_school/stat/;
14 expires 30d;
15 }
16
17 }
- HTTPS相关
- sftp传安全访问证书到服务器(project>conf>cert>):2248813_weejoo.cn.key 2248813_weejoo.cn.pem
(venv) [root@izuf6hgznqeh3d0hgxzyrqz cert]# ls
2248813_weejoo.cn.key 2248813_weejoo.cn.pem
- 配置HTTPS
(venv) [root@izuf6hgznqeh3d0hgxzyrqz project]# vim conf/nginx.conf
server {
19 listen 443;
20 server_name _;
21 access_log /root/project/logs/access.log;
22 error_log /root/project/logs/error.log;
23
24 ssl on;
25 ssl_certificate /root/project/conf/cert/2248813_weejoo.cn.pem;
26 ssl_certificate_key /root/project/conf/cert/2248813_weejoo.cn.key;
27 ssl_session_timeout 5m;
28 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
29 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
30 ssl_prefer_server_ciphers on;
31
32 location / {
33 include uwsgi_params;
34 uwsgi_pass 服务器内网IP:8000;
35 }
36
37 location /static/ {
38 alias /root/project/stat/;
39 expires 30d;
40 }
41 }
- 保存退出/重启nginx
systemctl restart nginx - 设置云解析DNS
记录类型: A
主机记录:@
记录值:服务器公网地址