创建私有PIP源

1. pip 使用国内源

PIP 命令默认使用国外源,但是下载速度太慢,不仅浪费时间,而且可能出现下载后安装出错的情况。因此将pip安装源替换成国内镜像,不仅可以大幅降低下载时间,还能够提高安装成功率。国内主要镜像源如下,注意当http下载失败时尝试更换https。

阿里源:http://mirrors.aliyun.com/pypi/simple
豆瓣源:http://pypi.douban.com/simple
清华源:https://pypi.tuna.tsinghua.edu.cn/simple

临时使用国内源 pip install numpy -i http://pypi.douban.com/simple,如果报错则在后面添加 trusted-host 参数。

  1. $ pip install numpy -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

如果不想每次使用国内源都添加参数,那么可以修改 pip 配置文件,一劳永逸。首先创建 pip 命令的配置文件 ~/.pip/pip.conf

  1. $ cd ~ && mkdir .pip && touch .pip/pip.conf

然后添加下载源信息并保存,之后即可正常使用命令从国内源下载并安装 python 包,例如 pip install numpy

[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com

2. 创建私有的 pip 源

出于安全因素,企业内多数服务器不能访问外网,这些服务可能有安装某些 python package 的需求,所以很有必要搭建企业私有的 pypi 源。

2.1 使用 pypi-server 创建 pip 源

连接外网的计算机作为服务器,使用 pypi-server 创建私有 pip 源。该方法简便快捷,但是需要手动下载所需要的安装包,适合独立的小项目使用。

  1. # 安装
  2. $ pip install pypiserver
  3. # 创建安装包存储文件夹
  4. $ mkdir /home/packages
  5. # copy some source packages or eggs to this directory
  6. $ cd /home/packages && pip download numpy
  7. # 启动,不添加参数默认使用 8080 端口和 packages 文件夹
  8. $ pypi-server
  9. # 指定端口和文件夹启动服务
  10. $ pypi-server -p 9090 /home/packages

无法连接外网的内网计算机作为客户端使用服务器的pip源下载包,假设服务器的 IP 地址为 555.666.777.888

  1. $ pip install numpy -i http://555.666.777.888:9090/simple --trusted-host 555.666.777.888

2.2 使用 bandersnatch 创建 pip 源

bandersnatch 能够自动下载和同步官网所有的安装包到本地 packages,需要非常大的存储空间,适合作为公司的私有源供内部多个项目使用。

  1. # 安装
  2. $ pip install bandersnatch
  3. # 生成配置文件
  4. $ bandersnatch mirror
  5. # 修改配置文件 bandersnatch.conf
  6. # 第3行 directory 参数值同步的 package 在本地存放的位置
  7. # 第11行参数 master 指所同步的下载源
  8. $ sed -i "3c directory = /home/pypi" /etc/bandersnatch.conf
  9. $ sed -i "11c master = http://pypi.python.org" /etc/bandersnatch.conf

Download and Synchronize, 并将官网源修改为豆瓣源。bandersnatch/master.py 第39行get函数的作用是下载文件,复制 get 函数并修改为 get_cn 函数

  1. def get_cn(self, path, required_serial, **kw):
  2. logger.debug(f"Getting {path} (serial {required_serial})")
  3. if not path.startswith(("https://", "http://")):
  4. path = self.url + path
  5. # r = self.session.get(path, timeout=self.timeout, **kw)
  6. # url_cn = "http://pypi.douban.com"
  7. url_cn = "http://mirrors.aliyun.com/pypi/simple"
  8. path = path.replace(self.url, url_cn)
  9. r = self.session.get(path, timeout=self.timeout, **kw)
  10. r.raise_for_status()

将 bandersnatch/package.py 中第357行调用 master.get 方法修改为 master.get_cn

  1. r = self.mirror.master.get_cn(url, required_serial=None, stream=True)

重新启动服务

  1. $ bandersnatch mirror

下载和同步需要大量的时间,有时候网速不好可能会中断下载。因此需要监控bandersnatch服务,当下载进程中断时重新启动。
创建监控脚本 /home/monitor/bander_snatch.sh,添加如下内容,然后执行该脚 ./bander_snatch.sh,如果希望不中断的进行监控,那么执行命令 nohup bander_snatch.sh &

  1. #!/bin/bash
  2. base_path = $(cd `dirname $0`; pwd)
  3. while true
  4. do
  5. process_number = `ps -ef | grep "bandersnatch" | grep -v grep | wc -l`
  6. if [ $process_number -eq 0 ]
  7. then
  8. bandersnatch mirror # 需要重新执行的命令
  9. echo `date +%Y-%m-%d` `date +%H:%M:%S` "restart bandersnatch" >> $base_path/bandersnatch.log
  10. fi
  11. sleep 300 # 每300秒监控一次
  12. done

3. 使用 nginx 发布本地 pip 源

在容器中挂载 pypi 下载包的存储路径,服务器的 IP 地址为 555.666.777.888,容器绑定的主机端口号为 7788,启动 nginx 服务

  1. $ docker run -tid -v /home/pypi:/mnt/pypi -p 7788:80 --name nginx_pypi centos:7.5 /bin/bash
  2. $ docker exec -it nginx_pypi /bin/bash --login

进入容器之后,安装 nginx,修改配置文件,添加 pypi 文件夹,并启动 nginx 服务。

3.1 安装 nginx

  1. $ yum install nginx

在默认情况下,由于 Centos7 中没有 nginx 的下载源,直接使用 yum 无法安装成功。有如下三种解决方案,推荐使用第一种。

3.1.1 官方yum源

  1. $ rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装完成后,可以在 /etc/yum.repos.d 目录中看到名为 nginx.repo 的文件,其内容如下

  1. $ cat /etc/yum.repos.d/nginx.repo
  2. # 内容如下
  3. [nginx]
  4. name=nginx repo
  5. baseurl=http://nginx.org/packages/centos/7/$basearch/
  6. gpgcheck=0
  7. enabled=1
  8. # 重新安装 nginx
  9. $ yum install -y nginx

安装成功之后即可使用命令 whereis nginx 检查安装目录,nginx各种文件默认路径如下:

  • (1) Nginx 配置路径: /etc/nginx
  • (2) PID目录:/var/run/nginx.pid
  • (3) 错误日志:/var/log/nginx/error.log
  • (4) 访问日志:/var/log/nginx/access.log
  • (5) 默认站点目录:/usr/share/nginx/html

事实上,只需要知道 Nginx 配置文件的路径,其他路径均可在 /etc/nginx/nginx.conf/etc/nginx/conf.d/default.conf 中查询到。

3.1.2 官网方法

参照 官网 的方法,
首先安装依赖文件 yum install yum-utils,然后新建文件 /etc/yum.repos.d/nginx.repo,和4.1中文件相似,添加如下内容。

  1. [nginx-stable]
  2. name=nginx stable repo
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
  4. gpgcheck=0
  5. enabled=1
  6. gpgkey=https://nginx.org/keys/nginx_signing.key
  7. [nginx-mainline]
  8. name=nginx mainline repo
  9. baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
  10. gpgcheck=0
  11. enabled=0
  12. gpgkey=https://nginx.org/keys/nginx_signing.key

默认使用 nginx-stable 稳定版本,若要使用 nignx-mainline,则需要修改参数 enabled 的值,若参数 gpgcheck 的值不为0,则需要验证 GPG key。
此时,即可重新使用命令 yum install nginx 安装 nginx。

3.1.3 源码安装

  1. # 先检查是否已经安装
  2. $ find -name nginx
  3. # 卸载
  4. $ yum remove nginx
  5. # 依赖包
  6. $ yum install gcc-c++
  7. $ yum install pcre pcre-devel
  8. $ yum install zlib zlib-devel
  9. $ yum install openssl openssl-devel
  10. # 下载源码包
  11. $ wget http://nginx.org/download/nginx-1.12.2.tar.gz
  12. $ tar -zxvf nginx-1.12.2.tar.gz
  13. $ cd nginx-1.12.2
  14. $ ./configure && make && make install
  15. # 启动 nginx
  16. $ cd /usr/local/nginx/sbin && ./nginx

3.2 Nginx常用命令

  1. # 启动
  2. $ nginx
  3. # 测试Nginx配置是否正确
  4. $ nginx -t
  5. # 优雅重启
  6. $ nginx -s reload
  7. # 查看nginx进程号
  8. $ ps -ef | grep nginx
  9. # 停止
  10. $ nginx -s stop

如果启动 Nginx 时端口被占用,使用如下命令关闭 nginx 进程

  1. $ yum install psmisc
  2. $ killall -9 nginx

3.3 配置 nginx 服务

修改文件 /etc/nginx/nginx.conf,在 http 服务器模块添加 server{} 中的内容

  1. http {
  2. ...
  3. # gzip on;
  4. # include /etc/nginx/conf.d/*.conf;
  5. server {
  6. listen *:80; # 监听端口
  7. server_name localhost; # 服务器IP地址
  8. root /home/pypi/web; # python包存储路径
  9. autoindex on;
  10. charset utf-8;
  11. }
  12. }

配置完成后即可启动 nginx 服务

  1. $ nginx

4. 客户端 pip 配置文件

创建 pip 命令配置文件 ~/.pip/pip.conf

  1. $ cd && mkdir .pip && touch .pip/pip.conf

添加下载源

[global]
index-url = http://555.666.777.888:7788/simple
[install]
trusted-host = 555.666.777.888

在没有连接外网的客户端使用 pip 命令下载 python 包

  1. $ pip install numpy
-------------本文结束 感谢阅读-------------
0%