部署基於python語言的WEB發佈環境

1、部署說明

一、python語言介紹

  python簡介html

二、實驗環境

  實驗機器:Vmware虛擬機 8核10Gpython

  網卡:橋接模式mysql

  系統:centos7.5linux

  防火牆:關閉nginx

  Selinux:關閉web

  網段:192.168.10.0/25sql

  WEB01:192.168.10.42數據庫

2、部署流程

  Centos7.5 + Nginx + python + Django + uwsgi + mysql來部署網站(服務)。django

一、部署Nginx

$ wget http://nginx.org/download/nginx-1.15.5.tar.gz -P /usr/src     # 下載nginx
$ cd /usr/src
$ tar xvf nginx-1.15.5.tar.gz
$ cd nginx-1.15.5
$ yum -y install gcc            # nginx是c寫的
$ yum -y install pcre-devel   # url重寫用到的包
$ yum -y install zlib  zlib-devel    # 解壓縮用到的包
# 配置安裝環境
$ ./configure  --prefix=/usr/local/nginx

# 編譯源碼生成可執行程序  依賴gcc
$ make  -j4
# 安裝程序
$ make install

# 啓動nginx
$ /usr/local/nginx/sbin/nginx
# 訪問nginx首頁
$ elinks http://192.168.10.42 -dump
# 當前系統監聽tcp端口的進程
$ netstat -ntpl

# 刪除再也不須要的源碼
$ rm -rf nginx-1.15.5

二、Mysql安裝部署

  Mysql是一個關係型數據庫,由瑞典的AB公司開發,後賣給oracle公司,目前分爲商業版和社區版。vim

  如今主要是使用兩大Myql版本:mysql5和mysql8。目前大多數公司使用5版本,所以在這裏使用5.7的最新版本。

# 1.安裝依賴包
$ yum -y install ncurses-devel gcc-* bzip2-* bison

# 2.升級cmake工具
# 軟件獲取:https://cmake.org/download
$ wget https://cmake.org/files/v3.13/cmake-3.13.0-rc2.tar.gz
$ tar xf cmake-3.13.0-rc2.tar.gz   # 解壓
$ ./configure     # 配置
$ make -j4        # 多核編譯減小等待時間
$ make install   # 安裝
# 檢查是否安裝完成
$ cmake --version

# 3. 升級boost庫文件
# boost庫獲取:https://www.boost.org
# 因爲這裏是安裝5.7的mysql,所以下載的是boost_1_59_0.tar.bz2
$ tar xf boost_1_59_0.tar.bz2    # 解壓
$ mv boost_1_59_0   /usr/local/boost

  在上面部署準備完成後,開始執行mysql安裝

# 4.安裝mysql
# 添加用戶與組
$ useradd -s /sbin/nologin -r mysql
$ mkdir -pv /usr/local/mysql/data

# 軟件獲取:https://www.oracle.com=>下載=>myql=>社區版本,此處下載mysql-5.7.24.tar.gz
# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

$ tar xf mysql-5.7.24.tar.gz      # mysql解壓

# 用cmake配置
# 若是配置失敗要從新配置,刪除CMakeCache.txt文件便可
$ cmake . \
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  \
    -DMYSQL_DATADIR=/usr/local/mysql/data/ \
    -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
    -DWITH_INNBASE_STORAGE_EGNINE=1 \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DENABLED_LOCAL_INFILE=1 \
    -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf-8 -DDEFAULT_COLLATION=utf8_general_ci \
    -DWITH_DEBUG=0 \
    -DWITH_EMBEDDED_SERVER=1 \
    -DDOWNLOAD_BOOST=1 -DENABLE_DOWNLOADS=1 -DWITH_BOOST=/usr/local/boost

# 解釋
$ cmake . \
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  \   # 指定安裝路徑
    -DMYSQL_DATADIR=/usr/local/mysql/data/ \    # 指定數據目錄
    -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \   # 指定sock文件路徑
    -DWITH_INNBASE_STORAGE_EGNINE=1 \    # 安裝Innodb存儲引擎
    -DWITH_MYISAM_STORAGE_ENGINE=1 \      # 安裝myisam存儲引擎
    -DENABLED_LOCAL_INFILE=1 \    # 運行使用Load data命令從本地導入數據
    -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf-8 -DDEFAULT_COLLATION=utf8_general_ci \   # 安裝全部字符集、默認字符集utf-8、檢驗字符
    -DWITH_DEBUG=0 \    # 關閉debug
    -DWITH_EMBEDDED_SERVER=1   \   # 生成一個libmysqld.a(.so)的庫,這個庫同時集成了mysql服務與客戶端API
    -DDOWNLOAD_BOOST=1 -DENABLE_DOWNLOADS=1 -DWITH_BOOST=/usr/local/boost   # 運行boost    容許下載boost庫文件

