Rather than reinventing the wheel, there’s a fully automated script that’s been available for years:
https://www.dslreports.com/forum/r30661088-PBX-FreePBX-for-the-Raspberry-Pi
The included script (install) and archive (install.tar.gz) will build
FreePBX 13, 14, or 15 plus Asterisk 13, 15, 16, or 16-GVSIP on a Raspberry Pi.
iptables, dnsmasq, and exim4 are also installed.
Installation takes a approximately 35 minutes to complete on a Raspberry Pi 4B.