AirSonic is a free and open-source media streaming server. In this tutorial, I will guide you through the process of deploying an AirSonic server instance from scratch on a CentOS 7 Server instance.
A newly deployed AKLWEB HOST CentOS 7 Server instance with at least 2GB of memory. Say it has an IPv4 address 203.0.113.1.
A sudo user.
The domain airsonic.example.com is being pointed to the server instance mentioned above.
To get better system performance, it’s recommended to create a 2GB (2048 MB) swap file on a machine with 2GB of memory:
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 -mNote: If you are using a different server size, the suitable size of the swap partition may vary.
Properly setting up a hostname and an FQDN for the machine is required for enabling HTTPS security with a Let’s Encrypt SSL Certificate.
The following commands will set up a hostname airsonic and an FQDN airsonic.example.com for the machine:
sudo hostnamectl set-hostname airsonic
cat <<EOF | sudo tee /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
203.0.113.1 airsonic.example.com airsonic
127.0.0.1 airsonic
::1 airsonic
EOFThe results can be confirmed with the following:
hostname
hostname -fRemove CentOS 7’s default block on ports 80 (HTTP) and 443 (HTTPS):
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld.serviceInstall the EPEL YUM repo and then update the system:
sudo yum install -y epel-release
sudo yum -y update && sudo shutdown -r nowAfter the system reboots, log back in as the same user sudo user to move on.
Install OpenJDK JRE 8 and then confirm the result on CentOS 7:
sudo yum install -y java-1.8.0-openjdk.x86_64
java -versionThe output of the second command will be similar to the following:
openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-8u171-b11-0ubuntu0.18.04.1-b11)
OpenJDK 64-Bit Server VM (build 25.171-b11, mixed mode)In addition, you need to set up the JAVA_HOME environment variable as follows:
echo "JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")" | sudo tee -a /etc/profile
source /etc/profileAirSonic can be deployed using various methods. In this tutorial, we will install AirSonic using the AirSonic WAR package.
Create a dedicated user and a dedicated group, both named airsonic:
sudo groupadd airsonic
sudo mkdir /var/airsonic
sudo useradd -s /bin/nologin -g airsonic -d /var/airsonic -M airsonicDownload the latest AirSonic WAR package:
cd /var/airsonic
sudo wget https://github.com/airsonic/airsonic/releases/download/v10.1.2/airsonic.war
sudo chown -R airsonic:airsonic /var/airsonicDownload the predefined AirSonic systemd unit files and then start the AirSonic service:
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/sysconfig/airsonic
sudo systemctl daemon-reload
sudo systemctl start airsonic.service
sudo systemctl enable airsonic.serviceNote: You may need to review and customize the two AirSonic systemd unit files on your machine.
AirSonic will be up and running now, listening on port 8080. You can use the following command to confirm that this is the case:
ps -ef|grep airsonicYou can also directly visit the AirSonic site, but you need to temporarily modify firewall rules first:
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo systemctl reload firewalld.serviceNext, point your favorite web browser to http://203.0.113.1:8080/airsonic, and then use the default credentials listed below to log in:
Username: admin
Password: admin
For security purposes, you should change the administrator’s password immediately after logging in.
Once the result is confirmed, restrict access to the port 8080 again:
sudo firewall-cmd --permanent --remove-port=8080/tcp
sudo systemctl reload firewalld.serviceFor security purposes, it’s recommended to enable HTTPS security on every newly created website. The most convenient practice for that is to deploy a Let’s Encrypt SSL Certificate as follows.
Install the Certbot utility on CentOS 7:
sudo yum -y install yum-utils
sudo yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional
sudo yum install -y certbotUse Certbot to apply for a Let’s Encrypt SSL Certificate for the domain airsonic.example.com:
sudo certbot certonly --standalone --agree-tos --no-eff-email -m admin@example.com -d airsonic.example.comThe certificate and chain will be saved at the following:
/etc/letsencrypt/live/airsonic.example.com/fullchain.pemThe key file will be saved here:
/etc/letsencrypt/live/airsonic.example.com/privkey.pemThe Let’s Encrypt SSL Certificate is designed to expire in three months. You can set up a cron job to renew your certificates automatically:
sudo crontab -ePress I, and then input the following entry:
0 0,12 * * python -c 'import random; import time; time.sleep(random.random() 3600)' && certbot renewSave and quit:
:wqThis cron job will attempt to update the Let’s Encrypt certificate every day at noon.
With the help of Nginx, you can both facilitate visitors’ access (so that they no longer need to input the 8080 port number) and enable HTTPS security on your AirSonic website.
Install Nginx using YUM:
sudo yum install -y nginxNext, create a config file for AirSonic:
cat <<EOF | sudo tee /etc/nginx/conf.d/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://;
}
}
EOFRestart Nginx to put your configuration into effect:
sudo systemctl restart nginx.service
sudo systemctl enable nginx.serviceFinally, point your favorite web browser to http://airsonic.example.com/airsonic or https://airsonic.example.com/airsonic to start exploring your AirSonic website.