centos 7下 django 1.11 + nginx 1.12 + uwsgi 2.0
之前写过一个博客关于如何安装django的,见下网址,
http://www.cnblogs.com/qinhan/p/8732626.html
还有一个网址如何安装nginx的
http://www.cnblogs.com/qinhan/p/8780098.html
这次说说django搭配nginx+uwsgi部署生产环境
部署之前测试环境把防火墙都关掉,用以下两条命令
iptables -X
iptables -F
首先安装uwsgi, 可以登陆官网下载
http://uwsgi-docs.readthedocs.io/en/latest/Download.html
随后查看安装手册
http://uwsgi-docs.readthedocs.io/en/latest/Install.html
yum groupinstall "Development Tools" yum install python yum install python-devel python uwsgiconfig.py --build
python3 setup.py install #官方文档里面没写这步,但是需要执行
官方文档用的是标红的安装过程,执行红色的python uwsgiconfig.py --build 需要继续执行python3 setup.py install才能用的时候环境变量找到uwsgi
以下截取部分安装过程
(monitor2) insp_ap@inspect01:~/project/hanqin/django/monitor2/tmp/uwsgi-2.0.17> python3 setup.py install
################# uWSGI configuration ################# kernel = Linux execinfo = False ifaddrs = True locking = pthread_mutex event = epoll timer = timerfd filemonitor = inotify pcre = False routing = False capabilities = False yaml = embedded json = False ssl = False xml = False debug = False plugin_dir = . zlib = True ucontext = True malloc = libc ############## end of uWSGI configuration ############# total build time: 1 seconds *** uWSGI is ready, launch it with /home/insp_ap/project/hanqin/django/monitor2/bin/uwsgi *** running build running build_py creating build creating build/lib copying uwsgidecorators.py -> build/lib running install_lib copying build/lib/uwsgidecorators.py -> /home/insp_ap/project/hanqin/django/monitor2/lib/python3.6/site-packages byte-compiling /home/insp_ap/project/hanqin/django/monitor2/lib/python3.6/site-packages/uwsgidecorators.py to uwsgidecorators.cpython-36.pyc running install_egg_info running egg_info creating uWSGI.egg-info writing uWSGI.egg-info/PKG-INFO writing dependency_links to uWSGI.egg-info/dependency_links.txt writing top-level names to uWSGI.egg-info/top_level.txt writing manifest file 'uWSGI.egg-info/SOURCES.txt' reading manifest file 'uWSGI.egg-info/SOURCES.txt' writing manifest file 'uWSGI.egg-info/SOURCES.txt' Copying uWSGI.egg-info to /home/insp_ap/project/hanqin/django/monitor2/lib/python3.6/site-packages/uWSGI-2.0.17-py3.6.egg-info running install_scripts
(monitor2) insp_ap@inspect01:~/project/hanqin/django/monitor2> uwsgi --version
2.0.17
如果没有报错,表明安装完成,安装完成后可以用对应命令uwsgi --version查看安装版本
接下来启动uwsgi, 我这里只说用配置文件进行启动
insp_ap@inspect01:~/project/hanqin/django/monitor2/config> view uwsgi.ini
[uwsgi] # 项目目录 chdir=/home/insp_ap/project/hanqin/django/monitor2/jiankong2 # 指定项目的application module=jiankong2.wsgi:application # 指定sock的文件路径 socket=/home/insp_ap/project/hanqin/django/monitor2/tmp/uwsgi.sock #socket=127.0.0.1:8001 # 进程个数 workers=1 pidfile=/home/insp_ap/project/hanqin/django/monitor2/tmp/uwsgi.pid # 指定IP端口 http=10.129.6.78:9001 # 指定静态文件 static-map=/static=/home/insp_ap/project/hanqin/django/monitor2/jiankong2/statics # 启动uwsgi的用户名和用户组 uid=insp_ap gid=insp_ap # 启用主进程 master=true # 自动移除unix Socket和pid文件当服务停止的时候 vacuum=true # 序列化接受的内容,如果可能的话 thunder-lock=true # 启用线程 enable-threads=true # 设置自中断时间 harakiri=30 # 设置缓冲 post-buffering=4096 # 设置日志目录 daemonize=/home/insp_ap/project/hanqin/django/monitor2/tmp/uwsgi.log
执行启动命令
(monitor2) insp_ap@inspect01:~/project/hanqin/django/monitor2/config> uwsgi --ini uwsgi.ini
可以看到进程启动,并且访问网站成功
(monitor2) insp_ap@inspect01:~/project/hanqin/django/monitor2/config> ps -fu $USER | grep uwsgi
insp_ap 9576 1 28 12:00 ? 00:00:00 uwsgi --ini uwsgi.ini
insp_ap 9942 9576 0 12:00 ? 00:00:00 uwsgi --ini uwsgi.ini
insp_ap 9943 9576 0 12:00 ? 00:00:00 uwsgi --ini uwsgi.ini
接下来我们要加入Nginx, 做成最终的nginx+django+uwsgi模式
首先修改uwsgi.ini文件,可以看到和前面相比修改了部分内容,其中端口8001要注意,待会会和nginx里面的conf文件里面的一个配置相同
(monitor2) insp_ap@inspect01:~/project/hanqin/django/monitor2/config> view uwsgi.ini
# uwsig使用配置文件启动 [uwsgi] # 项目目录 chdir=/home/insp_ap/project/hanqin/django/monitor2/jiankong2 # 指定项目的application module=jiankong2.wsgi:application # 指定sock的文件路径 #socket=/home/insp_ap/project/hanqin/django/monitor2/tmp/uwsgi.sock socket=10.129.6.78:8001 # 进程个数 workers=1 pidfile=/home/insp_ap/project/hanqin/django/monitor2/tmp/uwsgi.pid # 指定IP端口 #http=144.131.252.51:9001 # 指定静态文件 #static-map=/static=/home/yy1s/myproject/mon2/jiankong2/statics # 启动uwsgi的用户名和用户组 uid=1000 gid=100 # 启用主进程 master=true # 自动移除unix Socket和pid文件当服务停止的时候 vacuum=true # 序列化接受的内容,如果可能的话 thunder-lock=true # 启用线程 enable-threads=true # 设置自中断时间 harakiri=30 # 设置缓冲 post-buffering=4096 # 设置日志目录 daemonize=/home/insp_ap/project/hanqin/django/monitor2/tmp/uwsgi.log
修改nginx配置
inspect01:/usr/local/nginx1_12_2/conf # pwd
/usr/local/nginx1_12_2/conf
inspect01:/usr/local/nginx1_12_2/conf # view nginx.conf
#这里只显示修改的部分内容,其中location /static是指向django项目里面的静态内容包括css font js等,并且8001端口需要和前面的uwsgi.ini配置的8001端口相同
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /static {
#expires 30d;
#autoindex on;
#add_header Cache-Control private;
alias /home/insp_ap/project/hanqin/django/monitor2/jiankong2/statics;
}
location / {
include uwsgi_params;
uwsgi_pass 10.129.6.78:8001;
uwsgi_read_timeout 20;
#root html;
#index index.html index.htm;
}
配置完成以后启动nginx以及uwsgi
inspect01:/usr/local/nginx1_12_2/sbin # ./nginx
(monitor2) insp_ap@inspect01:~/project/hanqin/django/monitor2/config> uwsgi --ini uwsgi.ini
启动以后可以看到8080以及8001都处于监听状态
inspect01:/usr/local/nginx1_12_2/sbin # netstat -an | grep 8080
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
inspect01:/usr/local/nginx1_12_2/sbin # netstat -an | grep 8001
tcp 0 0 10.129.6.78:8001 0.0.0.0:* LISTEN
访问网页,可以看到10.129.6.78:8080 端口可以访问
这里说明下8080端口是前端页面也就是浏览器访问Nginx的端口,8001端口为uwsgi和nginx之间通讯的端口,
如果暂时搞不明白就先按照我上面的配置配起来以后在研究。