在centos7上部署dajngo(nginx+uWSGI)

来源:互联网 发布:三千网络创业平台 编辑:程序博客网 时间:2024/04/28 18:06

以下的前提环境是django通过python runserver 0.0.0.0:80外网输入IP可正常访问
此文总结自此文,十分感谢作者分享
uWSGI

安装uWSGI
不知为何 pip install uwsgi 后uwsgi 总是无法找到命令,只好下载源码安装
wget https://pypi.python.org/pypi/uWSGI/
tar -zxvf uwsgi-2.0.15.tar.gz
cd uwsgi-2.0.15
make
然后将其加入连接
ln -s /root/uwsgi-2.0.15/uwsgi /usr/bin/uwsgi
测试:uwsgi –http 0.0.0.0:80 –wsgi-file text.py
text.py

def application(env, start_response):
start_response(‘200 OK’, [(‘Content-Type’,’text/html’)])
return [b”Hello World”]

uwsgi常用命令:uwsgi –ini /root/web/uwsgi.ini #以此配置运行uwsgi
lsof -i :80 查看80端口被那些进程占用(yum install lsof)
kill -9 pid 杀死pid号的进程

nginx

yum install epel-release
yum install nginx

nginx 服务器重启命令,关闭
nginx -s reload :修改配置后重新加载生效
nginx -s reopen :重新打开日志文件
nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确

关闭nginx:
nginx -s stop :快速停止
nginx -s quit :完整有序的停止nginx
其他的停止nginx 方式:
ps -ef | grep nginx
kill -QUIT 主进程号 :从容停止Nginx
kill -TERM 主进程号 :快速停止Nginx
pkill -9 nginx :强制停止Nginx

启动nginx:
nginx -c /path/to/nginx.conf
平滑重启nginx:
kill -HUP 主进程号

综合:在安装好uwsgi和nginx后我需要在Django的APP同级目录下添加2个文件,

1.uwsgi.ini

使用时需要把注释删掉(在阿里云控制台安全组入方向8000端口要放开)

[uwsgi]
socket = 127.0.0.1:8000 #此地址要和下面的nginx的配置文件web.conf中一致
chdir=/root/web #dajngo的project
module=web.wsgi #指明Django的wsgi的地址
master = true
processes=2
threads=2
max-requests=2000
chmod-socket=664
vacuum=true
daemonize = /root/web/uwsgi.log #日志存放位置

2.web.conf

# For more information on configuration, see:#   * Official English Documentation: http://nginx.org/en/docs/#   * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;worker_processes auto;worker_rlimit_nofile 65535; #设置最大并发数error_log /var/log/nginx/error.log;pid /run/nginx.pid;# Load dynamic modules. See /usr/share/nginx/README.dynamic.include /usr/share/nginx/modules/*.conf;events {    worker_connections 1024;}http {    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  /var/log/nginx/access.log  main;    sendfile            on;    tcp_nopush          on;    tcp_nodelay         on;    keepalive_timeout   65;    types_hash_max_size 2048;    include             /etc/nginx/mime.types;#此为绝对路径    default_type        application/octet-stream;    # Load modular configuration files from the /etc/nginx/conf.d directory.    # See http://nginx.org/en/docs/ngx_core_module.html#include    # for more information.    include /etc/nginx/conf.d/*.conf;#其余部分和/etc/nginx/nginx.conf一样,在server这部分需要修改如下    server {    listen 80;    server_name localhost;    charset     utf-8;    access_log      /root/web/nginx_access.log; #根据需要修改    error_log       /root/web/nginx_error.log; #根据需要修改    client_max_body_size 75M;    location /static {        alias /root/web/static; #根据需要修改    }    location / {        include     /etc/nginx/uwsgi_params; #根据需要修改        uwsgi_pass  127.0.0.1:8000;#必须和uwsgi.ini一致    }}# Settings for a TLS enabled server.##    server {#        listen       443 ssl http2 default_server;#        listen       [::]:443 ssl http2 default_server;#        server_name  _;#        root         /usr/share/nginx/html;##        ssl_certificate "/etc/pki/nginx/server.crt";#        ssl_certificate_key "/etc/pki/nginx/private/server.key";#        ssl_session_cache shared:SSL:1m;#        ssl_session_timeout  10m;#        ssl_ciphers HIGH:!aNULL:!MD5;#        ssl_prefer_server_ciphers on;##        # Load configuration files for the default server block.#        include /etc/nginx/default.d/*.conf;##        location / {#        }##        error_page 404 /404.html;#            location = /40x.html {#        }##        error_page 500 502 503 504 /50x.html;#            location = /50x.html {#        }#    }}

3.启动uwsgi

uwsgi uwsgi.ini

4.启动nginx

nginx -c /path/to/nginx.conf

5.访问IP地址验证是否成功。

原创粉丝点击