CentOS搭建本地PyPi源

之前写过一篇搭建本地PyPi源,《Centos下如何搭建内网pypi源》。这边文章用的是pip2pi的方法,今天写的则是用bandsnatch方法。

 

0x01  环境准备

系统:CentOS 6.5

软件:Python 2.7、Pip 2.7、bandsnatch1.11

 

0x02  相关依赖

$ yum -y install gcc automake autoconf libtool make //安装python需要

$ yum -y install zlib zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel //安装disttibute需要

$ yum -y install unzip //解压需要

 

0x03  升级Python

CentOS6.5系统的Python默认是2.6版本的,这边我们需要用到2.7版本,故需要手动升级

升级流程参考《CentOS 6.5 升级Python版本到2.7

 

0x04  安装distribute

distribute是setuptools的取代品

1、下载

$ wget https://pypi.python.org/packages/source/d/distribute/distribute-0.7.3.zip

2、解压

$ unzip distribute-0.7.3.zip

3、安装

$ cd distribute-0.7.3

$ python setup.py install

$ which easy_install

/usr/local/bin/easy_install

$ which easy_install-2.7

/usr/local/bin/easy_install-2.7

*easy_install

当需要安装第三方python包时,可能会用到easy_install命令,相当于yum命令。但是系统都没有预装easy_install命令

 

0x05  安装Pip

如果已经安装旧版的pip,则需要升级到pip2.7

1、下载

$ wget https://pypi.python.org/packages/source/p/pip/pip-7.1.2.tar.gz

2、解压

$ tar zxvf pip-7.1.2.tar.gz

3、安装

$ cd pip-7.1.2

$ python setup.py install

$ which pip2.7

/usr/local/bin/pip2.7

*PIP

Pip 是安装python包的工具,是对easy_install的取代,提供了和easy_install相同的安装包、列出已经安装的包、查找包的功能。

 

0x06  安装virtualenv

1、下载

$ wget https://pypi.python.org/packages/source/v/virtualenv/virtualenv-13.1.2.tar.gz

2、解压

$ tar -zxvf virtualenv-13.1.2.tar.gz

3、安装

$ cd virtualenv-13.1.2

$ python setup.py install

$ which virtualenv-2.7

/usr/local/bin/virtualenv-2.7

 

0x07  安装bandsnatch

Python2.7只能安装bandsnatch1.11以下版本,bandsnatch2.0以上版本至少需要Python3.5

1、下载

$ wget https://mirrors.aliyun.com/pypi/packages/39/39/036b377709e2d9e79d7c10f35d81826da0717fdd91f75e1f3a51b154d57c/bandersnatch-1.11.tar.gz

2、解压

$ tar -zxvf bandersnatch-1.11.tar.gz

3、安装

$ /usr/local/bin/virtualenv-2.7 /usr/local/bandersnatch

$ cd /usr/local/bandersnatch

$ bin/pip install -r /opt/bandersnatch-1.11/requirements.txt

 

0x08  配置bandsnatch

$ cd /usr/local/bandersnatch

$ bin/bandersnatch mirror //先生成配置文件

2018-04-23 15:59:54,076 WARNING: Config file '/etc/bandersnatch.conf' missing, creating default config.

2018-04-23 15:59:54,076 WARNING: Please review the config file, then run 'bandersnatch' again.

编辑/etc/bandersnatch.conf文件,修改pypi源的存储路径。重新执行bin/bandersnatch mirror,就开始同步pip官方源到本地

$ mkdir -p /cache1/pypi //创建pypi源的存储路径

$ vim /etc/bandersnatch.conf //修改配置文件里的存储路径

directory = /cache1/pypi

$ bin/bandersnatch mirror //开始同步源

 

0x09  配置WEB

以nginx为例。修改nginx.conf,添加一个server,然后把路径配置为root /cache1/pypi/web即可

 

0x10  客户端设置

1、全局配置

$ mkdir ~/.pip

$ vim ~/.pip/pip.conf

---------------pip.conf---------------

[global]

index-url = http://xxx.xxx.xxx.xxx/cache1/pypi/web/simple/

--trusted-host xxx.xxx.xxx.xxx

2、指定源安装

$ pip install django -i http://xxx.xxx.xxx.xxx/cache1/pypi/web/simple/ --trusted-host xxx.xxx.xxx.xxx

//pip推荐使用https,如果没有启用,需在参数后面添加--trusted-host

3、脚本配置

$ wget -O - http://xxx.xxx.xxx.xxx/pypi.sh | sh //客户端直接运行命令即可完成配置

附脚本:

#!/bin/bash
# -*- coding: utf-8 -*-
# @Date : 2018-4-24
# @Author : nie
REPOD="/root"
if [ "$(ls -A $REPOD)" ] ; then
[ -d ${REPOD}/.pip ] || /bin/mkdir ${REPOD}/.pip
fi
if [ -a ${REPOD}/.pip/pip.conf ] ; then
/bin/mv ${REPOD}/.pip/pip.conf ${REPOD}/.pip/pip.conf_$(date "+%Y%m%d") -f
fi
/bin/cat >>${REPOD}/.pip/pip.conf <<EOF
[global]
INdex-url=http://xxx.xxx.xxx.xxx/simple
trusted-host=xxx.xxx.xxx.xxx
EOF
exit 1

weinxin
聂扬帆博客
一个分享IT运维相关工作经验和实战技巧的个人博客

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: