Skip to content

Commit dbbb26b

Browse files
committed
日常更新
1 parent ac67455 commit dbbb26b

File tree

13 files changed

+1565
-157
lines changed

13 files changed

+1565
-157
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
layout: post
3+
title: "Nginx、uWSGI、Flask 项目部署"
4+
description: ""
5+
category: articles
6+
tags: [Nginx, uWSGI, Flask 项目部署]
7+
image:
8+
feature:
9+
credit: Michael Rose
10+
creditlink: http://mademistakes.com
11+
comments: true
12+
share: true
13+
---
14+
15+
我使用 Flask 作为开发框架比较少,所以在部署 Flask 项目方面经验相对较少,现将部署流程记录一下,留作今后参考。
16+
17+
# 1. 开发 Flask
18+
19+
开发好的 Flask 项目包含一个运行文件 manage.py,其中包含一个 app 对象,例如项目结构:
20+
21+
```
22+
├── project
23+
│ ├── __init__.py
24+
│ ├── main
25+
│ ├── models.py
26+
│ ├── static
27+
│ └── templates
28+
├── manage.py
29+
└── uwsgi.ini
30+
```
31+
32+
# 2. 安装配置 Nginx
33+
34+
## 下载 nginx 源码 nginx-1.12.1.tar.gz,解压后安装;
35+
36+
```
37+
$ cd Downloads
38+
$ tar zxf nginx-1.12.1.tar.gz
39+
$ cd nginx-1.12.1
40+
$ ./configure --prefix=/opt/nginx
41+
$ make && make install
42+
```
43+
44+
## 配置 Nginx修改配置文件,增加 server 配置;
45+
46+
```
47+
$ cd /opt/nginx
48+
$ vi conf/nginx.conf
49+
```
50+
51+
52+
```
53+
#user nobody;
54+
worker_processes 4; #可以设置成cpu个数,体验较佳的性能
55+
56+
#error_log logs/error.log;
57+
#error_log logs/error.log notice;
58+
#error_log logs/error.log info;,
59+
60+
#pid logs/nginx.pid;
61+
62+
worker_rlimit_nofile 65535; # 最大打开文件数,这个值需要<= worker_connections
63+
64+
events {
65+
worker_connections 65535; # 最大连接数,这个值依赖系统的配置。
66+
}
67+
68+
http {
69+
include mime.types;
70+
default_type application/octet-stream;
71+
72+
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
73+
# '$status $body_bytes_sent "$http_referer" '
74+
# '"$http_user_agent" "$http_x_forwarded_for"';
75+
76+
#access_log logs/access.log main;
77+
78+
sendfile on;
79+
tcp_nopush on;
80+
81+
#keepalive_timeout 0;
82+
keepalive_timeout 65;
83+
84+
gzip on;
85+
86+
# 以下为您需要增加的配置
87+
88+
upstream my_servers {
89+
server 192.168.0.100:8080;
90+
server 192.168.0.101:8080;
91+
server 192.168.0.102:8080;
92+
}
93+
94+
# uWSGI dynamic
95+
server {
96+
listen 80;
97+
server_name dynamic.youdomain.cn;
98+
99+
location / {
100+
include uwsgi_params;
101+
uwsgi_pass http://my_servers;
102+
uwsgi_ignore_client_abort on;
103+
uwsgi_param UWSGI_PYHOME /home/ubuntu/.virtualenvs/pyenv;
104+
uwsgi_param UWSGI_CHDIR /home/ubuntu/my_project;
105+
uwsgi_param UWSGI_SCRIPT manage;
106+
}
107+
}
108+
109+
# uWSGI static
110+
server {
111+
listen 80;
112+
server_name static.youdomain.cn;
113+
114+
location /static {
115+
root html;
116+
}
117+
}
118+
119+
}
120+
```
121+
122+
## 启动 Nginx
123+
124+
```
125+
$ cd /opt/nginx/sbin
126+
$ ./nginx
127+
```
128+
129+
# 3. 安装配置uWSGI
130+
131+
## 安装 uWSGI
132+
133+
```
134+
$ pip install uwsgi
135+
```
136+
137+
## 创建配置文件 uwsgi.ini
138+
139+
```
140+
[uwsgi]
141+
socket = :8080
142+
wsgi-file = manage.py
143+
callable = app
144+
master = true //主进程
145+
vhost = true //多站模式
146+
no-site = true //多站模式时不设置入口模块和文件
147+
workers = 24 //子进程数
148+
reload-mercy = 10
149+
vacuum = true //退出、重启时清理文件
150+
max-requests = 65535
151+
limit-as = 512
152+
buffer-size = 30000
153+
chmod-socket = 666
154+
pidfile = ./logs/%n.pid
155+
daemonize = ./logs/%n.log
156+
```
157+
158+
## 运行 uWSGI
159+
160+
```
161+
$ cd /home/ubuntu/my_project
162+
$ uwsgi --ini uwsgi.ini
163+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: post
3+
title: "Nginx静态资源跨域设置"
4+
description: ""
5+
category: articles
6+
tags: [Nginx, 跨域]
7+
image:
8+
feature:
9+
credit: Michael Rose
10+
creditlink: http://mademistakes.com
11+
comments: true
12+
share: true
13+
---
14+
15+
16+
在nginx的配置文件 `conf/nginx.conf` 中增加头部信息,具体如下所示:
17+
18+
```
19+
http {
20+
...
21+
22+
server {
23+
listen 80;
24+
server_name static.mydomain.cn;
25+
26+
location /static {
27+
root html;
28+
add_header Access-Control-Allow-Origin *; # 你需要增加的 HTTP 头
29+
}
30+
}
31+
}
32+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
layout: post
3+
title: "uWSGI listen queue of socket full 队列溢出的问题"
4+
description: ""
5+
category: articles
6+
tags: [uWSGI, 队列溢出]
7+
image:
8+
feature:
9+
credit: Michael Rose
10+
creditlink: http://mademistakes.com
11+
comments: true
12+
share: true
13+
---
14+
15+
使用 uWSGI 时,uWSGI的日志中出现错误 `uWSGI listen queue of socket full` 的错误信息,想要解决这个问题,需要修改服务器的配置,具体如下:
16+
17+
18+
```
19+
$ vi /etc/sysctl.conf
20+
21+
在文件最后添加一行记录:
22+
23+
net.core.somaxconn = 1024
24+
```
25+
26+
```
27+
重载配置,使配置立即生效:
28+
$ sysctl -p
29+
```
30+
31+
在 uWSGI 的配置项中增加 --listen 1024,也可以将此配置项写入 uWSGI 的配置文件中;
32+
33+
重启 uWSGI,如此可以在某种程度上缓解此问题。

0 commit comments

Comments
 (0)