AirSonic是一個免費的開源媒體流媒體服務器。在本教程中,我將指導您從頭開始在Ubuntu 18.04 LTS服務器實例上部署一個AirSonic服務器實例。
準備:
一個新部署的Ubuntu 18.04 LTS 的VPS服務器實例,至少有2GB的內存。假設它的IPv4地址是203.0.113.1。
sudo用戶。
指向上面提到的服務器實例的域airsonic.example.com。
步驟1:基本系統配置
創建交換文件
爲了獲得更好的系統性能,建議在具有2GB內存的機器上創建2GB (2048M)交換文件:
sudo dd if=/dev/zero of=/swapfile count=2048 bs=1M
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
free -m
注意:如果您使用不同的服務器大小,交換分區的適當大小可能會有所不同。
設置計算機的主機名和完全限定域名(FQDN)
正確地爲計算機設置主機名和FQDN是使用SSL加密證書啓用HTTPS安全性所必需的。
以下命令將爲機器設置主機名airsonic和FQDN airsonic.example.com:
sudo hostnamectl set-hostname airsonic
sudo sed -i 's/^127.0.1.1.*$/127.0.1.1 airsonic.example.com airsonic/g' /etc/hosts
結果如下:
hostname
hostname -f
修改防火牆規則以允許入站SSH、HTTP和HTTPS通信
設置UFW防火牆規則運行一個AirSonic服務器:
sudo ufw allow OpenSSH
sudo ufw allow http
sudo ufw allow https
sudo ufw enable
當你看到Command may disrupt existing ssh connections. Proceed with operation (y|n)?時,輸入y,然後按回車鍵。
更新系統
爲了安全和性能,有必要將Ubuntu 18.04 LTS系統升級到最新狀態:
sudo apt update
sudo apt upgrade -y && sudo shutdown -r now
在升級過程中,可能會通知您,當前安裝的grub配置文件已在本地修改。由於我們實際上並不負責修改,所以使用up箭頭高亮install the package maintainer's version選項,然後按回車鍵。
在系統重新啓動後,以相同的sudo用戶身份重新登錄以繼續運行。
步驟2:安裝OpenJDK Java運行時環境(JRE) 8
安裝OpenJDK JRE 8,然後確認結果:
sudo apt install -y openjdk-8-jre-headless
java -version
第二個命令的輸出如下:
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.18.04.1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
另外,您需要設置JAVA_HOME環境變量如下:
echo "JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")" | sudo tee -a /etc/profile
source /etc/profile
第三步:安裝AirSonic
可以使用各種方法部署AirSonic。在本教程中,我們將使用AirSonic WAR包安裝AirSonic。
創建一個專用用戶和一個專用組,都叫airsonic:
sudo groupadd airsonic
sudo mkdir /var/airsonic
sudo useradd -s /bin/nologin -g airsonic -d /var/airsonic -M airsonic
下載最新的AirSonic WAR包,AirSonic v10.1.2:
cd /var/airsonic
sudo wget https://github.com/airsonic/airsonic/releases/download/v10.1.2/airsonic.war
sudo chown -R airsonic:airsonic /var/airsonic
下載預定義的AirSonic systemd單元文件,然後啓動AirSonic服務:
sudo wget https://raw.githubusercontent.com/airsonic/airsonic/master/contrib/airsonic.service -O /etc/systemd/system/airsonic.service
sudo wget https://raw.githubusercontent.com/airsonic/airsonic/master/contrib/airsonic-systemd-env -O /etc/default/airsonic
sudo systemctl daemon-reload
sudo systemctl start airsonic.service
sudo systemctl enable airsonic.service
注意:您可能需要在自己的機器上檢查和定製兩個AirSonic systemd單元文件。
步驟4:測試安裝
AirSonic將會啓動並運行,在8080端口收聽。您可以使用以下命令來確認這是事實:
ps -ef|grep airsonic
你也可以直接訪問AirSonic網站,但你需要先暫時修改防火牆規則:
sudo ufw allow in 8080/tcp
接下來,將您最喜歡的web瀏覽器指向http://203.0.113.1:8080/airsonic,
然後使用下面列出的默認憑證登錄:
用戶名:admin
密碼:admin
出於安全考慮,您應該在登錄後立即更改管理員的密碼。
一旦確認結果,再次限制端口8080的訪問:
sudo ufw deny in 8080/tcp
步驟5:獲得一個讓我們爲您的AirSonic站點加密SSL證書
出於安全考慮,建議在每個新創建的網站上啓用HTTPS安全性。最方便的實踐是部署一個Let’s Encrypt證書,方法如下。
安裝Certbot實用程序:
sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository -y ppa:certbot/certbot
sudo apt update
sudo apt install -y certbot
使用Certbot申請域名爲airsonic.example.com加密SSL證書:
sudo certbot certonly --standalone --agree-tos --no-eff-email -m [email protected] -d airsonic.example.com
證書和鏈將保存在以下位置:
/etc/letsencrypt/live/airsonic.example.com/fullchain.pem
密鑰文件將保存在這裏:
/etc/letsencrypt/live/airsonic.example.com/privkey.pem
Let's Encrypt SSL證書的有效期是3個月。您可以設置一個cron作業來自動更新您的證書:
sudo crontab -e
當提示您選擇編輯器時,輸入2,然後按ENTER鍵選擇/usr/bin/vim.basic
接下來,按O,然後添加一行如下所示:
0 0,12 python -c 'import random; import time; time.sleep(random.random() 3600)' && certbot renew
保存並退出
:wq!
這個cron作業將嘗試每天中午更新Let's加密證書。
步驟6:將Nginx安裝爲反向代理
在Nginx的幫助下,您既可以方便訪問者訪問(這樣他們就不再需要輸入8080端口號),又可以在AirSonic網站上啓用HTTPS安全性。
使用APT安裝Nginx:
sudo apt install -y nginx
接下來,爲AirSonic創建一個配置文件:
cat <<EOF | sudo tee /etc/nginx/sites-available/airsonic.conf
Redirect HTTP to HTTPS
server {
listen 80;
server_name airsonic.example.com;
return 301 https://\$server_name\$request_uri;
}server {
# Setup HTTPS certificates listen 443 default ssl; server_name airsonic.example.com; ssl_certificate /etc/letsencrypt/live/airsonic.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/airsonic.example.com/privkey.pem; # Proxy to the Airsonic server location /airsonic { proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_set_header X-Forwarded-Host \$http_host; proxy_set_header Host \$http_host; proxy_max_temp_file_size 0; proxy_pass http://127.0.0.1:8080; proxy_redirect http:// https://; }
}
EOF
創建一個符號鏈接指向新創建的AirSonic Nginx配置文件:
sudo ln -s /etc/nginx/sites-available/airsonic.conf /etc/nginx/sites-enabled/
重新啓動Nginx,使你的配置生效:
sudo systemctl restart nginx.service
sudo systemctl enable nginx.service
最後,將您最喜歡的web瀏覽器指向http://airsonic.example.com/airsonic或https://airsonic.example.com/airsonic
開始探索您的AirSonic網站。