一.安装Nginx
1.1打开ubuntu控制台(ctrl+alt+t)利用Ubuntu的仓库安装。
sudo apt-get install nginx #安装
1.2启动Nginx:
/etc/init.d/nginx start #启动
/etc/init.d/nginx stop #关闭
/etc/init.d/nginx restart #重启
1.3修改Nginx默认端口号,打开/etc/nginx/nginx.conf 文件,修改端口号。
在http标签下添加如下代码
server{
listen 8080;
server_name loaclhost;
location / {
root html;
index index.html index.htm;
}
}
1.4测试是否成功
启动nginx
/etc/init.d/nginx start #启动
浏览器输入ip:8080
2.安装uwsgi
2.1通过pip安装uwsgi。
python -m pip install uwsgi
2.2测试uwsgi,在/home/chengxun创建test.py文件:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
2.3通过uwsgi运行该文件。
/home/chengxun 执行uwsgi --http :8001 --wsgi-file test.py
出现这个错误,网上找了好多,发现以下解决方案适合我:
找到uwsgi执行位置,建立软链接
1.查找出所有uwsgi文件的路径
find / -name uwsgi
2.筛选有效的uwsgi,每个路径执行 --plugins-list 看结果条数
/var/lib/dpkg/alternatives/uwsgi --plugins-list #看结果条数
/var/lib/nginx/uwsgi --plugins-list #看结果条数
/etc/alternatives/uwsgi --plugins-list #看结果条数
/usr/bin/python3.7/bin/uwsgi --plugins-list #看结果条数
/usr/bin/uwsgi --plugins-list #看结果条数
/usr/lib/uwsgi --plugins-list #看结果条数
3.有效的uwsgi如下:
*** uWSGI loaded generic plugins ***
gevent
nagios
rrdtool
carbon
corerouter
fastrouter
http
ugreen
syslog
rsyslog
logsocket
router_uwsgi
router_redirect
router_basicauth
zergpool
redislog
mongodblog
router_rewrite
router_http
logfile
router_cache
rawrouter
router_static
sslrouter
cheaper_busyness
transformation_tofile
transformation_gzip
transformation_chunked
transformation_offload
router_memcached
router_redis
router_hash
router_expires
router_metrics
transformation_template
stats_pusher_socket
*** uWSGI loaded request plugins ***
0: python
17: spooler
18: symcall
100: ping
110: signal
111: cache
173: rpc
--- end of plugins list ---
我发现/usr/bin/python3.7/bin/uwsgi下的uwsgi是有效的
4.建立软链接
如果存在/usr/bin/uwsgi,那么久删除。接下来执行下面命令,以后输入uwsgi就是调用/usr/bin/python3.7/bin/uwsgi
ln -s /usr/bin/python3.7/bin/uwsgi /usr/bin/uwsgi
2.4浏览器输入 ip:8001
2.5接下来配置Django与uwsgi连接。
此处,假定的我的django项目位置为:/home/project/hxuner
执行下面命令:
uwsgi --http :8001 --chdir /home/project/hxuner
--wsgi-file hxuner/wsgi.py
--master --processes 4 --threads 2 --stats 127.0.0.1:9191
常用选项:
http : 协议类型和端口号
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
浏览器输入ip:8001,但是没有加载静态文件。
三、Nginx+uwsgi+Django
3.1接下来,我们要将三者结合起来。首先罗列一下项目的所需要的文件:
hxuner/
├── manage.py
├── hxuner/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── hxuner_uwsgi.ini
在我们通过Django创建hxuner项目时,在子目录hxuner下已经帮我们生成的 wsgi.py文件。所以,我们只需要再创建hxuner_uwsgi.ini配置文件即可,当然,uwsgi支持多种类型的配置文件,如xml,ini等。此处,使用ini类型的配置。
# hxuner_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8000
# the base directory (full path)
chdir = /home/project/hxuner
# Django s wsgi file
module = hxuner.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
这个配置,其实就相当于在上一小节中通过uwsgi命令,后面跟一堆参数的方式,给文件化了。
socket 指定项目执行的端口号。
chdir 指定项目的目录。
module hxuner.wsgi ,可以这么来理解,对于hxuner_uwsgi.ini文件来说,与它的平级的有一个hxuner目录,这个目录下有一个wsgi.py文件。
其它几个参数,可以参考上一小节中参数的介绍。
3.2接下来,切换到hxuner项目目录下,通过uwsgi命令读取hxuner_uwsgi.ini文件启动项目。
cd /home/project/hxuner
uwsgi --ini hxuner_uwsgi.ini
注意查看uwsgi的启动信息,如果有错,就要检查配置文件的参数是否设置有误。
3.3再接下来要做的就是修改nginx.conf配置文件。打开/etc/nginx/nginx.conf文件,添加如下内容。
server {
listen 80;
server_name 127.0.0.1
charset UTF-8;
access_log /var/log/nginx/hxuner_access.log;
error_log /var/log/nginx/hxuner_error.log;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /home/project/hxuner/static/;
}
}
listen 指定的是nginx代理uwsgi对外的端口号。
server_name 网上大多资料都是设置的一个网址(例,www.example.com),我这里如果设置成网址无法访问,所以,指定的到了本机默认ip。
在进行配置的时候,我有个问题一直想不通。nginx到底是如何uwsgi产生关联。现在看来大概最主要的就是这两行配置。
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
include 必须指定为uwsgi_params;而uwsgi_pass指的本机IP的端口与hxuner_uwsgi.ini配置文件中的必须一致。
3.4启动nginx
/etc/init.d/nginx start #启动
3.5浏览器输入ip就行,因为80默认
发现原来80端口已经被nginx欢迎页占用
解决方案:
cd /etc/nginx/sites-enables/
vim defalut
将80改为用不到的端口
3.6现在浏览器输入ip就可以直接访问
四 总结 Nginx+uwsgi+django项目部署
以后在一台环境全部安装完成的机器上,并且python manage.py runserver 0.0.0.0:port运行起来之后,只需4步就可以通过nginx+uwsgi部署django项目。
4.1在项目的主目录下新建 项目名_uwsgi.ini,
内容如下:
# hxuner_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8000
# the base directory (full path)
chdir = /home/project/hxuner
# Django s wsgi file
module = hxuner.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
2.切换到项目目录下,通过uwsgi命令读取
项目名_uwsgi.ini文件启动项目。
cd /home/project/项目名
uwsgi --ini 项目名_uwsgi.ini
3.配置nginx.conf
vim /etc/nginx/nginx.conf
server {
listen 80;
server_name 127.0.0.1
charset UTF-8;
access_log /var/log/nginx/hxuner_access.log;
error_log /var/log/nginx/hxuner_error.log;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /home/project/hxuner/static/;
}
}
4.启动nginx
/etc/init.d/nginx start #启动
5.浏览器输入nginx.conf配置的listen端口