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


一、部署說明

1、python語言介紹

  python簡介

2、實驗環境

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

  網卡:橋接模式

  系統:centos7.5

  防火牆:關閉

  Selinux:關閉

  網段:192.168.10.0/25

  WEB01:192.168.10.42

二、部署流程

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

1、部署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

2、Mysql安裝部署

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

  現在主要是使用兩大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
$ ln -s /usr/local/mysql/mysql.sock /tmp/mysql.sock

# 修改配置文件 /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 

3、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
# 建立軟鏈
$ ln -s /usr/local/python3/bin/python3 /usr/bin/python3

  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。

4、升級pip

  pip是python包管理工具,該工具提供對python包的查找、下載、安裝、卸載功能。由於我們下載了最新的python,因此需要更新pip。

$ ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
$ 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。

5、安裝python虛擬環境

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

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

[root@web01 ~]# pip3 install virtualenv

6、使用虛擬環境及安裝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初始頁面:

  

三、發布WEB

1、業務邏輯圖

  

2、安裝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

3、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 

4、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;
        }
    }
}

四、測試WEB

  重啟uwsgi和啟動nginx:

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

  訪問查看頁面:

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM