1. 程式人生 > >linux下搭建nginx+uwsgi+django環境(一)

linux下搭建nginx+uwsgi+django環境(一)

nginx是什麼?

uwsgi是什麼?

django又是什麼?

不做過多的解釋,有不明白的朋友們自行學習吧。此篇只講環境搭建及解決環境搭建過程中遇到的問題。

作業系統:centos 7

登入使用者:root

第一步,安裝python

    本例中使用的是django2.0,而django2.0不支援python2,所以只能安裝python3了,本例以python3.6.5為例。

    1、安裝python3的依賴包openssl-devel

               python3的pip3需要ssl,如果不安裝openssl-devel依賴包,則pip3無法執行成功。

                在控制檯執行命令:

              yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel gcc gcc-c++  openssl-devel libffi-devel   -y

                執行結果如下:                                       


  Verifying  : e2fsprogs-libs-1.42.9-12.el7_5.x86_64                                                                                                                13/39
  Verifying  : systemd-libs-219-57.el7_5.1.x86_64                                                                                                                   14/39
  Verifying  : lz4-1.7.5-2.el7.x86_64                                                                                                                               15/39
  Verifying  : libsepol-devel-2.5-8.1.el7.x86_64                                                                                                                    16/39
  Verifying  : krb5-libs-1.15.1-19.el7.x86_64                                                                                                                       17/39
  Verifying  : libsepol-2.5-8.1.el7.x86_64                                                                                                                          18/39
  Verifying  : libcom_err-1.42.9-12.el7_5.x86_64                                                                                                                    19/39
  Verifying  : systemd-sysv-219-57.el7_5.1.x86_64                                                                                                                   20/39
  Verifying  : libselinux-2.5-12.el7.x86_64                                                                                                                         21/39
  Verifying  : libselinux-utils-2.5-12.el7.x86_64                                                                                                                   22/39
  Verifying  : libkadm5-1.15.1-19.el7.x86_64                                                                                                                        23/39
  Verifying  : 1:openssl-1.0.2k-12.el7.x86_64                                                                                                                       24/39
  Verifying  : libselinux-utils-2.2.2-6.el7.x86_64                                                                                                                  25/39
  Verifying  : libcom_err-1.42.9-7.el7.x86_64                                                                                                                       26/39
  Verifying  : systemd-219-19.el7_2.11.x86_64                                                                                                                       27/39
  Verifying  : 1:openssl-libs-1.0.1e-51.el7_2.5.x86_64                                                                                                              28/39
  Verifying  : libgudev1-219-19.el7_2.11.x86_64                                                                                                                     29/39
  Verifying  : systemd-libs-219-19.el7_2.11.x86_64                                                                                                                  30/39
  Verifying  : e2fsprogs-1.42.9-7.el7.x86_64                                                                                                                        31/39
  Verifying  : e2fsprogs-libs-1.42.9-7.el7.x86_64                                                                                                                   32/39
  Verifying  : libsepol-2.1.9-3.el7.x86_64                                                                                                                          33/39
  Verifying  : 1:openssl-1.0.1e-51.el7_2.5.x86_64                                                                                                                   34/39
  Verifying  : libselinux-2.2.2-6.el7.x86_64                                                                                                                        35/39
  Verifying  : systemd-sysv-219-19.el7_2.11.x86_64                                                                                                                  36/39
  Verifying  : libselinux-python-2.2.2-6.el7.x86_64                                                                                                                 37/39
  Verifying  : libss-1.42.9-7.el7.x86_64                                                                                                                            38/39
  Verifying  : krb5-libs-1.13.2-12.el7_2.x86_64                                                                                                                     39/39

Installed:
  openssl-devel.x86_64 1:1.0.2k-12.el7

Dependency Installed:
  keyutils-libs-devel.x86_64 0:1.5.8-3.el7     krb5-devel.x86_64 0:1.15.1-19.el7       libcom_err-devel.x86_64 0:1.42.9-12.el7_5     libkadm5.x86_64 0:1.15.1-19.el7
  libselinux-devel.x86_64 0:2.5-12.el7         libsepol-devel.x86_64 0:2.5-8.1.el7     libverto-devel.x86_64 0:0.2.5-4.el7           lz4.x86_64 0:1.7.5-2.el7

