个人工具

“UbuntuHelp:Apt-Cacher-Server”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
 
(未显示同一用户的8个中间版本)
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/Apt-Cacher-Server}}
 
{{From|https://help.ubuntu.com/community/Apt-Cacher-Server}}
 
{{Languages|UbuntuHelp:Apt-Cacher-Server}}
 
{{Languages|UbuntuHelp:Apt-Cacher-Server}}
 
 
 
 
== Introduction ==
 
== Introduction ==
 
 
This page describes the installation and configuration of apt-cacher.  It was drawn from http://www.debuntu.org/how-to-set-up-a-repository-cache-with-apt-cacher and Ubuntu Hacks by Oxer, Rankin, and Childers http://www.oreilly.com/catalog/ubuntuhks/
 
This page describes the installation and configuration of apt-cacher.  It was drawn from http://www.debuntu.org/how-to-set-up-a-repository-cache-with-apt-cacher and Ubuntu Hacks by Oxer, Rankin, and Childers http://www.oreilly.com/catalog/ubuntuhks/
 
+
''Added by pablodav:In Newer installations like Jaunty I recommend use this cacher instead others like apt-proxy.
 +
=== Warning: Using Apt-Cacher with more than one distribution ===
 +
Because Debian and Ubuntu have identically named (but different) .deb packages in their repositories, it is unwise to setup a single apt-cacher to be a caching package server for both Debian and Ubuntu clients at the same time. A workaround for advanced users is to run two separate instances of apt-cacher on two separate ports with two separate caches.
 
== Server Installation ==
 
== Server Installation ==
 +
1. Install apt-cacher and apache2 webserver
  
1. Install apt-cacher and apache2 webserver
 
 
sudo apt-get install apt-cacher apache2
 
sudo apt-get install apt-cacher apache2
 +
2. Enable apt-cacher
  
2. Enable apt-cacher
 
 
Edit /etc/default/apt-cacher and change autostart to 1
 
Edit /etc/default/apt-cacher and change autostart to 1
 
 
Note: I had problems doing this on a machine with apache already installed.
 
Note: I had problems doing this on a machine with apache already installed.
 
+
''Added by JT: Restart apache if you already had it installed:''  <code><nowiki>sudo invoke-rc.d apache2 restart</nowiki></code>
 +
''Added by pablodav: I suggest to restart apt-cacher also: <code><nowiki>sudo invoke-rc.d apt-cacher restart</nowiki></code>
 
Then test by going to http://server/apt-cacher to verify that it's running.
 
Then test by going to http://server/apt-cacher to verify that it's running.
 
+
''Added by pablodav: In Jaunty use http://server:3142 instead http://server/apt-cacher (this seems to be not working anymore with /apt-cacher).
 
== Server Configuration ==
 
== Server Configuration ==
 
 
Edit /etc/apt-cacher/apt-cacher.conf if you want to change any options.  The only one I changed was admin_email.
 
Edit /etc/apt-cacher/apt-cacher.conf if you want to change any options.  The only one I changed was admin_email.
 
 
Import any existing apt-get cache:
 
Import any existing apt-get cache:
 
sudo /usr/share/apt-cacher/apt-cacher-import.pl /var/cache/apt/archives
 
sudo /usr/share/apt-cacher/apt-cacher-import.pl /var/cache/apt/archives
 
+
''Added by pablodav: I strongly recommend to use -d option for symlinks, In Jaunty I had to use sudo /usr/share/apt-cacher/apt-cacher-import.pl -s /var/cache/apt/archives
== Client Configuration ==
+
=== Load Packages on Server from CD ===
 
+
Around upgrade time, you may find it useful to get the CD image instead of using the slow update servers, and populate your cache using that. 
 +
First, download your CD image(s) of choice (I'd recommend using bit-torrent).
 +
Second, mount the CD image on the server running apt-cacher :
 +
<pre><nowiki>
 +
sudo mount -o loop /home/username_or_other_path/ubuntu-9.10-rc-alternate-i386.iso /media/cdrom0
 +
</nowiki></pre>
 +
Third, run the import on the CD image, you need the -R is needed to recurse into the CD directory structure, the -r just makes sure they are copied to the cache instead of trying to link:
 +
<pre><nowiki>
 +
sudo /usr/share/apt-cacher/apt-cacher-import.pl -R -r /media/cdrom0
 +
</nowiki></pre>
 +
You should see the script saying that it is importing a lot of packages. 
 +
== Client Configuration Options ==
 
There are two ways to configure your clients to use apt-cacher.
 
There are two ways to configure your clients to use apt-cacher.
 
+
''Added by pablodav: I don't recommend to change every line with step 1. In Jaunty /apt-cache does not works. Jump to step 2 for Client Configuration.
1. Modify your sources.list
+
=== Modify your sources.list ===
 
In client /etc/apt/sources.list insert apt-cacher-server/apt-cacher/ in each line, ie
 
In client /etc/apt/sources.list insert apt-cacher-server/apt-cacher/ in each line, ie
 
deb http://archive.ubuntu.com/ubuntu/ dapper main restricted
 
deb http://archive.ubuntu.com/ubuntu/ dapper main restricted
 
becomes
 
becomes
 
deb http://apt-cacher-server/apt-cacher/archive.ubuntu.com/ubuntu/ dapper main restricted
 
deb http://apt-cacher-server/apt-cacher/archive.ubuntu.com/ubuntu/ dapper main restricted
 +
(Warning: The apt-cacher web page advises to also add ':3142' - this caused the system to fail for me)
 +
''Added by JT: on gutsy and hardy, I could not get this to work with'' <code><nowiki>server/apt-cacher/</nowiki></code> ''but'' <code><nowiki>server:3142/</nowiki></code> ''(NB: without the /apt-cacher/) worked fine.''
 +
''To clarify, use eg.'' <code><nowiki>deb http://apt-cacher-server:3142/archive.ubuntu.com/ubuntu/ hardy main restricted</nowiki></code>
 +
''Scientus: I think this is because apt-cacher is its own http server and this setup use apache''
 
Then run
 
Then run
 
apt-get update
 
apt-get update
 
+
=== Use as a proxy to APT ===
2. Use a proxy.
+
==== Static configuration ====
 
In a terminal, type:
 
In a terminal, type:
 
 
<pre><nowiki>
 
<pre><nowiki>
 
sudo nano /etc/apt/apt.conf.d/01proxy
 
sudo nano /etc/apt/apt.conf.d/01proxy
 
</nowiki></pre>
 
</nowiki></pre>
 
 
Inside your new file, add a line that says:
 
Inside your new file, add a line that says:
 
 
<pre><nowiki>
 
<pre><nowiki>
 
Acquire::http::Proxy "http://<IP address or hostname of the apt-cacher server>:3142";
 
Acquire::http::Proxy "http://<IP address or hostname of the apt-cacher server>:3142";
 
</nowiki></pre>
 
</nowiki></pre>
 +
==== "Roaming" mode ====
 +
This method is useful if you are alternating between office and home with a laptop for example. It involves using the ping command to determine if the apt-cacher server is available at boot-time and then configure the APT proxy or not.
 +
* Open /etc/rc.local (alt-F2, "gksu gedit /etc/rc.local")
 +
* Change the top from "#!/bin/sh -e" to "#!/bin/bash"
 +
* put this near the end (before "exit 0" if present) replacing "SERVER_NAME_HERE" with your server's resolvable name or it's IP:
 +
<pre><nowiki>
 +
. /lib/lsb/init-functions
 +
log_daemon_msg "Configuring APT cache proxy" "(based on SERVER_NAME_HERE's presence...)"
 +
ping -c 1 SERVER_NAME_HERE &> /dev/null
 +
if [ $? = "0" ]; then
 +
  echo "Acquire::http::Proxy \"http://SERVER_NAME_HERE:3142\";" > /etc/apt/apt.conf.d/01SERVER_NAME_HEREproxy
 +
else
 +
  rm /etc/apt/apt.conf.d/01SERVER_NAME_HEREproxy &> /dev/null
 +
fi
 +
log_end_msg 0
 +
</nowiki></pre>
 +
==== "Roaming" mode alternative solution ====
 +
Added by "nuaimat":
 +
The above method didn't work for me, so I created two sources.list versions, one named sources.list.home and other one named sources.list.work (both inside /etc/apt)
 +
I modified sources.list.home as mentioned above (Static configuration Section)
 +
then I login as root
 +
<pre><nowiki>
 +
  sudo su -
 +
</nowiki></pre>
 +
then created a shell script (don't forget to replace "SERVER_NAME_HERE" with your server's resolvable name or it's IP ) :
 +
<pre><nowiki>
 +
#!/bin/bash
  
/!\ Currently the 2nd method gives an error when upgrading to a newer release. You should use 1st method to get around this. See [https://bugs.launchpad.net/ubuntu/+source/update-manager/+bug/113658 bug 113658 in Launchpad]
+
. /lib/lsb/init-functions
 +
log_daemon_msg "Configuring APT cache proxy" "(based on SERVER_NAME_HERE's presence...)"
 +
unlink /etc/apt/sources.list
  
=== Switching proxy settings ===
+
ping -c 1 SERVER_NAME_HERE &> /dev/null
If you are using a laptop as an apt-cacher client, you won't be able to connect to get any updates when you are away from your network. The easiest way I know of to get around this requires using the proxy method, and editing the /etc/apt/apt.conf.d/01proxy file each time you are away from your network. Add a line to your 01proxy file that says:
+
if [ $? = "0" ]; then
 +
  log_daemon_msg "found live $?"
 +
ln -s /etc/apt/sources.list.home /etc/apt/sources.list &> /dev/null
  
<pre><nowiki>
+
  for i in /etc/apt/sources.list.d/*.list;
Acquire::http::Proxy "http://";
+
  do
</nowiki></pre>
+
  local_online=`grep "SERVER_NAME_HERE" $i|wc -l`
 +
  if [ $local_online = "0" ]; then
 +
  sed -i 's|http://|http://SERVER_NAME_HERE/apt-cacher/|g' $i &> /dev/null
 +
  fi;
 +
done;
 +
else
  
Apt will use whichever line is LAST, so if you are connected to your home network, put this line at the top of the fileIf you are connected to the internet elsewhere, just cut and paste (in nano, Ctrl+k is cut, and Ctrl+u is paste/uncut) this line so that it is at the bottom, and you will get the updates directlyRemember to switch it back when you are back at home, or you will not be using apt-cacher to cache your updates!
+
log_daemon_msg "not found"
 +
ln -s /etc/apt/sources.list.work /etc/apt/sources.list &> /dev/null
 +
   
 +
for i in /etc/apt/sources.list.d/*.list;
 +
  do
 +
  sed -i 's|http://SERVER_NAME_HERE/apt-cacher/|http://|g' $i &> /dev/null
 +
done;
 +
fi
  
There is likely a better/easier way of doing this, but I haven't found it.  If you know the solution, please update this!
 
  
 +
 
  
 +
 +
log_end_msg 0
 +
 +
</nowiki></pre>
 +
* save it as /root/check-apt-cacher
 +
* chmod +x /root/check-apt-cacher
 +
* crontab -e
 +
* using your favourite editor add the following line to the crontabs
 +
<pre><nowiki>
 +
*/1 * * * * /bin/bash /root/check-apt-cacher
 +
</nowiki></pre>
 +
this script will run each minute (*/1) you can change it as you see fit.
 
----
 
----
[[category:CategoryDocumentation]]
+
[[category:CategoryPackageManagement]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2010年5月19日 (三) 21:33的最新版本


Introduction

This page describes the installation and configuration of apt-cacher. It was drawn from http://www.debuntu.org/how-to-set-up-a-repository-cache-with-apt-cacher and Ubuntu Hacks by Oxer, Rankin, and Childers http://www.oreilly.com/catalog/ubuntuhks/ Added by pablodav:In Newer installations like Jaunty I recommend use this cacher instead others like apt-proxy.

Warning: Using Apt-Cacher with more than one distribution

Because Debian and Ubuntu have identically named (but different) .deb packages in their repositories, it is unwise to setup a single apt-cacher to be a caching package server for both Debian and Ubuntu clients at the same time. A workaround for advanced users is to run two separate instances of apt-cacher on two separate ports with two separate caches.

Server Installation

1. Install apt-cacher and apache2 webserver

sudo apt-get install apt-cacher apache2 2. Enable apt-cacher

Edit /etc/default/apt-cacher and change autostart to 1 Note: I had problems doing this on a machine with apache already installed. Added by JT: Restart apache if you already had it installed: sudo invoke-rc.d apache2 restart Added by pablodav: I suggest to restart apt-cacher also: sudo invoke-rc.d apt-cacher restart Then test by going to http://server/apt-cacher to verify that it's running. Added by pablodav: In Jaunty use http://server:3142 instead http://server/apt-cacher (this seems to be not working anymore with /apt-cacher).

Server Configuration

Edit /etc/apt-cacher/apt-cacher.conf if you want to change any options. The only one I changed was admin_email. Import any existing apt-get cache: sudo /usr/share/apt-cacher/apt-cacher-import.pl /var/cache/apt/archives Added by pablodav: I strongly recommend to use -d option for symlinks, In Jaunty I had to use sudo /usr/share/apt-cacher/apt-cacher-import.pl -s /var/cache/apt/archives

Load Packages on Server from CD

Around upgrade time, you may find it useful to get the CD image instead of using the slow update servers, and populate your cache using that. First, download your CD image(s) of choice (I'd recommend using bit-torrent). Second, mount the CD image on the server running apt-cacher :

sudo mount -o loop /home/username_or_other_path/ubuntu-9.10-rc-alternate-i386.iso /media/cdrom0

Third, run the import on the CD image, you need the -R is needed to recurse into the CD directory structure, the -r just makes sure they are copied to the cache instead of trying to link:

sudo /usr/share/apt-cacher/apt-cacher-import.pl -R -r /media/cdrom0

You should see the script saying that it is importing a lot of packages.

Client Configuration Options

There are two ways to configure your clients to use apt-cacher. Added by pablodav: I don't recommend to change every line with step 1. In Jaunty /apt-cache does not works. Jump to step 2 for Client Configuration.

Modify your sources.list

In client /etc/apt/sources.list insert apt-cacher-server/apt-cacher/ in each line, ie deb http://archive.ubuntu.com/ubuntu/ dapper main restricted becomes deb http://apt-cacher-server/apt-cacher/archive.ubuntu.com/ubuntu/ dapper main restricted (Warning: The apt-cacher web page advises to also add ':3142' - this caused the system to fail for me) Added by JT: on gutsy and hardy, I could not get this to work with server/apt-cacher/ but server:3142/ (NB: without the /apt-cacher/) worked fine. To clarify, use eg. deb http://apt-cacher-server:3142/archive.ubuntu.com/ubuntu/ hardy main restricted Scientus: I think this is because apt-cacher is its own http server and this setup use apache Then run apt-get update

Use as a proxy to APT

Static configuration

In a terminal, type:

sudo nano /etc/apt/apt.conf.d/01proxy

Inside your new file, add a line that says:

Acquire::http::Proxy "http://<IP address or hostname of the apt-cacher server>:3142";

"Roaming" mode

This method is useful if you are alternating between office and home with a laptop for example. It involves using the ping command to determine if the apt-cacher server is available at boot-time and then configure the APT proxy or not.

  • Open /etc/rc.local (alt-F2, "gksu gedit /etc/rc.local")
  • Change the top from "#!/bin/sh -e" to "#!/bin/bash"
  • put this near the end (before "exit 0" if present) replacing "SERVER_NAME_HERE" with your server's resolvable name or it's IP:
. /lib/lsb/init-functions
log_daemon_msg "Configuring APT cache proxy" "(based on SERVER_NAME_HERE's presence...)"
ping -c 1 SERVER_NAME_HERE &> /dev/null
if [ $? = "0" ]; then
  echo "Acquire::http::Proxy \"http://SERVER_NAME_HERE:3142\";" > /etc/apt/apt.conf.d/01SERVER_NAME_HEREproxy
else
  rm /etc/apt/apt.conf.d/01SERVER_NAME_HEREproxy &> /dev/null
fi
log_end_msg 0

"Roaming" mode alternative solution

Added by "nuaimat": The above method didn't work for me, so I created two sources.list versions, one named sources.list.home and other one named sources.list.work (both inside /etc/apt) I modified sources.list.home as mentioned above (Static configuration Section) then I login as root

 
   sudo su -

then created a shell script (don't forget to replace "SERVER_NAME_HERE" with your server's resolvable name or it's IP ) :

#!/bin/bash

. /lib/lsb/init-functions
log_daemon_msg "Configuring APT cache proxy" "(based on SERVER_NAME_HERE's presence...)"
unlink /etc/apt/sources.list

ping -c 1 SERVER_NAME_HERE &> /dev/null
if [ $? = "0" ]; then
 log_daemon_msg "found live $?"
 ln -s /etc/apt/sources.list.home /etc/apt/sources.list &> /dev/null

  for i in /etc/apt/sources.list.d/*.list; 
  do 
  local_online=`grep "SERVER_NAME_HERE" $i|wc -l`
  if [ $local_online = "0" ]; then
   sed -i 's|http://|http://SERVER_NAME_HERE/apt-cacher/|g' $i &> /dev/null
  fi;
 done;
else

 log_daemon_msg "not found"
 ln -s /etc/apt/sources.list.work /etc/apt/sources.list &> /dev/null
 
 for i in /etc/apt/sources.list.d/*.list; 
 do 
  sed -i 's|http://SERVER_NAME_HERE/apt-cacher/|http://|g' $i &> /dev/null
 done;
fi


  


log_end_msg 0

  • save it as /root/check-apt-cacher
  • chmod +x /root/check-apt-cacher
  • crontab -e
  • using your favourite editor add the following line to the crontabs
 
*/1 * * * * /bin/bash /root/check-apt-cacher

this script will run each minute (*/1) you can change it as you see fit.