diff --git a/CentOS/Install-LEMP-Stack.bash b/CentOS/Install-LEMP-Stack.bash new file mode 100755 index 0000000..f6de7dd --- /dev/null +++ b/CentOS/Install-LEMP-Stack.bash @@ -0,0 +1,170 @@ +#!/bin/bash + +set -e + +echo_rgb() { + # Echo a colored string to the terminal based on rgb values + # + # Positional Arguments: + # + # message + # - The message to be printed to stdout + # red + # - The red value from 0 to 255 + # green + # - The green value from 0 to 255 + # blue + # - The blue value from 0 to 255 + # + # Usage: + # echo_rgb "Yep" 10 8 30 + # + # POSIX Compliant: + # N/A + # + + local red + local green + local blue + local input + + input="${1}" + red="${2}" + green="${3}" + blue="${4}" + + printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}" +} + +log() { + # Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc. + # + # Arguments: + # level + # - The log level, defined within a case check in this function + # message + # - The info message + # line_number + # - The line number of the calling function (${LINNO}) + # + # Usage: + # log "info" "Could not find that directory" + # + # POSIX Compliant: + # Yes + # + + # Set debug status depending if a global debug variable has been set to either 1 or 0 + local debug + if [ ${DEBUG} ]; then + debug=${DEBUG} + else + debug=0 + fi + + local FORMAT + FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]" + + # Convert the level to uppercase + local level + level=$(echo "${1}" | tr '[:lower:]' '[:upper:]') + + local message + message="${2}" + + case "${level}" in + INFO) + # Output all info log levels to stdout + printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1 + return 0 + ;; + WARN | WARNING) + # Output all info log levels to stdout + printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1 + return 0 + ;; + DEBUG) + [[ ${debug} == 0 ]] && return + printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1 + return 0 + ;; + ERROR) + # Output all error log levels to stderr + printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2 + return 0 + ;; + # Further log levels can be added by extending this switch statement with more comparisons + + *) # Default case, no matches + # Returns non-zero code as an improper log option was passed, this helps with using `set -e` + printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2 + return 1 + ;; + esac +} + +[[ "$(id -u)" -ne 0 ]] && log "error" "$(basename "${0}") must be ran as root, exiting..." && exit 1 + +log "info" "Adding firewall rules for http & https" +firewall-cmd --permanent --add-service=http +firewall-cmd --permanent --add-service=https +firewall-cmd --reload + + +log "info" "Installing nginx" +dnf -y install nginx + +log "info" "Enabling nginx at startup and starting nginx" +systemctl enable nginx +systemctl start nginx + +log "info" "Installing mariadb & mariadb-server (mysql)" +dnf -y install mariadb mariadb-server + +log "info" "Enabling mariadb at startup and starting nginx" +systemctl enable mariadb +systemctl start mariadb + +log "info" "Running mariadb secure installation" +mysql_secure_installation + +log "info" "Installing pfp-fpm, php-mysqlnd, & php-cli" +dnf -y install php-fpm php-mysqlnd php-cli + +PHP_INI_LOCATION="/etc/php.ini" +log "info" "Updating php.ini located at ${PHP_INI_LOCATION}" +while read -r line; do + if [[ "${line}" == cgi.fix_pathinfo=* ]]; then + # Overwrites cgi.fix_pathinfo by setting it to 1 + echo "${line//cgi.fix_path_info=*/cgi.fix_pathinfo=1}" + else + echo "${line}" + fi +done < "${PHP_INI_LOCATION}" > "php.temp" && mv "php.temp" "${PHP_INI_LOCATION}" + + +PHP_WWW_CONF_LOCATION="/etc/php-fpm.d/www.conf" +log "info" "Updating php-fpm www.conf located at ${PHP_WWW_CONF_LOCATION}" +while read -r line; do + if [[ "${line}" == pm.min_spare_servers* ]]; then + # Uncomments the pm.min_spare_servers line + echo "${line//*pm.min_spare_servers*/pm.min_spare_servers = 5}" + elif [[ "${line}" == pm.max_spare_servers* ]]; then + # Uncomments the pm.max_spare_servers line + echo "${line//*pm.max_spare_servers*/pm.max_spare_servers = 35}" + elif [[ "${line}" == "listen ="* ]]; then + # Changes the listen parameter to 127.0.0.1 + echo "${line//listen =*/listen = 127.0.0.1:9000}" + else + echo "${line}" + fi +done < "${PHP_WWW_CONF_LOCATION}" > "php.www.temp" && mv "php.www.temp" "${PHP_WWW_CONF_LOCATION}" + +log "info" "Restoring SELinux settings for php-fpm.conf" +restorecon -v "${PHP_WWW_CONF_LOCATION}" + +log "info" "Enabling php-fpm and starting php-fpm" +systemctl enable php-fpm +systemctl start php-fpm + +log "info" "Finished installing LEMP stack, virtual host files should be place in /etc/nginx/conf.d/"