uwsgi + nginx + django 部署项目

开始正式部署

  1. pip install uwsgi
  2. 进入django目录下 测试uwsgi
uwsgi --http 0.0.0.0:9000 --file Polaris/wsgi.py --static-map=/static=static

参数说明:

--http 这个就和runserver一样指定IP 端口
--file 这个文件就里有一个反射,如果你在调用他的时候没有指定Web Server就使用默认的
-- static 做一个映射,指定静态文件

此时,访问http://192.168.2.108:9000/,能访问到自己的项目,表示项目启动成功

使用 uwsgi.ini 配置需要的参数

[uwsgi]
#作为web服务器来用  
#http=0.0.0.0:5656
#nginx
socket=127.0.0.1:5656
#项目目录
chdir=/opt/nsfocus/Polaris/web/Polaris
#指定wsgi.py所在目录,相对于上面的项目目录
module = Polaris.wsgi:application
#virtualenv目录(绝对路径)
home = /opt/Polaris/

master=True
#工作进程的数目
processes=4
#工程进程中的线程数
threads=2
#退出后清理文件
vacuum = True

#主进程id存放的文件  放在了项目下面, 可自行调整
pidfile=uwsgi.pid
max-requests = 5000
#日志信息存放的文件  这个是相对于项目目录的 放在了项目下面
daemonize=uwsgi.log

#使用touch-reload 重启服务
touch-reload = /opt/nsfocus/Polaris/web/Polaris/manage.py

配置好,可启用http 配置 做为一个web 服务起直接启动看是不是访问, 但是没有静态文件, 就可以了
更多参数可参考:

# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/opt/proj/teacher/
# 指定项目的application
module=teacher.wsgi:application
# 指定sock的文件路径       
socket=/opt/proj/script/uwsgi.sock
# 进程个数       
workers=5
pidfile=/opt/proj/script/uwsgi.pid
# 指定IP端口       
http=192.168.2.108:8080
# 指定静态文件
static-map=/static=/opt/proj/teacher/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/opt/proj/script/uwsgi.log

安装nginx

linux版本 CentOs6.6 64位

http://www.runoob.com/linux/nginx-install-setup.html

  1. 安装编译工具及库文件
  2. 2.
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

二、首先要安装 PCRE
PCRE 作用是让 Nginx 支持 Rewrite 功能。
安装包的下载位置 /usr/local/src 中

[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

2、解压安装包:

[root@bogon src]# tar zxvf pcre-8.35.tar.gz

3、进入安装包目录
[root@bogon src]# cd pcre-8.35
4、编译安装

[root@bogon pcre-8.35]# ./configure
[root@bogon pcre-8.35]# make && make install

5、查看pcre版本

[root@bogon pcre-8.35]# pcre-config --version

下载nginx

解压
进入目录
编译安装

[root@localhost src]# tar zxvf nginx-1.10.2.tar.gz
省略安装内容...
[root@localhost src]# cd nginx-1.10.2
[root@localhost nginx-1.10.2]# ./configure && make && make install

3、启动Nginx

先找一下nginx安装到什么位置上了

whereis nginx
/usr/local/nginx
启动:
/usr/local/nginx/sbin/nginx
报错:
error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

查找确少的库 pcre库已经安装过了

查看缺少的库
ldd $(which /usr/local/nginx/sbin/nginx)
    linux-vdso.so.1 =>  (0x00007ffcd91f9000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f0e865e5000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f0e863c8000)
    libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007f0e86190000)
    libpcre.so.1 => not found
    libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007f0e85dab000)
    libz.so.1 => /lib64/libz.so.1 (0x00007f0e85b94000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f0e85800000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f0e867f3000)
    libfreebl3.so => /lib64/libfreebl3.so (0x00007f0e855fd000)
[root@tech-xrf-5-253-30 sbin]# 

查找位置建立连接

对于/lib64/* 64位系统来说:
#查看lib库
#  ls /lib64/ |grep pcre
libpcre.so.0
libpcre.so.0.0.1
#添加软连接
# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1

再次启动nginx
/usr/local/nginx/sbin/nginx
查看启动情况
ps -aux | grep nginx

root     19490  0.0  0.0  23964   820 ?        Ss   15:33   0:00 nginx: master process ./nginx
nobody   19491  0.0  0.0  24384  1404 ?        S    15:33   0:00 nginx: worker process
root     19495  0.0  0.0 103260   884 pts/3    S+   15:34   0:00 grep nginx

访问 80端口

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

其他的nginx命令:

/usr/local/nginx/sbin/nginx -s reload            # 重新载入配置文件
    /usr/local/nginx/sbin/nginx -s reopen            # 重启 Nginx
    /usr/local/nginx/sbin/nginx -s stop              # 停止 Nginx

启动成功

开始配置和uwsgi的连接

进入nginx的配置文件目录

cd /usr/local/nginx/conf/

编辑nginx的配置文件 或者键一个默认的配置文件

vim nginx.conf


#user  nobody;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  本机ip地址;
        charset utf-8; # Nginx编码
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        error_page 404 /404.html; # 错误页面
        error_page 500 502 503 504 /50x.html; # 错误页面

        # 指定项目路径uwsgi
        location / {
        include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
        uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
        uwsgi_pass 127.0.0.1:5656; # 指定uwsgi的sock文件所有动态请求就会直接丢给他就是 uwsgi 上的 socket 地址 一定 要一样
        }
        # 静态文件路径
        location /static {
            alias /opt/nsfocus/Polaris/web/Polaris/static;  # 直接指定静态文件 不用在收集了
        }
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

配置完成后先启动 uwsgi 然后再启动 nginx

使用 ps -aux | grep uwsgi 和 ps -aux | grep nginx 看服务是不是启动了

访问设定好的端口就可以了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值