# 編譯
$ make -j4
# 安裝
$ make install

# 5.啓動測試
$ cp /usr/local/mysql/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

# 修改配置文件 /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql          # mysql軟件在哪
datadir=/usr/local/mysql/data   # mysql的數據在哪
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysql.log
pid-file=/var/run/mysql.pid

# 初始化數據庫
$ /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysqld/data/
# 注意:初始化後會獲得一個臨時密碼

# 啓動數據庫
$ /etc/init.d/mysql start
$ lsof -i :3306  # 查看端口狀況

# 修改密碼
$ mysql_secure_installation   # 要使用剛剛獲得的臨時密碼

# 登陸數據庫
$ mysql -uroot -pabc123 

三、python安裝

# 下載python包
$ wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz

# python安裝
$ tar xf python-3.7.1.tar.xz
$ cd Python-3.7.1
$ yum -y install gcc-* openssl-* libffi-devel sqlite-devel
$ ./configure --prefix=/usr/local/python3
--enable-optimizations --with-openssl=/usr/bin/oponssl # --enable-optimizations是包優化參數 
$ make -j4 # 因爲有加配置優化,這個步驟會好久 
$ make install

 

 

  python的默認安裝路徑:/usr/local/lib/python3.7

  安裝測試:

[root@web01 Python-3.7.1]# python3
Python 3.7.1 (default, Oct 27 2018, 22:51:15)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information. 
>>>
>>> exit();

  當咱們從Python官方網站下載並安裝好Python 3.7後,咱們就直接得到一個官方版本的解釋 :CPython。

四、升級pip

  pip是python包管理工具,該工具提供對python包的查找、下載、安裝、卸載功能。因爲咱們下載了最新的python,所以須要更新pip。

$ pip3 install --upgrade pip

  若是出現報錯:pip is configured with locations that require TLS/SSL,however the ssl module in Python is not available.

  須要修改 ~/Python-3.7.1/Modules/Setup文件,將註釋去除。

211 SSL=/usr/local/ssl
212 _ssl _ssl.c \
213         -DUSE_SSL -l$(SSL)/include -l$(SSL)/include/openssl \
214         -L$(SSL)/lib -lssl -lcrypto

  而後從新執行make && make install。

五、安裝python虛擬環境

  virtualenv 是一個建立隔絕的Python環境的工具。

  virtualenv建立一個包含全部必要的可執行文件的文件夾,用來使用 Python 工程所需的包。

[root@web01 ~]# pip3 install virtualenv

六、使用虛擬環境及安裝django

[root@web01 ~]# virtualenv web01   # 建立環境
[root@web01 ~]# source web01/bin/activate    # 環境生效

# 前面帶有(web01)說明在環境中
(web01) [root@web01 ~]# pip3 install django
# 用django新建一個項目
(web01) [root@web01 ~]#django-admin.py startproject myweb
# django啓動
(web01) [root@web01 ~]#python3 manage.py runserver 192.168.10.42:8000
# 修改ALLOWED_HOSTS
(web01) [root@web01 ~]# vim myweb/settings.py
    ALLOWED_HOSTS = ['*']

  此時在網站上能夠看到django初始頁面:

  

3、發佈WEB

一、業務邏輯圖

  

二、安裝uwsgi

  uwsgi是服務器和服務端應用程序的通訊協議,規定了怎麼把請求轉發給應用程序和返回。

  uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的做用是與uWSGI服務器進行交換。

  nginx 和 uWSGI交互就必須使用同一個協議,而上面說了uwsgi支持fastcgi、uwsgi、http協議,這些都是nginx支持的協議,只要你們溝通好使用哪一個協議,就能夠正常運行了。

[root@web01 ~]# pip3 install uwsgi 
# 運行uwsgi 報錯:[uwsgi: command not found] 解決方案:創建軟連接 
[root@web01 ~]# ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi

(1)編輯uwsgi配置文件

[root@web01 ~]# mkdir /etc/uwsgi
[root@web01 ~]# vim /etc/uwsgi/uwsgi.ini
[uwsgi]
chdir=/home/huangqs/EduCRM
module=EduCRM.wsgi:application
socket=127.0.0.1:7070
wsgi-file=EduCRM/wsgi.py
master=true       //主進程
reload-mercy=10
processes=4
threads=2
vacuum=true         // 退出、重啓時清理文件
max-requests=1000
limit-as=512
pidfile=/var/run/uwsgi7070.pid
daemonize=/var/log/uwsgi7070.log
home=/home/huangqs/educrm_env

 

