| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 | #!/bin/bash################################################################## auto-mediawiki												## Instala un servidor LAMP (Linux+Apache+MySQL+PHP) e instala	## MediaWiki y lo configura.										##																## Guzman Castanedo Villalba (guzman@castanedo.es) Junio 2018	## GPLv3 (https://www.gnu.org/licenses/gpl.html)					##################################################################OSInfo() {	#printf "Detectando SO..."	debianOS=false	rhelOS=false	OS=$(uname -s)	if [ $OS = "Linux" ]; then		OS="GNU/Linux"		if [ -f /etc/os-release ]; then			DIST=$(grep ^NAME= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)			REV=$(grep ^VERSION= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)			ID_LIKE=$(grep ^ID_LIKE= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)			for i in $ID_LIKE; do				#printf "$i\n"				case $i in					debian|ubuntu)						debianOS=true						break						;;					rhel|fedora)						rhelOS=true						break						;;					*)						debianOS=false						rhelOS=false						;;				esac			done		elif [ -f /etc/debian-version ]; then			# Familia Debian (Debian, Ubuntu, Linux Mint, ...)			DIST="Debian"			REV=""			ID_LIKE="debian"			debianOS=true		elif [ -f /etc/redhat-release ]; then			# Familia Red-Hat (RHEL, Fedora, CentOS, ...)			DIST="Red-Hat"			REV=""			ID_LIKE="rhel"			rhelOS=true		else			# Other Linux (No Soportado)			DIST=""			REV=""			ID_LIKE=""		fi	else		# UNIX, OS X, ... (No Soportado)		DIST=$OS		REV=""	fi	#printf " $DIST $REV\n"	HDInfo=$(df -h | head -1)"\n"$(df -h | grep ^/dev/sd)"\n"$(df -h | grep ^/dev/mapper)}comprobarRoot() {	if [ $(id -u) -ne 0 ]; then		printf "ERROR:\tEs necesario ser root ('sudo $0').\n"		exit 1	fi}comprobarDependencias() {	# Comprobamos whiptail	which whiptail > /dev/null 2>&1	if [ $? -ne 0 ];then		printf "ERROR:\t'whiptail' no disponible.\n"		exit 1	fi	# Comprobamos hostnamectl	which hostnamectl > /dev/null 2>&1	if [ $? -ne 0 ];then		printf "ERROR:\t'hostnamectl' no disponible.\n"		exit 1	fi	if [ $debianOS = true ];then		# Comprobamos apt-get		which apt-get > /dev/null 2>&1		if [ $? -ne 0 ]; then			printf "ERROR:\t'apt-get' no está disponible.\n"			exit 1		fi		# Actualizamos base de datos del repositorio		printf "Actualizando repositorio APT..."		result=$(apt-get -q -y update)		if [ $? -ne 0 ]; then			printf "\nERROR:\tImposible actualizar repositorio.\n"			printf "Detalles:\n$result\n"			exit 1		fi		printf " OK.\n"		# Comprobamos Firewall (ufw)		which ufw > /dev/null 2>&1		if [ $? -ne 0 ]; then			printf "ERROR:\t'ufw' no disponible.\n"			exit 1		fi	fi	if [ $rhelOS = true ]; then		# Comprobamos yum		which yum > /dev/null 2>&1		if [ $? -ne 0 ]; then			printf "ERROR:\t'yum' no está disponible.\n"			exit 1		fi		# Actualizamos base de datos del repositorio		printf "Actualizando repositorio YUM..."		result=$(yum -y makecache 2>&1)		if [ $? -ne 0 ]; then			printf "\nERROR:\tImposible actualizar repositorio.\n"			printf "Detalles:\n$result\n"			exit 1		fi		printf " OK.\n"		# Comprobamos Firewall (firewall-cmd)		which firewall-cmd > /dev/null 2>&1		if [ $? -ne 0 ]; then			printf "ERROR:\t'firewall-cmd' no disponible.\n"			exit 1		fi	fi}comprobarHostname() {	while [ -z $hostname ]; do		hostname=$(whiptail --title "Hostname" --inputbox "El nombre de dominio de este servidor es:\n"$(hostname)"\n\nQuieres cambiarlo por otro?" --ok-button "Cambiar" --cancel-button "No Cambiar" 20 70 3>&1 1>&2 2>&3)		if [ $? -eq 0 ] && [ ! -z $hostname ]; then			hostnamectl set-hostname $hostname		else			hostname=$(hostname)		fi	done	#printf "\nHostname: $hostname\n"}mostrarBienvenida() {	if [ $debianOS = false ] && [ $rhelOS = false ]; then		whiptail --title "ERROR OS NO SOPORTADO" --msgbox "Este script automatiza la creación de una web MediaWiki SOLO para distribuciones Linux de la familia Debian (Ubuntu, Linux Mint, ...) y de la familia Red-Hat (CentOS, Fedora, ...).\n\nInformación del sistema:\nOS: $OS $DIST $REV\n$HDInfo" --ok-button "Salir" 20 70		exit 1	fi	whiptail --title "Instalación MediaWiki" --yesno "Este script automatiza completamente la instalación de una wiki.\nPara ello instala un servidor LAMP, el software MediaWiki y lo configura todo.\n\nInformación del sistema:\nOS: $OS $DIST $REV\n$HDInfo" --yes-button "Continuar" --no-button "Salir" 20 70	if [ $? -ne 0 ]; then		printf "ERROR:\tInstalación interrumpida por el usuario.\n"		exit 2	fi}mostrarComponentes() {	componentes=$(whiptail --title "Instalación" --checklist "<ESPACIO>: seleccionar   <TAB>: cambiar   <FLECHAS>: moverse\n\nEscoge los componentes que quieres instalar:" 20 70 6 \	"WebServer" "Instalar servidor web http/https" ON \	"Database" "Instalar una base de datos SQL" ON \	"PHP" "Instala PHP7" ON \	"SSL/TLS" "Instalar certificados para activar HTTPS" ON \	"MediaWiki" "Instalar wiki con MediaWiki" ON \	"Moodle" "Instalar campus virtual con Moodle" ON \	--ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)	if [ $? -ne 0 ]; then		printf "ERROR:\tInstalación interrumpida por el usuario.\n"		exit 2	fi	# Mejora: autodetección de componentes ya instalados	for i in $componentes; do		case $i in			\"WebServer\")				printf "Instalando Servidor Web"				instalarWebServer				printf " OK.\n"				;;			\"Database\")				printf "Instalando Servidor Database"				instalarDatabase				printf " OK.\n"				;;			\"PHP\")				printf "Instalando PHP..."				printf " OK.\n"				;;			\"SSL/TLS\")				printf "Instalando SSL/TLS..."				printf " OK.\n"				;;			\"MediaWiki\")				printf "Instalando MediaWiki..."				printf " OK.\n"				;;			\"Moodle\")				printf "Instalando Moodle..."				printf " OK.\n"				;;			*)				printf "ERROR:\tError interno (selección de componentes).\n"				exit 1				;;		esac	done}instalarWebServer() {	webServer=$(whiptail --title "Servidor Web" --radiolist "<ESPACIO>: seleccionar   <TAB>: cambiar   <FLECHAS>: moverse\n\nEscoge el servidor web que quieres usar:" 20 70 2 \	"Apache" "Instalar el servidor web Apache2" ON \	"Nginx" "Instalar el servidor web Nginx" OFF \	--ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)	if [ $? -ne 0 ]; then		printf "\nERROR:\tInstalación interrumpida por el usuario.\n"		exit 2	fi	# Preguntar otras opciones de configuración	comprobarHostname	case $webServer in		Apache)			printf " Apache..."			instalarApache			;;		Nginx)			printf " Nginx..."			instalarNginx			;;		*)			printf "\nERROR:\tError interno (selección de web server).\n"			exit 1			;;	esac}instalarApache() {	if [ $debianOS = true ];then		result=$(apt-get -q -y install apache2 2>&1)		if [ $? -ne 0 ]; then			printf "\nERROR:\tError al instalar apache2.\n"			printf "Detalles:\n$result\n"			exit 1		fi	elif [ $rhelOS = true ]; then		result=$(yum -y install httpd 2>&1)		if [ $? -ne 0 ]; then			printf "\nERROR:\tError al instalar apache2.\n"			printf "Detalles:\n$result\n"			exit 1		fi	else		printf "\nERROR:\tError interno (instalación apache).\n"		exit 1	fi}instalarNginx() {	if [ $debianOS = true ];then		result=$(apt-get -q -y install nginx 2>&1)		if [ $? -ne 0 ]; then			printf "\nERROR:\tError al instalar apache2.\n"			printf "Detalles:\n$result\n"			exit 1		fi	elif [ $rhelOS = true ]; then		# Hay que instalar primero otro repositorio		result=$(yum -y install nginx 2>&1)		if [ $? -ne 0 ]; then			printf "\nERROR:\tError al instalar apache2.\n"			printf "Detalles:\n$result\n"			exit 1		fi	else		printf "\nERROR:\tError interno (instalación apache).\n"		exit 1	fi}instalarDatabase() {	if [ $debianOS = true ]; then		database=$(whiptail --title "Base de Datos" --radiolist "<ESPACIO>: seleccionar   <TAB>: cambiar   <FLECHAS>: moverse\n\nEscoge la base de datos que quieres usar:" 20 70 2 \		"MySQL" "Instalar la base de datos MySQL (no uso comercial)" ON \		"MariaDB" "Instalar la base de datos MariaDB (fork de MySQL)" OFF \		--ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)		if [ $? -ne 0 ]; then			printf "\nERROR:\tInstalación interrumpida por el usuario.\n"			exit 2		fi		# Preguntar otras opciones de configuración		case $database in			MySQL)				printf " MySQL..."				instalarMySQL				;;			MariaDB)				printf " MariaDB..."				instalarMariaDB				;;			*)				printf "\nERROR:\tError interno (selección de base de datos).\n"				exit 1				;;		esac	elif [ $rhelOS = true ]; then		whiptail --title "Base de Datos" --yesno "Para su distribucion $OS $DIST $REV, sólo está disponible la base de datos MariaDB." --yes-button "Continuar" --no-button "Salir" 20 70		if [ $? -ne 0 ]; then			printf "\nERROR:\tInstalación interrumpida por el usuario.\n"			exit 2		fi		printf " MariaDB.."		instalarMariaDB	else		printf "\nERROR:\tError interno (instalación Base de Datos).\n"		exit 1	fi}instalarMySQL() {	if [ $debianOS = true ];then		result=$(apt-get -q -y install mysql-server mysql-client 2>&1)		if [ $? -ne 0 ]; then			printf "\nERROR:\tError al instalar MySQL.\n"			printf "Detalles:\n$result\n"			exit 1		fi	elif [ $rhelOS = true ]; then		# MySQL no disponible en RHEL. 2 opciones:		# 1) Instalar un repositorio adicional		# 2) No instalar MySQL en distribuciones RHEL		printf "\nERROR:\tRHEL no incluye MySQL en sus repositorios.\n"		exit 1	else		printf "\nERROR:\tError interno (instalación MySQL).\n"		exit 1	fi}instalarMariaDB() {	if [ $debianOS = true ];then		result=$(apt-get -q -y install mariadb-server mariadb-client 2>&1)		if [ $? -ne 0 ]; then			printf "\nERROR:\tError al instalar MariaDB.\n"			printf "Detalles:\n$result\n"			exit 1		fi	elif [ $rhelOS = true ]; then		result=$(yum -y install mariadb-server 2>&1)		if [ $? -ne 0 ]; then			printf "\nERROR:\tError al instalar MariaDB.\n"			printf "Detalles:\n$result\n"			exit 1		fi	else		printf "\nERROR:\tError interno (instalación MariaDB).\n"		exit 1	fi}# ComprobamoscomprobarRootOSInfocomprobarDependencias# BienvenidamostrarBienvenida# Mejora: instalación express vs instalación avanzada# Selección de componentesmostrarComponentes# Pre-configuración	# Habilitar cortafuegos -> PROBLEMA: se corta la conexión ssh# Instalación	# Cuando funcionen todos los instaladores, cambiarlo por variables de estado e instalar en este punto	# whiptail --gauge# Configuración	# Configuración Apache		# Virtual Hosts	# Configuración Database		# Configuración Segura	# Configuración PHP		# cgi.fix_pathinfo=0		# Configurar máximo de subida de archivos	# Configuración MediWiki	# Configuración Moodle	# Arrancar y habilitar todos los servicios (SystemD, Service o SystemV)	# Añadir reglas del cortafuegos	# Configuración SSL/TLS		# Generar certificados		# Configurar certificados# Autodestrucción{	for i in $(seq 0 5 50); do		sleep 0.1		echo $i	done} | whiptail --gauge "Autodestrucción..." 7 70 0{	for i in $(seq 50 5 100); do		sleep 0.1		echo $i	done} | whiptail --gauge "Autopulverización..." 7 70 50
 |