CentOS7下的Django2集成部署二:Nginx1.14.2Mysql5.7和Python3.7的安装

Posted zhujingxiu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7下的Django2集成部署二:Nginx1.14.2Mysql5.7和Python3.7的安装相关的知识,希望对你有一定的参考价值。

nginx

  •   安装依赖
    yum install -y gcc pcre-devel zlib zlib-devel
  •   默认安装
    rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    yum install -y nginx
  •   配置
    [[email protected] ~]# nginx -v
    nginx version: nginx/1.14.2
    [[email protected]-ct76211 ~]# systemctl enable nginx
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
    [[email protected]-ct76211 ~]# systemctl start nginx
    [[email protected]-ct76211 ~]# lsof -i :80
    COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    nginx   11227  root    6u  IPv4  34477      0t0  TCP *:http (LISTEN)
    nginx   11228 nginx    6u  IPv4  34477      0t0  TCP *:http (LISTEN)
  •   调试
    [[email protected] ~]# elinks http://localhost --dump
                                   Welcome to nginx!
    
       If you see this page, the nginx web server is successfully installed and
       working. Further configuration is required.
    
       For online documentation and support please refer to [1]nginx.org.
       Commercial support is available at [2]nginx.com.
    
       Thank you for using nginx.
    
    References
    
       Visible links
       1. http://nginx.org/
       2. http://nginx.com/

    此刻nginx基本上就已经可以正常工作了

