Cách cài đặt PowerDNS và PowerDNS-Admin trên Ubuntu 22.04

theanh

Administrator
Nhân viên
PowerDNS là một máy chủ tên miền có thẩm quyền miễn phí và mã nguồn mở được viết bằng C++. Nó là nền tảng chéo và có thể chạy trên các hệ điều hành Unix, Linux và macOS. Nó hỗ trợ một số cơ sở dữ liệu như MySQL, MariaDB, PostgreSQL và Oracle để lưu trữ các tệp và bản ghi vùng.

PowerDNS Admin là một ứng dụng dựa trên web có thể được sử dụng để quản lý PowerDNS thông qua trình duyệt web. Nó cho phép bạn tạo và quản lý các vùng DNS bằng giao diện web PowerDNS. Nó cung cấp các tính năng rất hữu ích, bao gồm hỗ trợ IPv4 và IPv6, miền số lượng lớn, hỗ trợ DNSSec, AD, LDAP, xác thực SAML, v.v.

Trong bài đăng này, chúng tôi sẽ giải thích cách cài đặt PowerDNS và quản trị viên PowerDNS trên máy chủ Ubuntu 22.04.

Điều kiện tiên quyết​

  • Một máy chủ chạy Ubuntu 22.04.
  • Một tên miền hợp lệ được trỏ bằng IP máy chủ của bạn.
  • Một mật khẩu gốc được cấu hình trên máy chủ.

Cài đặt và cấu hình máy chủ MariaDB​

Đầu tiên, bạn sẽ cần cài đặt máy chủ cơ sở dữ liệu MariaDB trên hệ thống.
Mã:
apt-get install mariadb-server -y
Sau khi MariaDB được cài đặt, bạn sẽ cần tạo cơ sở dữ liệu và người dùng cho PowerDNS.

Đầu tiên, hãy đăng nhập vào MariaDB bằng lệnh sau:
Mã:
mysql
Sau khi đăng nhập, hãy tạo cơ sở dữ liệu và người dùng bằng lệnh sau:
Mã:
MariaDB [(none)]> create database pdns;
MariaDB [(none)]> grant all on pdns.* to pdnsadmin@localhost identified by 'password';
Tiếp theo, xóa các đặc quyền và thoát khỏi shell MariaDB bằng lệnh sau lệnh:
Mã:
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;

Cài đặt PowerDNS​

Trước khi bắt đầu, bạn sẽ cần tắt dịch vụ systemd-resolved khỏi hệ thống của mình. Bạn có thể vô hiệu hóa nó bằng lệnh sau:
Mã:
systemctl disable --now systemd-resolved
Tiếp theo, xóa tệp resolv.conf mặc định và tạo một tệp mới:
Mã:
rm -rf /etc/resolv.conf
echo "nameserver 8.8.8.8" > /etc/resolv.conf
Tiếp theo, cài đặt máy chủ PowerDNS bằng lệnh sau lệnh:
Mã:
apt-get install pdns-server pdns-backend-mysql -y
Sau khi PowerDNS được cài đặt, bạn có thể tiến hành bước tiếp theo.

Cấu hình PowerDNS​

