apt-mirror搭建ubuntu离线软件源
一、准备环境
1. 系统:ubuntu 16.04.6(LTS) 64位
2. 镜像软件:apt-mirror 0.5.1-1ubuntu1
3. HTTP协议发布软件:nginx 1.10.3-0ubuntu0.16.04.4
二、安装软件
# 查看apt-mirror版本 macrored@ubuntu:~$ apt policy apt-mirror apt-mirror: Installed: 0.5.1-1ubuntu1 Candidate: 0.5.1-1ubuntu1 # 安装apt-mirror macrored@ubuntu:~$ sudo apt-get install apt-mirror # 查看nginx版本 macrored@ubuntu:~$ apt policy nginx nginx: Installed: 1.10.3-0ubuntu0.16.04.4 Candidate: 1.10.3-0ubuntu0.16.04.4 # 安装nginx macrored@ubuntu:~$ sudo apt-get install nginx
三、配置apt-mirror
打开配置文件:
sudo vi /etc/apt/mirror.list
根据注释修改相应内容,一般只需要修改base_path和更改、添加软件源。以下配置文件我只添加了Ubuntu 16.04和MongoDB的软件源,你可以根据你的需要添加Ubuntu其他版本软件源。配置文件可参考如下:
############# config ################## # # 设置数据根目录 set base_path /home/mirror-data/apt-mirror # # set mirror_path $base_path/mirror # set skel_path $base_path/skel # set var_path $base_path/var # set cleanscript $var_path/clean.sh # set defaultarch <running host architecture> # set postmirror_script $var_path/postmirror.sh # set run_postmirror 0 # 20个线程同时下载 set nthreads 20 # 是否替换URL中的波浪线,替换成%7E(HTML代码),否则会跳过不进行下载 set _tilde 0 # ############# end config ############## # 等会将会从这些网站上获取离线包,如果系统是64位,默认只下载离线的64位离线包;如果想更改,可以用deb-amd64和deb-i386 deb http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse deb http://cn.archive.ubuntu.com/ubuntu/ xenial-proposed main restricted universe multiverse deb http://cn.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse deb http://security.ubuntu.com/ubuntu xenial-security main restricted universe multiverse # deb-src http://cn.archive.ubuntu.com/ubuntu xenial main restricted universe multiverse # deb-src http://cn.archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse # deb-src http://cn.archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse # deb-src http://cn.archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse # deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted universe multiverse clean http://cn.archive.ubuntu.com/ubuntu clean http://security.ubuntu.com/ubuntu
保存退出后,接下来就可以开始下载离线镜像包了(第一次会比较久,大约需要150GB左右),直接运行命令:
sudo apt-mirror
如果CPU资源和网络资源都没有被占用的话,那就尝试将http://archive.ubuntu.com换国内的镜像源。
此外,还可以设置定时更新镜像源,我们需要配置apt-mirror每天定时同步,其实就是配置cron。这里apt-mirror提供了cron模板文件,在/etc/cron.d/apt-mirror 中,取消最后一行的#注释即可生效:
0 4 * * * apt-mirror /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron.log
以上设置每日4点同步一次。
四、配置HTTP访问
通过以上配置,已经可以在/home/mirror-data/apt-mirror目录下看到生成的几个数据文件。/home/mirror-data/apt-mirror下存放的就是软件镜像。但是如何向其他计算机发布这些数据呢?很明显通过HTTP服务可以做到这一点。我们通过使用Nginx将mirror目录下的内容通过HTTP协议发布。
打开配置文件:
sudo vi /etc/nginx/sites-enabled/default
配置文件参考如下:
## # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # http://wiki.nginx.org/Pitfalls # http://wiki.nginx.org/QuickStart # http://wiki.nginx.org/Configuration # # Generally, you will want to move this file somewhere, and start with a clean # file but keep this around for reference. Or just disable in sites-enabled. # # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. ## # Default server configuration # server { listen 80 default_server; # listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; # 显示目录 autoindex on; # 镜像所在的目录 root /home/mirror-data/apt-mirror/mirror; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; # 主机域名,填写localhost就是通过IP访问 server_name 192.168.0.108; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # 访问记录 access_log /home/mirror-data/apt-mirror.log; # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php7.0-fpm: # fastcgi_pass unix:/run/php/php7.0-fpm.sock; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # Virtual Host configuration for example.com # # You can move that to a different file under sites-available/ and symlink that # to sites-enabled/ to enable it. # #server { # listen 80; # listen [::]:80; # # server_name example.com; # # root /var/www/example.com; # index index.html; # # location / { # try_files $uri $uri/ =404; # } #}
保存配置文件后,先测试配置:
sudo nginx -t
如果测试通过,表示配置文件没有问题,那么就可以开启服务:
sudo service nginx restart
此时访问“http://服务器IP”即可访问到你发布的镜像。
五、客户端配置
在客户端的source.list文件中配置好镜像源就可以使用了。找另外一台ubuntu 16.04 64位系统,首先备份source.list:
sudo mv /etc/apt/source.list /etc/apt/source.list.bak
新建/etc/apt/source.list,内容参考如下:
deb [arch=amd64] http://自建源IP/cn.archive.ubuntu.com/ubuntu xenial main restricted universe multiverse deb [arch=amd64] http://自建源IP/cn.archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse deb [arch=amd64] http://自建源IP/cn.archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse deb [arch=amd64] http://自建源IP/cn.archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse deb [arch=amd64] http://自建源IP/security.ubuntu.com/ubuntu xenial-security main restricted universe multiverse # deb-src [arch=amd64] http://自建源IP/cn.archive.ubuntu.com/ubuntu xenial main restricted universe multiverse # deb-src [arch=amd64] http://自建源IP/cn.archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse # deb-src [arch=amd64] http://自建源IP/cn.archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse # deb-src [arch=amd64] http://自建源IP/cn.archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse # deb-src [arch=amd64] http://自建源IP/security.ubuntu.com/ubuntu xenial-security main restricted universe multiverse
保存/etc/apt/source.list,更新镜像源缓存:
sudo apt-get update
然后就大功告成了,可以随意下载一个软件测试一下:
sudo apt-get install htop