Install DNS manager for VPS

bởi anhcoo • 2025-10-02 10:36:59 • 👍 0 • 👎 0

#!/bin/bash

# =============================================
# DNS Manager Auto Install Script
# For AlmaLinux/CentOS/RHEL
# =============================================

echo "============================================="
echo "DNS Manager Auto Installation"
echo "Domain: occ.asia"
echo "IP: 103.75.183.76"
echo "============================================="

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Function to check success
check_success() {
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Success${NC}"
else
echo -e "${RED}✗ Failed${NC}"
exit 1
fi
}

# Step 1: Stop and remove existing DNS services
echo -e "\n${YELLOW}[1/6] Stopping existing DNS services...${NC}"
sudo systemctl stop pdns named docker 2>/dev/null
sudo systemctl disable pdns named 2>/dev/null
sudo dnf remove -y pdns pdns-backend-sqlite bind 2>/dev/null
check_success

# Step 2: Install dnsmasq
echo -e "\n${YELLOW}[2/6] Installing dnsmasq...${NC}"
sudo dnf install -y dnsmasq bind-utils
check_success

# Step 3: Configure dnsmasq
echo -e "\n${YELLOW}[3/6] Configuring dnsmasq...${NC}"

sudo tee /etc/dnsmasq.conf > /dev/null << 'EOF'
# Basic Configuration
listen-address=127.0.0.1,103.75.183.76
port=53
user=dnsmasq
group=dnsmasq

# Domain Records
address=/occ.asia/103.75.183.76
address=/www.occ.asia/103.75.183.76
address=/ns1.occ.asia/103.75.183.76
address=/ns2.occ.asia/103.75.183.76

# MX Record for Email
mx-host=occ.asia,occ.asia,10

# TXT Records
txt-record=occ.asia,v=spf1 a mx ~all

# Wildcard Subdomain
address=/.occ.asia/103.75.183.76
EOF

check_success

# Step 4: Configure firewall
echo -e "\n${YELLOW}[4/6] Configuring firewall...${NC}"
sudo firewall-cmd --permanent --add-service=dns
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
check_success

# Step 5: Start and enable services
echo -e "\n${YELLOW}[5/6] Starting services...${NC}"
sudo systemctl enable dnsmasq
sudo systemctl start dnsmasq
check_success

# Step 6: Final testing
echo -e "\n${YELLOW}[6/6] Running tests...${NC}"

# Test DNS
echo -e "\n${YELLOW}DNS Tests:${NC}"
domains=("occ.asia" "www.occ.asia" "ns1.occ.asia" "ns2.occ.asia" "test.occ.asia")
for domain in "${domains[@]}"; do
result=$(dig @localhost $domain +short 2>/dev/null | head -1)
if [ "$result" = "103.75.183.76" ]; then
echo -e "${GREEN}✓ $domain → $result${NC}"
else
echo -e "${RED}✗ $domain failed${NC}"
fi
done

# Service status
echo -e "\n${YELLOW}Service Status:${NC}"
sudo systemctl is-active dnsmasq >/dev/null && echo -e "${GREEN}✓ dnsmasq is running${NC}" || echo -e "${RED}✗ dnsmasq is not running${NC}"

# Port check
echo -e "\n${YELLOW}Port Check:${NC}"
sudo netstat -tulpn | grep :53 >/dev/null && echo -e "${GREEN}✓ DNS port 53 is listening${NC}" || echo -e "${RED}✗ DNS port 53 is not listening${NC}"

# Final instructions
echo -e "\n${GREEN}=============================================${NC}"
echo -e "${GREEN}Installation Completed!${NC}"
echo -e "${GREEN}=============================================${NC}"
echo -e "\n${YELLOW}Next steps at your domain registrar:${NC}"
echo "1. Set nameservers to:"
echo " - ns1.occ.asia"
echo " - ns2.occ.asia"
echo "2. Wait for DNS propagation (5 mins - 24 hours)"
echo -e "\n${YELLOW}To add more subdomains later:${NC}"
echo "sudo nano /etc/dnsmasq.conf"
echo "Add: address=/subdomain.occ.asia/103.75.183.76"
echo "sudo systemctl restart dnsmasq"
echo -e "\n${YELLOW}Check DNS propagation:${NC}"
echo "dig @8.8.8.8 occ.asia"
echo "Or visit: https://dnschecker.org"

echo -e "\n${GREEN}Your DNS manager is ready!${NC}"

Bình luận (1)

anhcoo : DNS server

Đăng nhập để bình luận.