使用apt-mirror搭建本地apt服务器(by quqi99)

来源:互联网 发布:阿里云备案在哪里 编辑:程序博客网 时间:2024/06/05 14:16

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99)

问题

你懂的,国内环境访问ubuntu-cloud.archive.canonical.com会非常慢,本文将使用apt-mirror搭建一个本地apt服务器以提升办公效率。

安装

sudo ufw disablesudo apt -y install apache2 apt-mirrormkdir -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 0set nthreads     30set _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/ubuntuclean http://ubuntu-cloud.archive.canonical.com/ubuntu

运行

sudo touch /images/xenial-repo/var/postmirror.shsudo 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-mirrorsudo chown -R www-data:www-data /imagessudo 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.listcat > /etc/apt/sources.list.d/mymirror.list <<EOF#deb http://node1/apt-mirror/mirror/cn.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiversedeb http://node1/apt-mirror/mirror/ubuntu-cloud.archive.canonical.com/ubuntu/ xenial-updates/ocata mainEOFrm -rf /etc/apt/sources.list.d/cloudarchive-ocata.list*apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 5EDB1B62EC4926EAapt 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 multiversedeb-src http://nova.clouds.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiversedeb http://nova.clouds.archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiversedeb-src http://nova.clouds.archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiversedeb http://nova.clouds.archive.ubuntu.com/ubuntu/ xenial-security main restricted universe multiverse
原创粉丝点击