主要介绍一下python的部署

Posted 爱吃小猪的小码农

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了主要介绍一下python的部署相关的知识,希望对你有一定的参考价值。

 前言:相信看到这篇文章的小伙伴都或多或少有一些编程基础,懂得一些linux的基本命令了吧,本篇文章将带领大家服务器如何部署一个使用django框架开发的一个网站进行云服务器端的部署。

文章使用到的的工具

  • Python:一种编程语言,只能进行后端数据的处理和管理前端html文件,不能用来处理HTTP请求
  • nginx:web服务器,用于处理HTTP请求
  • uWsgi:Python的第三方库,建立Nginx和Python环境的交互通信
  • Django:Python的Web开发框架,部署HTML文件和数据库

接下来我将通过下列 云服务器一步一步演示,

0.创建云服务器(选择免费的即可)

1.连接云服务器

1.0.0通过idea的ssh进行连接

 输入你的云服务器ip与密码,用户名默认是ssh,连接成功后会有一个控制台出来

在这个控制台中就可以操作您的云服务器了 

1.0.1建立文件连接

 配置好之添加文件映射(你本机的项目地址与服务器项目地址),后点击浏览远程主机

效果如下,可以直接看到目标虚拟机中的文件

 1.0.2把项目所需文件上传进云服务器

右击项目文件->部署->上传到sfy

2.服务器端应用的安装

2.0 组件流程图

 nginx:用作转发反向代理,基本所有服务器都会使用到这个,可能有人要问不使用可以不?如果只是测试的话可以不使用,python,java中的web容器能够承载的并发量过小,使用nginx可以提高承载量,举个例子:在不使用nginx时同时有100个人访问你的网站,你的网站挂了,如果使用nginx的话1000个人同时访问也没什么问题。

uwsgi:监听端口,主要监听nginx转发过来的请求进行处理。

组件了解即可,会安装使用就好了,

Python Fabric远程自动部署简介

Fabric是一个Python(2.5-2.7)库,用于简化使用SSH的应用程序部署或系统管理任务。

它提供的操作包括:执行本地或远程shell命令,上传/下载文件,以及其他辅助功能,如提示用户输入、中止执行等。

本文主要介绍CentOS 6.3上使用Fabric进行自动化部署的基本方法。

1. 环境部署

本节主要介绍python版本升级,pip及fabric安装方法。

1.1 Python版本升级

CentOS 6.3自带的Python版本为2.6,首先需要升级到2.7版本。由于旧版本的Python已被深度依赖,所以不能卸载原有的Python,只能全新安装。

1. 下载Python,选择Gzipped source tarball,下载地址:https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz

2. 解压安装,命令如下

$ tar -xvf Python-2.7.14.tgz
$ cd Python-2.7.14
$ ./configure --prefix=/usr/local/python2.7
$ make
$ make install

3. 创建链接来使系统默认python变为python2.7

$ ln -fs /usr/local/python2.7/bin/python2.7 /usr/bin/python

4. 查看Python版本

$ python –V

5. 修改yum配置(否则yum无法正常运行)

$ vi /usr/bin/yum

将第一行的#!/usr/bin/python修改为系统原有的python版本地址#!/usr/bin/python2.6

至此CentOS 6.3系统Python已成功升级至最新2.7.14版本。

1.2 安装pip

Pip是一个安装和管理python包的工具。

安装方法如下:

1. 下载pip,地址:https://raw.github.com/pypa/pip/master/contrib/get-pip.py

2. 执行安装命令

$ python get-pip.py

3. 创建连接(否则会报错提示“命令不存在”)

$ ln -s /usr/local/python2.7/bin/pip /usr/bin/pip

1.3 安装fabric

1. 执行安装命令

$ pip install fabric

2. 创建连接(否则会报错提示“命令不存在”)

$ ln -s /usr/local/python2.7/bin/fab /usr/bin/fab

2. Fabric脚本编写

本节对fabric用法进行简单介绍,并提供实例以供参考。

2.1 Hello,fab

1. 在当前目录下新建文件fabfile.py,输入内容如下:

def hello():
    print("Hello fab!")

2. 执行命令fab hello,结果如下:

$ fab hello

Hello fab!

3. 文件名不为fabfile.py时需使用-f进行指定:

$ mv fabfile.py test.py

$ fab hello

Fatal error: Couldnt find any fabfiles!

Remember that -f can be used to specify fabfile path, and use -h for help.

$ fab -f test.py hello

Hello fab!

4. 参数传递

使用vi fabfile.py,修改fabfile.py:

def hello(name):
    print Hello %s!%name

可以通过如下两种方式进行参数传递:

$ fab hello:name=fab

Hello fab!

$ fab hello:fab

Hello fab!

2.2 本地操作

执行本地操作命令使用local

1. fabfile.py脚本内容如下

from fabric.api import local

def test():
    local(cd /home/)
    local(ls -l|wc -l)

2. 执行命令fab test,结果如下

$ fab test

[localhost] local: cd /home/

[localhost] local: ls -l|wc -l

8

2.3 远程操作

执行远程操作命令使用run

1. fabfile.py脚本内容如下

from fabric.api import cd,run,env,hosts

env.hosts=[192.168.85.99:22,192.168.85.101:22]
env.password=test

def test():
    with cd(/home):
        run("du -sh")

2. 执行命令fab test,结果如下

$ fab test

[192.168.85.99:22] Executing task test

[192.168.85.99:22] run: du -sh

[192.168.85.99:22] out: 392G .

[192.168.85.99:22] out:

[192.168.85.101:22] Executing task test

[192.168.85.101:22] run: du -sh

[192.168.85.101:22] out: 5.6G .

[192.168.85.101:22] out:

Disconnecting from 192.168.85.99... done.

Disconnecting from 192.168.85.101... done.

3. 多服务器混合,需要在不同服务器进行不同操作时,可参考如下脚本

from fabric.api import env,roles,run,execute

env.roledefs = {server1: [[email protected]:22,],server2: [[email protected]:22, ]}
env.password = test

@roles(server1)
def task1():
    run(ls /home/ -l | wc -l)

@roles(server2)
def task2():
    run(du -sh /home)

def test():
    execute(task1)
    execute(task2)

结果如下:

$ fab test

[[email protected]192.168.85.99:22] Executing task task1

[[email protected]192.168.85.99:22] run: ls /home/ -l | wc -l

[[email protected]192.168.85.99:22] out: 27

[[email protected]192.168.85.99:22] out:

[[email protected]192.168.85.100:22] Executing task task2

[[email protected]192.168.85.100:22] run: du -sh /home

[[email protected]192.168.85.100:22] out: 1.4G /home

[[email protected]192.168.85.100:22] out:

Disconnecting from 192.168.85.99... done.

Disconnecting from 192.168.85.100... done.

3. 参考

上面只是对Python+Fabric自动部署脚本编写方法的简单介绍,在实际应用过程中根据具体需求编写相应的脚本时可以参考如下文章:

1. http://docs.fabfile.org/en/latest/index.html

2. http://wklken.me/posts/2013/03/25/python-tool-fabric.html

以上是关于主要介绍一下python的部署的主要内容,如果未能解决你的问题,请参考以下文章

1Python全栈之路系列Web框架介绍

Python常用的库简单介绍一下

python发送各类邮件的主要方法

Python3 - 使用uwsgin部署Python项目到Centos服务器

Python爬虫学习教程,批量爬取下载抖音视频

小白 Python 爬虫部署 Linux