Updated:
  systemd.x86_64 0:219-57.el7_5.1

Dependency Updated:
  e2fsprogs.x86_64 0:1.42.9-12.el7_5       e2fsprogs-libs.x86_64 0:1.42.9-12.el7_5     krb5-libs.x86_64 0:1.15.1-19.el7          libcom_err.x86_64 0:1.42.9-12.el7_5
  libgudev1.x86_64 0:219-57.el7_5.1        libselinux.x86_64 0:2.5-12.el7              libselinux-python.x86_64 0:2.5-12.el7     libselinux-utils.x86_64 0:2.5-12.el7
  libsepol.x86_64 0:2.5-8.1.el7            libss.x86_64 0:1.42.9-12.el7_5              openssl.x86_64 1:1.0.2k-12.el7            openssl-libs.x86_64 1:1.0.2k-12.el7
  systemd-libs.x86_64 0:219-57.el7_5.1     systemd-sysv.x86_64 0:219-57.el7_5.1

Complete!

       說明openssl-devel已經安裝成功了,一般情況下,只要yum 源配置好了,登入使用者有許可權,就可以安裝成功。

    2、安裝python3.6.5

             2.2、解壓.tar.xz包(需要預先安裝xz工具):tar -xJvf Python-3.6.5.tar.xz

                     切換到解壓後的目錄:cd Python3.6.5

             2.3、執行配置命令:./configure --prefix=/usr --enable-optimizations                        

checking for %zd printf() format support... yes
checking for socklen_t... yes
checking for broken mbstowcs... no
checking for --with-computed-gotos... no value specified
checking whether gcc -pthread supports computed gotos... yes
checking for build directories... done
checking for -O2... yes
checking for glibc _FORTIFY_SOURCE/memmove bug... no
checking for gcc ipa-pure-const bug... no
checking for stdatomic.h... no
checking for GCC >= 4.7 __atomic builtins... yes
checking for ensurepip... upgrade
checking if the dirent structure of a d_type field... yes
checking for the Linux getrandom() syscall... yes
checking for the getrandom() function... no
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile

       當看到"creating Makefile"的時候,配置就成功了,在當前目錄已經生成了Makefile檔案

            2.4、編譯並安裝:make -j4 && make install

                     說明:1、-j4指示make在編譯的時候使用4執行緒(編譯速度比make要快很多),如果你的cpu支援8核,則可使用make -j8 &&make install

                                 2、這是一個比較漫長的過程,因為編譯完成後執行了單元測試,一共有406個單元測試

(cd /usr/lib/pkgconfig; ln -s python-3.6.pc python3.pc)
rm -f /usr/bin/idle3
(cd /usr/bin; ln -s idle3.6 idle3)
rm -f /usr/bin/pydoc3
(cd /usr/bin; ln -s pydoc3.6 pydoc3)
rm -f /usr/bin/2to3
(cd /usr/bin; ln -s 2to3-3.6 2to3)
rm -f /usr/bin/pyvenv
(cd /usr/bin; ln -s pyvenv-3.6 pyvenv)
if test "x" != "x" ; then \
        rm -f /usr/bin/python3-32; \
        (cd /usr/bin; ln -s python3.6-32 python3-32) \
fi
rm -f /usr/share/man/man1/python3.1
(cd /usr/share/man/man1; ln -s python3.6.1 python3.1)
if test "xupgrade" != "xno"  ; then \
        case upgrade in \
                upgrade) ensurepip="--upgrade" ;; \
                install|*) ensurepip="" ;; \
        esac; \
         ./python -E -m ensurepip \
                $ensurepip --root=/ ; \
fi
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.3 setuptools-39.0.1

           最終,成功安裝。

    3、進行簡單的測試

              3.1、執行命令“python3 --version”來驗證安裝的正確性,如果安裝正確,則會在控制檯輸出:Python 3.6.5

              3.2、執行命令“pip3 search django"來驗證pip3能否正常工作。

      注:安裝python3的時候,pip3也會捎帶著安裝到系統中,所以不需要額外的安裝pip3了。                 

