部署基于python语⾔的WEB发布环境
01
部署说明
02
部署流程
03
发布web
04
测试
部署说明 01
1)面向对象的语言
2)拥有丰富的库
3)可移植性
4)免费、开源
5)简单易学
可做软件开发、⼈⼯智能、web开发等等
02 部署流程
Cnetos7.5+Nginx+python+Django+uwsgi+mysql
实验部署流程:
1)安装Nginx
2)安装python
3)安装mysql
4)部署发布平台
5)测试
部署Nginx
声明
• 实验机器: Vmware 虚拟机 8核10G
• ⽹卡:桥接
• 系统:centos7.5
• 防⽕墙:关闭
• Selinux:关闭
• ⽹段:192.168.10.0/24
• WEB01:192.168.10.42
MySQL部署
Mysql介绍
1个关系型数据库,由瑞典的AB公司开发,后来卖给了oracle公司,⽬前分为商业版和社区版
⽬前有量⼤版本 mysql5 和 mysql8
⽬前建议⼤家先使⽤5.7最新版本即可。官⽅提供了RPM和源码两种格式
Mysql安装步骤
1)安装依赖包
2) 升级cmake⼯具
3) 升级boost库⽂件
4) 安装mysql
5) 启动测试
⼀、前期准备
依赖包
https://cmake.org/download/
https://www.boost.org/mysql # boost Boost库是⼀个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之⼀
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
⼆、编译环境安装
yum -y install ncurses-devel gcc-* bzip2-*
1)cmake install
tar xf cmake-3.6.0-rc1.tar
cd cmake-3.6.0-rc1
./configure
make
make install
2)boost
tar xf boost_1_59_0.tar.bz2
mv boost_1_59_0 /usr/local/boost
3)mysql
useradd -s /sbin/nologin -r mysql
mkdir -pv /usr/local/mysql/data
tar xf mysql...tar.xx
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 -DMYSQL_USER=mysql -DWITH_DEBUG=0
-DWITH_EMBEDED_SERVER=0 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
##解释
-DCMAKE_INSTALL_PREFIX 指定安装路径
-DMYSQL_DATADIR 指定存放数据⽂件的⽬录位置
-DMYSQL_UNIX_ADDR 指定mysql.sock的路径
-DDOWNLOAD_BOOST=0 不下载boost库⽂件
-DWITH_INNODBBASE_STORAGE_ENGINE=1 指定默认存储引擎
-DENABLE_LOCAL_INFILE=1 允许客户端使⽤local data
local 导⼊本地数据⽂件
-DEXTRA_CHARSETS=all ⽀持所有字符集
-DDEFAULT_CHARSET=utf8 默认字符集是UTF-8
-DDEFAULT_COLLATION=utf8_general_ci 数据库校对规则
-DMYSQL_USER=mysql 管理⽤户是mysql
-DWITH_DEBUG=0 关闭debug
-DWITH_EMBEDED_SERVER=0 ⽣成⼀个libmysqld.a(.so)的库,这个库同时集成了mysql服务与客户端API
-DWITH_BOOST=/usr/local/boost 指定boost的路径
make
make install
4)安装后操作
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/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
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/
usr/local/mysql/ --datadir=/usr/local/mysql/data/
修改配置⽂件 确保路径正确
[root@web01 ~]# egrep -v "^#|^$" /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysql.log
pid-file=/var/run/mysql.pid
!includedir /etc/my.cnf.d
5)启动mysql
初始化 root@localhost: /q%Zpaoio5i1
6)密码修改
cmake 安装:
1)获得软件
https://cmake.org/download/
2)安装软件
tar xf cmake-3.6.0-rc1.tar
cd cmake-3.6.0-rc1
./configure
make
make install
boost
解压到指定⽬录即可
cmake 参数分解:
-DCMAKE_INSTALL_PREFIX 指定安装路径
-DMYSQL_DATADIR 指定存放数据⽂件的⽬录位置
-DMYSQL_UNIX_ADDR 指定mysql.sock的路径
-DDOWNLOAD_BOOST=0 不下载boost库⽂件
-DWITH_INNODBBASE_STORAGE_ENGINE=1 指定默认存储引擎
-DENABLE_LOCAL_INFILE=1 允许客户端使⽤local data local 导⼊本地数据⽂件
-DEXTRA_CHARSETS=all ⽀持所有字符集
-DDEFAULT_CHARSET=utf8 默认字符集是UTF-8
-DDEFAULT_COLLATION=utf8_general_ci 数据库校对规则
-DMYSQL_USER=mysql 管理⽤户是mysql
-DWITH_DEBUG=0 关闭debug
-DWITH_EMBEDED_SERVER=0 ⽣成⼀个libmysqld.a(.so)的库,这个库同时集成了mysql服务与客户端API
-DWITH_BOOST=/usr/local/boost 指定boost的路径
安装后操作:
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/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
初始化数据库
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
修改配置⽂件:
vim /etc/my.cnf
确保所有参数的路径正确
[root@web01 ~]# egrep -v "^#|^$" /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysql.log
pid-file=/var/run/mysql.pid
!includedir /etc/my.cnf.d
Python部署
软件获得
wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
Python安装
[root@web01 ~]# tar xf Python-3.7.1.tar.xz
[root@web01 ~]# cd Python-3.7.1
[root@web01 Python-3.7.1]# yum -y install gcc-* openssl-* libffi-devel sqlite-devel
[root@web01 Python-3.7.1]# ./configure --enable-optimizations --with-openssl=/usr/bin/openssl
[root@web01 Python-3.7.1]# make -j4
[root@web01 Python-3.7.1]# make install
默认安装路径:/usr/local/lib/python3.7
ln -s python /usr/bin/python3
安装测试
[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.5后,我们就直接获得了⼀个官⽅版本的解释器:CPython。
升级pip
Pip: pip 是 Python 包管理⼯具,该⼯具提供了对Python 包的查找、下载、安装、卸载的功能。
[root@web01 ~]# pip3 install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl
(1.3MB)
100% |████████████████████████████████| 1.3MB 106kB/s
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
安装python虚拟环境
virtualenv 是⼀个创建隔绝的Python环境的⼯具。virtualenv创建⼀个包含所有必要的可执⾏⽂件的⽂件夹,⽤来使⽤Python⼯程所需的包。
[root@web01 ~]# pip3 install virtualenv
Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-noneany.whl (1.9MB)
100% |████████████████████████████████| 1.9MB 55kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.0.0
使用虚拟环境
[root@web01 ~]# virtualenv web01
[root@web01 ~]# source web01/bin/activate
(web01) [root@web01 ~]# pip3 install django
(web01) [root@web01 ~]#django-admin.py startproject www
python manage.py migrate
(web01) [root@web01 ~]#python manage.py runserver 192.168.10.42:8000
(web01) [root@web01 ~]# vim www/settings.py
ALLOWED_HOSTS = ['*']
deactivate # 退出虚拟环境
安装Django
[root@web01 ~]# pip3 install django
Collecting django
Downloading https://files.pythonhosted.org/packages/32/ab/22530cc1b2114e6067eece94a333d6c749fa1c56a009f0721e51c181ea53/Django-2.1.2-py3-none-any.whl
(7.3MB)
100% |████████████████████████████████| 7.3MB 96kB/s
Collecting pytz (from django)
Downloading https://files.pythonhosted.org/packages/52/8b/876c5745f617630be90cfb8fafe363c6d7204b176dc707d1805d1e9a0a35/pytz-2018.6-py2.py3-none-any.whl
(507kB)
100% |████████████████████████████████| 512kB 118kB/s
Installing collected packages: pytz, django
Successfully installed django-2.1.2 pytz-2018.6
03 发布⼀个WEB
安装uwsgi
uwsgi是服务器和服务端应⽤程序的通信协议,规定了怎么把请求转发给应⽤程序和返回
uWSGI是⼀个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作⽤是与uWSGI服务器进⾏交换。
nginx 和 uWSGI交互就必须使⽤同⼀个协议,⽽上⾯说了uwsgi⽀持fastcgi,uwsgi,http协议,这些都是nginx⽀持的协议,只要⼤家沟通好使⽤哪个协议,就可以正常运⾏了。
[root@web01 ~]# pip3 install uwsgi
Collecting uwsgi
Downloading https://files.pythonhosted.org/packages/a2/c9/a2d5737f63cd9df4317a4acc15d1ddf4952e28398601d8d7d706c16381e0/uwsgi-2.0.17.1.tar.gz (800kB)
100% |████████████████████████████████| 808kB 64kB/s
Installing collected packages: uwsgi
Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.17.1
编辑uwsgi配置⽂件
[root@web01 ~]# 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 //退出、重启时清理⽂件
max-requests = 1000 //开启10000个进程后, ⾃动respawn下
limit-as = 512 // 将进程的总内存量控制在512M
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid //pid⽂件,⽤于下⾯的脚本启动、停⽌该进程
daemonize = /website/uwsgi9090.log
编辑uwsgi配置⽂件
[root@web01 ~]# 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 //退出、重启时清理⽂件
max-requests = 1000 //开启10000个进程后, ⾃动respawn下
limit-as = 512 // 将进程的总内存量控制在512M
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid //pid⽂件,⽤于下⾯的脚本启动、停⽌该进程
daemonize = /website/uwsgi9090.log
python环境更加详细的部署内容
部署⼀个python业务的web服务器
软件环境
rhel7.5
python3.7.1
wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
uwsgi 2.0.17
wget https://files.pythonhosted.org/packages/a2/c9/
a2d5737f63cd9df4317a4acc15d1ddf4952e28398601d8d7d706c16381e0/
uwsgi-2.0.17.1.tar.gz
#/usr/local/lib/python3.7
[root@web01 ~]# tar xf Python-3.7.1.tar.xz
[root@web01 ~]# cd Python-3.7.1
[root@web01 Python-3.7.1]# yum -y install gcc-* openssl-* libffidevel sqlite-devel
[root@web01 Python-3.7.1]# ./configure --enable-optimizations --
with-openssl=/usr/bin/openssl
[root@web01 Python-3.7.1]# make -j4
[root@web01 Python-3.7.1]# make install
[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.5后,我们就直接获得了⼀个官⽅版本的
解释器:CPython。
CPython。这个解释器是⽤C语⾔开发的,所以叫CPython。在命令⾏下运⾏python就是启
动CPython解释器。
CPython是使⽤最⼴的Python解释器。教程的所有代码也都在CPython下执⾏。
⽤ Python 的语法混合编写 Python 和 C/C++ 代码,提升 Python 速度
调⽤ C/C++ 代码
pip is configured with locations that require TLS/SSL, however the
ssl module in Python is not available.
[root@web01 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
[root@web01 Python-3.7.1]# make
[root@web01 Python-3.7.1]# make install
#升级pip⼯具 pip 是 Python 包管理⼯具,该⼯具提供了对Python 包的查找、下
载、安装、卸载的功能。
[root@web01 ~]# pip3 install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/c2/
d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/
pip-18.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 106kB/s
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
virtualenv 是⼀个创建隔绝的Python环境的⼯具。virtualenv创建⼀个包含所有必要
的可执⾏⽂件的⽂件夹,⽤来使⽤Python⼯程所需的包。
[root@web01 ~]# 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 55kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.0.0
[root@web01 ~]# virtualenv web01
[root@web01 ~]# source web01/bin/activate
(web01) [root@web01 ~]# pip3 install django
(web01) [root@web01 ~]#django-admin.py startproject www
(web01) [root@web01 ~]#python3 manage.py runserver
192.168.10.42:8000
(web01) [root@web01 ~]# vim www/settings.py
ALLOWED_HOSTS = ['*']
[root@web01 ~]# pip3 install django
Collecting django
Downloading https://files.pythonhosted.org/packages/32/ab/
22530cc1b2114e6067eece94a333d6c749fa1c56a009f0721e51c181ea53/
Django-2.1.2-py3-none-any.whl (7.3MB)
100% |████████████████████████████████| 7.3MB 96kB/s
Collecting pytz (from django)
Downloading https://files.pythonhosted.org/packages/52/8b/
876c5745f617630be90cfb8fafe363c6d7204b176dc707d1805d1e9a0a35/
pytz-2018.6-py2.py3-none-any.whl (507kB)
100% |████████████████████████████████| 512kB 118kB/s
Installing collected packages: pytz, django
Successfully installed django-2.1.2 pytz-2018.6
uwsgi是服务器和服务端应⽤程序的通信协议,规定了怎么把请求转发给应⽤程序和返回
uWSGI是⼀个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中
HttpUwsgiModule的作⽤是与uWSGI服务器进⾏交换。
nginx 和 uWSGI交互就必须使⽤同⼀个协议,⽽上⾯说了uwsgi⽀持
fastcgi,uwsgi,http协议,这些都是nginx⽀持的协议,只要⼤家沟通好使⽤哪个协议,
就可以正常运⾏了。
[root@web01 ~]# pip3 install uwsgi
Collecting uwsgi
Downloading https://files.pythonhosted.org/packages/a2/c9/
a2d5737f63cd9df4317a4acc15d1ddf4952e28398601d8d7d706c16381e0/
uwsgi-2.0.17.1.tar.gz (800kB)
100% |████████████████████████████████| 808kB 64kB/s
Installing collected packages: uwsgi
Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.17.1
uwsgi
[root@web01 ~]# mkdir /etc/uwsgi
[root@web01 ~]# 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 //退出、重启时清理⽂件
max-requests = 1000 //开启10000个进程后, ⾃动respawn下
limit-as = 512 // 将进程的总内存量控制在512M
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid //pid⽂件,⽤于下⾯的脚本启动、停⽌该
进程
daemonize = /website/uwsgi9090.log
[root@web01 ~]# cat /etc/init.d/uwsgi
#!/bin/sh
DESC="uwsgi daemon"
NAME=uwsgi
DAEMON=/usr/local/bin/uwsgi
CONFIGFILE=/etc/uwsgi/$NAME.ini
PIDFILE=/var/run/$NAME.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
[root@web01 ~]# chmod 755 /etc/init.d/uwsgi
Nginx发布
server {
listen 80;
server_name localhost;
#access_log logs/abc.log main;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
uwsgi_param UWSGI_SCRIPT baism_web.wsgi;
uwsgi_param UWSGI_CHDIR /usr/local/nginx/html/baism_web;
index index.html index.htm;
client_max_body_size 35m;
#uwsgi_cache_valid 1m;
#uwsgi_temp_file_write_size 64k;
#uwsgi_busy_buffers_size 64k;
#uwsgi_buffers 8 64k;
#uwsgi_buffer_size 64k;
#uwsgi_read_timeout 300;
#uwsgi_send_timeout 300;
#uwsgi_connect_timeout 300;
}
}
pythonpath = /usr/local/lib/python3.7/site-packages