• Centos 6.5部署nginx+uwsgi+django


    Centos 6.5部署nginx+uwsgi+django

    一、安装python3,系统默认是python2.6

    1、安装依赖软件
    yum -y install sqlite-devel
    yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
    2、下载安装python3.6
    tar zxvf Python-3.6.1.tgz
    cd Python-3.6.1
    ./configure --prefix=/usr/local/python3
    make && make install
    3、创建python及pip命令软链接
    ln -s /usr/local/python3/bin/python3 /usr/bin/python3
    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
     
    4、测试
    [root@localhost ~]# python3
    Python 3.6.1 (default, Dec 21 2017, 16:14:49)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print('test_haha');
    test_haha
     

    二、安装django

    1、访问链接下载最新django版本,https://www.djangoproject.com/download/
    2、安装django依赖组件pytz
    pip3 install pytz-2017.3-py2.py3-none-any.w
    3、安装django
    tar zxvf Django-2.0.tar.gz
    cd Django-2.0
    python3 setup.py install
    4、测试
    上传django项目
    cd django_project
    python3 manage.py runserver 0.0.0.0:80
    访问http://127.0.0.1 可以访问
     
    三、安装uwsgi web网关
    1、下载安装uwsgi
    tar zxvf uwsgi-2.0.1.tar.gz
    cd uwsgi-2.0.1
    python3 uwsgiconfig.py --build
    python3 uwsgiconfig.py --clean
    cp -R /root/uwsgi-2.0.1 /usr/local/uwsgi
    ln -s /usr/local/uwsgi/uwsgi /usr/bin/uwsgi
    2、启动,测试
    创建测试文件
    # vim test.py
    def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]
    启动
    /usr/local/uwsgi/uwsgi --http :9090 --wsgi-file /root/test.py
    访问http://ip:9090 可以访问成功
     
     
    3、uwsgi和django结合
    uwsgi --http :9090 --chdir /home/code/Ticket_system --wsgi-file Ticket_system/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9192
     
    访问http:ip:9090 可以访问成功
    #如果在访问django项目时静态文件加载失败可以先忽略,在nginx配置中会指定静态文件路径,配置后可以正常访问
    ps:
    --chdir /home/code/Ticket_system django项目目录
     
      • 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)
    注意:–wsgi-file后面跟的是相对目录
     
    4、uwsgi有两种启动方式,一种通过端口即上边这种方式,另一种通过配置.ini文件启动即下边用到的方式
     

    四、安装nginx

    1、下载安装依赖第三方软件pcre
    wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.34/pcre-8.34.tar.bz2
    tar jxvf pcre-8.34.tar.bz2
    cd pcre-8.34
    ./configure --enable-utf8
    make && make install
    2、下载安装依赖第三方软件openssl
    下载openssl-1.0.2h.tar.gz
    tar zxvf openssl-1.0.2h.tar.gz
    3、下载安装nginx
    nginx-1.9.9.tar.gz
    tar zxvf nginx-1.9.9.tar.gz
    cd nginx-1.9.9
    ./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-openssl=/root/openssl-1.0.2h --with-http_gzip_static_module --with-http_sub_module --with-cc=/usr/bin/gcc
    make && makeinstall
    4、新建www用户
    useradd -s /sbin/nologin -M www
    5、修改配置文件
    备份nginx配置文件
    cd /usr/local/nginx/conf
    cp -f nginx.conf nginx.conf_bak
    vim nginx.conf
    user www;         #修改启动用户为www
    worker_processes 4;         #启动4个进程,根据实际需求配置
    6、启动nginx
    /usr/local/nginx/sbin/nginx
    访问测试http://ip 默认80端口,可以访问
     

    五、nginx+uwsgi+django三者结合

    1、django项目中/根目录下创建uwsgi.ini
    # vim uwsgi.ini
    [uwsgi]
    #uwsgi启动端口
    socket = 127.0.0.1:9090 
    #django项目目录
    chdir=/home/code/Ticket_system 
    module=Ticket_system.wsgi
    master = true
    processes=2
    threads=2
    max-requests=2000
    chmod-socket=664
    vacuum=true
    # 日志路径
    daemonize = /home/code/Ticket_system/logs/uwsgi.log 
     
    2、配置nginx
    # vim /usr/local/nginx/conf/nginx.conf
    server {
    listen 80;
    server_name localhost;
     
    #charset koi8-r;
     
    #access_log logs/host.access.log main;
    #指定静态文件路径,避免django项目中静态文件无法加载
    location /static {
    alias /home/code/Ticket_system/app01/static;
    }
    location / {
    #新增加两个uwsgi配置
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:9090;
    root html;
    index index.html index.htm;
    }
    .......
    #下边必须操作,否则后台管理admin的静态资源会无法访问
    1)第一种方法:找到django-admin静态文件的目录
    # python3 
    >>> import django  
    >>> django.__file__  
    '/usr/local/python3/lib/python3.6/site-packages/Django-2.0.4-py3.6.egg/django/__init__.py'
    找到根目录之后我需要的绝对路径为(不同版本可能不一样,根据自己实际情况来)
    /usr/local/python3/lib/python3.6/site-packages/Django-2.0.4-py3.6.egg/django/contrib/admin/static
    把static 拷贝到自己定义的static目录,一般是在项目的根目录下,或者应用的根目录下,
     
    2)第二种方法,建立自己的静态文件夹
    修改settings,建立自己的文件夹
    STATIC_ROOT = “/home/code/Ticket_system/static"  
    运行下面命令把相关文件copy到这个目录
    python manage.py collectstatic  
     
    3、启动uwsgi
    uwsgi --ini /home/code/Ticket_system/uwsgi.ini
     
    ps:如果启动时报错uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
    解决:  

      cd /lib64
      ln -s libpcre.so.0.0.1 libpcre.so.1

     
    4、启动nginx(如果之前启动了kill停掉再启动)
    /usr/local/nginx/sbin/nginx
     
    5、访问成功
      
     
    ps:
    遇到的问题访问时报错,/usr/local/nginx/logs/error.log
    2017/12/25 17:06:52 [error] 2086#0: *12 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:9090", host: "192.168.199.212"
    先尝试把djnago项目目录加权限 chown www:www /home/code/Ticket_system
    发现还是不行 后来把项目的上级目录也加www权限 chown www:www /home/code就可以了
    --end--
     
     
     
     
     
     
     

  • 相关阅读:
    CustomDrawableTextView
    Snippet: align a TextView around an image
    How to import library ?
    Gradle自定义你的BuildConfig
    使用adb shell dumpsys检测Android的Activity任务栈
    Activity intent经常使用的 FLAG
    使用 ContentProviderOperation 来提升性能
    幻方算法
    自己制作的粉碎机批处理程序
    Ubuntu 16.10 server 相关
  • 原文地址:https://www.cnblogs.com/zhangmeixia/p/8109986.html
Copyright © 2020-2023  润新知