mysql

  •   安装依赖
    yum -y install ncurses-devel gcc-* bzip2-*
  •   编译安装
    • cmake
      #获取cmake
      wget https://github.com/Kitware/CMake/releases/download/v3.13.1/cmake-3.13.1.tar.gz
      #解压
      tar xf cmake-3.13.1.tar.gz
      #进入程序目录
      cd cmake-3.13.1
      #编译并安装
      ./configuare
      
      make
      
      make install
    • boost
      #获取boost1.59
      wget https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.bz2
      #解压
      tar xf boost_1_59_0.tar.bz2
      #移动目录给mysql编译使用
      mv boost_1_59_0 /usr/local/boost
    • mysql
      #获取mysql
      wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24.tar.gz
      tar xf
      mysql-5.7.24.tar.gz
      cd mysql-5.7.24

      用cmake编译,耗时有点久编译安装差不多1小时左右

      cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data/ -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDOWNLOAD_BOOST=0 -DWITH_INNODBBASE_STORAGE_ENGINE=1 -DENABLE_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DWITH_EMBEDED_SERVER=0 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost


      # -DMYSQL_USER=mysql 稍后新建mysql用户
      make && make install

       

  •   配置
    • 添加启动文件
      cp support-files/mysql.server /etc/init.d/mysql
      #授权
      chmod 755 /etc/init.d/mysql
    • 用户配置
      #添加用户
      useradd -s /sbin/nologin -r mysql
      #用户授权
      chown mysql.mysql /usr/local/mysql/ -R
    • 设置链接文件
      ln -sf /usr/local/mysql/bin/* /usr/bin/
      ln -sf /usr/local/mysql/lib/* /usr/lib/
      ln -sf /usr/local/mysql/libexec/* /usr/local/libexec
      ln -sf /usr/local/mysql/share/man/man1/* /usr/share/man/man1
      ln -sf /usr/local/mysql/share/man/man8/* /usr/share/man/man8
    • 配置文件
      #数据目录
      

      [[email protected] mysql-5.7.24]# mkdir -pv /usr/local/mysql/data
      mkdir: created directory ‘/usr/local/mysql/data’

       配置/etc/my.cnf

      技术分享图片
       1 [mysqld]
       2 datadir=/usr/local/mysql/data
       3 socket=/usr/local/mysql/mysql.sock
       4 # Disabling symbolic-links is recommended to prevent assorted security risks
       5 symbolic-links=0
       6 # Settings user and group are ignored when systemd is used.
       7 # If you need to run mysqld under a different user or group,
       8 # customize your systemd unit file for mariadb according to the
       9 # instructions in http://fedoraproject.org/wiki/Systemd
      10 
      11 [mysqld_safe]
      12 log-error=/var/log/mysql.log
      13 pid-file=/var/run/mysql.pid
      14 
      15 #
      16 # include all files from the config directory
      17 #
      18 !includedir /etc/my.cnf.d
      vim /etc/my.cnf

       

    • 生成root密码
      #生成默认密码
      /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

      启动MySQL

      /etc/init.d/mysql start

      设置root密码

      mysql_secure_installation
      技术分享图片
       1 [[email protected] mysql-5.7.24]# mysql_secure_installation
       2 
       3 Securing the MySQL server deployment.
       4 
       5 Enter password for user root: 
       6 
       7 The existing password for the user account root has expired. Please set a new password.
       8 
       9 New password: 
      10 
      11 Re-enter new password: 
      12 
      13 VALIDATE PASSWORD PLUGIN can be used to test passwords
      14 and improve security. It checks the strength of password
      15 and allows the users to set only those passwords which are
      16 secure enough. Would you like to setup VALIDATE PASSWORD plugin?
      17 
      18 Press y|Y for Yes, any other key for No: n
      19 Using existing password for root.
      20 Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
      21 
      22 New password: 
      23 
      24 Re-enter new password: 
      25 By default, a MySQL installation has an anonymous user,
      26 allowing anyone to log into MySQL without having to have
      27 a user account created for them. This is intended only for
      28 testing, and to make the installation go a bit smoother.
      29 You should remove them before moving into a production
      30 environment.
      31 
      32 Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
      33 Success.
      34 
      35 
      36 Normally, root should only be allowed to connect from
      37 localhost. This ensures that someone cannot guess at
      38 the root password from the network.
      39 
      40 Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
      41 Success.
      42 
      43 By default, MySQL comes with a database named test that
      44 anyone can access. This is also intended only for testing,
      45 and should be removed before moving into a production
      46 environment.
      47 
      48 
      49 Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
      50  - Dropping test database...
      51 Success.
      52 
      53  - Removing privileges on test database...
      54 Success.
      55 
      56 Reloading the privilege tables will ensure that all changes
      57 made so far will take effect immediately.
      58 
      59 Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
      60 Success.
      61 
      62 All done! 
      View Code
  •   调试
    技术分享图片
     1 [[email protected] mysql-5.7.24]# mysql -u root -p
     2 Enter password: 
     3 Welcome to the MySQL monitor.  Commands end with ; or g.
     4 Your MySQL connection id is 5
     5 Server version: 5.7.24 Source distribution
     6 
     7 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
     8 
     9 Oracle is a registered trademark of Oracle Corporation and/or its
    10 affiliates. Other names may be trademarks of their respective
    11 owners.
    12 
    13 Type help; or h for help. Type c to clear the current input statement.
    14 
    15 mysql> show databases;
    16 +--------------------+
    17 | Database           |
    18 +--------------------+
    19 | information_schema |
    20 | mysql              |
    21 | performance_schema |
    22 | sys                |
    23 +--------------------+
    24 4 rows in set (0.00 sec)
    25 
    26 mysql> 
    mysql -u root -p

python

  •   安装依赖
    yum install -y gcc-* openssl-* libffi-devel sqlite-devel
  •   下载安装
    wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
    #解压
    tar xf Python-3.7.1.tar.xz
    #进入目录
    cd Python-3.7.1
    #配置
    ./configure --enable-optimizations --with-openssl=/usr/bin/openssl
    #编译安装
    make -j4
    make install
  •   调试

    ... ...

    Collecting setuptools
    Collecting pip
    Installing collected packages: setuptools, pip
    Successfully installed pip-10.0.1 setuptools-39.0.1
    [[email protected] Python-3.7.1]# python3
    Python 3.7.1 (default, Dec 14 2018, 11:39:37)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>

  •   升级pip
    技术分享图片
     1 [[email protected] ~]# pip3 install --upgrade pip
     2 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
     3 Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by SSLError("Cant connect to HTTPS URL because the SSL module is not available.")‘: /simple/pip/
     4 Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by SSLError("Cant connect to HTTPS URL because the SSL module is not available.")‘: /simple/pip/
     5 Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by SSLError("Cant connect to HTTPS URL because the SSL module is not available.")‘: /simple/pip/
     6 Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by SSLError("Cant connect to HTTPS URL because the SSL module is not available.")‘: /simple/pip/
     7 Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by SSLError("Cant connect to HTTPS URL because the SSL module is not available.")‘: /simple/pip/
     8 Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=‘pypi.org‘, port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")) - skipping
     9 Requirement already up-to-date: pip in /usr/local/lib/python3.7/site-packages (10.0.1)
    10 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
    11 Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=‘pypi.org‘, port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can‘t connect to HTTPS URL because the SSL module is not available.")) - skipping
    pip3 install --upgrade pip

    其实在编译的时候已经有提示了,下面把Module/Setup重新编辑下

    [[email protected] Python-3.7.1]# vim Modules/Setup
    #把下边这段话的#去掉
    211 SSL=/usr/local/ssl 212 _ssl _ssl.c 213 -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl 214 -L$(SSL)/lib -lssl -lcrypto

    再次  make -j4 && make install ,然后再升级pip

    技术分享图片
    [[email protected] Python-3.7.1]# pip3 install --upgrade pip
    Collecting pip
    ... ...
    Installing collected packages: pip
      Found existing installation: pip 10.0.1
        Uninstalling pip-10.0.1:
          Successfully uninstalled pip-10.0.1
    Successfully installed pip-18.1
    pip3 install --upgrade pip
  •        virtualenv
    技术分享图片
    [[email protected] Python-3.7.1]# pip3 install virtualenv
    Collecting virtualenv
      Downloading 
    ... ...
    Installing collected packages: virtualenv
    Successfully installed virtualenv-16.1.0
    pip3 install virtualenv
    #创建py3web虚拟环境
    [[email protected]-ct75211 ~]# virtualenv -p python3 py3web
    Running virtualenv with interpreter /usr/local/bin/python3
    Using base prefix /usr/local
    New python executable in /root/py3web/bin/python3
    Also creating executable in /root/py3web/bin/python
    Installing setuptools, pip, wheel...
    done.
    #进入py3web虚拟环境,顺便安装django2
    [[email protected]-ct75211 ~]# source py3web/bin/activate
    (py3web) [[email protected]-ct75211 ~]# pip3 install django==2.*
    ... ...
    Installing collected packages: pytz, django
    Successfully installed django-2.1.4 pytz-2018.7
    #退出虚拟环境
    (py3web) [[email protected]-ct75211 ~]# deactivate
    [[email protected] ~]# 
  •        uwsgi
    • pip安装
      [[email protected] ~]# pip3 install uwsgi
      Collecting uwsgi
        Downloading 
      ... ...
      Installing collected packages: uwsgi
        Running setup.py install for uwsgi ... done
      Successfully installed uwsgi-2.0.17.1

       

    • 制作启动脚本
      [[email protected] ~]# mkdir /etc/uwsgi
      [[email protected]-ct75211 ~]# vim /etc/uwsgi/uwsgi.ini
      技术分享图片
      [uwsgi]
      uid = root
      gid = root
      socket = 127.0.0.1:9090
      # 启动主进程
      master = true 
      # 多站模式
      vhost = true 
      # 多站模式时不设置??模块和?件
      no-site = true 
      # ?进程数
      workers = 2
      # 平滑的重启
      reload-mercy = 10
       # 退出、重启时清理?件
      vacuum = true
      # 开启10000个进程后, ?动respawn下
      max-requests = 1000 
      # 将进程的总内存量控制在512M
      limit-as = 512 
      buffer-size = 30000
      # pid?件,?于下?的脚本启动、停?该进程
      pidfile = /var/run/uwsgi9090.pid 
      daemonize = /var/log/uwsgi9090.log
      /etc/uwsgi/uwsgi.ini
      # 启动
      [[email protected]-ct75211 ~]# uwsgi --ini /etc/uwsgi/uwsgi.ini
      [uWSGI] getting INI configuration from /etc/uwsgi/uwsgi.ini
      # 关闭
      [[email protected]-ct75211 ~]# cat /var/run/uwsgi9090.pid 
      26516
      [[email protected]-ct75211 ~]# kill -9 26516

      加入脚本管理

      技术分享图片
       1 #!/bin/sh
       2   DESC="uwsgi daemon"
       3   NAME=uwsgi
       4   DAEMON=/usr/local/bin/uwsgi
       5   CONFIGFILE=/etc/uwsgi/$NAME.ini
       6   PIDFILE=/var/run/${NAME}9090.pid
       7   SCRIPTNAME=/etc/init.d/$NAME
       8   FIFOFILE=/tmp/uwsgififo
       9   set -e
      10   [ -x "$DAEMON" ] || exit 0
      11 
      12   do_start() {
      13   if [ ! -f $PIDFILE ];then
      14       $DAEMON $CONFIGFILE || echo -n "uwsgi  running"
      15   else
      16       echo "The PID is exist..."
      17   fi
      18   }
      19 
      20   do_stop() {
      21   if [ -f $PIDFILE ];then
      22       $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
      23       rm -f $PIDFILE
      24       echo "$DAEMON STOPED."
      25   else
      26       echo "The $PIDFILE doesn‘t found"
      27   fi
      28   }
      29 
      30   do_reload() {
      31   if [ -p $FIFOFILE ];then
      32       echo w > $FIFOFILE
      33   else
      34       $DAEMON --touch-workers-reload $PIDFILE || echo -n "uwsgi can‘t reload"
      35   fi
      36   }
      37 
      38   do_status() {
      39       ps aux|grep $DAEMON
      40   }
      41 
      42   case "$1" in
      43   status)
      44       echo -en "Status $NAME: 
      "
      45       do_status
      46   ;;
      47   start)
      48       echo -en "Starting $NAME: 
      "
      49       do_start
      50   ;;
      51   stop)
      52       echo -en "Stopping $NAME: 
      "
      53       do_stop
      54   ;;  
      55   reload|graceful)
      56       echo -en "Reloading $NAME: 
      "
      57       do_reload
      58   ;;
      59   *)
      60       echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
      61       exit 3
      62   ;;
      63   esac
      64   exit 0
      vim /etc/init.d/uwsgi
      #授权
      [[email protected]-ct75211 html]# chmod 755 /etc/init.d/uwsgi
      
      #关闭
      [[email protected]-ct75211 html]# /etc/init.d/uwsgi stop
      Stopping uwsgi: 
      /usr/local/bin/uwsgi STOPED.
      #启动
      [[email protected]-ct75211 html]# /etc/init.d/uwsgi start
      Starting uwsgi: 
      [uWSGI] getting INI configuration from /etc/uwsgi/uwsgi.ini
      #查看
      [[email protected]-ct75211 html]# netstat -ntpl
      Active Internet connections (only servers)
      Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
      tcp        0      0 127.0.0.1:9090          0.0.0.0:*               LISTEN      26572/uwsgi         
      tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      26560/nginx: master 
      tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1081/sshd           
      tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1380/master         
      tcp6       0      0 :::3306                 :::*                    LISTEN      2425/mysqld         
      tcp6       0      0 :::22                   :::*                    LISTEN      1081/sshd           
      tcp6       0      0 ::1:25                  :::*                    LISTEN      1380/master   

       

    • 整合Nginx测试
      #进入虚拟环境
      [[email protected]-ct75211 ~]# source ~/py3web/bin/activate
      #创建my_django项目
      
      (py3web) [[email protected]-ct75211 ~]# django-admin.py startproject my_django
      (py3web) [[email protected]-ct75211 ~]# cd my_django
      (py3web) [[email protected]-ct75211 my_django]# ll
        total 4
        -rw-r--r--. 1 root root 0 Dec 14 21:37 db.sqlite3
        -rwxr-xr-x. 1 root root 541 Dec 14 21:35 manage.py
        drwxr-xr-x. 3 root root 93 Dec 14 21:37 my_django
      # 用Django的内置web服务访问下
      (py3web) [[email protected]-ct75211 my_django]# python manage.py runserver 192.168.23.211:8000
      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.
      
      December 15, 2018 - 02:46:48
      Django version 2.1.4, using settings my_django.settings
      Starting development server at http://192.168.23.211:8000/
      [[email protected] ~]# elinks http://192.168.23.211:8000/ --dump
                                    DisallowedHost at /
      
       Invalid HTTP_HOST header: 192.168.23.211:8000. You may need to add 192.168.23.211 to ALLOWED_HOSTS.
      。。。
      需要ALLOWED_HOSTS加入* vim my_django/settings.py  ALLOWED_HOSTS = [*] ,编辑后再访问下,就可以了
      技术分享图片
      [[email protected] ~]# elinks http://192.168.23.211:8000/ --dump
      [1]django
      
         View [2]release notes for Django 2.1
      
      The install worked successfully! Congratulations!
      
         You are seeing this page because [3]DEBUG=True is in your settings file
         and you have not configured any URLs.
      
          [4]Django Documentation
      
         Topics, references, & how-tos
      
          [5]Tutorial: A Polling App
      
         Get started with Django
      
          [6]Django Community
      
         Connect, get help, or contribute
      elinks http://192.168.23.211:8000/ --dump
      # 用Nginx来访问
      (py3web) [[email protected] ~]# mv my_django /usr/share/nginx/html
      (py3web) [[email protected] ~]# vim /etc/nginx/conf.d/my_django.conf
      技术分享图片
       1 server {
       2         listen       80;
       3         server_name  www.my-django.cc;
       4 
       5         #charset koi8-r;
       6 
       7         #access_log  logs/host.access.log  main;
       8 
       9         location / {
      10             include  uwsgi_params;
      11             uwsgi_pass  127.0.0.1:9090;
      12             uwsgi_param UWSGI_SCRIPT my_django.wsgi;
      13             uwsgi_param UWSGI_CHDIR  /usr/share/nginx/html/my_django;
      14             index  index.html index.htm;
      15             client_max_body_size 35m;
      16             #uwsgi_cache_valid 1m;
      17             #uwsgi_temp_file_write_size 64k;
      18             #uwsgi_busy_buffers_size 64k;
      19             #uwsgi_buffers 8 64k;
      20             #uwsgi_buffer_size 64k;
      21             #uwsgi_read_timeout 300;
      22             #uwsgi_send_timeout 300;
      23             #uwsgi_connect_timeout 300;
      24         }
      25 
      26         #error_page  404              /404.html;
      27 
      28         # redirect server error pages to the static page /50x.html
      29         #
      30         error_page   500 502 503 504  /50x.html;
      31         location = /50x.html {
      32             root   html;
      33         }
      34 
      35 
      36     }
      /etc/nginx/conf.d/my_django.conf

       

       (py3web) [[email protected] ~]# systemctl restart nginx

      配置本地域名

      技术分享图片
      1 (py3web) [[email protected] ~]# vim /etc/hosts
      2 
      3 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
      4 ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
      5 127.0.0.1       www.my-django.cc
      6 127.0.0.1       www.my-nginx.cc
      7 127.0.0.1       www.my-blog.cc
      (py3web) [roo[email protected] ~]# vim /etc/hosts

      由于这里使用的是virtualenv环境,在uwsgi的配置文件中还要加入虚拟环境的pythonpath

      技术分享图片
      (py3web) [[email protected] ~]# vim /etc/uwsgi/uwsgi.ini
      # 最后的文件内容
      (py3web) [[email protected] ~]# vim /etc/uwsgi/uwsgi.ini

       

               重启uwsgi服务

[[email protected] ~]# /etc/init.d/uwsgi stop
Stopping uwsgi: 
/usr/local/bin/uwsgi STOPED.
[[email protected]-ct75211 ~]# /etc/init.d/uwsgi start
Starting uwsgi: 
[uWSGI] getting INI configuration from /etc/uwsgi/uwsgi.ini
[[email protected]-ct75211 ~]# elinks http://www.my-django.cc --dump
[1]django

   View [2]release notes for Django 2.1

The install worked successfully! Congratulations!

   You are seeing this page because [3]DEBUG=True is in your settings file
   and you have not configured any URLs.

    [4]Django Documentation

   Topics, references, & how-tos

    [5]Tutorial: A Polling App

   Get started with Django

    [6]Django Community

   Connect, get help, or contribute

References

   Visible links
   1. https://www.djangoproject.com/
   2. https://docs.djangoproject.com/en/2.1/releases/
   3. https://docs.djangoproject.com/en/2.1/ref/settings/#debug
   4. https://docs.djangoproject.com/en/2.1/
   5. https://docs.djangoproject.com/en/2.1/intro/tutorial01/
   6. https://www.djangoproject.com/community/

 

       大功告成!!!


















以上是关于CentOS7下的Django2集成部署二:Nginx1.14.2Mysql5.7和Python3.7的安装的主要内容,如果未能解决你的问题,请参考以下文章

阿里云centos7.3部署django2

部署CentOS7+Python3+Django2+UWSGI2+Nginx生产环境(实测)

CentOS 7环境部署——Django

在阿里云Centos7.6中部署nginx1.16+uwsgi2.0.18+Django2.0.4

CentOS7.2 LNMP安装部署zabbix3.2

Centos7下部署Nginx