使用apt-mirror搭建本地apt伺服器(by quqi99)
問題
你懂的,國內環境訪問ubuntu-cloud.archive.canonical.com會非常慢,本文將使用apt-mirror搭建一個本地apt伺服器以提升辦公效率。
安裝
sudo ufw disable
sudo apt -y install apache2 apt-mirror
mkdir -p /images/xenial-repo
$ cat /etc/apt/mirror.list
############# config ##################
#
set base_path /images/xenial-repo
#set defaultarch amd64
#
# 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 30
set _tilde 0
#
############# end config ##############
#deb http://cn.archive.ubuntu.com/ubuntu xenial main restricted universe multiverse
#deb http://cn.archive.ubuntu.com/ubuntu xenial-security 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-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
#see http://ubuntu-cloud.archive.canonical.com/ubuntu/dists/xenial-updates/
deb http://ubuntu-cloud.archive.canonical.com/ubuntu xenial-updates/ocata main
#deb http://ubuntu-cloud.archive.canonical.com/ubuntu xenial-updates/queens main
#clean http://archive.ubuntu.com/ubuntu
clean http://ubuntu-cloud.archive.canonical.com/ubuntu
執行
sudo touch /images/xenial-repo/var/postmirror.sh
sudo apt-mirror
或者執行“sudo crontab -e”新增下列crob在每天四點半執行:
30 4 * * * root /usr/bin/apt-mirror >> /var/log/apt-mirror.log
配置Apache
Apache的已有配置如下:
$ sudo grep -r 'DocumentRoot' /etc/apache2/sites-available/000-default.conf
DocumentRoot /var/www/html
$ grep -r '/var/www/' /etc/apache2/apache2.conf -A 4
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
建立軟鏈及注意一個關鍵易犯問題
sudo ln -s /images/xenial-repo /var/www/html/apt-mirror
sudo chown -R www-data:www-data /images
sudo service apache2 reload
注:上面這一句”chown -R www-data:www-data /images”異常關鍵,網上幾乎所有能搜尋的答案全部是關於新增’Options FollowSymLinks’的,其實如上/etc/apache2/apache2.conf檔案中已經包含該設定。apache 2.4使用的目錄的使用者名稱和組必須全部是www-data,否則/var/log/apache2/error.log會報這個錯誤“Symbolic link not allowed or link target not accessible”(WEB頁上看到的錯誤是‘You don’t have permission to access”)。可用該命令“sudo -u www-data ls /var/www/html/apt-mirror”驗證它是否成功。之前我一直在這個地方犯錯是因為我執行的是“chown -R www-data:www-data /images/xenial-repo”,這樣導致仍然無法順利執行““sudo -u www-data ls /var/www/html/apt-mirror”命令。
使用它
touch /etc/apt/sources.list.d/mymirror.list
cat > /etc/apt/sources.list.d/mymirror.list <<EOF
#deb http://node1/apt-mirror/mirror/cn.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse
deb http://node1/apt-mirror/mirror/ubuntu-cloud.archive.canonical.com/ubuntu/ xenial-updates/ocata main
EOF
rm -rf /etc/apt/sources.list.d/cloudarchive-ocata.list*
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 5EDB1B62EC4926EA
apt update
20171101更新
此次apt update慢的原因最後通過”Software & Update -> Ubuntu Software -> Download from Other - Select Best Server”將之前的mirrors.aliyun.com改成mirrors.yun-idc.com就好了。之前各種源也都換過,為什麼這次行了具體原因不明待下次遇到了待查。
20171115更新
今天在安裝libvirt0時總是發生包依賴問題(libvirt0: Depends: libxen-4.6 (>=4.6.5) but 4.6.0-1ubuntu4.3 is to be installed),原來是mirrors.aliyun.com的問題,這個源更新不及時會造成眾多問題。
為了克服這種更新不及時造成的問題,看樣子還是最好別搭私有源和使用國內源,所以換回官方源:
deb http://nova.clouds.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://nova.clouds.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse
deb http://nova.clouds.archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://nova.clouds.archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://nova.clouds.archive.ubuntu.com/ubuntu/ xenial-security main restricted universe multiverse