Đầu tiên, nhập lược đồ cơ sở dữ liệu PowerDNS vào cơ sở dữ liệu PowerDNS bằng lệnh sau:
Mã:
mysql -u pdnsadmin -p pdns < /usr/share/pdns-backend-mysql/schema/schema.mysql.sql
Tiếp theo, bạn sẽ cần tạo tệp cấu hình PowerDNS và xác định chi tiết kết nối cơ sở dữ liệu PowerDNS:
Mã:
nano /etc/powerdns/pdns.d/pdns.local.gmysql.conf
Thêm các dòng sau:
Mã:
# MySQL Configuration
#
# Launch gmysql backend
launch+=gmysql
# gmysql parameters
gmysql-host=127.0.0.1
gmysql-port=3306
gmysql-dbname=pdns
gmysql-user=pdnsadmin
gmysql-password=password
gmysql-dnssec=yes
# gmysql-socket=
Lưu và đóng tệp, sau đó đặt quyền thích hợp cho tệp pdns.local.gmysql.conf:
Mã:
chmod 640 /etc/powerdns/pdns.d/pdns.local.gmysql.conf
chown pdns:pdns /etc/powerdns/pdns.d/pdns.local.gmysql.conf
Tiếp theo, dừng máy chủ PowerDNS và kiểm tra PowerDNS bằng lệnh sau:
Mã:
systemctl stop pdns
pdns_server --daemon=no --guardian=no --loglevel=9
Nếu mọi thứ đều ổn, bạn sẽ nhận được thông báo sau đầu ra:
Mã:
Aug 06 10:43:47 gmysql Connection successful. Connected to database 'pdns' on '127.0.0.1'.
Aug 06 10:43:47 gmysql Connection successful. Connected to database 'pdns' on '127.0.0.1'.
Aug 06 10:43:47 gmysql Connection successful. Connected to database 'pdns' on '127.0.0.1'.
Aug 06 10:43:47 Done launching threads, ready to distribute questions
Tiếp theo, khởi động máy chủ PowerDNS bằng lệnh sau:
Mã:
systemctl start pdns
Bây giờ bạn có thể kiểm tra trạng thái của PowerDNS bằng lệnh sau lệnh:
Mã:
systemctl status pdns
Bạn sẽ thấy kết quả sau:
Mã:
? pdns.service - PowerDNS Authoritative Server Loaded: loaded (/lib/systemd/system/pdns.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2022-08-06 10:37:28 UTC; 8s ago Docs: man:pdns_server(1) man:pdns_control(1) https://doc.powerdns.com Main PID: 93982 (pdns_server) Tasks: 8 (limit: 2242) Memory: 43.1M CPU: 166ms CGroup: /system.slice/pdns.service ??93982 /usr/sbin/pdns_server --guardian=no --daemon=no --disable-syslog --log-timestamp=no --write-pid=no
Aug 06 10:37:28 ubuntu2204 pdns_server[93982]: UDP server bound to [::]:53
Aug 06 10:37:28 ubuntu2204 pdns_server[93982]: TCP server bound to 0.0.0.0:53
Aug 06 10:37:28 ubuntu2204 pdns_server[93982]: TCP server bound to [::]:53
Aug 06 10:37:28 ubuntu2204 pdns_server[93982]: PowerDNS Authoritative Server 4.5.3 (C) 2001-2021 PowerDNS.COM BV
Aug 06 10:37:28 ubuntu2204 pdns_server[93982]: Using 64-bits mode. Built using gcc 11.2.0.
Aug 06 10:37:28 ubuntu2204 pdns_server[93982]: PowerDNS comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redi>
Aug 06 10:37:28 ubuntu2204 pdns_server[93982]: Creating backend connection for TCP
Aug 06 10:37:28 ubuntu2204 systemd[1]: Started PowerDNS Authoritative Server.
Aug 06 10:37:28 ubuntu2204 pdns_server[93982]: About to create 3 backend threads for UDP
Aug 06 10:37:29 ubuntu2204 pdns_server[93982]: Done launching threads, ready to distribute questions
Tại thời điểm này, PowerDNS đã được khởi động và đang lắng nghe trên cổng 53. Bạn có thể kiểm tra bằng lệnh sau:
Mã:
ss -alnp4 | grep pdns
Bạn sẽ nhận được thông báo sau đầu ra:
Mã:
udp UNCONN 0 0 0.0.0.0:53 0.0.0.0:* users:(("pdns_server",pid=93982,fd=5))
tcp LISTEN 0 128 0.0.0.0:53 0.0.0.0:* users:(("pdns_server",pid=93982,fd=7))

Cài đặt PowerDNS Admin​

Trong phần này, chúng tôi sẽ chỉ cho bạn cách cài đặt PowerDNS admin với Nginx.

Cài đặt các Dependencies bắt buộc​

Đầu tiên, hãy cài đặt tất cả các dependency cần thiết cho PowerDNS admin với lệnh sau lệnh:
Mã:
apt-get install nginx python3-dev libsasl2-dev libldap2-dev libssl-dev libxml2-dev libxslt1-dev libxmlsec1-dev libffi-dev pkg-config apt-transport-https virtualenv build-essential libmariadb-dev git python3-flask libpq-dev-y
Sau khi tất cả các phụ thuộc được cài đặt, hãy thêm kho lưu trữ Node.js bằng lệnh sau:
Mã:
curl -sL https://deb.nodesource.com/setup_16.x | bash -
Tiếp theo, hãy cài đặt Node.js bằng lệnh sau lệnh:
Mã:
apt-get install nodejs -y
Tiếp theo, thêm kho lưu trữ yarn bằng lệnh sau:
Mã:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
Tiếp theo, cập nhật kho lưu trữ và cài đặt Yarn bằng lệnh sau lệnh:
Mã:
apt-get update -y
apt-get install yarn -y
Lúc này, tất cả các phụ thuộc cần thiết đã được cài đặt, giờ bạn có thể tiến hành bước tiếp theo.

Tải xuống PowerDNS Admin​

Tiếp theo, tải xuống phiên bản mới nhất của PowerDNS admin từ kho lưu trữ Git vào thư mục gốc Nginx thư mục:
Mã:
git clone https://github.com/ngoduykhanh/PowerDNS-Admin.git /var/www/html/pdns
Tiếp theo, hãy thay đổi thư mục thành thư mục đã tải xuống và tạo môi trường ảo Python bằng lệnh sau:
Mã:
cd /var/www/html/pdns/
virtualenv -p python3 flask
Tiếp theo, hãy kích hoạt môi trường ảo và cài đặt tất cả các phụ thuộc Python bằng lệnh sau lệnh:
Mã:
source ./flask/bin/activate
pip install -r requirements.txt
Tiếp theo, hủy kích hoạt môi trường ảo bằng lệnh sau:
Mã:
deactivate

Cấu hình kết nối cơ sở dữ liệu​

Tiếp theo, bạn sẽ cần xác định chi tiết kết nối cơ sở dữ liệu PowerDNS vào default_config.py tệp:
Mã:
nano /var/www/html/pdns/powerdnsadmin/default_config.py
Thay đổi các dòng sau:
Mã:
SALT = 'yoursecretekey'
SECRET_KEY = 'yoursecretekey'
BIND_ADDRESS = '0.0.0.0'
PORT = 9191
HSTS_ENABLED = False
OFFLINE_MODE = False
SQLA_DB_USER = 'pdnsadmin'
SQLA_DB_PASSWORD = 'password'
SQLA_DB_HOST = '127.0.0.1'
SQLA_DB_NAME = 'pdns'
SQLALCHEMY_TRACK_MODIFICATIONS = True
Lưu và đóng tệp, sau đó thay đổi thư mục thành pdns và kích hoạt máy ảo môi trường:
Mã:
cd /var/www/html/pdns/
source ./flask/bin/activate
Tiếp theo, cập nhật cơ sở dữ liệu bằng lệnh sau:
Mã:
export FLASK_APP=powerdnsadmin/__init__.py
flask db upgrade
yarn install --pure-lockfile
flask assets build
Tiếp theo, hủy kích hoạt môi trường ảo bằng lệnh sau:
Mã:
deactivate

Bật API quản trị PowerDNS​

Quản trị PowerDNS sử dụng API JSON để đọc số liệu thống kê và sửa đổi nội dung vùng, siêu dữ liệu và tài liệu khóa DNSSEC. Bạn có thể bật tính năng này bằng cách chỉnh sửa tệp pdns.conf:
Mã:
nano /etc/powerdns/pdns.conf
Thay đổi các dòng sau:
Mã:
api=yes
api-key=yoursecretekey
Lưu và đóng tệp, sau đó khởi động lại dịch vụ PowerDNS để áp dụng thay đổi:
Mã:
systemctl restart pdns

Cấu hình Nginx làm Proxy ngược cho Quản trị viên PowerDNS​

Tiếp theo, bạn sẽ cần cấu hình Nginx làm proxy ngược cho quản trị viên PowerDNS. Để thực hiện, hãy tạo tệp cấu hình máy chủ ảo Nginx bằng lệnh sau:
Mã:
nano /etc/nginx/conf.d/pdns-admin.conf
Thêm các dòng sau:
Mã:
server { listen	*:80; server_name pdnsadmin.example.com; index index.html index.htm index.php; root /var/www/html/pdns; access_log /var/log/nginx/pdnsadmin_access.log combined; error_log /var/log/nginx/pdnsadmin_error.log; client_max_body_size 10m; client_body_buffer_size 128k; proxy_redirect off; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffers 32 4k; proxy_buffer_size 8k; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_headers_hash_bucket_size 64; location ~ ^/static/ { include /etc/nginx/mime.types; root /var/www/html/pdns/powerdnsadmin; location ~* \.(jpg|jpeg|png|gif)$ { expires 365d; } location ~* ^.+.(css|js)$ { expires 7d; } } location / { proxy_pass http://unix:/run/pdnsadmin/socket; proxy_read_timeout 120; proxy_connect_timeout 120; proxy_redirect off; }
}
Lưu và đóng tệp, sau đó kiểm tra Nginx xem có lỗi cú pháp nào không bằng lệnh sau lệnh:
Mã:
nginx -t
Bạn sẽ nhận được kết quả sau:
Mã:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Tiếp theo, hãy thay đổi quyền sở hữu của pdns thành www-data:
Mã:
chown -R www-data:www-data /var/www/html/pdns
Cuối cùng, khởi động lại dịch vụ Nginx để áp dụng các thay đổi:
Mã:
systemctl restart nginx

Tạo tệp dịch vụ Systemd cho PowerDNS Admin​

Tiếp theo, bạn sẽ cần tạo tệp systemd tệp dịch vụ để quản lý dịch vụ PowerDNS.

Đầu tiên, tạo tệp dịch vụ pdns bằng lệnh sau:
Mã:
nano /etc/systemd/system/pdnsadmin.service
Thêm các dòng sau:
Mã:
[Unit]
Description=PowerDNS-Admin
Requires=pdnsadmin.socket
After=network.target
[Service]
PIDFile=/run/pdnsadmin/pid
User=pdns
Group=pdns
WorkingDirectory=/var/www/html/pdns
ExecStart=/var/www/html/pdns/flask/bin/gunicorn --pid /run/pdnsadmin/pid --bind unix:/run/pdnsadmin/socket 'powerdnsadmin:create_app()'
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Lưu và đóng tệp, sau đó tạo tệp sockt pdnsadmin bằng lệnh sau lệnh:
Mã:
nano /etc/systemd/system/pdnsadmin.socket
Thêm các dòng sau:
Mã:
[Unit]
Description=PowerDNS-Admin socket
[Socket]
ListenStream=/run/pdnsadmin/socket
[Install]
WantedBy=sockets.target
Lưu và đóng tệp, sau đó tạo các tệp và thư mục cần thiết bằng lệnh sau lệnh:
Mã:
echo "d /run/pdnsadmin 0755 pdns pdns -" >> /etc/tmpfiles.d/pdnsadmin.conf
mkdir /run/pdnsadmin/
chown -R pdns: /run/pdnsadmin/
chown -R pdns: /var/www/html/pdns/powerdnsadmin/
Tiếp theo, tải lại daemon systemd bằng lệnh sau:
Mã:
systemctl daemon-reload
Tiếp theo, bật dịch vụ pdnsadmin để bắt đầu khi khởi động lại hệ thống bằng lệnh sau lệnh:
Mã:
systemctl enable --now pdnsadmin.service pdnsadmin.socket
Tiếp theo, xác minh trạng thái của cả hai dịch vụ bằng lệnh sau:
Mã:
systemctl status pdnsadmin.service pdnsadmin.socket
Bạn sẽ nhận được kết quả đầu ra sau:
Mã:
? pdnsadmin.service - PowerDNS-Admin Loaded: loaded (/etc/systemd/system/pdnsadmin.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2022-08-06 10:52:44 UTC; 9s ago
TriggeredBy: ? pdnsadmin.socket Main PID: 98696 (gunicorn) Tasks: 2 (limit: 2242) Memory: 63.1M CPU: 913ms CGroup: /system.slice/pdnsadmin.service ??98696 /var/www/html/pdns/flask/bin/python /var/www/html/pdns/flask/bin/gunicorn --pid /run/pdnsadmin/pid --bind unix:/run/pdns> ??98697 /var/www/html/pdns/flask/bin/python /var/www/html/pdns/flask/bin/gunicorn --pid /run/pdnsadmin/pid --bind unix:/run/pdns>
Aug 06 10:52:44 ubuntu2204 systemd[1]: Started PowerDNS-Admin.
Aug 06 10:52:44 ubuntu2204 gunicorn[98696]: [2022-08-06 10:52:44 +0000] [98696] [INFO] Starting gunicorn 20.0.4
Aug 06 10:52:44 ubuntu2204 gunicorn[98696]: [2022-08-06 10:52:44 +0000] [98696] [INFO] Listening at: unix:/run/pdnsadmin/socket (98696)
Aug 06 10:52:44 ubuntu2204 gunicorn[98696]: [2022-08-06 10:52:44 +0000] [98696] [INFO] Using worker: sync
Aug 06 10:52:44 ubuntu2204 gunicorn[98697]: [2022-08-06 10:52:44 +0000] [98697] [INFO] Booting worker with pid: 98697
? pdnsadmin.socket - PowerDNS-Admin socket Loaded: loaded (/etc/systemd/system/pdnsadmin.socket; enabled; vendor preset: enabled) Active: active (running) since Sat 2022-08-06 10:52:44 UTC; 9s ago Triggers: ? pdnsadmin.service Listen: /run/pdnsadmin/socket (Stream) CGroup: /system.slice/pdnsadmin.socket
Aug 06 10:52:44 ubuntu2204 systemd[1]: Listening on PowerDNS-Admin socket.

Truy cập Quản trị PowerDNS​

Bây giờ bạn có thể mở trình duyệt web và truy cập giao diện web quản trị PowerDNS bằng URL . Bạn sẽ được chuyển hướng đến trang sau:


data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%22750%22%20height=%22442%22%3E%3C/svg%3E


Nhấp vào nút Tạo tài khoản. Bạn sẽ thấy màn hình sau:


data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%22750%22%20height=%22460%22%3E%3C/svg%3E


Cung cấp thông tin chi tiết về người dùng quản trị của bạn và nhấp vào nút Đăng ký để tạo tài khoản. Bạn sẽ thấy giao diện web quản trị PowerDNS trên màn hình sau:


data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%22750%22%20height=%22371%22%3E%3C/svg%3E


Cung cấp URL API PowerDNS để kết nối với PowerDNS và quản lý nó. Sau đó, nhấp vào nút Cập nhật để lưu các thay đổi. Bạn sẽ thấy trang sau:


data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%22750%22%20height=%22372%22%3E%3C/svg%3E


Nhấp vào nút Bảng điều khiển. Bạn sẽ thấy bảng điều khiển quản trị PowerDNS trên màn hình sau:


data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20width=%22750%22%20height=%22373%22%3E%3C/svg%3E

Kết luận​

Xin chúc mừng! Bạn đã cài đặt và cấu hình thành công PowerDNS và quản trị PowerDNS với Nginx trên máy chủ Ubuntu 22.04. Bây giờ bạn có thể tạo vùng và thêm bản ghi thông qua giao diện web quản trị PowerDNS rồi kiểm tra. Hãy thoải mái hỏi tôi nếu bạn có bất kỳ câu hỏi nào
 
Back
Bên trên