|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Bash Script for Setting Up a LAMP Stack with Additional Services simple edition v1 |
| 4 | +# This script installs and configures Apache, PHP, vsftpd and SSL certificates using Certbot. |
| 5 | +# Applicable for RHEL/CentOS 8/9 and similar distributions. |
| 6 | + |
| 7 | +# Exit immediately if a command exits with a non-zero status |
| 8 | +set -e |
| 9 | + |
| 10 | +# Enable debug mode (optional, uncomment for troubleshooting) |
| 11 | +# set -x |
| 12 | + |
| 13 | +####################################### |
| 14 | +# Variables |
| 15 | +####################################### |
| 16 | +PRIMARY_DOMAIN="yourdomainhere.com" |
| 17 | +WEB_USER="webadmin" |
| 18 | +WEB_GROUP="apache" # Changed from 'www' to 'apache' to match typical group name |
| 19 | +WEB_PASS=$(openssl rand -base64 12) |
| 20 | + |
| 21 | +####################################### |
| 22 | +# Function Definitions |
| 23 | +####################################### |
| 24 | + |
| 25 | +# Function to check if a package is installed |
| 26 | +is_installed() { |
| 27 | + dnf list installed "$1" &> /dev/null |
| 28 | +} |
| 29 | + |
| 30 | +# Function to install EPEL repository |
| 31 | +install_epel() { |
| 32 | + echo "Installing EPEL repository..." |
| 33 | + if ! dnf repolist | grep -q "^epel/"; then |
| 34 | + dnf install -y epel-release || { |
| 35 | + echo "Failed to install epel-release via dnf. Attempting manual install..." |
| 36 | + # Determine OS version |
| 37 | + OS_VERSION=$(rpm -E %rhel) |
| 38 | + if [ "$OS_VERSION" -ge 9 ]; then |
| 39 | + dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm |
| 40 | + elif [ "$OS_VERSION" -eq 8 ]; then |
| 41 | + dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm |
| 42 | + else |
| 43 | + echo "Unsupported OS version. Please install EPEL repository manually." |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + } |
| 47 | + else |
| 48 | + echo "EPEL repository is already installed." |
| 49 | + fi |
| 50 | + |
| 51 | + # Enable EPEL repository |
| 52 | + dnf config-manager --enable epel |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +####################################### |
| 60 | +# Main Script Execution |
| 61 | +####################################### |
| 62 | + |
| 63 | +# Update and install prerequisites |
| 64 | +echo "Updating system packages and installing prerequisites..." |
| 65 | +dnf update -y |
| 66 | +dnf install -y wget curl |
| 67 | + |
| 68 | +# Install Apache |
| 69 | +echo "Installing Apache..." |
| 70 | +dnf install -y httpd |
| 71 | +systemctl enable httpd |
| 72 | + |
| 73 | +# Configure firewalld for necessary services |
| 74 | +echo "Configuring firewalld..." |
| 75 | +systemctl start firewalld |
| 76 | +systemctl enable firewalld |
| 77 | +firewall-cmd --permanent --add-service=http |
| 78 | +firewall-cmd --permanent --add-service=https |
| 79 | +firewall-cmd --permanent --add-service=ftp |
| 80 | +firewall-cmd --permanent --add-service=ssh |
| 81 | +firewall-cmd --reload |
| 82 | + |
| 83 | +# Install mod_ssl and enable mod_rewrite |
| 84 | +echo "Installing mod_ssl and enabling mod_rewrite for Apache..." |
| 85 | +dnf install -y mod_ssl |
| 86 | +# Create a backup of the httpd configuration file |
| 87 | +cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak |
| 88 | + |
| 89 | +# If a line to load the rewrite module doesn't exist, append it |
| 90 | +grep -q 'LoadModule rewrite_module' /etc/httpd/conf/httpd.conf \ |
| 91 | + || echo 'LoadModule rewrite_module modules/mod_rewrite.so' >> /etc/httpd/conf/httpd.conf |
| 92 | + |
| 93 | +# If a line to load the rewrite module exists but is commented, uncomment it |
| 94 | +sed -i '/LoadModule rewrite_module/s/^#//g' /etc/httpd/conf/httpd.conf |
| 95 | + |
| 96 | +# Create a directory for the primary domain |
| 97 | +echo "Creating directory for domain hosting..." |
| 98 | +mkdir -p /var/www/${PRIMARY_DOMAIN} |
| 99 | + |
| 100 | +# Create a special user for web management with a random password |
| 101 | +echo "Creating web user and group with permissions..." |
| 102 | +# Check if group exists; if not, create it |
| 103 | +if ! getent group $WEB_GROUP > /dev/null 2>&1; then |
| 104 | + groupadd $WEB_GROUP |
| 105 | + echo "Group '$WEB_GROUP' created." |
| 106 | +else |
| 107 | + echo "Group '$WEB_GROUP' already exists." |
| 108 | +fi |
| 109 | + |
| 110 | +# Check if user exists; if not, create it |
| 111 | +if ! id -u $WEB_USER > /dev/null 2>&1; then |
| 112 | + useradd -M -d /var/www -s /bin/bash -g $WEB_GROUP $WEB_USER |
| 113 | + echo "$WEB_USER:$WEB_PASS" | chpasswd |
| 114 | + echo "User '$WEB_USER' created with a random password." |
| 115 | +else |
| 116 | + echo "User '$WEB_USER' already exists." |
| 117 | +fi |
| 118 | + |
| 119 | +# Set ownership of the web directory |
| 120 | +chown -R $WEB_USER:$WEB_GROUP /var/www |
| 121 | + |
| 122 | +# Set up Apache configuration for the primary domain |
| 123 | +echo "Setting up Apache virtual host for $PRIMARY_DOMAIN..." |
| 124 | +cat <<EOL >/etc/httpd/conf.d/${PRIMARY_DOMAIN}.conf |
| 125 | +<VirtualHost *:80> |
| 126 | + ServerName ${PRIMARY_DOMAIN} |
| 127 | + DocumentRoot /var/www/${PRIMARY_DOMAIN} |
| 128 | + <Directory /var/www/${PRIMARY_DOMAIN}> |
| 129 | + AllowOverride All |
| 130 | + Require all granted |
| 131 | + </Directory> |
| 132 | +</VirtualHost> |
| 133 | +EOL |
| 134 | + |
| 135 | +# Install PHP |
| 136 | +echo "Installing the latest PHP version..." |
| 137 | +echo "Resetting PHP module..." |
| 138 | +dnf module reset php -y || true # Reset PHP module without stopping the script |
| 139 | + |
| 140 | +# Install the Remi repository |
| 141 | +dnf install -y https://rpms.remirepo.net/enterprise/remi-release-$(rpm -E '%{rhel}').rpm |
| 142 | +dnf module enable php:remi-8.2 -y |
| 143 | +dnf install -y php php-mysqlnd |
| 144 | + |
| 145 | +# Install EPEL repository |
| 146 | +install_epel |
| 147 | + |
| 148 | + |
| 149 | +# Install vsftpd for FTP access |
| 150 | +echo "Installing vsftpd..." |
| 151 | +dnf install -y vsftpd |
| 152 | +systemctl enable vsftpd |
| 153 | + |
| 154 | +# Configure vsftpd |
| 155 | +echo "Configuring vsftpd..." |
| 156 | +cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak |
| 157 | + |
| 158 | +cat <<EOL >/etc/vsftpd/vsftpd.conf |
| 159 | +anonymous_enable=NO |
| 160 | +local_enable=YES |
| 161 | +local_root=/var/www |
| 162 | +write_enable=YES |
| 163 | +chroot_local_user=YES |
| 164 | +allow_writeable_chroot=YES |
| 165 | +hide_ids=YES |
| 166 | +local_umask=022 |
| 167 | +dirmessage_enable=YES |
| 168 | +xferlog_enable=YES |
| 169 | +connect_from_port_20=YES |
| 170 | +xferlog_std_format=YES |
| 171 | +listen=NO |
| 172 | +listen_ipv6=YES |
| 173 | +pam_service_name=vsftpd |
| 174 | +userlist_enable=YES |
| 175 | +userlist_deny=NO |
| 176 | +EOL |
| 177 | + |
| 178 | +# Add the web user to vsftpd user list |
| 179 | +echo "$WEB_USER" | tee -a /etc/vsftpd/user_list >/dev/null |
| 180 | + |
| 181 | +# Set SELinux Boolean for FTP |
| 182 | +setsebool -P ftpd_full_access 1 |
| 183 | + |
| 184 | +# Restart vsftpd to apply changes |
| 185 | +systemctl restart vsftpd |
| 186 | + |
| 187 | + |
| 188 | +# Start Apache HTTP server |
| 189 | +echo "Starting Apache HTTP server..." |
| 190 | +systemctl start httpd |
| 191 | + |
| 192 | +# Install Certbot for SSL setup |
| 193 | +echo "Installing Certbot for SSL certificates..." |
| 194 | +dnf install -y certbot python3-certbot-apache |
| 195 | + |
| 196 | +# Obtain SSL certificates with Let's Encrypt |
| 197 | +echo "Obtaining SSL certificates..." |
| 198 | +certbot --apache -d ${PRIMARY_DOMAIN} --agree-tos -m admin@${PRIMARY_DOMAIN} --non-interactive --redirect |
| 199 | + |
| 200 | +# Set up automatic renewal for the certificates |
| 201 | +echo "Setting up certificate renewal..." |
| 202 | +echo "0 2 * * * root certbot renew --quiet" > /etc/cron.d/certbot-renew |
| 203 | + |
| 204 | +# Output credentials |
| 205 | +echo "=========================================" |
| 206 | +echo "Setup Completed Successfully!" |
| 207 | +echo "=========================================" |
| 208 | + |
| 209 | +echo "" |
| 210 | +echo "Web user credentials:" |
| 211 | +echo "Username: $WEB_USER" |
| 212 | +echo "Password: $WEB_PASS" |
| 213 | + |
| 214 | + |
| 215 | +echo "" |
| 216 | +echo "LAMP stack with MongoDB and FTP setup is complete with SSL support for domain $PRIMARY_DOMAIN." |
| 217 | +echo "=========================================" |
0 commit comments