第二步,安裝python虛擬環境——virtualenv

            此步不是必須的,如果你的環境安裝了多個版本的python環境(python2.7、python3.4、python3.6),則需要隔離不同python版本的依賴包,否則會出問題,此時就需要安裝virtualenv虛擬環境了,如下:

             1、執行安裝命令:pip3 install virtualenv

Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl (1.9MB)
    100% |████████████████████████████████| 1.9MB 287kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.0.0

               安裝成功。

             2、測試是否安裝成功

                  2.1、建立一個工程目錄:mkdir rms

                 2.2、在test目錄,建立一個獨立的python執行環境:virtualenv --no-site-packages env

[[email protected] rms]# virtualenv --no-site-packages env
Using base prefix '/usr'
New python executable in /root/download/test/env/bin/python3.6
Also creating executable in /root/download/test/env/bin/python
Installing setuptools, pip, wheel...done.

             2.3、進入python虛擬環境:source venv/bin/activate

[[email protected] rms]# source env/bin/activate
(env) [[email protected] test]#

             注意:[[email protected] test]前面的“(env)”,這程式碼已經進入到python的虛擬環境了。

            2.4、離開虛擬環境:deactivate

           如果測試均成功,則完成了virtualenv的安裝。

第三步,安裝django及django需要的元件

          1、首選,安裝django框架依賴的mysqlclient的依賴包:

               yum install python-devel -y              #安裝python開發工具包

               yum install mysql-devel -y               #安裝mysql開發工具包

               yum install gcc -y                            #安裝gcc開發工具包

             當然,也可以使用一條命令完成三個庫的安裝:yum install python-devel mysql-devel gcc -y

           2、使用vi或vim建立requirements.txt,然後輸入以下內容:

                Django==2.0
                mysqlclient==1.3.13
                pytz==2018.5
                uwsgi

                並儲存。主要是安裝django2.0、mysqlclient1.3.13、pytz2018.5和uwsgi等

        3、進入rms的virtualenv虛擬環境,執行命令:

                    pip install -r requirements.txt

                安裝requirements.txt檔案中指定相應版本的依賴包                

(env) [[email protected] rms]# pip install -r requirements.txt
Collecting Django==2.0 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/44/98/35b935a98a17e9a188efc2d53fc51ae0c8bf498a77bc224f9321ae5d111c/Django-2.0-py3-none-any.whl (7.1MB)
    100% |████████████████████████████████| 7.1MB 283kB/s
Collecting mysqlclient==1.3.13 (from -r requirements.txt (line 2))
  Downloading https://files.pythonhosted.org/packages/ec/fd/83329b9d3e14f7344d1cb31f128e6dbba70c5975c9e57896815dbb1988ad/mysqlclient-1.3.13.tar.gz (90kB)
    100% |████████████████████████████████| 92kB 251kB/s
Collecting pytz==2018.5 (from -r requirements.txt (line 3))
  Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
    100% |████████████████████████████████| 512kB 282kB/s
Collecting uwsgi (from -r requirements.txt (line 4))
  Downloading https://files.pythonhosted.org/packages/a2/c9/a2d5737f63cd9df4317a4acc15d1ddf4952e28398601d8d7d706c16381e0/uwsgi-2.0.17.1.tar.gz (800kB)
    100% |████████████████████████████████| 808kB 316kB/s
Building wheels for collected packages: mysqlclient, uwsgi
  Running setup.py bdist_wheel for mysqlclient ... done
  Stored in directory: /root/.cache/pip/wheels/71/9b/6f/a8c79bceb4a35f4acf49363883f958f31b113470a3b504df87
  Running setup.py bdist_wheel for uwsgi ... done
  Stored in directory: /root/.cache/pip/wheels/32/d6/90/0239cc69219013d9f402b098b7c5ef7454792c21acd1d6c24e
Successfully built mysqlclient uwsgi
Installing collected packages: pytz, Django, mysqlclient, uwsgi
Successfully installed Django-2.0 mysqlclient-1.3.13 pytz-2018.5 uwsgi-2.0.17.1

      4、測試是否安裝成功:

            在虛擬環境,執行python,執行如下命令:                 

(env) [[email protected] rms]# python
Python 3.6.5 (default, Sep 17 2018, 15:01:45)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(2, 0, 0, 'final', 0)
>>>

           能夠匯入django並顯示django的版本,則證明django已經安裝成功。

           至此,django和uwsgi已經成功安裝了,下一篇講解nginx的安裝及nginx+uwsgi+django的配置。