python webService配置流程

Nginx部署

下载安装包:

1
2
wget --no-check-certificate https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
wget http://nginx.org/download/nginx-1.12.2.tar.gz

解压安装包:

1
2
tar -zxvf master.tar.gz
mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ nginx-sticky

安装环境(逐条):

1
2
3
4
5
6
7
8
yum -y install gcc-c++ autoconf automake
yum -y install libxml2 libxml2-dev
yum -y install libxslt-devel
yum -y install openssl openssl-devel
yum install gd-devel -y
yum -y install perl-devel perl-ExtUtils-Embed
yum -y install GeoIP GeoIP-devel GeoIP-data
yum install libxslt libraries

修改配置文件(一整块复制):

1
./configure --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-debug --add-module=/opt/tmp/nginx-sticky/

验证:进入NGINX的安装目录-执行完成后输入echo $?验证,返回值为0表示执行成功

安装:

1
2
cd nginx-1.12.2
./configure && make && make install

配置:

1
vim conf/nginx.conf

修改如下内容:

1
2
3
4
5
6
7
8
service {
linsten 80;
server_name xxx.xxx.xxx //此处为域名
location / {
root html;
index index.html index.htm;
}
}

:wq 保存退出vim

查看是否成功:

在浏览器输入服务器IP ,查看是否有Nginx的欢迎页面,若访问不成功,检查阿里云/腾讯云是否开启80端口。

相关指令:

1
2
3
4
5
/usr/local/nginx/sbin/nginx                         //启动  
/usr/local/nginx/sbin/nginx -s stop(quit、reload) //停止/重启
/usr/local/nginx/sbin/nginx -h //命令帮助
/usr/local/nginx/sbin/nginx -t //验证配置文件
vim /usr/local/nginx/conf/nginx.conf //配置文件

安装Python3

下载:

1
wget https://www.Python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz

解压:

1
tar xJf  Python-3.6.1.tar.xz

进入 python-3.6.1 目录:

1
cd  Python-3.6.1

安装:

1
./configure  --prefix=/usr/local/python3 && make && make install

创建软连接:

1
2
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

virtualenv(Python 虚拟机)

用来区分python环境

安装:

1
pip3 install virtualenv

创建虚拟环境:

1
/usr/local/python3/bin/virtualenv  -p /usr/bin/python3 venv

相关指令:

1
2
source venv/bin/activate    //激活
deactivate //关闭

Tornado (Python web service框架)

  • GET & POST

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding:utf-8 -*-

from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
from tornado.options import options, define
from tornado.escape import json_encode

define("port", default=8000, type=int)


class IndexHandler(RequestHandler):

def get(self):
# 获取get方式的参数
user = self.get_argument("user")
print("get方式获取参数:" + str(user))
result = {
"userName": user,
"code": 0
}
self.write(json_encode(result))

def post(self):
# 获取post方式的参数
user = self.get_argument("user")
print("post方式获取参数:" + user)
result = {
"userName": user,
"code": 0
}
self.write(json_encode(result))

if __name__ == "__main__":
app = Application([(r"/", IndexHandler)])
app.listen(8888)
IOLoop.current().start()

supervisor

  • python 虚拟环境保活
    1
    vim /etc/supervisord.conf

    在末尾增加:

    1
    2
    3
    4
    [program:xxx]
    command= venv/bin/python3 WebService/TornadoWS.py
    autorestart=true
    user=root
    wq保存退出 ```
    1
    2
    3
    4

    ### 执行:
    ```bash
    supervisorctl relod
  • supervisor更多指令:

supervisorctl status 查看supervisor状态
supervisorctl start xxx 开启xxx进程
supervisorctl stop xxx 停止xxx进程
supervisorctl 进入supervisor控制台