1. 程式人生 > >【Django2x 學習筆記 04】centos7 部署Python3.7x+Django2x+nginx+uwsgi

【Django2x 學習筆記 04】centos7 部署Python3.7x+Django2x+nginx+uwsgi

# 安裝Python3.7.1和Django:

yum install gcc-c++

yum install libxml*

yum install wget

python的安裝依賴包:

yum groupinstall "Development tools"  

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

安裝Python3x:

官方Python下載地址:https://www.python.org/downloads/release/python-371/

wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz

tar -zxvf Python-3.7.1.tgz

-

進入python3.7.1資料夾:

cd Python-3.7.1

./configure

3.7版本需要一個新的包libffi-devel,安裝此包之後再次進行編譯安裝即可 :

yum install libffi-devel -y

編譯:

make && make install  

移除python的安裝依賴包:

yum groupremove "Development tools"
yum remove zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

檢視版本:

python3 -V

python -V

鏈路python3:

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

 鏈路python2.7:

mv /usr/bin/python /usr/bin/python_bak

ln -s /usr/local/bin/python3 /usr/bin/python

鏈路pip3:
ln -s /usr/local/bin/pip3 /usr/bin/pip3

升級最新版pip:

pip3 install --upgrade pip

-

現在,產生了兩個命令開頭 python3或python 和 pip3或者pip。兩種命令(帶3和不帶3的都用用)都可用。

-

重新鏈路yum命令(安裝鏈路python3的時候破壞了yum):

vi usr/bin/yum

第一行#!/usr/bin/python改成#!/usr/bin/python2.7

按esc退出編輯,鍵入:wq退出並儲存。

vi /usr/libexec/urlgrabber-ext-down

這裡也需要修改成#! /usr/bin/python2.7

不然yum install -y zlib-devel會報錯。

yum install -y zlib-devel

此處如果報錯,請看:centos7升級Python3x後yum install zlib-devel報錯問題 https://blog.csdn.net/weixin_41827162/article/details/84537404

安裝setuptool:

yum install setuptool

安裝Django框架:

pip3 install django

-

檢測Django是否已經安裝:

[[email protected] ~]# python3
Python 3.7.1 (default, Nov 26 2018, 11:35:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
2.1.3
>>>

-

pip3 install uwsgi

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

-

至此,CentOS7+Python3.7x+Django2x環境已經搭建完成。

注意,每個安裝步驟要保證Successful才能最終安裝成功。

 

#2 新建一個Django專案:

注意與win10下新建專案的區別:

新建一個名叫mysite的專案,我讓/home/目錄作為web目錄,你也可以自定義/home/www/作為web目錄:

cd /home/

django-admin.py startproject mysite

[[email protected] /]# cd home
[[email protected] home]# ls
[[email protected] home]# django-admin.py startproject mysite
[[email protected] home]# ls
mysite
[[email protected] home]# ls
mysite
[[email protected] home]# cd mysite
[[email protected] mysite]# ls
manage.py  mysite
[[email protected] mysite]# cd mysite
[[email protected] mysite]# ls
__init__.py  settings.py  urls.py  wsgi.py
[[email protected] mysite]#

可以看到,初始檔案已經生成了。

啟動執行:

cd /home/mysite/

測試啟動(利用自帶服務,還沒有利用nginx):

python3 manage.py runserver 8080

[[email protected] ~]# cd /home/
[[email protected] home]# ls
mysite
[[email protected] home]# cd mysite
[[email protected] mysite]# ls
apptest  db.sqlite3  manage.py  mysite
[[email protected] mysite]# python3 manage.py runserver 8080
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

November 26, 2018 - 04:30:03
Django version 2.1.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CONTROL-C.

-

yum install net-tools

此處如果報錯,請看:centos7升級Python3x後yum install zlib-devel報錯問題 https://blog.csdn.net/weixin_41827162/article/details/84537404

檢視端口占用情況:

netstat -lpnt

-

至此,可以說簡單的Django服務已經搭建好了。

但是還不能經過外網訪問服務埠(因為埠未開放),接下來需要配置nginx服務,轉發代理Django服務。

-

#3 安裝nginx服務:

-

wget http://nginx.org/download/nginx-1.15.5.tar.gz

tar -zxvf nginx-1.15.5.tar.gz

cd nginx-1.15.5

編譯nginx-1.15.5:

./configure
make
make install

此處如果報錯,請看:centos7升級Python3x後yum install zlib-devel報錯問題 https://blog.csdn.net/weixin_41827162/article/details/84537404

-

vi /usr/local/nginx/conf/nginx.conf

加入:

server {
    listen 8091; #暴露給外部訪問的埠
    server_name localhost;
        charset utf-8;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8090; #外部訪問8090就轉發到內部8091
    }
    location /static/ {
        alias /home/mysite/apptest/static/; #專案靜態路徑設定
    }
}

