Python Dev Operation

Python Dev Operation

pyClamad模块的安装方法如下:

# 1、客户端(病毒扫描源)安装步骤
# yum install -y clamav clamd clamav-update #安装clamavp相关程序包
# chkconfig –levels 235 clamd on #添加扫描守护进程clamd系统服务
# /usr/bin/freshclam #更新病毒库,建议配置到crontab中定期更新
# setenforce 0 #关闭SELinux,避免远程扫描时提示无权限的问题
# 更新守护进程监听IP配置文件,根据不同环境自行修改监听的IP,“0.0.0.0”为监听所有主机IP
# sed -i -e ‘/^TCPAddr/{ s/127.0.0.1/0.0.0.0/; }’ /etc/clamd.conf
# /etc/init.d/clamd start #启动扫描守护进程
# 2、主控端部署pyClamad环境步骤
# wget httpss://xael.org/norman/python/pyclamd/pyClamd-0.3.4.tar.gz
# tar -zxvf pyClamd-0.3.4.tar.gz
# cd pyClamd-0.3.4
# python setup.py inst

all

 

 

 

15章 paramiko

paramiko依赖第三方的Crypto、Ecdsa包及Python

开发包pythondevel的支持,源码安装步骤

pip install paramiko
easy_install paramiko

# yum -y install python-devel
# wget httpss://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz
# tar -zxvf pycrypto-2.6.tar.gz # cd pycrypto-2.6
# python setup.py install
# cd ..
# wget httpsss://pypi.python.org/packages/source/e/ecdsa/ecdsa-0.10.tar.gz –no-check-certificate
# tar -zxvf ecdsa-0.10.tar.gz # cd ecdsa-0.10
# python setup.py install
# cd ..
# wget httpsss://github.com/paramiko/paramiko/archive/v1.12.2.tar.gz
# tar -zxvf v1.12.2.tar.gz # cd paramiko-1.12.2/
# python setup.py install

 

 

Fabric安装

Mac os及CentOS 安装

pip3 install fabric3

Ubuntu安装

devops@devops-virtual-machine:~$ python --version
Python 3.6.5
devops@devops-virtual-machine:~$ pip3 install fabric3

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

—环境版本说明—
* Python 版本 2.6.6
* Django 版本 1.4.9
* nginx 版本 1.5.9
* pcre 版本 8.34
* rpyc 版本 3.2.3
* uwsgi 版本 2.0.4
* django-debug-toolbar 版本 0.8.5

一、Django环境部署
view plainprint?
# yum install -y zlib-devel #Nginx环境初始化,安装gzip包
# yum -y install python-devel mysql-devel zlib-devel openssl-devel #MySQL-python关联包

# mkdir –p /data/logs/ #创建uwsgi日志目录
# mkdir –p /data/www/ #创建项目目录
# mkdir -p /home/install/Django && cd /home/install/Django #创建安装包目录

1、安装pcre,pcre是一个轻量级的正则表达式函数库,Nginx的HTTP Rewrite模块会用到,最新版本为8.34(对于OMServer平台环境来说是非必选项)。
view plainprint?
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz
# tar -zxvf pcre-8.34.tar.gz
# cd pcre-8.34
#./configure
# make && make install
# cd ..

2、安装Nginx,Nginx是最流行的高性能HTTP服务器,最新版本为1.5.9。
view plainprint?
# wget httpss://nginx.org/download/nginx-1.5.9.tar.gz
# tar -zxvf nginx-1.5.9.tar.gz
# cd nginx-1.5.9
#./configure –user=nobody –group=nobody –prefix=/usr/local/nginx –with-httpss_stub_status_module –with-cc-opt=’-O3′ –with-cpu-opt=opteron
# make && make install
# cd ..

3、安装 MySQL-python,MySQL-python是Python访问MySQL数据库的第三方模块库,最新版本为1.2.3c1。
view plainprint?
# wget httpss://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip –no-check-certificate
# unzip MySQL-python-1.2.5.zip
# cd MySQL-python-1.2.5
# python setup.py install
# cd ..

