项目部署

本质

三件事

租个服务器(含公网ip)+配置环境

电脑(服务器)+公网ip

自己电脑+拉专线(公网ip)

代码放在服务上

将程序运行起来

  1. 服务器为什么使用centos
  • 日常windows,收费+图形化界面慢
  • 部署linux 免费+图形化界面/非图形化界面
  1. 公网ip:47.109.35.113
  • 登录服务器(xshell)

    1
    ssh root@47.109.35.113
  1. 端口

22端口:SSH,远程链接

3306端口:Mysql

6379端口:Redis

80端口:http

443端口:https

  1. 安全组

git代码仓库,存代码

  • gitlab(公司自己的仓库)

  • github

  • githee(创建仓库)

    1. 创建仓库

远程仓库地址:https://gitee.com/bkystop/nbiot.git

​ 2. 在项目跟目录配置.gitignore

.gitignore

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
  1. 进入,初始化,推送至代码仓库
  • 第一次
1
2
3
cd 项目目录
git init
git remote add origin https://gitee.com/bkystop/nbiot.git
  • 常用
1
2
3
git add .
git commit -m 'init'
git push origin master

服务器,拉取代码

  1. 安装git(/data/)
1
yum install git -y
  • 第一次
1
2
3
4
5
6
cd /data/
mkdir www
cd /data/www/

git clone https://gitee.com/bkystop/nbiot.git
> 码云账号和密码
  • 后续
1
2
cd /data/www/nbiot  # 项目目录
git pull origin master

此时是https链接拉取,也可基于SSH配置gitee

环境配置

  • python3.9
  • 虚拟环境
  • uwsgi
  • nginx

安装python3.9

  1. 安装gcc
1
yum install gcc -y # 安装gcc
  1. 安装python3相关依赖
1
2
3
4
5
6
7
8
9
10
11
12
yum install zlib zlib-devel -y
yum install bzip2 bzip2-devel -y
yum install ncurses ncurses-devel-y
yum install readline readline-devel -y
yum install openssl openssl-devel-y
yum install xz lzma xz-devel -y
yum install sqlite sqlite-devel-y
yum install gdbm gdbm-devel-y
yum install tk tk-devel -y
yum install mysql-devel -y
yum install python-devel -y
yum install libffi-devel -y
  1. 下载python3.9源码 https://www.python.org/ftp/python/****
1
2
cd /data/
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tgz

如果没有wget,先安装yum install wget -y

  1. 解压&编译&安装
  • 解压
1
tar -xvf Python-3.9.5.tgz
  • 进入目录并编译安装
1
2
3
4
5
cd Python-3.9.5
ls
./configure
make all
make install
  • 测试
1
2
3
4
5
python3 --version

/user/local/bin/python3
/user/local/bin/pip3
/user/local/bin/pip3.9

报错:WARNING: Running pip as root will break packages and permissions. You should install packages reliably by using venv: https://pip.pypa.io/warnings/venv

python3 -m venv tutorial-env

source tutorial-env/bin/activate

  1. python解释器配置豆瓣yuan
1
pip3.9 config set global.index-url https://pypi.douban.com/simple/

虚拟环境

  1. 安装virtualenv
1
pip3.9 install virtualenv

pip install asgiref==3.5.2 -i http://pypi.douban.com/simple/ –trusted-host pypi.douban.com

​ 2.创建虚拟环境(day28)

代码:/data/www/nbiot

虚拟环境:可在任意位置,一般在 /envs/nb

  • 创建
1
2
mkdir /envs
virtualenv /envs/nb --python=python3.9
  1. 激活虚拟环境
1
source /envs/nb/bin/activate
  1. 安装uwsgi
1
pip install uwsgi

pip install uwsgi -i http://pypi.douban.com/simple/ –trusted-host pypi.douban.com

  • 命令运行
1
uwsgi --http :8080 --wsgi-file app.py --callable app
  • 配置文件
1
2
3
4
5
6
7
8
9
10
11
[uwsgi]
socket = 127.0.0.1:8001 #nginx模式
http = 127.0.0.1:8000 # 二选一
chdir = /data/www/nbiot/djangoProject1
wsgi-file = /djangoProject1/wsgi.py
processes = 2
threads = 2
pidfile = uwsgi.pid
daemonize = uwsgi.log
master = true
virtualenv = /envs/nb/

settings,py中DEBUG=Ture该为DEBUG=False

settings,py中ALLOWED_HOSTS=[] 改为 ALLOWED_HOSTS=[“网站域名/服务监听的ip地址”]

  • 运行
1
2
3
source /envs/nb/bin/activate # 先激活虚拟环境
uwsgi --ini nb_uwsgi.ini # 运行
uwsgi --ini nb_uwsgi.ini & # 后台运行
  • 停止后台运行
1
2
ps -ef|grep nb_uwsgi
kill -9 5735 # 根据进程号kill

nginx

  1. 安装
1
yum install nginx -y
  1. 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 80;
server_name localhost;
charset UTF-8;

location /static {
alias /data/www/nbiot/app01/static;
}

location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass 127.0.0.1:8000;

}
}

1
2
cd /etc/nginx
vim nginx.conf
  1. 启动
1
2
3
4
systemctl start nginx
systemctl stop nginx
systemctl restart nginx # 重启
systemctl restart nginx # 开机启动
  1. 访问