儲存後,啟動nginx:

cd /usr/local/nginx/sbin/

./nginx -t

./nginx

終端沒有任何提示就證明nginx啟動成功。

-

進入你的django專案路徑,

cd /home/mysite/

新增一個叫mysite.xml到檔案:

rouch mysite.xml

vi mysite.xml

填入如下內容:

<uwsgi>
    <socket>127.0.0.1:8090</socket><!-- 內部埠,自定義 -->
        <chdir>/home/mysite</chdir><!-- 專案路徑 -->
            <module>mysite.wsgi</module>
                <processes>4</processes> <!-- 程序數 --> 
    <daemonize>uwsgi.log</daemonize><!-- 日誌檔案 -->
</uwsgi>

 

#4 設定防火牆,配置白名單IP和埠:

請修改:

vi /usr/bin/firewall-cmd

第一行#!/usr/bin/python改成#!/usr/bin/python2.7

vi /usr/sbin/firewalld

第一行#!/usr/bin/python改成#!/usr/bin/python2.7

此處報錯請檢視 https://blog.csdn.net/cenylon/article/details/79954524 

防火牆狀態,新增與刪除埠,請參看:https://www.cnblogs.com/moxiaoan/p/5683743.html

開放外部8091:

firewall-cmd --zone=public --add-port=8091/tcp --permanent

關閉防火牆:

 systemctl stop firewalld

success以後,

vi /home/mysite/mysite/settings.py

ALLOWED_HOSTS = ['*']  #在這裡請求的host添加了*,將開啟所有IP白名單。

重啟nginx。

進入你的django專案路徑,執行以下命令:

cd /home/mysite/
uwsgi -x mysite.xml

或者直接 uwsgi -x /home/mysite/mysite.xml

瀏覽器輸入:http://172.16.156.136:8091/

成功部署。

檢視端口占用情況:

netstat -lpnt

[[email protected] sbin]# netstat -lpnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1352/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1033/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1250/master
tcp        0      0 127.0.0.1:8090          0.0.0.0:*               LISTEN      1342/uwsgi
tcp        0      0 0.0.0.0:8091            0.0.0.0:*               LISTEN      1352/nginx: master
tcp6       0      0 :::22                   :::*                    LISTEN      1033/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1250/master
[[email protected] sbin]#

-

#5 總結下:

1. 當將python2.7鏈路到python3x上以後,需要將

vi usr/bin/yum

vi /usr/libexec/urlgrabber-ext-down

vi /usr/bin/firewall-cmd

vi /usr/sbin/firewalld

第一行#!/usr/bin/python改成#!/usr/bin/python2.7

2. 埠轉發,將uwsgi與nginx聯絡起來。

3. 設定Django框架IP白名單。

-

內容比較多,比較雜,第一次安裝差不多用了4個小時,安裝過程充滿了各種疑惑。還好,除了問題搜了一下都解決了。

配置Django path路由請參考我的博文《Django2x 學習筆記 03】建立path路由url(建立新路由URL,pathinfo模式多個傳參,GET模式多個傳參,返回json)》 https://blog.csdn.net/weixin_41827162/article/details/84500537

win10下部署Django請參考我的博文 《Django2x 學習筆記 02】win10下Django及新專案部署》 https://blog.csdn.net/weixin_41827162/article/details/84025100

-

參考部落格:

https://blog.csdn.net/a249900679/article/details/51527200

https://www.cnblogs.com/levelksk/p/7921066.html