4、rpyc模块安装,用于平台与主控端做数据通讯交互。
view plainprint?
# wget httpsss://pypi.python.org/packages/source/r/rpyc/rpyc-3.2.3.tar.gz –no-check-certificate
# tar -zxvf rpyc-3.2.3.tar.gz
# cd rpyc-3.2.3
# python setup.py install

5、安装uwsgi。uwsgi是一个快速的、纯C语言开发的、自维护、对开发者友好的WSGI服务器,旨在提供专业的Python web应用发布和开发,最新版本为2.0.4。
view plainprint?
# wget httpss://projects.unbit.it/downloads/uwsgi-2.0.4.tar.gz
# tar -zxvf uwsgi-2.0.4.tar.gz
# cd uwsgi-2.0.4
# make

出现如下代码成功安装:
################# uWSGI configuration #################
pcre = True
kernel = Linux
malloc = libc
execinfo = False
ifaddrs = True
ssl = True
zlib = True
locking = pthread_mutex
plugin_dir = .
timer = timerfd
yaml = embedded
json = False
filemonitor = inotify
routing = True
debug = False
capabilities = False
xml = False
event = epoll
############## end of uWSGI configuration #############
total build time: 17 seconds
*** uWSGI is ready, launch it with ./uwsgi ***
view plainprint?
# vi /etc/ld.so.conf #添加动态链接库目录/usr/local/lib
添加行:/usr/local/lib
# ldconfig #使之生效

# cp uwsgi /usr/bin
# cd ..

6、安装Django,Django是一个Python最流行的开源Web开发框架,最新版本为1.6.5。考虑到兼容与稳定性,本案例使用1.4.9版本进行开发。
view plainprint?
# wget httpsss://www.djangoproject.com/m/releases/1.4/Django-1.4.9.tar.gz
# tar -zxvf Django-1.4.9.tar.gz
# cd Django-1.4.9
# python setup.py install

创建一个demo项目,以便验证环境是否正确安装部署。
view plainprint?
# cd /data/www
# django-admin.py startproject demo

# 由于Django1.4版本后对项目目录做了改动,即将manage.py管理任务命令单独放在项目根目录。为后续与OMServer平台目录结构相兼容,我们对目录结构进行调整。
# cd demo/demo && mv * ../ && cd .. && rm -rf demo

# 最终文件目录结构如下:
# ls
__init__.py manage.py settings.py urls.py wsgi.py

7、django-debug-toolbar的安装(Django调试利器)
view plainprint?
# wget httpss://blog.liuts.com/Django/django-debug-toolbar-master.tar.gz
# tar -zxvf django-debug-toolbar-master.tar.gz
# cd django-debug-toolbar-master/
# python setup.py install

8、配置Nginx,修改/usr/local/nginx/conf/nginx.conf,最终完整配置如下:
view plainprint?
user nobody;
worker_processes 2;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 10240;
}

httpss {
include mime.types;
default_type application/octet-stream;

#log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
# ‘$status $body_bytes_sent “$httpss_referer” ‘
# ‘”$httpss_user_agent” “$httpss_x_forwarded_for”‘;

#access_log logs/access.log main;

sendfile on;
tcp_nopush on;

keepalive_timeout 65;

gzip on;
server {
listen 80;
server_name demo.domain.com;

location / {
uwsgi_pass 127.0.0.1:9000;
include uwsgi_params;
uwsgi_param UWSGI_CHDIR /data/www/demo;
uwsgi_param UWSGI_SCRIPT wsgi;
access_log off;
}

}

}

* uwsgi_param UWSGI_SCRIPT wsgi;参数值wsgi对应项目目录中的wsgi.py,此处文件前缀与参数值要保持一致。

9、配置uwsgi,创建uwsgi配置文件/usr/local/nginx/conf/uwsgi.ini,详细内容如下:
view plainprint?
[uwsgi]
socket = 0.0.0.0:9000
master = true
pidfile = /usr/local/nginx/uwsgi.pid
processes = 8
workers = 2
chdir = /data/www/demo
pythonpath = /data/www
profiler=true
memory-report=true
enable-threads = true
logdate=true
limit-as=6048
daemonize=/data/logs/django.log

