1. 程式人生 > >centos7下部署Django(nginx+uWSGI+Python3+Django)

centos7下部署Django(nginx+uWSGI+Python3+Django)

sbin socket `` onf upload SQ fig 目錄 down

部署代碼後uWSGI需要重新啟動,關閉系統防火墻或者開放端口

  • 系統版本:CentOS7.0
  • Python版本:Python3.6.3
  • Django版本:2.0.5
  • uWSGI版本:2.0.17
  • nginx版本1.4.4

    1.安裝需要的依賴
    ```shell
    yum install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

yum install libxml*

yum -y install gcc automake autoconf libtool make gcc-c++ glibc libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel pcre pcre-devel libmcrypt libmcrypt-devel cmake
```
安裝libxml模塊是為了讓uwsig支持使用“-x"選項,能通過xml文件啟動項目

2.編譯安裝python3

進入目錄,依次執行以下命令:

wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz
#下載完成後,執行解壓命令:
tar -zxvf Python-3.6.3.tar.gz
cd Python-3.6.3
#將python3安裝到/usr/local/python3/路徑下
./configure --prefix=/usr/local/python3
make && make install
#創建軟連接
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

技術分享圖片

3.安裝Django和uWSGI配置啟動項目xml文件
pip3 install django
pip3 install uwsgi

創建uWSGI軟連接:

ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi3

將你的Django項目放到你想放的路徑下,並在項目裏新建 django_study.xml,內容如下:

<uwsgi>
    <!-- 內部端口,自定義 -->
    <socket>127.0.0.1:8000</socket>
    <!-- 項目路徑 -->
    <chdir>
項目的路徑</chdir> <module>django_study.wsgi</module> <!-- 進程數 --> <processes>4</processes> <!-- 日誌文件 --> <daemonize>uwsgi.log</daemonize> </uwsgi>
4.安裝nginx和配置nginx.conf文件
  • 安裝nginx

    wget http://nginx.org/download/nginx-1.4.4.tar.gz /usr/local/src
    cd /usr/local/src
    tar xf nginx-1.4.4.tar.gz
    cd nginx-1.4.4
    創建用戶www和用戶組www
    groupadd www
    useradd -g www www
    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-http_gzip_static_module --with-ipv6
    make && make install
  • 配置nginx

    server {
    #暴露給外部訪問的端口
    listen 80; 
    server_name localhost;
    charset utf-8;
    location / {
        include uwsgi_params;
        #外部訪問80就轉發到內部8000
        uwsgi_pass 127.0.0.1:8000; 
    }
    }
    #檢查配置文件是否有誤
    /usr/local/nginx/sbin/nginx -t

    提示nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful的話說明配置文件無誤,啟動nginx
    /usr/local/nginx/sbin/nginx

    5.訪問項目頁面

    進入Django項目路徑,執行以下命令:

    uwsgi3 -x django_study.xml

    技術分享圖片

centos7下部署Django(nginx+uWSGI+Python3+Django)