[技術博客]nginx 部署 apt 源

[技術博客] nginx 部署 apt 源

出於各類各樣的緣由, 有時須要本身配置apt源, 好比發佈本身編寫的debian軟件包, 內網中只有一臺電腦能夠訪問外網,或者在本地配置本身的apt源。咱們已有本身的包,須要發佈, 讓終端上的app可以經過url get到。html

參考nginx

安裝部署工具

sudo apt-get install dpkg-dev
  sudo apt-get install apache2 # 若是使用nginx: sudo apt-get install nginx
  sudo apt-get install dpkg-sig

建立軟件庫目錄

Note:若是沒有在/var/www目錄下建立倉庫,那麼就須要建立一個軟連接把本身的倉庫連接到這個目錄shell

好比: 假設是在/home/目錄下:apache

sudo ln -s ~/repository_dir /var/www/repository_dir

在/var/www 目錄下建立目錄:ubuntu

sudo mkdir -p repository_dir/dists/stable/main/binary

把已有的deb文件導入到二進制文件目錄:安全

sudo mv location_of_package/package_name.deb

簽名加密倉庫

你也能夠選擇不進行簽名加密, 這樣apt-get update時會報warning, apt update會報error.bash

建立 gpg 密鑰:服務器

gpg --gen-key

因爲咱們僅使用密鑰來生成數字簽名,所以使用RSA能夠得到最大的安全性。app

Please select what kind of key you want:
     (1) RSA and RSA (default)
     (2) DSA and Elgamal
     (3) DSA (sign only)
     (4) RSA (sign only)
  Your selection? 4
  RSA keys may be between 1024 and 4096 bits long.
  What keysize do you want? (2048) 4096 # 這裏可選, 1024 ~ 4096 皆可
  Requested keysize is 4096 bits

有效期選擇密鑰不會過時dom

Please specify how long the key should be valid.
           0 = key does not expire
        <n>  = key expires in n days
        <n>w = key expires in n weeks
        <n>m = key expires in n months
        <n>y = key expires in n years
  Key is valid for? (0) 0
  Key does not expire at all
  Is this correct? (y/N) y

給出新密鑰的名稱:

You need a user ID to identify your key; the software constructs the user ID from the Real Name, Comment and Email Address in this form:
      "Zaphod Beeblebrox (Galactic President) <zbeeblebrox@pres.galaxy.com>"
  Real name: Repository # 這裏,給出新密鑰的名稱
  Email address:
  Comment:
  You selected this USER-ID:
      "Repository"
  Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O

您須要密碼來保護您的密鑰。必定要選擇一個你會記得的。以後會開始生成密碼, 在過程當中可能會有提示熵不夠

好比:

**We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy. # 須要足夠的熵,隨便作點啥吧。
Not enough random bytes available.  Please do some other work to give
the OS a chance to collect more entropy! (Need 210 more bytes)**

可是實際上,不論怎麼操做,咱們也只得到了少許的熵。能夠開啓另一個terminal, 輸入以下命令來製造足夠的熵:

dd if=/dev/sda of=/dev/zero # 從硬盤/sda 讀取內容並丟棄輸出到/dev/zero

而後就能夠正常生成密鑰了。按照屏幕上的說明建立密鑰。您應該得到相似於此的輸出 -

gpg: key 041DA354 marked as ultimately trusted
  public and secret key created and signed.
  gpg: checking the trustdb
  gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
  gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
  pub   4096R/041DA354 2012-06-01
        Key fingerprint = 2253 4C89 DE74 CF68 39D7  2A2E DB3E 384F 041D A354
  uid                  Repository

查看gpg密鑰:

gpg --list-keys

將生成的公鑰處處到文本文件而且保存到根目錄:

sudo gpg --output keyFile --armor --export 041DA354 # 這個數字是你本身生成的密鑰所對應的

使用密鑰簽名加密軟件包:

sudo dpkg-sig --sign builder file1.deb # 你的包
  sudo dpkg-sig --sign builder file2.deb

若是有大量的包須要簽名,就須要寫一寫shell腳本了。

配置nginx 代理

若是是使用apache代理服務器,則跳過這步,由於此時apt 源若設定爲 deb http://(xx.xx.xx.xx遠程機域名或者ip)/repository_dir/dists/stable/main/binary / (binary空格/)這個url應該已經能夠訪問了。而且http://(xx.xx.xx.xx遠程機域名或者ip)/repository_dir/dists/stable/main/binary也應該是可以直接訪問的

在nginx上配置server監聽80端口:

env PATH;
user XXX; # Nginx運行使用用戶, 自定義
worker_processes xxx; # worker個數 能夠設爲auto
pid /run/nginx.pid;

events {
    worker_connections xxx;
    # multi_accept on;
}

http {
    ##
    # Virtual Host Configs
    ##
    server{
        # 監聽80端口
        listen 80;
        server_name xxx.xxx; # 域名或者Ip
            root /var/www/deb_dir; # apt 倉庫目錄
            charset utf-8;
            location /{
                autoindex on; # autoindex選項默認關閉, 必定要打開
                index index.html;   
        }
    }
}

檢查配置文件正確性:

nginx -t

重載配置文件&重啓nginx

nginx -s reload

Done

後續

本地機器

sudo vi /etc/apt/sources.list

添加本身的源,(註釋掉本來的源,若是不想用的話)

deb http://xx.xx.xx.xx/repository_dir/dists/stable/main/binary / # 注意

保存以後, 更新源

sudo apt-get update

Note: 若是報錯"cannot find packages"

下載該倉庫的公鑰:

wget -O - http://10.31.31.89/repository_dir/keyFile | sudo apt-key add -

查看已經有的公鑰:

apt-key list

再次更新,若是還不行, 記得更新倉庫的用戶權限:

sudo chown user:user -R .

遠程機:

建立index文件而且壓縮, 壓縮文件和源文件都須要有放在repo裏

# repo_dir
  apt-ftparchive packages . &gt; Packages
  gzip -c Packages &gt; Packages.gz

這個命令不大好使,我不大&gt有些奇怪,能夠直接把生成的信息重定向到Packages而後再壓縮

apt-ftparchive packees . > Packages

建立Release文件:

若是沒有進行gpg簽名加密, InRelease和Release.gpg能夠不生成

apt-ftparchive release . &gt; Release
  gpg --clearsign -o InRelease Release
  gpg -abs -o Release.gpg Release

再來一次,全部的步驟都完成後:

sudo apt-get update
  sudo apt-get install package_name

Bingo!

Pass
nginx資源推薦:
首推: Nginx開發從入門到精通
其次: Nginx中文文檔
而後: 理解 Nginx 源碼
最後: 官方文檔

相關文章
相關標籤/搜索