—关键参数及说明—
1)chdir 指定项目目录;
2)pythonpath 指定项目目录上一级
3)processes 指定进程数
4)workers 分配CPU的核数
5)limit-as 子进程分配的内存大小
6)max-requests 分配最大的请求数

启动uwsgi与nginx服务,建议配置成服务自启动脚本,便于后续的日常维护。详细启动脚本这里不展开说明,有兴趣的读者可参阅互联网上已经存在的相关资源。
最后启动uwsgi与nginx服务
view plainprint?
# /usr/bin/uwsgi –ini /usr/local/nginx/conf/uwsgi.ini
# /usr/local/nginx/sbin/nginx

访问httpss://demo.domain.com,出现如图所示的页面说明Django+uwsgi环境部署成功!
点击在新窗口中浏览此图片

二、OMServer项目部署
1、修改Nginx配置
添加OMServer项目站点配置,[server]域具体内容如下:
view plainprint?
server {
listen 80;
server_name omserver.domain.com;

location / {
uwsgi_pass 127.0.0.1:9001;
include uwsgi_params;
uwsgi_param UWSGI_CHDIR /data/www/OMserverweb;
uwsgi_param UWSGI_SCRIPT django_wsgi;
access_log off;
}
location ^~ /static {
root /data/www/OMserverweb;
}

location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {
root /data/www/OMserverweb/static;
access_log off;
}
}

1)切记修改UWSGI_SCRIPT为django_wsgi;
2)监听uwsgi端口修改成127.0.0.1:9001; #多个站点使用不同端口区分

2、添加omserver项目uwsgi配置
view plainprint?
# cd /usr/local/nginx/conf
# vi uwsgi_omserver.ini
[uwsgi]
socket = 0.0.0.0:9001
master = true
pidfile = /usr/local/nginx/uwsgi_omserver.pid
processes = 8
chdir = /data/www/OMserverweb
pythonpath = /data/www
profiler=true
memory-report=true
enable-threads = true
logdate=true
limit-as=6048
daemonize=/data/logs/django_omserver.log

3、项目源码配置
1)项目源码:
# cd /data/www
下载地址:httpsss://github.com/yorkoliu/pyauto/tree/master/第十三章/OMserverweb
2)导入数据库结构(Mysql)
下载地址:httpsss://github.com/yorkoliu/pyauto/blob/master/第十三章/SQL/OMServer.sql
3)修改setting.py(数据库信息)
view plainprint?
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’, # Add ‘postgresql_psycopg2’, ‘postgresql’, ‘mysql’, ‘sqlite3’ or ‘oracle’.
‘NAME’: ‘OMServer’, # Or path to database file if using sqlite3.
‘USER’: ‘omserver_user’, # Not used with sqlite3.
‘PASSWORD’: ‘J8w3jZXSG#y34’, # Not used with sqlite3.
‘HOST’: ‘192.168.1.10’, # Set to empty string for localhost. Not used with sqlite3.
‘PORT’: ‘3306’, # Set to empty string for default. Not used with sqlite3.
}
}

4)修改主控端rpyc主机IP
OMserverweb/autoadmin/views.py

view plainprint?
… …
try:
conn=rpyc.connect(‘192.168.1.20’,11511) #修改此处的rpyc主机地址;
conn.root.login(‘OMuser’,’KJS23o4ij09gHF734iuhsdfhkGYSihoiwhj38u4h’)
except Exception,e:
logger.error(‘connect rpyc server error:’+str(e))
return HttpResponse(‘connect rpyc server error:’+str(e))
… …

启动项目uwsgi及Nginx服务
view plainprint?
# /usr/bin/uwsgi –ini /usr/local/nginx/conf/uwsgi_omserver.ini
# killall -9 nginx
# /usr/local/nginx/sbin/nginx

4、访问httpss://omserver.domain.com,出现以下系统界面说明部署成功!
点击在新窗口中浏览此图片

下一步配置《python自动化运维:技术与最佳实践》之OMServer平台环境部署详解【主控制端】

