Last active
December 26, 2023 03:31
-
-
Save rameerez/75c7711fce95051e13e066e623911579 to your computer and use it in GitHub Desktop.
Bash script to set up a new site on a LAMP-based Ubuntu server (Wordpress, PHP, etc.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Ensure the script is run as root | |
if [ "$(id -u)" -ne 0 ]; then | |
echo -e "\e[31mERROR: This script must be run as root.\e[0m" | |
exit 1 | |
fi | |
# Define aesthetics | |
GREEN='\033[0;32m' | |
RED='\033[0;31m' | |
NC='\033[0m' # No Color | |
ALIEN='\xF0\x9F\x91\xBD' | |
# ARGUMENTS | |
# 1: website domain without alias / subdomains (ex: example.com) | |
if [ -z "$1" ]; then | |
echo -e "${RED}${ALIEN} ERROR: CRITICAL: No valid website name passed as an argument to configure a new website.${NC}" | |
exit 1 | |
fi | |
DOMAIN=$1 | |
myip=$(curl -s http://ifconfig.me) | |
read -p "Have you already pointed your site DNS to this server's IP? IP: $myip [Y/n]" domain_dns_configured | |
if [ "$domain_dns_configured" != "Y" ]; then | |
echo -e "${RED}${ALIEN} ERROR: You need to first configure your domain's DNS to point to this server's IP address, or else this script will fail trying to set up an SSL certificate.${NC}" | |
exit 1 | |
fi | |
echo -e "${GREEN}${ALIEN} Starting website creation and configuration for domain name: $DOMAIN${NC}" | |
echo -e "${GREEN}${ALIEN} Creating $DOMAIN folder structure under /var/www ...${NC}" | |
mkdir -p /var/www/$DOMAIN/public_html | |
echo -e "${GREEN}${ALIEN} Setting correct permissions for the new folder...${NC}" | |
chown -R www-data:www-data /var/www/$DOMAIN/public_html | |
echo -e "${GREEN}${ALIEN} Setting correct permissions for /var/www...${NC}" | |
chmod 755 /var/www | |
echo -e "${GREEN}${ALIEN} Creating new Apache2 VirtualHost configuration file for $DOMAIN...${NC}" | |
cat > /etc/apache2/sites-available/$DOMAIN.conf <<EOM | |
<VirtualHost *:80> | |
ServerName www.$DOMAIN | |
Redirect permanent / http://$DOMAIN/ | |
</VirtualHost> | |
<VirtualHost *:80> | |
ServerName $DOMAIN | |
# Main website configuration | |
DocumentRoot /var/www/$DOMAIN/public_html | |
<Directory /var/www/$DOMAIN/public_html> | |
Options -Indexes +FollowSymLinks | |
AllowOverride All | |
Require all granted | |
</Directory> | |
ErrorLog \${APACHE_LOG_DIR}/error.log | |
CustomLog \${APACHE_LOG_DIR}/access.log combined | |
</VirtualHost> | |
EOM | |
echo -e "${GREEN}${ALIEN} Enabling HTTP VirtualHost in Apache...${NC}" | |
a2ensite $DOMAIN.conf | |
echo -e "${GREEN}${ALIEN} Restarting Apache2 service for HTTP VirtualHost changes to take effect...${NC}" | |
systemctl restart apache2 | |
echo -e "${GREEN}${ALIEN} VirtualHost for $DOMAIN correctly configured and is now active.${NC}" | |
echo -e "${GREEN}${ALIEN} Generating SSL certificate for $DOMAIN using Let's Encrypt's Certbot and setting up HTTPS redirect...${NC}" | |
certbot --apache --agree-tos --redirect --hsts --uir -n -m admin@$DOMAIN -d $DOMAIN -d www.$DOMAIN | |
echo -e "${GREEN}${ALIEN} Restarting Apache2 service for SSL configuration to take effect...${NC}" | |
systemctl restart apache2 | |
echo -e "${GREEN}${ALIEN} Success: Correctly created and configured web space and SSL certificate for $DOMAIN 🎉${NC}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment