版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (http://blog.csdn.net/quqi99)
你懂的,国内环境访问ubuntu-cloud.archive.canonical.com会非常慢(注: 其实下列方法可以同步UA, UCA, PPA),本文将使用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
# 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的已有配置如下:
$ 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
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
创建软链及注意一个关键易犯问题
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”命令。
也可创建/etc/apache2/sites-available/ubuntu-mirror.conf使用VirtualHost同时支持对UA, UCA, PPT的同步:
ServerName your-repo.yourdomain.com
DocumentRoot /var/spool/apt-mirror/mirror/archive.ubuntu.com/
LogLevel info
ErrorLog /var/log/apache2/mirror-archive.ubuntu.com-error.log
CustomLog /var/log/apache2/mirror-archive.ubuntu.com-access.log combined
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
ServerName your-cloudrepo.yourdomain.com
DocumentRoot /var/spool/apt-mirror/mirror/ubuntu-cloud.archive.canonical.com/
LogLevel info
ErrorLog /var/log/apache2/mirror-cloudarchive.canonical.com-error.log
CustomLog /var/log/apache2/mirror-cloudarchive.canonical.com-access.log combined
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
ServerName your-ppa.yourdomain.com
DocumentRoot /var/spool/apt-mirror/mirror/ppa.launchpad.net/
LogLevel info
ErrorLog /var/log/apache2/mirror-ppaarchive.canonical.com-error.log
CustomLog /var/log/apache2/mirror-ppaarchive.canonical.com-access.log combined
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
或要支持ssl,再添加:
SSLCACertificatePath /etc/ssl/certs
SSLCertificateFile /etc/pki/tls/certs/mirror.crt
SSLEngine On
SSLCertificateKeyFile /etc/pki/tls/private/mirror.key
之后执行:
a2enmod ssl
a2ensite *
systemctl restart apache2
touch /etc/apt/sources.list.d/mymirror.list
cat > /etc/apt/sources.list.d/mymirror.list <
此次apt update慢的原因最后通过"Software & Update -> Ubuntu Software -> Download from Other - Select Best Server"将之前的mirrors.aliyun.com改成mirrors.yun-idc.com就好了。之前各种源也都换过,为什么这次行了具体原因不明待下次遇到了待查。
今天在安装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
sudo apt -y install simplestreams
sudo mkdir -p /var/spool/sstreams/maas && sudo chown -R $USER /var/spool/sstreams/maas
#MAAS images simplestreams
workdir=/var/spool/sstreams/maas
sstream-mirror --keyring=/usr/share/keyrings/ubuntu-cloudimage-keyring.gpg --progress --max=1 https://images.maas.io/ephemeral-v3/daily/ $workdir 'arch=amd64' 'release~(bionic)'
sstream-mirror --keyring=/usr/share/keyrings/ubuntu-cloudimage-keyring.gpg --progress --max=1 https://images.maas.io/ephemeral-v3/daily/ $workdir 'os~(grub*|pxelinux)'
#Use MAAS images simplestreams
maas root boot-source update 1 url=https://maas-images.yourdomain.com/ keyring_filename=/usr/share/keyrings/ubuntu-cloudimage-keyring.gpg
maas root domains create name="yourdomain.com"
maas root dnsresources create fqdn="maas-images.yourdomain.com" ip_addresses= #orconfigure the mirror in infra.yaml
#Juju agents simplestreams
workdir=/var/spool/sstreams/juju
sstream-mirror --no-verify --progress --max=2 --path=streams/v1/index2.sjson https://streams.canonical.com/juju/tools/ $workdir 'arch=amd64' 'release~(bionic)' 'version~(2.6)'
#Use Juju agents simplestreams
#let juju know where to get contrainers from, or defined it in juju-model-default.yaml
juju model-config container-image-stream=released container-image-metadata-url=https://lxd-images.yourdomain.com/ image-metadata-url=https://lxd-images.yourdomain.com
#let juju know where to fetach the agents, or defined it in juju-model-default.yaml
juju model-config agent-metadata-url=https://juju-agents.yourdomain.com/ agent-stream=released
#LXD and KVM images simplestreams
workdir=/var/spool/sstreams/lxdkvm
sudo sstream-mirror --keyring=/usr/share/keyrings/ubuntu-cloudimage-keyring.gpg --progress --max=1 --path=streams/v1/index.json https://cloud-images.ubuntu.com/releases/ $workdir 'arch=amd64' 'release~(bionic)' 'ftype~(lxd.tar.xz|squashfs|root.tar.xz|root.tar.gz|disk1.img|.json|.sjson)'
sudo sstream-mirror --keyring=/usr/share/keyrings/ubuntu-cloudimage-keyring.gpg --progress --max=1 --path=streams/v1/index.sjson https://cloud-images.ubuntu.com/releases/ $workdir 'arch=amd64' 'release~(bionic)' 'ftype~(lxd.tar.xz|squashfs|root.tar.xz|root.tar.gz|disk1.img|.json|.sjson)'
https://cloud-images.ubuntu.com/releases/ $workdir 'arch=amd64' 'release~(bionic)' 'ftype~(lxd.tar.xz|squashfs|root.tar.xz|root.tar.gz|disk1.img|.json|.sjson)'
sudo apt install apache2 -y
cat < /etc/apache2/sites-available/sstreams-mirror.conf
ServerName maas-images.yourdomain.com
DocumentRoot /var/spool/sstreams/maas
LogLevel info
ErrorLog /var/log/apache2/mirror-maas-images-error.log
CustomLog /var/log/apache2/mirror-maas-images-access.log combined
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
ServerName lxdkvm-images.yourdomain.com
DocumentRoot /var/spool/sstreams/lxdkvm
LogLevel info
ErrorLog /var/log/apache2/mirror-lxdkvm-error.log
CustomLog /var/log/apache2/mirror-lxdkvm-access.log combined
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
ServerName juju-agents.yourdomain.com
DocumentRoot /var/spool/sstreams/juju
LogLevel info
ErrorLog /var/log/apache2/mirror-juju-error.log
CustomLog /var/log/apache2/mirror-juju-access.log combined
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
EOF