记一次centos7部署pip私有源
2021-11-29 本文已影响0人
带着小猪闯天下
背景
因公司推行桌面云办公,且桌面云无法连接外网,需要自建pip源供下载python相关软件
一,环境准备
操作系统:centos7.6
关闭防火墙 selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0 (临时关闭)
永久关闭
image.png
二,环境准备
1),安装Python3.7
1,安装编译工具(建议第一次装完在检查一遍)
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum install libffi-devel -y
2,下载安装包
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
3,安装
tar -xvJf Python-3.7.0.tar.xz
mkdir /usr/local/python3
cd Python-3.7.0
./configure --prefix=/usr/local/python3
make && make install
4,创建软链接
ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
5,验证
[root@testdb simple]# python -V
Python 2.7.5
[root@testdb simple]# pip3 -V
pip 21.3.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)
2),自建源部署pip2pi
1,安装pip2pi
pip install pip2pi
2,编写requirements.txt(需要使用的安装包写到此文件中)
#cat requirements.txt
ansible>=2.5.0,!=2.7.0
jinja2>=2.9.6
netaddr
pbr>=1.6
hvac
Django
3,下载
pip download -r requirements.txt -d /opt/repos/pip-packages/
##(/opt/repos/pip-packages/为下载包存放地址,可自定义修改)
下载完成后可在对应目录看到下载的文件
image.png
4,创建index索引
dir2pi /opt/repos/pip-packages/
下载测试
image.png
三,部署nginx
1)安装nginx服务
创建nginx.repo源
[root@testdb yum.repos.d]# cat nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
安装
yum -y install nginx
2)配置nginx服务
server {
···
location / {
root /opt/repos; #根目录修改成存放pip源安装包位置,其他无需改动
# autoindex on;
# autoindex_exact_size off;
# autoindex_localtime on;
}
···
3)启动nginx
systemctl restart nginx
至此自建pip源搭建完成
如果有新增加的源文件,将安装包名写到requirements.txt文件
执行命令
pip download -r requirements.txt -d /opt/repos/pip-packages/
dir2pi /opt/repos/pip-packages/ (一定要记得创建index索引)
测试
在另一台python环境机器配置本地pip源地址
/root/.pip/pip.conf 此文件及路径自己创建
[root@pipser02 ~]# cat /root/.pip/pip.conf
[global]
trusted-host = 192.188.2.202 #自建pip源ip地址
index-url = http://192.168.2.202/pip-packages/simple
PS:要求将所有pip源下载到本地,但是PIP不同于YUM,整了好几天给我整麻了,所以我放弃了,选择了另外一种方式—>用nginx做转发,也能实现。
SERVER端nginx配置(使用豆瓣源)
[root@pip-server01 conf.d]# cat default.conf
server {
···
location / {
# root /opt/repo;
proxy_pass http://pypi.doubanio.com;
# autoindex on;
# autoindex_exact_size off;
# autoindex_localtime on;
}
···
AGENT端配置
[root@testdb ~]# cat .pip/pip.conf
[global]
trusted-host = 192.188.2.202
index-url = http://192.188.2.202/simple/
测试能用(收!)
image.png