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