apt-mirror搭建ubuntu软件源

Posted macrored

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 vim /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
set nthreads     20
set _tilde 0
#
############# end config ##############

# 等会将会从这些网站上获取离线包,如果系统是64位,默认只下载离线的64位离线包;如果想更改,可以用deb-amd64和deb-i386
deb http://archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse

# deb-src http://archive.ubuntu.com/ubuntu xenial main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse

clean http://archive.ubuntu.com/ubuntu

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

clean http://localhost/downloads-distro.mongodb.org

保存退出后,接下来就可以开始下载离线镜像包了(第一次会比较久,大约需要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协议发布。

打开配置文件:

##
# 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;

    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;
#    
#

五、客户端配置

在客户端的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/archive.ubuntu.com/ubuntu trusty main restricted universe multiverse
deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse

# deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty main restricted universe multiverse
# deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
# deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
# deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
# deb-src [arch=amd64] http://自建源IP/archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse

保存/etc/apt/source.list,更新镜像源缓存:

sudo apt-get update

然后就大功告成了,可以随意下载一个软件测试一下:

sudo apt-get install htop

 

以上是关于apt-mirror搭建ubuntu软件源的主要内容,如果未能解决你的问题,请参考以下文章

利用apt-mirror制作Ubuntu本地源

搭建ubuntu arm/arm64的本地源

ubuntu下的apt-get内网本地源的搭建

使用apt-mirror搭建debian镜像源

ubuntu apt-mirror 同步源到本地

Ubuntu16.04建立本地更新源