-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaddy_install_script.sh
More file actions
186 lines (153 loc) · 4.9 KB
/
caddy_install_script.sh
File metadata and controls
186 lines (153 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
# Caddy Installation Script for Ubuntu
# This script installs Caddy web server using the official repository
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if running as root
check_root() {
if [[ $EUID -eq 0 ]]; then
print_error "This script should not be run as root. Please run as a regular user with sudo privileges."
exit 1
fi
}
# Function to check if sudo is available
check_sudo() {
if ! command -v sudo &> /dev/null; then
print_error "sudo is required but not installed. Please install sudo first."
exit 1
fi
}
# Function to check Ubuntu version
check_ubuntu() {
if [[ ! -f /etc/os-release ]]; then
print_error "Cannot determine OS version. This script is designed for Ubuntu."
exit 1
fi
. /etc/os-release
if [[ "$ID" != "ubuntu" ]]; then
print_warning "This script is designed for Ubuntu. Current OS: $ID"
read -p "Do you want to continue anyway? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
}
# Main installation function
install_caddy() {
print_status "Starting Caddy installation..."
# Update package list
print_status "Updating package list..."
sudo apt update
# Install required packages
print_status "Installing required packages..."
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
# Add Caddy's GPG key
print_status "Adding Caddy's GPG key..."
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
# Add Caddy repository
print_status "Adding Caddy repository..."
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list > /dev/null
# Update package list again
print_status "Updating package list with new repository..."
sudo apt update
# Install Caddy
print_status "Installing Caddy..."
sudo apt install -y caddy
# Start and enable Caddy service
print_status "Starting and enabling Caddy service..."
sudo systemctl start caddy
sudo systemctl enable caddy
print_success "Caddy installation completed!"
}
# Function to verify installation
verify_installation() {
print_status "Verifying installation..."
# Check if Caddy is installed
if command -v caddy &> /dev/null; then
CADDY_VERSION=$(caddy version)
print_success "Caddy is installed: $CADDY_VERSION"
else
print_error "Caddy installation failed - command not found"
return 1
fi
# Check service status
if systemctl is-active --quiet caddy; then
print_success "Caddy service is running"
else
print_warning "Caddy service is not running"
print_status "Service status:"
sudo systemctl status caddy --no-pager
fi
# Check if service is enabled
if systemctl is-enabled --quiet caddy; then
print_success "Caddy service is enabled (will start on boot)"
else
print_warning "Caddy service is not enabled"
fi
}
# Function to show next steps
show_next_steps() {
print_status "Installation complete! Here are your next steps:"
echo
echo "1. Configure Caddy by editing the Caddyfile:"
echo " sudo nano /etc/caddy/Caddyfile"
echo
echo "2. Reload Caddy after making changes:"
echo " sudo systemctl reload caddy"
echo
echo "3. Check Caddy logs:"
echo " sudo journalctl -u caddy -f"
echo
echo "4. Test Caddy configuration:"
echo " caddy validate --config /etc/caddy/Caddyfile"
echo
echo "5. Caddy documentation: https://caddyserver.com/docs/"
echo
}
# Main execution
main() {
echo "========================================"
echo " Caddy Installation Script "
echo "========================================"
echo
# Perform checks
check_root
check_sudo
check_ubuntu
# Ask for confirmation
print_status "This script will install Caddy web server on your Ubuntu system."
read -p "Do you want to continue? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_status "Installation cancelled."
exit 0
fi
# Install Caddy
install_caddy
# Verify installation
verify_installation
# Show next steps
show_next_steps
print_success "All done! Caddy is now installed and running."
}
# Run main function
main "$@"