(2)啓動uwsgi

[root@web01 ~]# uwsgi --ini /etc/uwsgi/uwsgi.ini

(3)關閉uwsgi

[root@web01 ~]# cat /var/run/uwsgi7070.pid
101446
[root@web01 ~]# kill -9 101446

三、uwsgi服務腳本管理

(1)定製uwsgi管理腳本

  vim /etc/init.d/uwsgi  ,init腳本內容以下所示:

#! /bin/sh
  DESC="uwsgi daemon"
  NAME=uwsgi            
  DAEMON=/usr/bin/uwsgi          #指向uwsgi的命令路徑
  CONFFILE=/etc/uwsgi/$NAME.ini   #uwsgi.ini配置文件路徑
  PIDFILE=/var/run/${NAME}7070.pid     #pid文件路徑
  SCRIPTNAME=/etc/init.d/$NAME   #啓動腳本路徑
  FIFOFILE=/tmp/uwsgififo
  set -e
  [ -x "$DAEMON" ] || exit 0

  do_start() {
    if [ ! -f $PIDFILE ];then
      $DAEMON $CONFIGFILE || echo -n 「uwsgi running」
    else
      echo "The PID is exist..."
    fi
  }

  do_stop() {
  if [ -f $PIDFILE ];then
    $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
    rm -f $PIDFILE
    echo "$DAEMON STOPED."
  else
    echo "The $PIDFILE doesn't found."
  fi
  }

    do_reload() {
  if [ -p $FIFOFILE ];then
      echo w > $FIFOFILE
  else
      $DAEMON --touch-workers-reload $PIDFILE || echo -n "uwsgi can't reload"
  fi
  }

  do_status() {
      ps aux|grep $DAEMON
  }

  case "$1" in
  status)
      echo -en "Status $NAME: \n"
      do_status
  ;;
  start)
      echo -en "Starting $NAME: \n"
      do_start
  ;;
  stop)
      echo -en "Stopping $NAME: \n"
      do_stop
  ;;  
  reload|graceful)
      echo -en "Reloading $NAME: \n"
      do_reload
  ;;
  *)
      echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
      exit 3
  ;;
  esac
  exit 0

(2)腳本配置和使用

#受權
[root@home-ct75211 html]# chmod 755 /etc/init.d/uwsgi

#關閉
[root@home-ct75211 html]# /etc/init.d/uwsgi stop
Stopping uwsgi: 
/usr/local/bin/uwsgi STOPED.
#啓動
[root@home-ct75211 html]# /etc/init.d/uwsgi start
Starting uwsgi: 
[uWSGI] getting INI configuration from /etc/uwsgi/uwsgi.ini

#查看
[root@home-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:21190         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   

(3)設置開機啓動

$ chkconfig --add /etc/init.d/uwsgi
# 'service uwsgi does not support chkconfig' 若是出現了這樣的報錯,是由於添加腳本用service啓動,必需要腳本里麪包含這2行:
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve

$ cd /etc/init.d
$ chkconfig --level 2345 uwsgi on
$ chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

jenkins            0:off    1:off    2:on    3:on    4:on    5:on    6:off
jexec              0:off    1:on     2:on    3:on    4:on    5:on    6:off
netconsole         0:off    1:off    2:off   3:off   4:off   5:off   6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
uwsgi              0:off    1:off    2:on    3:on    4:on    5:on    6:off 

四、nginx服務配置

  nginx服務網站頁面目錄默認在/root/myweb目錄下,可是/root目錄權限不足,須要將應用目錄換地方:

$ ls -ld /root/
dr-xr-x--- .   10  root  root  4096  10月  28  07:55  /root/
$ mv /root/myweb  /usr/local/nginx/html

  修改默認配置文件:/usr/local/nginx/conf/nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8000;
        server_name  localhost;

        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:7070;                       // 必須與uwsgi配置一致
            uwsgi_param UWSGI_CHDIR /home/huangqs/EduCRM;    // 項目根目錄
            uwsgi_param UWSGI_SCRIPT EduCRM.EduCRM.wsgi;     // 入口文件,即wsgi.py相對項目根目錄的位置
            index  index.html index.htm;
            client_max_body_size 35m;
        }
        location /static {
            alias /home/huangqs/EduCRM/statics;
        }
    }
}

4、測試WEB

  重啓uwsgi和啓動nginx:

$ /etc/init.d/uwsgi stop
$ /etc/init.d/uwsgi start $ netstat -ntpl # 查看服務狀態 $ /usr/local/nginx/sbin/nginx # 啓動nginx

  訪問查看頁面:

  

相關文章
相關標籤/搜索