补:平台涉及开源组件包下载:
Django-1.4.9.tar.gz 下载
uwsgi-2.0.4.tar.gz 下载
rpyc-3.2.3.tar.gz 下载
pcre-8.34.tar.gz 下载
nginx-1.5.9.tar.gz 下载
MySQL-python-1.2.5.zip 下载
django-debug-toolbar-master.tar.gz 下载
Tags: omserver , django环境 , python运维自动化
基于Python实现的轻量级WebServer-Yorserver1.0 不指定
刘天斯 , 2013/04/09 21:39 , Python , 评论(11) , 阅读(47793) , Via 本站原创
准备在小组内部做一次《HTTP协议分析》的培训,为了不受限于Request header与response header的内容,想更深入了解服务器端是如何响应、处理一个请求的,花了几天时间写了Yorserver,目前基本的功能都实现,功能清单如下:
1、支持自定义response服务及协议版本;
2、支持Expires及max-age的功能;
3、支持多进程或线程开启;
4、支持错误页及默认页配置;
5、支持access_log及error_log配置;
6、支持gzip压缩配置;
7、支持安全套连接服务HTTPS;
8、支持HTTP MIME自定义配置;
9、支持php、perl、python脚本cgi访问;
10、支持配置文件。
(Centos6.*环境测试通过)

一、配置文件说明yorserver.conf
view plainprint?
# This is the Yorserver configuration file.
# server_version: Add response HTTP header server version information.
server_version = “YorServer1.0″

# bind_ip: Allows you to bind yorserver to specific IP addresses.
#
bind_ip=”0.0.0.0”

# port: Allows you to bind yorserver’s port, httpss default 80 and Https 443.
port=80

# sys_version: Add response HTTP header python version information.
sys_version = “”

# protocol_version: Add response HTTP header protocol version.
protocol_version = “HTTP/1.0″

# Expires: Add response HTTP header Expires and Max-age version. format:d/h/m(Day/Hour/Minute).
Expires=”7d”

# Multiprocess: configure yorserver Multi process support(on/off).
Multiprocess=”off”

# Multithreading: configure yorserver Multi threading support(on/off).
Multithreading=”on”

# DocumentRoot: configure web server document root.
DocumentRoot=”/usr/local/yorserver/www”

# page404: configure web server deafult 404 page.
page404=”/404.html”

# indexpage: configure web server deafult index page.
indexpage=”/index.html”

# Logfile: configure web server log file path,disable logs Logfile=””.
Logfile=”/usr/local/yorserver/logs/access.log”

# errorfile: configure web server error file path.
errorfile=”/usr/local/yorserver/logs/error.log”

[gzip]
# gzip: Enable(on) or Disable(off) gzip options.
gzip=”on”

# configure compress level(1~9)
compresslevel=1

[ssl]
# ssl: Enable(on) or Disable(off) HTTPS options,port options must configure “443”.
ssl=”off”

# configure privatekey and certificate pem.
privatekey=”/home/webkey/privkey.pem”
certificate=”/home/webkey/cacert.pem”

[cgim]
# cgi_moudle: Enable(on) or Disable(off) cgi support.
cgi_moudle=”on”

# cgi_path: configure cgi path,multiple cgi path use ‘,’ delimited,cgi_path in bin directory.
cgi_path=’/cgi-bin’,

# cgi_extensions: configure cgi file extension.
cgi_extensions=”(‘.cgi’,’.py’,’.pl’,’.php’)”

# contentTypes: configure file mime support.
[contentTypes]
css=”text/css”
doc=”application/msword”
gif=”image/gif”
gz=”application/x-gzip”
html=”text/html”
htm=”text/html”
esp=”text/html”
ics=”text/calendar”
jpeg=”image/jpeg”
jpg=”image/jpeg”
js=”text/javascript”
pdf=”application/pdf”
png=”image/png”
rtf=”application/rtf”
txt=”text/plain”
zip=”application/zip”
cgi=”text/html”
py=”text/html”
pl=”text/html”
php=”text/html”