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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM