-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall_apache_letsencrypt_2Vhost.sh
More file actions
79 lines (67 loc) · 2.62 KB
/
install_apache_letsencrypt_2Vhost.sh
File metadata and controls
79 lines (67 loc) · 2.62 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
#!/bin/bash
# Define domain names
DEV_DOMAIN="dev.techmahato.com"
PROD_DOMAIN="prod.techmahato.com"
YOUR_EMAIL="your_email@example.com"
# Step 1: Update and upgrade packages
sudo apt update
sudo apt upgrade -y
# Step 2: Install Apache HTTP Server
sudo apt install -y apache2
sudo systemctl enable apache2
sudo systemctl start apache2
sudo systemctl status apache2
# Step 3: Configure Firewall
sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw enable
# Step 4: Create Apache Virtualhosts for dev.techmahato.com and prod.techmahato.com
sudo mkdir -p /var/www/html/$DEV_DOMAIN/
sudo mkdir -p /var/www/html/$PROD_DOMAIN/
# Create dev.techmahato.com virtualhost configuration
sudo tee /etc/apache2/sites-available/$DEV_DOMAIN.conf > /dev/null <<EOF
<VirtualHost *:80>
ServerName $DEV_DOMAIN
ServerAlias www.$DEV_DOMAIN
ServerAdmin admin@$DEV_DOMAIN
DocumentRoot /var/www/html/$DEV_DOMAIN
ErrorLog \${APACHE_LOG_DIR}/$DEV_DOMAIN_error.log
CustomLog \${APACHE_LOG_DIR}/$DEV_DOMAIN_access.log combined
<Directory /var/www/html/$DEV_DOMAIN>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
# Create prod.techmahato.com virtualhost configuration
sudo tee /etc/apache2/sites-available/$PROD_DOMAIN.conf > /dev/null <<EOF
<VirtualHost *:80>
ServerName $PROD_DOMAIN
ServerAlias www.$PROD_DOMAIN
ServerAdmin admin@$PROD_DOMAIN
DocumentRoot /var/www/html/$PROD_DOMAIN
ErrorLog \${APACHE_LOG_DIR}/$PROD_DOMAIN_error.log
CustomLog \${APACHE_LOG_DIR}/$PROD_DOMAIN_access.log combined
<Directory /var/www/html/$PROD_DOMAIN>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
# Enable the virtualhosts and required Apache modules
sudo a2ensite $DEV_DOMAIN.conf
sudo a2ensite $PROD_DOMAIN.conf
sudo a2enmod ssl rewrite
sudo systemctl restart apache2
# Create index.html files with some message
echo "<h1>Welcome to $DEV_DOMAIN</h1><p>This is the development server.</p>" | sudo tee /var/www/html/$DEV_DOMAIN/index.html > /dev/null
echo "<h1>Welcome to $PROD_DOMAIN</h1><p>This is the production server.</p>" | sudo tee /var/www/html/$PROD_DOMAIN/index.html > /dev/null
# Step 5: Secure Apache with Let's Encrypt
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache --non-interactive --agree-tos --email $YOUR_EMAIL -d $DEV_DOMAIN -d $PROD_DOMAIN
#sudo certbot --apache --non-interactive --agree-tos --email $YOUR_EMAIL -d $DEV_DOMAIN -d www.$DEV_DOMAIN -d $PROD_DOMAIN -d www.$PROD_DOMAIN
# Step 6: Check SSL renewal status
sudo systemctl status certbot.timer
sudo certbot renew --dry-run