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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lianshaohua/article/details/82735984

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.1、下载python3.6.5压缩包:wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz

             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

[root@localhost 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

[root@localhost rms]# source env/bin/activate
(env) [root@localhost test]#

             注意:[root@localhost 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) [root@localhost 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) [root@localhost 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的配置。

猜你喜欢

转载自blog.csdn.net/lianshaohua/article/details/82735984