Skip to content

Commit 93b5e99

Browse files
committed
添加部署相关文件
1 parent 11e8137 commit 93b5e99

File tree

7 files changed

+239
-1
lines changed

7 files changed

+239
-1
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# Python-Web应用部署步骤
2+
3+
> 假设:
4+
> * 操作系统为原生`Ubuntu 18.04 LTS`
5+
> * 当前登录用户为`xyz`
6+
> * Web程序目录: `/home/xyz/myblog`
7+
> * 虚拟环境运行启动脚本: `/home/xyz/runinenv.sh`
8+
9+
* `/home/xyz/runinenv.sh`内容如下:
10+
```shell
11+
#!/bin/bash
12+
export SOME_ENV=test-message
13+
14+
VENV=$1
15+
if [ -z $VENV ]; then
16+
echo "usage:runinenv [virtualenv_path] CMDS"
17+
exit 1
18+
fi
19+
source ${VENV}/bin/activate
20+
shift 1
21+
echo "Executing $@ in ${VENV}"
22+
exec "$@"
23+
deactivate
24+
```
25+
26+
---
27+
28+
1. 安装必须的软件
29+
```shell
30+
sudo apt-get install python3-pip virtualenv -y
31+
sudo apt-get install vim -y
32+
sudo apt-get install nginx supervisor -y
33+
```
34+
2. 创建虚拟环境、安装包、测试程序
35+
```shell
36+
cd ~
37+
virtualenv v3web --python=python3
38+
39+
source /home/xyz/v3web/bin/activate
40+
pip install Flask flask_sqlalchemy
41+
42+
cd /home/xyz/myblog
43+
python index.py
44+
45+
deactivate
46+
```
47+
3. 使用supervisor管理进程(后台运行)
48+
```shell
49+
cd /etc/supervisor/conf.d/
50+
vim demo.conf
51+
```
52+
`/etc/supervisor/conf.d/demo.conf`内容如下:
53+
```
54+
[program:demo]
55+
user=xyz
56+
directory=/home/xyz/myblog/
57+
command=/bin/bash /home/xyz/runinenv.sh /home/xyz/v3web python /home/xyz/myblog/index.py
58+
autostart=true
59+
autorestart=true
60+
startsecs=5
61+
stopsignal=HUP
62+
stopasgroup=true
63+
stopwaitsecs=5
64+
stdout_logfile_maxbytes=20MB
65+
stdout_logfile=/var/log/supervisor/%(program_name)s-out.log
66+
stderr_logfile_maxbytes=20MB
67+
stderr_logfile=/var/log/supervisor/%(program_name)s-err.log
68+
```
69+
配置要点:
70+
- `[program:<名称>]`
71+
- `user=<运行用户>`
72+
- `directory=<启动目录>`
73+
- `command=<运行的命令>`
74+
- `environment=<环境变量>`
75+
参考: [Supervisor and Environment Variables](https://stackoverflow.com/questions/12900402/supervisor-and-environment-variables/19611920#19611920)
76+
4. supervisor管理命令
77+
```shell
78+
# 重新加载配置
79+
sudo supervisorctl reload
80+
# 查看进程状态
81+
sudo supervisorctl status
82+
# 停止/启动/重启某个进程(此处为demo)
83+
sudo supervisorctl stop/start/restart demo
84+
```
85+
5. Linux基础命令
86+
```shell
87+
# 查看和python相关的tcp连接
88+
netstat -antp | grep python
89+
# 查看和python相关的进程
90+
ps uax | grep python
91+
# 杀死指定名称的进程(此处为python)
92+
sudo pkill python
93+
```
94+
6. 配置nginx
95+
```shell
96+
cd /etc/nginx/conf.d/
97+
sudo vim demo.conf
98+
```
99+
`/etc/nginx/conf.d/demo.conf`内容如下:
100+
```
101+
server {
102+
listen 80;
103+
listen [::]:80;
104+
server_name localhost;
105+
106+
location / {
107+
proxy_set_header X-Real-IP $remote_addr;
108+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
109+
proxy_set_header Host $http_host;
110+
proxy_set_header X-NginX-Proxy true;
111+
112+
proxy_pass http://127.0.0.1:5000/;
113+
# proxy_pass http://10.0.0.10:8999/;
114+
proxy_redirect off;
115+
}
116+
117+
location /static/ {
118+
alias /home/xyz/myblog/static/;
119+
}
120+
}
121+
```
122+
7. nginx管理命令
123+
```shell
124+
# 重启nginx服务(会重新加载配置文件)
125+
sudo service nginx restart
126+
# 启动、停止服务器
127+
sudo service nginx start/stop
128+
```
129+
8. nginx静态文件403解决方案
130+
修改nginx配置文件(改完了需要重启)
131+
```shell
132+
vim /etc/nginx/nginx.conf
133+
```
134+
将如下的行(nginx运行用户默认为`www-data`)
135+
```
136+
user www-data;
137+
```
138+
改为(将nginx运行用户改为`root`)
139+
```
140+
user root;
141+
```
142+
一般情况下,也将nginx运行用户改为当前用户(此处为`xyz`
143+
```
144+
user xyz;
145+
```
146+
重启服务器
147+
```shell
148+
sudo service nginx restart
149+
```
150+
9. 添加用户, 并添加到sudo组
151+
```shell
152+
# 创建名为pyuser的用户
153+
adduser pyuser
154+
# 将用户pyuser添加到sudo组里
155+
usermod -a -G sudo pyuser
156+
```

deploy-demo/nginx-demo.conf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
server {
2+
listen 80;
3+
listen [::]:80;
4+
server_name _;
5+
6+
location / {
7+
proxy_set_header X-Real-IP $remote_addr;
8+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9+
proxy_set_header Host $http_host;
10+
proxy_set_header X-NginX-Proxy true;
11+
12+
proxy_pass http://127.0.0.1:<PORT>/;
13+
proxy_redirect off;
14+
}
15+
16+
location /static/ {
17+
alias <PROJ-ROOT-ABSPATH>/static/;
18+
}
19+
}

deploy-demo/runinenv.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
export SOME_ENV=some-value
3+
4+
VENV=$1
5+
if [ -z $VENV ]; then
6+
echo "usage:runinenv [virtualenv_path] CMDS"
7+
exit 1
8+
fi
9+
source ${VENV}bin/activate
10+
shift 1
11+
echo "Executing $@ in ${VENV}"
12+
exec "$@"
13+
deactivate

deploy-demo/supervisor-demo.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[program:<APP-NAME>]
2+
user=<USER>
3+
directory=<PROJ-ROOT-ABSPATH>/
4+
command=/bin/bash <RUNINENV-ABSPATH>/runinenv.sh <VEVE-ABSPATH>/ python <PY-FILE-ABSPATH>
5+
autostart=true
6+
autorestart=true
7+
startsecs=5
8+
stopsignal=HUP
9+
stopasgroup=true
10+
stopwaitsecs=5
11+
stdout_logfile_maxbytes=20MB
12+
stdout_logfile=/var/log/supervisor/%(program_name)s-out.log
13+
stderr_logfile_maxbytes=20MB
14+
stderr_logfile=/var/log/supervisor/%(program_name)s-err.log

main/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@
2525
path('admin/', admin.site.urls),
2626
path('martor/', include('martor.urls')),
2727
path('api/uploader/', markdown_uploader, name='markdown_uploader_page'),
28-
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
28+
]
29+
30+
if settings.DEBUG:
31+
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

main/views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
from martor.utils import LazyEncoder
1414

1515

16+
def image_thumbnail(path, width=128, height=None):
17+
'''
18+
创建缩略图
19+
TOOD: 图片尺寸修改和保存
20+
'''
21+
from PIL import Image
22+
img = Image.open(path[0])
23+
img = img.resize((width, height), Image.ANTIALIAS)
24+
import ipdb; ipdb.set_trace()
25+
pass
26+
1627
@login_required
1728
def markdown_uploader(request):
1829
"""
@@ -53,6 +64,7 @@ def markdown_uploader(request):
5364
def_path = default_storage.save(save_path, ContentFile(image.read()))
5465
img_url = os.path.join(settings.MEDIA_URL, tmp_file).replace('\\', '/')
5566
img_path = def_path
67+
# image_thumbnail((image, img_path, img_url), width=128) # debug
5668

5769
data = json.dumps({
5870
'status': 200,

wsgi-server.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
import tornado.httpserver
4+
import tornado.ioloop
5+
import tornado.wsgi
6+
from django.core.wsgi import get_wsgi_application
7+
8+
9+
def main():
10+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings")
11+
application = get_wsgi_application()
12+
container = tornado.wsgi.WSGIContainer(application)
13+
http_server = tornado.httpserver.HTTPServer(container)
14+
port = 8000
15+
host = '0.0.0.0'
16+
print('visit by: [http: //{0}:{1}/]'.format(host, port))
17+
http_server.listen(port, address=host)
18+
tornado.ioloop.IOLoop.instance().start()
19+
20+
if __name__ == "__main__":
21+
main()

0 commit comments

Comments
 (0)