install 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. #!/bin/bash
  2. #################################################################
  3. # auto-elearning #
  4. # Instala automáticamente una Wiki (MediaWiki) y un Campus #
  5. # Virtual (Moodle). #
  6. # Para ello instala un entorno LEMP (GNU/Linux+Nginx+MySQL/ #
  7. # MariaDB+PHP) o LAMP (GNU/Linux+Apache+MySQL/MariaDB+PHP). #
  8. # Además instala certificados SSL/TLS válidos de Let's Encrypt #
  9. # (o auto-firmados) y genera copias de seguridad diariamente. #
  10. # #
  11. # Guzman Castanedo Villalba (guzman@castanedo.es) Junio 2018 #
  12. # GPLv3 (https://www.gnu.org/licenses/gpl.html) #
  13. #################################################################
  14. comprobarError() {
  15. # Permite comprobar si se ha producido un error y de serlo devuelve un código de error
  16. # comprobarError exitNum codeNum extraInfo
  17. # 1xx: Servidor Web
  18. # 2xx: Base de Datos
  19. # 3xx: PHP
  20. # 4xx: SSL/TLS
  21. # 5xx: MediaWiki
  22. # 6xx: Moodle
  23. # 7xx: Backup automático
  24. # 8xx: Dependencias
  25. # 9xx: Servicios & Firewall
  26. type="ERROR"
  27. exitNum=$1
  28. shift
  29. codeNum=$1
  30. shift
  31. extraInfo=$@
  32. case $codeNum in
  33. 1)
  34. error="$type $codeNum:\tInstalación interrumpida por el usuario.\n"
  35. ;;
  36. 2)
  37. error="$type $codeNum:\tError interno (selección express).\n"
  38. ;;
  39. 3)
  40. error="$type $codeNum:\tError interno (selección avanzada).\n"
  41. ;;
  42. 4)
  43. error="\n$type $codeNum:\tEl archivo '"$(realpath $extraInfo)"' no existe.\n"
  44. ;;
  45. 5)
  46. error="\n$type $codeNum:\tEl directorio '"$(realpath $extraInfo)"' no existe.\n"
  47. ;;
  48. 6)
  49. error="$type $codeNum:\tError interno (selección SSL/TLS).\n"
  50. ;;
  51. 100)
  52. error="$type $codeNum:\tError interno (selección del servidor web).\n"
  53. ;;
  54. 101)
  55. error="\n$type $codeNum:\tError al instalar Apache2.\nDetalles:\n$extraInfo\n"
  56. ;;
  57. 102)
  58. error="\n$type $codeNum:\tError al instalar Nginx.\nDetalles:\n$extraInfo\n"
  59. ;;
  60. 103)
  61. error="\n$type $codeNum:\tError al nstalar repositorio 'epel-release'.\nDetalles:\n$extraInfo\n"
  62. ;;
  63. 104)
  64. error="\n$type $codeNum:\tError interno (instalación Apache2).\n"
  65. ;;
  66. 105)
  67. error="\n$type $codeNum:\tError interno (instalación Nginx).\n"
  68. ;;
  69. 106)
  70. type="WARNING"
  71. error="\n$type $codeNum:\tImposible instalar 'info.php'.\n"
  72. ;;
  73. 107)
  74. error="\n$type $codeNum:\tError al configurar Nginx.\n"
  75. ;;
  76. 108)
  77. error="\n$type $codeNum:\tError al configurar Apache2.\n"
  78. ;;
  79. 200)
  80. error="$type $codeNum:\tError interno (selección de base de datos).\n"
  81. ;;
  82. 201)
  83. error="\n$type $codeNum:\tError al instalar MySQL.\nDetalles:\n$extraInfo\n"
  84. ;;
  85. 202)
  86. error="\n$type $codeNum:\tError al instalar MariaDB.\nDetalles:\n$extraInfo\n"
  87. ;;
  88. 203)
  89. error="\n$type $codeNum:\tError interno (instalación MySQL).\n"
  90. ;;
  91. 204)
  92. error="\n$type $codeNum:\tError interno (instalación MariaDB).\n"
  93. ;;
  94. 205)
  95. error="\n$type $codeNum:\t$DIST no incluye MySQL en sus repositorios.\n"
  96. ;;
  97. 206)
  98. type="WARNING"
  99. error="\n$type $codeNum:\tUsuario 'root' ya dispone de contraseña."
  100. ;;
  101. 207)
  102. error="\n$type $codeNum:\tImposible cambiar la contraseña de 'root'."
  103. ;;
  104. 208)
  105. error="\n$type $codeNum:\tImposible desactivar acceso 'root' desde el exterior."
  106. ;;
  107. 209)
  108. error="\n$type $codeNum:\tImposible eliminar usuarios anónimos."
  109. ;;
  110. 210)
  111. type="WARNING"
  112. error="\n$type $codeNum:\tImposible eliminar bases de datos de pruebas."
  113. ;;
  114. 211)
  115. type="WARNING"
  116. error="\n$type $codeNum:\tImposible eliminar los permisos de las bases de datos de pruebas."
  117. ;;
  118. 212)
  119. error="\n$type $codeNum:\tImposible recargar base de datos."
  120. ;;
  121. 213)
  122. error="\n$type $codeNum:\tImposible configurar SQL.\n"
  123. ;;
  124. 214)
  125. type="WARNING"
  126. error="\n$type $codeNum:\tSQL ya está configurado."
  127. ;;
  128. 300)
  129. error="\n$type $codeNum:\tError interno (instalación PHP-7).\n"
  130. ;;
  131. 301)
  132. error="\n$type $codeNum:\tError interno (Web Server no seleccionado).\n"
  133. ;;
  134. 302)
  135. error="\n$type $codeNum:\tError al instalar PHP-7.\nDetalles:\n$extraInfo\n"
  136. ;;
  137. 303)
  138. error="\n$type $codeNum:\tError al instalar Repositorio Remi.\nDetalles:\n$extraInfo\n"
  139. ;;
  140. 304)
  141. error="\n$type $codeNum:\tSistema Operativo no compatible con PHP-7 ($OS $DIST $REV).\n"
  142. ;;
  143. 305)
  144. error="\n$type $codeNum:\tImposible configurar PHP-7.\n"
  145. ;;
  146. 401)
  147. error="\n$type $codeNum:\tError al instalar 'certbot' (Let's Encrypt).\n"
  148. ;;
  149. 402)
  150. error="\n$type $codeNum:\tImposible generar Certificados Autofirmados.\n"
  151. ;;
  152. 403)
  153. error="\n$type $codeNum:\tImposible generar claves de intercambio Diffie-Hellman.\n"
  154. ;;
  155. 404)
  156. error="\n$type $codeNum:\tImposible generar Certificados Let's Encrypt.\n"
  157. ;;
  158. 501)
  159. error="\n$type $codeNum:\tImposible descargar MediaWiki-1.31.0.\n"
  160. ;;
  161. 502)
  162. error="\n$type $codeNum:\tImposible configurar MediaWiki-1.31.0.\n"
  163. ;;
  164. 503)
  165. type="WARNING"
  166. error="\n$type $codeNum:\tYa existe una copia configurada de MediaWiki en el dominio: '$extraInfo'."
  167. ;;
  168. 601)
  169. error="\n$type $codeNum:\tImposible descargar Moodle-3.5.1.\n"
  170. ;;
  171. 602)
  172. error="\n$type $codeNum:\tImposible configurar Moodle-3.5.1.\n"
  173. ;;
  174. 603)
  175. type="WARNING"
  176. error="\n$type $codeNum:\tYa existe una copia configurada de Moodle en el dominio: '$extraInfo'."
  177. ;;
  178. 604)
  179. error="\n$type $codeNum:\tImposible configurar SELinux.\n"
  180. ;;
  181. 605)
  182. error="\n$type $codeNum:\tImposible instalar 'policycoreutils-python'.Detalles:\n$extraInfo\n"
  183. ;;
  184. 701)
  185. type="WARNING"
  186. error="\n$tupe $codeNum:\tImposible configurar actualizaciones automáticas.\n"
  187. ;;
  188. 800)
  189. error="$type $codeNum:\tEs necesario ser root ('sudo $0').\n"
  190. ;;
  191. 801)
  192. error="$type $codeNum:\t'whiptail' no instalado.\n"
  193. ;;
  194. 802)
  195. error="$type $codeNum:\t'hostnamectl' no instalado.\n"
  196. ;;
  197. 803)
  198. error="$type $codeNum:\t'apt-get' no instalado.\n"
  199. ;;
  200. 804)
  201. error="$type $codeNum:\t'yum' no instalado.\n"
  202. ;;
  203. 805)
  204. error="\n$type $codeNum:\tImposible actualizar repositorio\nDetalles:\n$extraInfo.\n"
  205. ;;
  206. 806)
  207. error="$type $codeNum:\t'ufw' no instalado.\nDetalles:\n$extraInfo\n"
  208. ;;
  209. 807)
  210. error="$type $codeNum:\t'firewall-cmd' no instalado.\n"
  211. ;;
  212. 808)
  213. error="$type $codeNum:\t'tput' no instalado.\nDetalles:\n$extraInfo\n"
  214. ;;
  215. 809)
  216. error="$type $codeNum:\t'sed' no instalado.\nDetalles:\n$extraInfo\n"
  217. ;;
  218. 810)
  219. error="$type $codeNum:\t'curl' no instalado.\nDetalles:\n$extraInfo\n"
  220. ;;
  221. 811)
  222. error="$type $codeNum:\t'tar' no instalado.\nDetalles:\n$extraInfo\n"
  223. ;;
  224. 812)
  225. error="$type $codeNum:\t'gzip' no instalado.\nDetalles:\n$extraInfo\n"
  226. ;;
  227. 813)
  228. error="$type $codeNum:\t'bzip2' no instalado.\nDetalles:\n$extraInfo\n"
  229. ;;
  230. 814)
  231. error="$type $codeNum:\t'xz' no instalado.\nDetalles:\n$extraInfo\n"
  232. ;;
  233. 815)
  234. error="$type $codeNum:\t'openssl' no instalado.\nDetalles:\n$extraInfo\n"
  235. ;;
  236. 816)
  237. error="$type $codeNum:\t'find' no instalado.\n"
  238. ;;
  239. 817)
  240. error="$type $codeNum:\tLa Distribución '$OS $DIST $REV' no está soportada.\nSoportadas: Ubuntu 16.04, 18.04, Debian 9 y CentOS 7.\n"
  241. ;;
  242. 900)
  243. error="$type $codeNum:\tError interno (habilitar servicio).\n"
  244. ;;
  245. 901)
  246. error="$type $codeNum:\tImposible encender servicio '$extraInfo'.\n"
  247. ;;
  248. 902)
  249. error="$type $codeNum:\tImposible habilitar servicio '$extraInfo' durante el arranque.\n"
  250. ;;
  251. 903)
  252. error="$type $codeNum:\tError interno (deshabilitar servicio).\n"
  253. ;;
  254. 904)
  255. error="$type $codeNum:\tImposible parar servicio '$extraInfo'.\n"
  256. ;;
  257. 905)
  258. error="$type $codeNum:\tImposible deshabilitar servicio '$extraInfo' durante el arranque.\n"
  259. ;;
  260. 906)
  261. type="WARNING"
  262. error="\n$type $codeNum:\tImposible añadir regla al cortafuegos ('$extraInfo').\n"
  263. ;;
  264. 907)
  265. type="WARNING"
  266. error="\n$type $codeNum:\tImposible encender cortafuegos.\n"
  267. ;;
  268. 908)
  269. error="\n$type $codeNum:\tError interno (instalar Virtual Host).\n"
  270. ;;
  271. 909)
  272. error="\n$type $codeNum:\tVirtual Host '$extraInfo' no existe.\n"
  273. ;;
  274. 910)
  275. error="\n$type $codeNum:\tImposible copiar Virtual Host '$extraInfo'.\n"
  276. ;;
  277. 911)
  278. error="\n$type $codeNum:\tImposible activar Virtual Host '$extraInfo'.\n"
  279. ;;
  280. 912)
  281. error="\n$type $codeNum:\tError interno (recargar servicio).\n"
  282. ;;
  283. 913)
  284. error="\n$type $codeNum:\tImposible recargar servicio '$extraInfo'.\n"
  285. ;;
  286. *)
  287. error="ERROR 13:\tError interno (comprobación de errores)\n"
  288. exitNum=1
  289. codeNum=13
  290. ;;
  291. esac
  292. if [ $exitNum -ne 0 ];then
  293. echo -en "$error" | tee -a $logFile
  294. if [ $type = "ERROR" ];then
  295. exit $codeNum
  296. fi
  297. fi
  298. unset error exitNum codeNum type
  299. }
  300. OSInfo() {
  301. # Detecta el OS en el que se está ejecutando el programa, así como su versión
  302. # Sólo continuará si se trata de una distribución compatible
  303. echo -en "Detectando SO..." >> $logFile
  304. OS=$(uname -s)
  305. if [ $OS = "Linux" ]; then
  306. OS="GNU/Linux"
  307. if [ -f /etc/os-release ]; then
  308. DIST=$(grep ^NAME= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)
  309. REV=$(grep ^VERSION= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)
  310. ID=$(grep ^ID= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)
  311. ID_LIKE=$(grep ^ID_LIKE= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)
  312. VERSION=$(grep ^VERSION_ID= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)
  313. for i in $ID; do
  314. case $i in
  315. debian)
  316. debianOS=true
  317. case $VERSION in
  318. 9)
  319. apacheName="apache2"
  320. nginxName="nginx"
  321. mysqlName="mysql"
  322. mariadbName="mariadb"
  323. phpFPMName="php7.0-fpm"
  324. ;;
  325. *)
  326. comprobarError 1 817
  327. ;;
  328. esac
  329. break
  330. ;;
  331. ubuntu)
  332. debianOS=true
  333. case $VERSION in
  334. 18.04)
  335. apacheName="apache2"
  336. nginxName="nginx"
  337. mysqlName="mysql"
  338. mariadbName="mariadb"
  339. phpFPMName="php7.2-fpm"
  340. ;;
  341. 16.04)
  342. apacheName="apache2"
  343. nginxName="nginx"
  344. mysqlName="mysql"
  345. mariadbName="mysql"
  346. phpFPMName="php7.0-fpm"
  347. ;;
  348. *)
  349. comprobarError 1 817
  350. ;;
  351. esac
  352. break
  353. ;;
  354. rhel|centos)
  355. rhelOS=true
  356. case $VERSION in
  357. 7)
  358. apacheName="httpd"
  359. nginxName="nginx"
  360. mysqlName=""
  361. mariadbName="mariadb"
  362. phpFPMName="php-fpm"
  363. ;;
  364. *)
  365. comprobarError 1 817
  366. ;;
  367. esac
  368. break
  369. ;;
  370. fedora)
  371. rhelOS=true
  372. case $VERSION in
  373. 28)
  374. apacheName="httpd"
  375. nginxName="nginx"
  376. mysqlName=""
  377. mariadbName="mariadb"
  378. phpFPMName="php-fpm"
  379. ;;
  380. *)
  381. comprobarError 1 817
  382. ;;
  383. esac
  384. break
  385. ;;
  386. *)
  387. debianOS=false
  388. rhelOS=false
  389. comprobarError 1 817
  390. ;;
  391. esac
  392. done
  393. else
  394. # Other Linux (No Soportado)
  395. comprobarError 1 817
  396. fi
  397. else
  398. # UNIX, OS X, ... (No Soportado)
  399. comprobarError 1 817
  400. fi
  401. echo -en " $OS $DIST $REV\n" >> $logFile
  402. HDInfo=$(df -h | head -1)"\n"$(df -h | grep ^/dev/sd)"\n"$(df -h | grep ^/dev/mapper)
  403. echo -en "$HDInfo\n" >> $logFile
  404. }
  405. comprobarRoot() {
  406. # Comprueba si se está ejecutando con privilegios de root
  407. comprobarError $(id -u) 800
  408. }
  409. comprobarDependencias() {
  410. # Comprueba si están instalados todos los programas necesarios.
  411. # En la mayoría de los casos, trata de instalarlos.
  412. # Comprobamos whiptail
  413. which whiptail > /dev/null 2>&1
  414. comprobarError $? 801
  415. # Comprobamos hostnamectl
  416. which hostnamectl > /dev/null 2>&1
  417. comprobarError $? 802
  418. which find > /dev/null 2>&1
  419. comprobarError $? 816
  420. if [ $debianOS = true ];then
  421. # Comprobamos apt-get
  422. which apt-get > /dev/null 2>&1
  423. comprobarError $? 803
  424. # Actualizamos base de datos del repositorio
  425. echo -en "Actualizando repositorio APT..." | tee -a $logFile
  426. result=$(apt-get -q -y update 2>&1)
  427. comprobarError $? 805 $result
  428. echo -en " OK.\n" | tee -a $logFile
  429. # Comprobamos Firewall (ufw)
  430. which ufw > /dev/null 2>&1
  431. if [ $? -ne 0 ];then
  432. result=$(apt-get -q -y install ufw 2>&1)
  433. comprobarError $? 806 $result
  434. fi
  435. # Comprobamos tput
  436. which tput > /dev/null 2>&1
  437. if [ $? -ne 0 ];then
  438. result=$(apt-get -q -y install ncurses-bin 2>&1)
  439. comprobarError $? 808 $result
  440. fi
  441. # Comprobar sed
  442. which sed > /dev/null 2>&1
  443. if [ $? -ne 0 ];then
  444. result=$(apt-get -q -y install sed 2>&1)
  445. comprobarError $? 809 $result
  446. fi
  447. # Comprobar curl
  448. which curl > /dev/null 2>&1
  449. if [ $? -ne 0 ];then
  450. result=$(apt-get -q -y install curl 2>&1)
  451. comprobarError $? 810 $result
  452. fi
  453. # Comprobamos tar, gzip, bzip2 y xz
  454. which tar > /dev/null 2>&1
  455. if [ $? -ne 0 ];then
  456. result=$(apt-get -q -y install tar 2>&1)
  457. comprobarError $? 811 $result
  458. fi
  459. which gzip > /dev/null 2>&1
  460. if [ $? -ne 0 ];then
  461. result=$(apt-get -q -y install gzip 2>&1)
  462. comprobarError $? 812 $result
  463. fi
  464. which bzip2 > /dev/null 2>&1
  465. if [ $? -ne 0 ];then
  466. result=$(apt-get -q -y install bzip2 2>&1)
  467. comprobarError $? 813 $result
  468. fi
  469. which xz > /dev/null 2>&1
  470. if [ $? -ne 0 ];then
  471. result=$(apt-get -q -y install xz-utils 2>&1)
  472. comprobarError $? 814 $result
  473. fi
  474. which openssl > /dev/null 2>&1
  475. if [ $? -ne 0 ];then
  476. result=$(apt-get -q -y install openssl 2>&1)
  477. comprobarError $? 815 $result
  478. fi
  479. fi
  480. if [ $rhelOS = true ]; then
  481. # Comprobamos yum
  482. which yum > /dev/null 2>&1
  483. comprobarError $? 804
  484. # Actualizamos base de datos del repositorio
  485. echo -en "Actualizando repositorio YUM..." | tee -a $logFile
  486. result=$(yum -y makecache 2>&1)
  487. comprobarError $? 805 $result
  488. echo -en " OK.\n" | tee -a $logFile
  489. # Comprobamos Firewall (firewall-cmd)
  490. which firewall-cmd > /dev/null 2>&1
  491. comprobarError $? 807
  492. # Comprobamos tput
  493. which tput > /dev/null 2>&1
  494. if [ $? -ne 0 ];then
  495. result=$(yum -y install ncurses 2>&1)
  496. comprobarError $? 808 $result
  497. fi
  498. # Comprobar sed
  499. which sed > /dev/null 2>&1
  500. if [ $? -ne 0 ];then
  501. result=$(yum -y install sed 2>&1)
  502. comprobarError $? 809 $result
  503. fi
  504. # Comprobar curl
  505. which curl > /dev/null 2>&1
  506. if [ $? -ne 0 ];then
  507. result=$(yum -y install curl 2>&1)
  508. comprobarError $? 810 $result
  509. fi
  510. # Comprobamos tar, gzip, bzip2 y xz
  511. which tar > /dev/null 2>&1
  512. if [ $? -ne 0 ];then
  513. result=$(yum -y install tar 2>&1)
  514. comprobarError $? 811 $result
  515. fi
  516. which gzip > /dev/null 2>&1
  517. if [ $? -ne 0 ];then
  518. result=$(yum -y install gzip 2>&1)
  519. comprobarError $? 812 $result
  520. fi
  521. which bzip2 > /dev/null 2>&1
  522. if [ $? -ne 0 ];then
  523. result=$(yum -y install bzip2 2>&1)
  524. comprobarError $? 813 $result
  525. fi
  526. which xz > /dev/null 2>&1
  527. if [ $? -ne 0 ];then
  528. result=$(yum -y install xz 2>&1)
  529. comprobarError $? 814 $result
  530. fi
  531. which openssl > /dev/null 2>&1
  532. if [ $? -ne 0 ];then
  533. result=$(yum -y install openssl 2>&1)
  534. comprobarError $? 815 $result
  535. fi
  536. fi
  537. }
  538. inicializarVariables() {
  539. # Inicializa las variables que necesitan de un estado previo
  540. debianOS=false
  541. rhelOS=false
  542. apacheOn=false
  543. nginxOn=false
  544. mySQLOn=false
  545. mariaDBOn=false
  546. phpOn=false
  547. sslOn=false
  548. letsEncryptOn=false
  549. mediaWikiOn=false
  550. moodleOn=false
  551. infoPHPOn=false
  552. backupOn=false
  553. hostname=""
  554. logFile="./."$(basename $0)".log"
  555. maxUpload="100M"
  556. apacheName=""
  557. nginxName=""
  558. mysqlName=""
  559. mariadbName=""
  560. webServerName=""
  561. webServerUser=""
  562. webServerGroup=""
  563. sqlServerName=""
  564. phpFPMName=""
  565. nombreMediaWiki=""
  566. dominioMediaWiki=""
  567. passwdMediaWiki=""
  568. nombreMoodle=""
  569. nombreCortoMoodle=""
  570. dominioMoodle=""
  571. passwdMoodle=""
  572. }
  573. instalacionExpress() {
  574. # Permite una instalación rápida, haciendo el menor número de preguntas
  575. nginxOn=true
  576. mariaDBOn=true
  577. phpOn=true
  578. sslOn=true
  579. letsEncryptOn=true
  580. mediaWikiOn=true
  581. moodleOn=true
  582. backupOn=true
  583. # Preguntas mínimas
  584. establecerFQDN
  585. leerSQLPasswd
  586. mostrarMediaWiki
  587. mostrarMoodle
  588. mostrarConfirmacion
  589. }
  590. mostrarBienvenida() {
  591. # Pantalla de bienvenida y muestra SO y estado de los discos
  592. ANCHO=$(tput cols)
  593. ALTO=$(tput lines)
  594. whiptail --title "INSTALACION MEDIAWIKI" --yesno "Este script automatiza completamente la instalación de una WIKI y un CAMPUS VIRTUAL.\nPara ello instala un servidor LAMP, el software MediaWiki y configura todo lo necesario.\n\nInformación del sistema:\nOS: $OS $DIST $REV\n$HDInfo" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --yes-button "Continuar" --no-button "Salir"
  595. comprobarError $? 1
  596. }
  597. mostrarExpress() {
  598. # Pantalla de elección entre instalación express o avanzada
  599. express=$(whiptail --title "INSTALACION EXPRESS" --radiolist "<ESPACIO>: seleccionar <TAB>: cambiar <FLECHAS>: moverse\n\nLa instalación express instala Nginx, MariaDB, PHP-7, Let's Encrypt, MediaWiki, Moodle y backups automático.\nSeleccione el tipo de instalación que desee:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) 2 \
  600. "Express" "Instalación rápida" ON \
  601. "Avanzada" "Permite escoger todas las opciones disponibles" OFF \
  602. --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  603. comprobarError $? 1
  604. case $express in
  605. Express)
  606. instalacionExpress
  607. # DECIDIR QUÉ OPCIONES SON LAS MÍNIMAS
  608. ;;
  609. Avanzada)
  610. mostrarAvanzada
  611. ;;
  612. *)
  613. comprobarError 1 2
  614. ;;
  615. esac
  616. unset express
  617. }
  618. mostrarAvanzada() {
  619. # Pantalla que permite instalar un servidor LAMP o LEMP
  620. webServer=$(whiptail --title "INSTALACION AVANZADA" --radiolist "<ESPACIO>: seleccionar <TAB>: cambiar <FLECHAS>: moverse\n\nEscoge el tipo de Servidor Web que quieres instalar:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) 2 \
  621. "LEMP" "GNU/Linux + (E)Nginx + MySQL/MariaDB + PHP-7" ON \
  622. "LAMP" "GNU/Linux + Apache + MySQL/MariaDB + PHP-7" OFF \
  623. --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  624. comprobarError $? 1
  625. for i in $webServer; do
  626. case $i in
  627. LEMP)
  628. nginxOn=true
  629. ;;
  630. LAMP)
  631. apacheOn=true
  632. ;;
  633. *)
  634. comprobarError 1 100
  635. ;;
  636. esac
  637. done
  638. # Opciones comunes
  639. establecerFQDN
  640. mostrarDatabase
  641. phpOn=true
  642. establecerMaxUpload
  643. mostrarComponentes
  644. mostrarConfirmacion
  645. unset webServer
  646. }
  647. mostrarComponentes() {
  648. # Pantalla de elección de componentes (SSL/TLS, MediaWiki, Moodle, InfoPHP y Backup)
  649. componentes=$(whiptail --title "INSTALACION AVANZADA" --checklist "<ESPACIO>: seleccionar <TAB>: cambiar <FLECHAS>: moverse\n\nEscoge los componentes que quieres instalar:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) 5 \
  650. "SSL/TLS" "Instalar certificados para activar HTTPS" ON \
  651. "MediaWiki" "Instalar wiki con MediaWiki" ON \
  652. "Moodle" "Instalar campus virtual con Moodle" ON \
  653. "InfoPHP" "Instalar info.php (sólo para pruebas)" OFF \
  654. "Backup" "Programar backups automáticos" ON \
  655. --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  656. comprobarError $? 1
  657. # Mejora: autodetección de componentes ya instalados
  658. for i in $componentes; do
  659. case $i in
  660. \"SSL/TLS\")
  661. # 2 Opciones: Let's Encrypt o Autofirmado
  662. mostrarSSL
  663. ;;
  664. \"MediaWiki\")
  665. mediaWikiOn=true
  666. mostrarMediaWiki
  667. ;;
  668. \"Moodle\")
  669. moodleOn=true
  670. mostrarMoodle
  671. ;;
  672. \"InfoPHP\")
  673. infoPHPOn=true
  674. ;;
  675. \"Backup\")
  676. backupOn=true
  677. ;;
  678. *)
  679. comprobarError 1 3
  680. ;;
  681. esac
  682. done
  683. unset componentes
  684. }
  685. mostrarSSL() {
  686. # Pantalla que permite seleccionar el tipo de certificados
  687. ssl=$(whiptail --title "INSTALACION SSL/TLS" --radiolist "<ESPACIO>: seleccionar <TAB>: cambiar <FLECHAS>: moverse\n\nEscoge el tipo de Certificado SSL/TLS que quieres instalar:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) 2 \
  688. "LetsEncrypt" "Compatible con todos los navegadores" ON \
  689. "Auto-Firmado" "Certificados auto-firmados (sólo para pruebas)" OFF \
  690. --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  691. comprobarError $? 1
  692. for i in $ssl; do
  693. case $i in
  694. LetsEncrypt)
  695. sslOn=true
  696. letsEncryptOn=true
  697. ;;
  698. Auto-Firmado)
  699. sslOn=true
  700. ;;
  701. *)
  702. comprobarError 1 6
  703. ;;
  704. esac
  705. done
  706. unset ssl
  707. }
  708. mostrarMediaWiki(){
  709. # Pantalla que pregunta sobre las opciones necesarias para configurar MediaWiki
  710. # Introducir nombre wiki
  711. while [ -z "$nombreMediaWiki" ]; do
  712. nombreMediaWiki=$(whiptail --title "CONFIGURACION MEDIAWIKI" --inputbox "Introduzca el nombre de la Wiki:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  713. comprobarError $? 1
  714. done
  715. # Introducir dominio
  716. while [ -z "$dominioMediaWiki" ]; do
  717. dominioMediaWiki=$(whiptail --title "CONFIGURACION MEDIAWIKI" --inputbox "Introduzca el dominio/subdominio de la Wiki.\nTenga en cuenta que debes apuntar este dominio a la dirección IP de esta máquina mediante un registro DNS de tipo CNAME.\nSi quiere introducir varios dominios sepárelos con espacios. El primero será el principal.\nPor ejemplo: 'www.$hostname $hostname wiki.$hostname'" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) wiki.$hostname --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  718. comprobarError $? 1
  719. done
  720. cont=1
  721. serverName=""
  722. aliasMediaWiki=""
  723. for dominio in $dominioMediaWiki;do
  724. if [ $cont -eq 1 ];then
  725. # Dominio Principal (ServerName)
  726. serverName=$dominio
  727. else
  728. # Dominios Secundarios (ServerAlias)
  729. aliasMediaWiki=$aliasMediaWiki" "$dominio
  730. fi
  731. cont=$((cont + 1))
  732. done
  733. dominioMediaWiki=$serverName
  734. # Introducir contraseña
  735. control=false
  736. error=""
  737. while [ $control = false ]; do
  738. passwdMediaWiki=$(whiptail --title "CONFIGURACION MEDIAWIKI" --passwordbox "$error""Introduzca la contraseña para el usuario administrador 'admin':" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Continuar" --nocancel 3>&1 1>&2 2>&3)
  739. comprobarError $? 1
  740. passwdMediaWiki2=$(whiptail --title "CONFIGURACION MEDIAWIKI" --passwordbox "Confirme la contraseña:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Continuar" --nocancel 3>&1 1>&2 2>&3)
  741. comprobarError $? 1
  742. if [ -z "$passwdMediaWiki" ] || [ -z "$passwdMediaWiki2" ]; then
  743. error="ERROR: LA CONTRASEÑA NO PUEDE ESTAR VACIA.\n"
  744. elif [ $passwdMediaWiki != $passwdMediaWiki2 ];then
  745. error="ERROR: LAS CONTRASEÑAS NO COINCIDEN.\n"
  746. else
  747. control=true
  748. fi
  749. done
  750. unset control error passwdMediaWiki2 dominio cont serverName
  751. }
  752. mostrarMoodle() {
  753. # Pantalla que pregunta sobre las opciones necesarias para configurar Moodle
  754. # Introducir Nombre Moodle
  755. while [ -z "$nombreMoodle" ]; do
  756. nombreMoodle=$(whiptail --title "CONFIGURACION MOODLE" --inputbox "Introduzca el NOMBRE COMPLETO del campus virtual.\nPor ejemplo: 'Campus Virtual Ejemplo'." $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  757. comprobarError $? 1
  758. done
  759. # Introducir Nombre Corto Moodle
  760. while [ -z "$nombreCortoMoodle" ]; do
  761. nombreCortoMoodle=$(whiptail --title "CONFIGURACION MOODLE" --inputbox "Introduzca el NOMBRE CORTO del campus virtual.\nPor ejemplo: 'Ejemplo'." $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  762. comprobarError $? 1
  763. done
  764. nombreCortoMoodle=$(echo $nombreCortoMoodle | cut -d ' ' -f 1)
  765. # Introducir dominio
  766. while [ -z "$dominioMoodle" ]; do
  767. dominioMoodle=$(whiptail --title "CONFIGURACION MOODLE" --inputbox "Introduzca el dominio/subdominio del campus virtual.\nMoodle SÓLO permite un ÚNICO DOMINIO." $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) moodle.$hostname --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  768. comprobarError $? 1
  769. done
  770. cont=1
  771. serverName=""
  772. aliasMoodle=""
  773. for dominio in $dominioMoodle;do
  774. if [ $cont -eq 1 ];then
  775. # Dominio Principal (ServerName)
  776. serverName=$dominio
  777. else
  778. # Dominios Secundarios (ServerAlias)
  779. aliasMeoodle=$aliasMoodle" "$dominio
  780. fi
  781. cont=$((cont + 1))
  782. done
  783. dominioMoodle=$serverName
  784. # Introducir contraseña
  785. control=false
  786. error=""
  787. while [ $control = false ]; do
  788. passwdMoodle=$(whiptail --title "CONFIGURACION MOODLE" --passwordbox "$error""Introduzca la contraseña para el usuario administrador 'admin':" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Continuar" --nocancel 3>&1 1>&2 2>&3)
  789. comprobarError $? 1
  790. passwdMoodle2=$(whiptail --title "CONFIGURACION MOODLE" --passwordbox "Confirme la contraseña:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Continuar" --nocancel 3>&1 1>&2 2>&3)
  791. comprobarError $? 1
  792. if [ -z "$passwdMoodle" ] || [ -z "$passwdMoodle2" ]; then
  793. error="ERROR: LA CONTRASEÑA NO PUEDE ESTAR VACIA.\n"
  794. elif [ $passwdMoodle != $passwdMoodle2 ];then
  795. error="ERROR: LAS CONTRASEÑAS NO COINCIDEN.\n"
  796. else
  797. control=true
  798. fi
  799. done
  800. unset control error passwdMoodle2 dominio cont serverName aliasMoodle
  801. }
  802. mostrarConfirmacion() {
  803. # Pantalla que presenta una confirmación antes de realizar la instalación
  804. whiptail --title "INSTALACION" --yesno "ESTA TODO LISTO PARA LA INSTALACION.\nNO SE OLVIDE DE GUARDAR LAS CONFIGURACIONES EN UN LUGAR SEGURO, YA QUE SERÁN IMPORTANTES POR SI DESEA HACER ALGÚN CAMBIO EN EL FUTURO.\n¿DESEA CONTINUAR?" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --yes-button "Instalar" --no-button "Salir"
  805. comprobarError $? 1
  806. }
  807. establecerFQDN() {
  808. # Configura el FQDN (dominio principal del servidor)
  809. while [ -z "$hostname" ]; do
  810. hostname=$(whiptail --title "FQDN" --inputbox "El nombre de dominio principal (FQDN) de este servidor es:\n"$(hostname)"\n\n¿Quieres cambiarlo por otro?" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Cambiar" --cancel-button "No Cambiar" 3>&1 1>&2 2>&3)
  811. if [ $? -eq 0 ] && [ ! -z "$hostname" ]; then
  812. hostnamectl set-hostname $hostname
  813. else
  814. hostname=$(hostname)
  815. fi
  816. done
  817. echo -en "Hostname: $hostname\n" >> $logFile
  818. }
  819. instalarApache() {
  820. # Instala Apache2
  821. webServerName=$apacheName
  822. if [ $debianOS = true ];then
  823. result=$(apt-get -q -y install apache2 2>&1)
  824. comprobarError $? 101 $result
  825. elif [ $rhelOS = true ]; then
  826. result=$(yum -y install httpd 2>&1)
  827. comprobarError $? 101 $result
  828. if [ $sslOn = true ];then
  829. # Instalamos módulo mod_ssl (no se instala por defecto)
  830. result=$(yum -y install mod_ssl 2>&1)
  831. comprobarError $? 101 $result
  832. fi
  833. else
  834. comprobarError 1 104
  835. fi
  836. deshabilitarServicio $webServerName
  837. }
  838. configurarApache() {
  839. # Configura Apache2
  840. if [ $debianOS = true ];then
  841. apacheConfFile="./etc/apache2/apache2.conf"
  842. if [ ! -f $apacheConfFile ];then
  843. comprobarError 1 4 $apacheConfFile
  844. fi
  845. cp -f $apacheConfFile /etc/$webServerName/apache2.conf 2>/dev/null
  846. comprobarError $? 108
  847. webServerUser=$(grep ^User /etc/$webServerName/apache2.conf | cut -d ' ' -f 2)
  848. webServerGroup=$(grep ^Group /etc/$webServerName/apache2.conf | cut -d ' ' -f 2)
  849. if [ ! -L /etc/$webServerName/logs ];then
  850. ln -s /var/log/apache2 /etc/$webServerName/logs 2>/dev/null
  851. comprobarError $? 108
  852. fi
  853. # Activamos mod_rewrite (no viene activado por defecto)
  854. a2enmod rewrite >/dev/null 2>&1
  855. comprobarError $? 108
  856. # Activamos mod_ssl y mod_headers (no viene activado por defecto)
  857. if [ $sslOn = true ];then
  858. a2enmod ssl >/dev/null 2>&1
  859. comprobarError $? 108
  860. a2enmod headers >/dev/null 2>&1
  861. comprobarError $? 108
  862. fi
  863. unset apacheConfFile
  864. elif [ $rhelOS = true ];then
  865. apacheConfFile="./etc/apache2/conf/httpd.conf"
  866. sslConfFile="./etc/apache2/conf.d/ssl.conf"
  867. welcomeConfFile="/etc/apache2/conf.d/welcome.conf"
  868. if [ ! -f $apacheConfFile ];then
  869. comprobarError 1 4 $apacheConfFile
  870. fi
  871. cp -f $apacheConfFile /etc/$webServerName/conf/httpd.conf 2>/dev/null
  872. comprobarError $? 108
  873. # Desactivamos Mensaje de Bienvenida
  874. if [ -f $welcomeConfFile ];then
  875. sed -i -e 's/^/#/' $welcomeConfFile
  876. comprobarError $? 108
  877. fi
  878. # Configuramos mod_ssl
  879. if [ $sslOn = true ];then
  880. if [ ! -f $sslConfFile ];then
  881. comprobarError 1 4 $sslConfFile
  882. fi
  883. cp -f $sslConfFile /etc/$webServerName/conf.d/ssl.conf 2>/dev/null
  884. comprobarError $? 108
  885. fi
  886. webServerUser=$(grep ^User /etc/$webServerName/conf/httpd.conf | cut -d ' ' -f 2)
  887. webServerGroup=$(grep ^Group /etc/$webServerName/conf/httpd.conf | cut -d ' ' -f 2)
  888. unset apacheConfFile sslConfFile welcomeConfFile
  889. fi
  890. if [ ! -d "/etc/$webServerName/sites-available" ];then
  891. mkdir "/etc/$webServerName/sites-available" 2>/dev/null
  892. comprobarError $? 5 "/etc/$webServerName/sites-available"
  893. fi
  894. if [ ! -d "/etc/$webServerName/sites-enabled" ]; then
  895. mkdir "/etc/$webServerName/sites-enabled" 2>/dev/null
  896. comprobarError $? 5 "/etc/$webServerName/sites-enabled"
  897. else
  898. # Eliminar Virtual Host por defecto
  899. if [ -f /etc/$webServerName/sites-enabled/000-default.conf ];then
  900. rm -f /etc/$webServerName/sites-enabled/000-default.conf 2>/dev/null
  901. fi
  902. fi
  903. }
  904. instalarNginx() {
  905. # Instala Nginx
  906. webServerName=$nginxName
  907. case $ID in
  908. ubuntu|debian)
  909. result=$(apt-get -q -y install nginx 2>&1)
  910. comprobarError $? 102 $result
  911. ;;
  912. centos)
  913. # Sólo para CentOS
  914. result=$(yum -y install epel-release)
  915. comprobarError $? 103 $result
  916. result=$(yum -y install nginx 2>&1)
  917. comprobarError $? 102 $result
  918. ;;
  919. fedora)
  920. result=$(yum -y install nginx 2>&1)
  921. comprobarError $? 102 $result
  922. ;;
  923. *)
  924. comprobarError 1 105
  925. ;;
  926. esac
  927. deshabilitarServicio $webServerName
  928. }
  929. configurarNginx() {
  930. # Configura Nginx
  931. rm -Rf "/etc/$webServerName/!(sites-available|sites-enabled)" 2>/dev/null
  932. comprobarError $? 107
  933. nginxConfFile="./etc/$webServerName"
  934. if [ -d "$nginxConfFile" ];then
  935. cp -Rf $nginxConfFile/* /etc/$webServerName/ 2>/dev/null
  936. comprobarError $? 107
  937. else
  938. comprobarError 1 5 "$nginxConfFile"
  939. fi
  940. if [ ! -d "/etc/$webServerName/sites-available" ];then
  941. mkdir "/etc/$webServerName/sites-available" 2>/dev/null
  942. comprobarError $? 5 "/etc/$webServerName/sites-available"
  943. fi
  944. if [ ! -d "/etc/$webServerName/sites-enabled" ]; then
  945. mkdir "/etc/$webServerName/sites-enabled" 2>/dev/null
  946. comprobarError $? 5 "/etc/$webServerName/sites-enabled"
  947. else
  948. # Eliminar Virtual Host por defecto
  949. if [ -f /etc/$webServerName/sites-enabled/default ];then
  950. rm -f /etc/$webServerName/sites-enabled/default 2>/dev/null
  951. fi
  952. fi
  953. # Configuramos Usuario y Grupo
  954. if [ $debianOS = true ];then
  955. webServerUser="www-data"
  956. webServerGroup=$webServerUser
  957. elif [ $rhelOS = true ];then
  958. webServerUser="nginx"
  959. webServerGroup=$webServerUser
  960. fi
  961. sed -i '/user /c\user '$webServerUser';' /etc/$webServerName/nginx.conf
  962. comprobarError $? 107
  963. unset nginxConfFile
  964. }
  965. instalarVirtualHost() {
  966. # Configuramos un Virtual Host para Apache o Nginx
  967. # Uso: instalarVirtualHost $root $virtualHost $dominio $alias
  968. if [ $# -ge 3 ];then
  969. rootVirtualHost=$(realpath $1)
  970. shift
  971. virtualHostFile=$(realpath $1)
  972. shift
  973. dominioVirtualHost=$1
  974. virtualHostName=$dominioVirtualHost".conf"
  975. shift
  976. aliasVirtualHost=$@
  977. else
  978. comprobarError 1 908
  979. fi
  980. if [ ! -f $virtualHostFile ];then
  981. comprobarError 1 909 $virtualHostName
  982. fi
  983. if [ $apacheOn = true ];then
  984. # Configuramos root, dominio y logs
  985. sed -i '/ServerName /c\\tServerName '"$dominioVirtualHost" $virtualHostFile
  986. comprobarError $? 908
  987. if [ ! -z "$aliasVirtualHost" ];then
  988. sed -i '/ServerName /a\\tServerAlias '"$aliasVirtualHost" $virtualHostFile
  989. comprobarError $? 908
  990. fi
  991. sed -i '/DocumentRoot /c\\tDocumentRoot '"$rootVirtualHost" $virtualHostFile
  992. comprobarError $? 908
  993. sed -i '/ErrorLog /c\\tErrorLog logs/'"$dominioVirtualHost"'-error.log' $virtualHostFile
  994. comprobarError $? 908
  995. sed -i '/CustomLog /c\\tCustomLog logs/'"$dominioVirtualHost"'-access.log common' $virtualHostFile
  996. comprobarError $? 908
  997. if [ $sslOn = true ];then
  998. sed -i '/SSLCertificateFile /c\\tSSLCertificateFile ssl/'"$dominioVirtualHost"'.crt' $virtualHostFile
  999. comprobarError $? 908
  1000. sed -i '/SSLCertificateKeyFile /c\\tSSLCertificateKeyFile ssl/'"$dominioVirtualHost"'.key' $virtualHostFile
  1001. comprobarError $? 908
  1002. fi
  1003. elif [ $nginxOn = true ];then
  1004. # Configuramos root, dominio, logs y php-fpm.sock (NGINX)
  1005. sed -i '/root /c\\troot '$rootVirtualHost';' "$virtualHostFile"
  1006. comprobarError $? 908
  1007. sed -i '/server_name /c\\tserver_name '"$dominioVirtualHost"' '"$aliasVirtualHost"';' $virtualHostFile
  1008. comprobarError $? 908
  1009. sed -i '/access_log /c\\taccess_log /var/log/nginx/'"$dominioVirtualHost"'-access.log;' $virtualHostFile
  1010. comprobarError $? 908
  1011. sed -i '/error_log /c\\terror_log /var/log/nginx/'"$dominioVirtualHost"'-error.log;' $virtualHostFile
  1012. comprobarError $? 908
  1013. socket=$(find /var/run/ -type s -name 'php*.sock')
  1014. sed -i '/fastcgi_pass /c\\t\tfastcgi_pass unix:'"$socket"';' $virtualHostFile
  1015. comprobarError $? 908
  1016. if [ $sslOn = true ];then
  1017. sed -i '/ssl_certificate /c\\tssl_certificate ssl/'"$dominioVirtualHost"'.crt;' $virtualHostFile
  1018. comprobarError $? 908
  1019. sed -i '/ssl_certificate_key /c\\tssl_certificate_key ssl/'"$dominioVirtualHost"'.key;' $virtualHostFile
  1020. comprobarError $? 908
  1021. fi
  1022. fi
  1023. # Copiamos ficheros de configuración
  1024. cp -f $virtualHostFile "/etc/$webServerName/sites-available/$virtualHostName" >> $logFile 2>&1
  1025. comprobarError $? 910
  1026. if [ -f "/etc/$webServerName/sites-enabled/$virtualHostName" ];then
  1027. rm -f /etc/$webServerName/sites-enabled/$virtualHostName 2>/dev/null
  1028. comprobarError 911 $virtualHostName
  1029. fi
  1030. ln -s "/etc/$webServerName/sites-available/$virtualHostName" "/etc/$webServerName/sites-enabled/$virtualHostName" >> $logFile 2>&1
  1031. comprobarError $? 911 $virtualHostName
  1032. recargarServicio $webServerName
  1033. unset rootVirtualHost dominioVirtualHost virtualHostFile virtualHostName aliasVirtualHost socket
  1034. }
  1035. mostrarDatabase() {
  1036. # Pantalla que permite escoger entre instalar MariaDB o MySQL
  1037. if [ $debianOS = true ]; then
  1038. database=$(whiptail --title "BASE DE DATOS" --radiolist "<ESPACIO>: seleccionar <TAB>: cambiar <FLECHAS>: moverse\n\nEscoge la base de datos que quieres usar:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) 2 \
  1039. "MariaDB" "Instalar la base de datos MariaDB (fork de MySQL)" ON \
  1040. "MySQL" "Instalar la base de datos MySQL (uso no comercial)" OFF \
  1041. --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  1042. comprobarError $? 1
  1043. case $database in
  1044. MariaDB)
  1045. mariaDBOn=true
  1046. ;;
  1047. MySQL)
  1048. mySQLOn=true
  1049. ;;
  1050. *)
  1051. comprobarError 1 200
  1052. ;;
  1053. esac
  1054. unset database
  1055. elif [ $rhelOS = true ]; then
  1056. whiptail --title "BASE DE DATOS" --yesno "Para su distribucion $OS $DIST $REV, sólo está disponible la base de datos MariaDB." $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --yes-button "Continuar" --no-button "Salir"
  1057. comprobarError $? 1
  1058. mariaDBOn=true
  1059. else
  1060. comprobarError 1 200
  1061. fi
  1062. # Otras opciones (contraseña)
  1063. leerSQLPasswd
  1064. }
  1065. leerSQLPasswd() {
  1066. # Pantalla para leer la contraseña para el usuario root de SQL
  1067. control=false
  1068. error=""
  1069. # Leemos la contreseña (stdin) y confirmamos
  1070. while [ $control = false ]; do
  1071. sqlPasswd=$(whiptail --title "CONTRASEÑA SQL" --passwordbox "$error""Introduzca la contraseña para el usuario 'root' de la base de datos:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Continuar" --nocancel 3>&1 1>&2 2>&3)
  1072. comprobarError $? 1
  1073. sqlPasswd2=$(whiptail --title "CONTRASEÑA SQL" --passwordbox "Confirme la contraseña:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Continuar" --nocancel 3>&1 1>&2 2>&3)
  1074. comprobarError $? 1
  1075. #echo -en "SQL Password 1: $sqlPasswd\n"
  1076. #echo -en "SQL Password 2: $sqlPasswd2\n"
  1077. if [ -z "$sqlPasswd" ] || [ -z "$sqlPasswd2" ]; then
  1078. error="ERROR: LA CONTRASEÑA NO PUEDE ESTAR VACIA.\n"
  1079. elif [ $sqlPasswd != $sqlPasswd2 ];then
  1080. error="ERROR: LAS CONTRASEÑAS NO COINCIDEN.\n"
  1081. else
  1082. control=true
  1083. fi
  1084. done
  1085. #echo -en "SQL Password: $sqlPasswd\n"
  1086. unset control error sqlPasswd2
  1087. }
  1088. establecerSQLPasswd() {
  1089. # Establecemos SQL root passwd y securizamos BD (mysql_secure_installation)
  1090. # Comprobamos si ya tiene una contraseña asignada
  1091. mysql -e "FLUSH PRIVILEGES" >> $logFile 2>&1
  1092. if [ $? -eq 0 ];then
  1093. # Establecemos contraseña del usuario root (y desactivamos posibles plugins para forzar login por contraseña)
  1094. mysql -e "UPDATE mysql.user SET plugin = '', Password = PASSWORD('$sqlPasswd') WHERE User = 'root'" >> $logFile 2>&1
  1095. if [ $? -ne 0 ];then
  1096. # Lo intentamos de otra forma (para versiones modernas de mysql)
  1097. mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$sqlPasswd'" >> $logFile 2>&1
  1098. comprobarError $? 207
  1099. fi
  1100. # Aplicamos el cambio de contraseña
  1101. mysql -e "FLUSH PRIVILEGES" >> $logFile 2>&1
  1102. # Desactivamos acceso root desde el exterior (solo localhost)
  1103. mysql -u root --password=$sqlPasswd -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')" >> $logFile 2>&1
  1104. comprobarError $? 208
  1105. # Eliminamos todos los usuarios anónimos
  1106. mysql -u root --password=$sqlPasswd -e "DELETE FROM mysql.user WHERE User=''" >> $logFile 2>&1
  1107. comprobarError $? 209
  1108. # Eliminamos bases de datos 'test'
  1109. mysql -u root --password=$sqlPasswd -e "DROP DATABASE IF EXISTS test" >> $logFile 2>&1
  1110. comprobarError $? 210
  1111. # Eliminamos privilegios de la base de datos 'test'
  1112. mysql -u root --password=$sqlPasswd -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" >> $logFile 2>&1
  1113. comprobarError $? 211
  1114. # Aplicamos los cambios
  1115. mysql -u root --password=$sqlPasswd -e "FLUSH PRIVILEGES" >> $logFile 2>&1
  1116. comprobarError $? 212
  1117. else
  1118. comprobarError 1 206
  1119. fi
  1120. }
  1121. instalarMySQL() {
  1122. # Instala MySQL (sólo para Debian)
  1123. sqlServerName=$mysqlName
  1124. if [ $debianOS = true ];then
  1125. result=$(apt-get -q -y install mysql-server mysql-client 2>&1)
  1126. comprobarError $? 201 $result
  1127. elif [ $rhelOS = true ];then
  1128. # MySQL no disponible en RHEL. 2 opciones:
  1129. # 1) Instalar un repositorio adicional
  1130. # 2) No instalar MySQL en distribuciones RHEL
  1131. comprobarError 1 205
  1132. else
  1133. comprobarError 1 203
  1134. fi
  1135. deshabilitarServicio $sqlServerName
  1136. }
  1137. instalarMariaDB() {
  1138. # Instala MariaDB
  1139. sqlServerName=$mariadbName
  1140. if [ $debianOS = true ];then
  1141. result=$(apt-get -q -y install mariadb-server mariadb-client 2>&1)
  1142. comprobarError $? 202 $result
  1143. elif [ $rhelOS = true ];then
  1144. result=$(yum -y install mariadb-server mariadb 2>&1)
  1145. comprobarError $? 202 $result
  1146. else
  1147. comprobarError 1 204
  1148. fi
  1149. deshabilitarServicio $sqlServerName
  1150. }
  1151. configurarSQL() {
  1152. # Configura BarracudaFS en mysql (necesario para Moodle)
  1153. # Configura MariaDB o MySQL
  1154. if [ $mariaDBOn = true ] && [ $debianOS = true ];then
  1155. sqlConfFile="/etc/mysql/my.cnf"
  1156. elif [ $mariaDBOn = true ] && [ $rhelOS = true ];then
  1157. sqlConfFile="/etc/my.cnf"
  1158. elif [ $mySQLOn = true ] && [ $debianOS = true ];then
  1159. sqlConfFile="/etc/mysql/my.cnf"
  1160. else
  1161. comprobarError 1 213
  1162. fi
  1163. if [ ! -f $sqlConfFile ];then
  1164. comprobarError 1 213
  1165. fi
  1166. grep "# Configuration for Moodle" $sqlConfFile >/dev/null 2>&1
  1167. if [ $? -ne 0 ];then
  1168. # Establecemos UTF8 (codificación de caracteres) y Barracuda (sistema de ficheros)
  1169. echo -en "\n# Configuration for Moodle\n" >> $sqlConfFile
  1170. echo -en "[client]\n" >> $sqlConfFile
  1171. echo -en "default-character-set = utf8mb4\n\n" >> $sqlConfFile
  1172. echo -en "[mysqld]\n" >> $sqlConfFile
  1173. echo -en "innodb_file_format = Barracuda\n" >> $sqlConfFile
  1174. echo -en "innodb_file_per_table = 1\n" >> $sqlConfFile
  1175. echo -en "innodb_large_prefix\n\n" >> $sqlConfFile
  1176. echo -en "character-set-server = utf8mb4\n" >> $sqlConfFile
  1177. echo -en "collation-server = utf8mb4_unicode_ci\n" >> $sqlConfFile
  1178. echo -en "skip-character-set-client-handshake\n\n" >> $sqlConfFile
  1179. echo -en "[mysql]\n" >> $sqlConfFile
  1180. echo -en "default-character-set = utf8mb4\n" >> $sqlConfFile
  1181. else
  1182. comprobarError 1 214
  1183. fi
  1184. unset sqlConfFile
  1185. }
  1186. instalarPHP() {
  1187. # Instalamos PHP-7 y los módulos PHP que necesitan MediaWiki y Moodle
  1188. case $ID in
  1189. ubuntu|debian)
  1190. if [ $apacheOn = true ]; then
  1191. result=$(apt-get -q -y install php libapache2-mod-php php-mysql php-intl php-mbstring php-xml php-apcu php-gd php-curl php-zip php-soap php-xmlrpc 2>&1)
  1192. comprobarError $? 302 $result
  1193. elif [ $nginxOn = true ];then
  1194. result=$(apt-get -q -y install php-fpm php-mysql php-intl php-mbstring php-xml php-apcu php-gd php-curl php-zip php-soap php-xmlrpc 2>&1)
  1195. comprobarError $? 302 $result
  1196. deshabilitarServicio $phpFPMName
  1197. else
  1198. comprobarError 1 301
  1199. fi
  1200. ;;
  1201. centos)
  1202. # Necesitamos un repositorio adicional para PHP-7 (EPEL-RELEASE) sólo en CentOS
  1203. result=$(yum -y install epel-release yum-utils 2>&1)
  1204. comprobarError $? 103 $result
  1205. # Necesitamos un repositorio adicional para PHP-7 (REMI-RELEASE)
  1206. if [ ! -f /etc/yum.repos.d/remi-php72.repo ];then
  1207. result=$(yum -y install "http://remi.mirrors.cu.be/enterprise/remi-release-7.rpm" 2>&1)
  1208. comprobarError $? 303 $result
  1209. fi
  1210. # Activamos repositorio remi-php72
  1211. result=$(yum-config-manager --enable remi-php72 2>&1)
  1212. comprobarError $? 303 $result
  1213. # Instalamos PHP-7.2
  1214. if [ $apacheOn = true ];then
  1215. result=$(yum -y install php php-mysql php-intl php-mbstring php-mcrypt php-xml php-pecl-apcu php-gd php-pear-Net-Curl php-pecl-zip php-soap php-xmlrpc 2>&1)
  1216. comprobarError $? 302 $result
  1217. elif [ $nginxOn = true ];then
  1218. result=$(yum -y install php php-fpm php-mysql php-intl php-mbstring php-mcrypt php-xml php-pecl-apcu php-gd php-pear-Net-Curl php-pecl-zip php-soap php-xmlrpc 2>&1)
  1219. comprobarError $? 302 $result
  1220. deshabilitarServicio $phpFPMName
  1221. else
  1222. comprobarError 1 301
  1223. fi
  1224. ;;
  1225. fedora)
  1226. # Instalamos PHP-7.2
  1227. if [ $apacheOn = true ];then
  1228. result=$(yum -y install php php-mysqlnd php-intl php-mbstring php-pecl-mcrypt php-xml php-pecl-apcu php-gd php-pear-Net-Curl php-pecl-zip php-soap php-xmlrpc php-json 2>&1)
  1229. comprobarError $? 302 $result
  1230. elif [ $nginxOn = true ];then
  1231. result=$(yum -y install php php-fpm php-mysqlnd php-intl php-mbstring php-pecl-mcrypt php-xml php-pecl-apcu php-gd php-pear-Net-Curl php-pecl-zip php-soap php-xmlrpc php-json 2>&1)
  1232. comprobarError $? 302 $result
  1233. deshabilitarServicio $phpFPMName
  1234. else
  1235. comprobarError 1 301
  1236. fi
  1237. ;;
  1238. *)
  1239. comprobarError 1 300
  1240. ;;
  1241. esac
  1242. }
  1243. configurarPHP() {
  1244. # Configura PHP para segurizarlo y establecer máximo de subida.
  1245. if [ $debianOS = true ];then
  1246. phpConfFile="./etc/php/php.ini.debian"
  1247. phpDest="/etc/php/7.2/cli/php.ini"
  1248. elif [ $rhelOS = true ];then
  1249. phpConfFile="./etc/php/php.ini.rhel"
  1250. phpDest="/etc/php.ini"
  1251. fi
  1252. if [ ! -f $phpConfFile ];then
  1253. comprobarError 1 4 "$phpConfFile"
  1254. fi
  1255. # Copiamos configuración
  1256. cp -f $phpConfFile $phpDest 2>/dev/null
  1257. comprobarError $? 305
  1258. # Configuramos cgi.fix_pathinfo
  1259. sed -i '/cgi.fix_pathinfo=/c\cgi.fix_pathinfo=0' $phpDest
  1260. comprobarError $? 305
  1261. # Configuramos post_max_size
  1262. sed -i '/post_max_size =/c\post_max_size = '$maxUpload $phpDest
  1263. comprobarError $? 305
  1264. # Configuramos upload_max_filesize
  1265. sed -i '/upload_max_filesize =/c\upload_max_filesize = '$maxUpload $phpDest
  1266. comprobarError $? 305
  1267. # Configurar php-fpm (sólo en RHEL con Nginx)
  1268. if [ $rhelOS = true ] && [ $nginxOn = true ];then
  1269. # Configurar Socket UNIX
  1270. phpConfFile="./etc/php/php-fpm.d/www.conf"
  1271. phpDest="/etc/php-fpm.d/www.conf"
  1272. if [ ! -f $phpConfFile ];then
  1273. comprobarError $? 4 "$phpConfFile"
  1274. fi
  1275. cp -f $phpConfFile $phpDest 2>/dev/null
  1276. comprobarError $? 305
  1277. # Configurar Permisos /var/lib/php
  1278. chown -R $webServerUser:$webServerGroup /var/lib/php/
  1279. comprobarError $? 305
  1280. fi
  1281. unset phpConfFile phpDest
  1282. }
  1283. establecerMaxUpload() {
  1284. # Pantalla para establecer el máximo de subida al servidor (vía PHP)
  1285. control=false
  1286. error=""
  1287. while [ $control = false ];do
  1288. maxUpload2=$(whiptail --title "PHP MAX UPLOAD" --inputbox "$error""Introduzca el tamaño máximo permitido de los ficheros subidos al servidor:\n(<K:Kilobyte> <M:Megabyte> <G:Gigabyte>)" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) $maxUpload --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  1289. comprobarError $? 1
  1290. if [ -z "$maxUpload2" ];then
  1291. error="ERROR: NO PUEDES DEJAR ESTE PARÁMETRO VACIO.\n"
  1292. else
  1293. control=true
  1294. fi
  1295. done
  1296. maxUpload=$maxUpload2
  1297. unset control maxUpload2
  1298. }
  1299. instalarLetsEncrypt() {
  1300. # Instala certbot (para certificados Let's Encrypt)
  1301. # Comprobamos si cerbot está instalado
  1302. which certbot > /dev/null 2>&1
  1303. if [ $? -ne 0 ];then
  1304. # Descargar Let's Encrypt, hacer ejecutable e instalar
  1305. curl -o ./var/certbot https://dl.eff.org/certbot-auto 2>/dev/null
  1306. comprobarError $? 401
  1307. chmod 755 ./var/certbot 2>/dev/null
  1308. comprobarError $? 401
  1309. cp -f ./var/certbot /usr/bin/certbot 2>/dev/null
  1310. comprobarError $? 401
  1311. # Configurar /etc/cron.d/certbot para renovar automáticamente los certificados
  1312. echo -en "# /etc/cron.d/certbot: certbot autorenew the certificates twice a day\n" > /etc/cron.d/certbot
  1313. echo -en "SHELL=/bin/sh\n" >> /etc/cron.d/certbot
  1314. echo -en "PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n\n" >> /etc/cron.d/certbot
  1315. echo -en "# Job start twice per day (4am & 2pm) in a random minute" >> /etc/cron.d/certbot
  1316. echo -en "0 4,14 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew\n" >> /etc/cron.d/certbot
  1317. fi
  1318. }
  1319. generarCertLetsEncrypt() {
  1320. # Generar Certificados SEGUROS Let's Encrypt
  1321. dominios=""
  1322. if [ $apacheOn = true ];then
  1323. # Usamos plugin para apache
  1324. if [ $mediaWikiOn = true ];then
  1325. if [ ! -z "$aliasMediaWiki" ];then
  1326. dominios=$dominioMediaWiki$(echo "$aliasMediaWiki" | sed -e 's/ /,/g')
  1327. else
  1328. dominios=$dominioMediaWiki
  1329. fi
  1330. certbot -n --agree-tos --email "admin@$hostname" --apache --domains "$dominios" >> $logFile 2>&1
  1331. comprobarError $? 404
  1332. fi
  1333. if [ $moodleOn = true ];then
  1334. certbot -n --agree-tos --email "admin@$hostname" --apache --domains "$dominioMoodle" >> $logFile 2>&1
  1335. comprobarError $? 404
  1336. fi
  1337. elif [ $nginxOn = true ];then
  1338. # Usamos plugin para nginx
  1339. if [ $mediaWikiOn = true ];then
  1340. if [ ! -z "$aliasMediaWiki" ];then
  1341. dominios=$dominioMediaWiki$(echo "$aliasMediaWiki" | sed -e 's/ /,/g')
  1342. else
  1343. dominios=$dominioMediaWiki
  1344. fi
  1345. certbot -n --agree-tos --email "admin@$hostname" --nginx --domains "$dominios" >> $logFile 2>&1
  1346. comprobarError $? 404
  1347. fi
  1348. if [ $moodleOn = true ];then
  1349. certbot -n --agree-tos --email "admin@$hostname" --nginx --domains "$dominioMoodle" >> $logFile 2>&1
  1350. comprobarError $? 404
  1351. fi
  1352. else
  1353. comprobarError 1 404
  1354. fi
  1355. unset dominios
  1356. }
  1357. generarCertAutofirmado() {
  1358. # Genera certificados autofirmados
  1359. # Estos certificados no son seguros, pero valen para pruebas o como paso intermedio para obtener los válidos
  1360. # Uso: generarCertAutofirmado $dominio
  1361. dominio=$1
  1362. sslDir="/etc/$webServerName/ssl"
  1363. keyFile="$sslDir/$dominio.key"
  1364. reqFile="$sslDir/$dominio.csr"
  1365. certFile="$sslDir/$dominio.crt"
  1366. dhParamFile="$sslDir/dhparam.pem"
  1367. if [ ! -d "$sslDir" ];then
  1368. mkdir "$sslDir" 2>/dev/null
  1369. comprobarError $? 5 "$sslDir"
  1370. fi
  1371. # Generamos clave privada RSA de 2048 bits
  1372. openssl genrsa -out "$keyFile" 2048 >/dev/null 2>&1
  1373. comprobarError $? 402
  1374. # Generamos petición de firma
  1375. echo -en "ES\nMadrid\nMadrid\n$dominio\n$dominio\n$dominio\nadmin@$dominio\n\n\n" | openssl req -new -key "$keyFile" -out "$reqFile" >/dev/null 2>&1
  1376. comprobarError $? 402
  1377. # Autofirmados clave para 2 años (730 días)
  1378. openssl x509 -req -days 730 -in "$reqFile" -signkey "$keyFile" -out "$certFile" >/dev/null 2>&1
  1379. comprobarError $? 402
  1380. # Eliminamos petición
  1381. rm -f $reqFile
  1382. comprobarError $? 402
  1383. # Añadimos clave Diffie-Hellman al certificado
  1384. if [ -f "$dhParamFile" ];then
  1385. cat "$dhParamFile" >> $certFile
  1386. comprobarError $? 402
  1387. fi
  1388. # Cambiamos permisos
  1389. chmod 644 $certFile
  1390. comprobarError $? 402
  1391. chmod 400 $keyFile
  1392. comprobarError $? 402
  1393. unset dominio sslDir keyFile reqFile certFile dhParamFile
  1394. }
  1395. generarDHParam() {
  1396. # Genera clave de intercambio Diffie-Hellman y lo configuramos
  1397. # Esto aumenta notablemente la seguridad de SSL/TLS
  1398. sslDir="/etc/$webServerName/ssl"
  1399. dhParamFile="$sslDir/dhparam.pem"
  1400. # apacheConfFile=""
  1401. nginxConfFile="/etc/$webServerName/nginx.conf"
  1402. if [ ! -d "$sslDir" ];then
  1403. mkdir "$sslDir" 2>/dev/null
  1404. comprobarError $? 5 "$sslDir"
  1405. fi
  1406. openssl dhparam -out "$dhParamFile" 2048 >/dev/null 2>&1
  1407. comprobarError $? 403
  1408. # Configuramos Servidor Web
  1409. # if [ $apacheOn = true ];then
  1410. # if [ $debianOS = true ];then
  1411. # apacheConfFile="/etc/$webServerName/apache2.conf"
  1412. # elif [ $rhelOS = true ];then
  1413. # # La version de httpd de RHEL no acepta SSLOpenSSLConfCmd
  1414. # # Solución: añadirlo al certificado
  1415. # apacheConfFile="/etc/$webServerName/conf/httpd.conf"
  1416. # else
  1417. # comprobarError 1 403
  1418. # fi
  1419. # echo -en "\n# Set Stronger Diffie-Hellman key exchange\n" >> $apacheConfFile
  1420. # echo -en "SSLOpenSSLConfCmd DHParameters \"$dhParamFile\"\n" >> $apacheConfFile
  1421. # el
  1422. if [ $nginxOn = true ];then
  1423. sed -i '/ssl_param /c\\tssl_dhparam '$dhParamFile';' $nginxConfFile
  1424. comprobarError $? 403
  1425. fi
  1426. # unset sslDir dhParamFile apacheConfFile nginxConfFile
  1427. unset sslDir dhParamFile nginxConfFile
  1428. }
  1429. habilitarServicio() {
  1430. # Arrancamos y habilitamos el servicio (con SystemD, Upstart o SystemV)
  1431. # Intentamos con systemctl (SystemD)
  1432. #echo -en "Habilitando Servicio '$1'" | tee -a $logFile
  1433. if [ $# -le 0 ];then
  1434. comprobarError 1 900
  1435. fi
  1436. which systemctl > /dev/null 2>&1
  1437. if [ $? -eq 0 ]; then
  1438. #echo -en " (SystemD)..." | tee -a $logFile
  1439. systemctl start $1 > /dev/null 2>&1
  1440. comprobarError $? 901 $1
  1441. systemctl enable $1 > /dev/null 2>&1
  1442. comprobarError $? 902 $1
  1443. else
  1444. # Intentamos con service (Upstart)
  1445. which service > /dev/null 2>&1
  1446. if [ $? -eq 0 ]; then
  1447. #echo -en " (Upstart)..." | tee -a $logFile
  1448. service $1 start > /dev/null 2>&1
  1449. comprobarError $? 901 $1
  1450. else
  1451. # Intentamos con init.d (SystemV)
  1452. #echo -en " (SystemV)..." | tee -a $logFile
  1453. /etc/init.d/$1 start > /dev/null 2>&1
  1454. comprobarError $? 901 $1
  1455. fi
  1456. # Intentamos habilitar en el arranque (Upstart)
  1457. which update-rc.d > /dev/null 2>&1
  1458. if [ $? -eq 0 ];then
  1459. update-rc.d $1 enable
  1460. comprobarError $? 902 $1
  1461. else
  1462. # Intentamos habilitar en el arranque (SystemV)
  1463. which chkconfig > /dev/null 2>&1
  1464. if [ $? -eq 0 ];then
  1465. chkconfig $1 on
  1466. comprobarError $? 902 $1
  1467. else
  1468. # ¿Qué mas opciones nos quedan?
  1469. comprobarError 1 902 $1
  1470. fi
  1471. fi
  1472. fi
  1473. # Mejora: comprobar si el servicio está parado
  1474. #echo -en " OK.\n" | tee -a $logFile
  1475. }
  1476. deshabilitarServicio() {
  1477. # Paramos y deshabilitamos el servicio (con SystemD, Upstart o SystemV)
  1478. # Intentamos con systemctl (SystemD)
  1479. #echo -en "Deshabilitando Servicio '$1'" | tee -a $logFile
  1480. if [ $# -le 0 ];then
  1481. comprobarError 1 903
  1482. fi
  1483. which systemctl > /dev/null 2>&1
  1484. if [ $? -eq 0 ]; then
  1485. #echo -en " (SystemD)..." | tee -a $logFile
  1486. systemctl stop $1 > /dev/null 2>&1
  1487. comprobarError $? 904 $1
  1488. systemctl disable $1 > /dev/null 2>&1
  1489. comprobarError $? 905 $1
  1490. else
  1491. # Intentamos con service (Upstart)
  1492. which service > /dev/null 2>&1
  1493. if [ $? -eq 0 ]; then
  1494. #echo -en " (Upstart)..." | tee -a $logFile
  1495. service $1 stop > /dev/null 2>&1
  1496. comprobarError $? 904 $1
  1497. else
  1498. # Intentamos con init.d (SystemV)
  1499. #echo -en " (SystemV)..." | tee -a $logFile
  1500. /etc/init.d/$1 stop > /dev/null 2>&1
  1501. comprobarError $? 904 $1
  1502. fi
  1503. # Intentamos habilitar en el arranque (Upstart)
  1504. which update-rc.d > /dev/null 2>&1
  1505. if [ $? -eq 0 ];then
  1506. update-rc.d $1 disable
  1507. comprobarError $? 905 $1
  1508. else
  1509. # Intentamos habilitar en el arranque (SystemV)
  1510. which chkconfig > /dev/null 2>&1
  1511. if [ $? -eq 0 ];then
  1512. chkconfig $1 off
  1513. comprobarError $? 905 $1
  1514. else
  1515. # ¿Qué mas opciones nos quedan?
  1516. comprobarError 1 905 $1
  1517. fi
  1518. fi
  1519. fi
  1520. # Mejora: comprobar si el servicio está funcionando
  1521. #echo -en " OK.\n" | tee -a $logFile
  1522. }
  1523. recargarServicio() {
  1524. # Recargamos el servicio (con SystemD, Upstart o SystemV)
  1525. # Intentamos con systemctl (SystemD)
  1526. #echo -en "Recargando Servicio '$1'" | tee -a $logFile
  1527. if [ $# -le 0 ];then
  1528. comprobarError 1 912
  1529. fi
  1530. which systemctl > /dev/null 2>&1
  1531. if [ $? -eq 0 ]; then
  1532. #echo -en " (SystemD)..." | tee -a $logFile
  1533. systemctl reload $1 > /dev/null 2>&1
  1534. comprobarError $? 913 $1
  1535. else
  1536. # Intentamos con service (Upstart)
  1537. which service > /dev/null 2>&1
  1538. if [ $? -eq 0 ]; then
  1539. #echo -en " (Upstart)..." | tee -a $logFile
  1540. service $1 reload > /dev/null 2>&1
  1541. comprobarError $? 913 $1
  1542. else
  1543. # Intentamos con init.d (SystemV)
  1544. #echo -en " (SystemV)..." | tee -a $logFile
  1545. /etc/init.d/$1 reload > /dev/null 2>&1
  1546. comprobarError $? 913 $1
  1547. fi
  1548. fi
  1549. # Mejora: comprobar si el servicio está funcionando
  1550. #echo -en " OK.\n" | tee -a $logFile
  1551. }
  1552. configurarCortafuegos() {
  1553. # Configuramos cortafuegos (añadir reglas y encender)
  1554. # Comprobamos si estamos usando SSH
  1555. esSSH $PPID
  1556. if [ $debianOS = true ];then
  1557. ufw allow 80/tcp >> $logFile 2>&1
  1558. comprobarError $? 906 "80/tcp"
  1559. if [ $sshControl = true ];then
  1560. ufw allow 22/tcp >> $logFile 2>&1
  1561. comprobarError $? 906 "22/tcp"
  1562. fi
  1563. if [ $sslOn = true ];then
  1564. ufw allow 443/tcp >> $logFile 2>&1
  1565. comprobarError $? 906 "443/tcp"
  1566. fi
  1567. ufw --force enable >> $logFile 2>&1
  1568. comprobarError $? 907
  1569. elif [ $rhelOS = true ];then
  1570. firewall-cmd --add-port=80/tcp >> $logFile 2>&1
  1571. comprobarError $? 906 "80/tcp"
  1572. if [ $sshControl = true ];then
  1573. firewall-cmd --add-port=22/tcp >> $logFile 2>&1
  1574. comprobarError $? 906 "22/tcp"
  1575. fi
  1576. if [ $sslOn = true ];then
  1577. firewall-cmd --add-port=443/tcp >> $logFile 2>&1
  1578. comprobarError $? 906 "443/tcp"
  1579. fi
  1580. firewall-cmd --runtime-to-permanent >> $logFile 2>&1
  1581. comprobarError $? 907
  1582. habilitarServicio firewalld >> $logFile 2>&1
  1583. comprobarError $? 907
  1584. fi
  1585. unset sshControl
  1586. }
  1587. esSSH() {
  1588. # Comprobamos si nuestro terminal usa SSH
  1589. p=${1:-$PPID}
  1590. #read pid name x ppid y < <( cat /proc/$p/stat )
  1591. read pid name ppid < <( ps -o pid= -o comm= -o ppid= -p $p)
  1592. [[ "$name" =~ sshd ]] && { sshControl=true; return 0; }
  1593. [ "$ppid" -le 1 ] && { sshControl=false; return 1; }
  1594. esSSH $ppid
  1595. }
  1596. descargarMediaWiki() {
  1597. # Descargar la versión 1.31.0 de MediaWiki
  1598. if [ ! -d ./var ];then
  1599. comprobarError 1 5 "./var"
  1600. fi
  1601. curl "https://releases.wikimedia.org/mediawiki/1.31/mediawiki-1.31.0.tar.gz" 2>/dev/null | tar -xz -C "./var/"
  1602. comprobarError $? 501
  1603. }
  1604. configurarMediaWiki() {
  1605. # Configuramos MediaWiki
  1606. if [ ! -f /var/www/$dominioMediaWiki/LocalSettings.php ];then
  1607. # Copiamos archivos
  1608. cp -fR ./var/mediawiki-1.31.0/ /var/www/$dominioMediaWiki
  1609. comprobarError $? 502
  1610. # Configuramos LocalSettings.php (y crea la base de datos por nosotros)
  1611. nombreDBMW=$(echo $dominioMediaWiki | sed -e 's/\./_/g' | sed -e 's/-/_/g')
  1612. userDBMW=$(echo "mediawiki_user" | sed -e 's/\./_/g' | sed -e 's/-/_/g')
  1613. php /var/www/$dominioMediaWiki/maintenance/install.php --dbname=$nombreDBMW --dbserver="localhost" --installdbuser=root --installdbpass=$sqlPasswd --dbuser=$userDBMW --dbpass=$sqlPasswd --scriptpath="" --lang=es --pass=$passwdMediaWiki --with-extensions "$nombreMediaWiki" "admin" >> $logFile 2>&1
  1614. comprobarError $? 502
  1615. # Configuraciones adicionales
  1616. sed -i '/$wgLanguageCode =/c\$wgLanguageCode = "es";' /var/www/$dominioMediaWiki/LocalSettings.php
  1617. comprobarError $? 502
  1618. sed -i '/$wgEnableEmail =/c\$wgEnableEmail = false;' /var/www/$dominioMediaWiki/LocalSettings.php
  1619. comprobarError $? 502
  1620. sed -i '/$wgEnableUploads =/c\$wgEnableUploads = true;' /var/www/$dominioMediaWiki/LocalSettings.php
  1621. comprobarError $? 502
  1622. echo -en "\$wgArticlePath = \"/wiki/\$1\";\n" >> /var/www/$dominioMediaWiki/LocalSettings.php
  1623. echo -en "\$wgUsePathInfo = true;\n" >> /var/www/$dominioMediaWiki/LocalSettings.php
  1624. echo -en "\$wgGroupPermissions['*']['createaccount'] = true;\n" >> /var/www/$dominioMediaWiki/LocalSettings.php
  1625. echo -en "\$wgGroupPermissions['*']['edit'] = false;\n" >> /var/www/$dominioMediaWiki/LocalSettings.php
  1626. echo -en "\$wgGroupPermissions['*']['read'] = true;\n" >> /var/www/$dominioMediaWiki/LocalSettings.php
  1627. # Actualizamos permisos
  1628. chown -R $webServerUser:$webServerGroup /var/www/$dominioMediaWiki
  1629. comprobarError $? 502
  1630. # Configuramos VirtualHost
  1631. if [ $apacheOn = true ] && [ $sslOn = false ];then
  1632. virtualHost="./etc/apache2/sites-available/mediawiki.conf"
  1633. elif [ $nginxOn = true ] && [ $sslOn = false ];then
  1634. virtualHost="./etc/nginx/sites-available/mediawiki.conf"
  1635. elif [ $apacheOn = true ] && [ $sslOn = true ];then
  1636. virtualHost="./etc/apache2/sites-available/mediawiki-ssl.conf"
  1637. elif [ $nginxOn = true ] && [ $sslOn = true ];then
  1638. virtualHost="./etc/nginx/sites-available/mediawiki-ssl.conf"
  1639. else
  1640. comprobarError 1 502
  1641. fi
  1642. if [ ! -f $virtualHost ];then
  1643. comprobarError $? 502
  1644. fi
  1645. instalarVirtualHost "/var/www/$dominioMediaWiki" $virtualHost $dominioMediaWiki $aliasMediaWiki
  1646. unset virtualHost
  1647. else
  1648. comprobarError 1 503 $dominioMediaWiki
  1649. fi
  1650. }
  1651. descargarMoodle() {
  1652. # Descargar la versión 1.31.0 de MediaWiki
  1653. if [ ! -d ./var ];then
  1654. comprobarError 1 5 "./var"
  1655. fi
  1656. #echo -en "Descargando Moodle-3.5-1..." | tee -a $logFile
  1657. #curl -o "./var/moodle-3.5.1.tgz" "https://download.moodle.org/download.php/direct/stable35/moodle-latest-35.tgz" >> $logFile 2>&1
  1658. curl "https://download.moodle.org/download.php/direct/stable35/moodle-latest-35.tgz" 2>/dev/null | tar -xz -C "./var/"
  1659. comprobarError $? 501
  1660. #echo -en " OK.\n" | tee -a $logFile
  1661. }
  1662. configurarMoodle() {
  1663. # Configurar Moodle
  1664. if [ ! -f /var/www/$dominioMoodle/config.php ];then
  1665. # Copiamos archivos
  1666. cp -Rf ./var/moodle/ /var/www/$dominioMoodle
  1667. comprobarError $? 602
  1668. # Crear carperta de datos (no online)
  1669. if [ ! -d /var/www/moodledata ];then
  1670. mkdir /var/www/moodledata 2>/dev/null
  1671. comprobarError $? 602
  1672. fi
  1673. #Actualizamos permisos
  1674. perm=$(stat -c %a /var/www/moodledata)
  1675. if [ $perm -ne 777 ];then
  1676. chmod -R 777 /var/www/moodledata 2>/dev/null
  1677. comprobarError $? 602
  1678. fi
  1679. perm=$(stat -c %U /var/www/moodledata)
  1680. if [ $perm != "$webServerUser" ];then
  1681. chown -R $webServerUser:$webServerGroup /var/www/moodledata 2>/dev/null
  1682. comprobarError $? 602
  1683. fi
  1684. unset perm
  1685. # Creamos Base de Datos
  1686. nombreDBMo=$(echo $dominioMoodle | sed -e 's/\./_/g' | sed -e 's/-/_/g')
  1687. userDBMo=$(echo "moodle_user" | sed -e 's/\./_/g' | sed -e 's/-/_/g')
  1688. crearDBMoodle
  1689. # Configurar SE-Linux (RHEL)
  1690. if [ $rhelOS = true ];then
  1691. configurarSELinux
  1692. fi
  1693. # Configuramos e instalamos Moodle
  1694. if [ $sslOn = true ];then
  1695. wwwroot="https://$dominioMoodle"
  1696. else
  1697. wwwroot="http://$dominioMoodle"
  1698. fi
  1699. if [ $mySQLOn = true ];then
  1700. dbtype="mysqli"
  1701. elif [ $mariaDBOn = true ];then
  1702. dbtype="mariadb"
  1703. else
  1704. comprobarError 1 602
  1705. fi
  1706. php /var/www/$dominioMoodle/admin/cli/install.php --lang=es --wwwroot="$wwwroot" --dataroot="/var/www/moodledata" --dbtype="$dbtype" --dbname="$nombreDBMo" --dbuser="$userDBMo" --dbpass="$sqlPasswd" --fullname="$nombreMoodle" --shortname="$nombreCortoMoodle" --adminuser=admin --adminpass="$passwdMoodle" --adminemail="admin@$dominioMoodle" --agree-license --non-interactive >>$logFile 2>&1
  1707. comprobarError $? 602
  1708. # Actualizamos permisos
  1709. chown -R $webServerUser:$webServerGroup /var/www/$dominioMoodle /var/www/moodledata
  1710. # Configuramos VirtualHost
  1711. if [ $apacheOn = true ] && [ $sslOn = false ];then
  1712. virtualHost="./etc/apache2/sites-available/moodle.conf"
  1713. elif [ $nginxOn = true ] && [ $sslOn = false ];then
  1714. virtualHost="./etc/nginx/sites-available/moodle.conf"
  1715. elif [ $apacheOn = true ] && [ $sslOn = true ];then
  1716. virtualHost="./etc/apache2/sites-available/moodle-ssl.conf"
  1717. elif [ $nginxOn = true ] && [ $sslOn = true ];then
  1718. virtualHost="./etc/nginx/sites-available/moodle-ssl.conf"
  1719. else
  1720. comprobarError 1 602
  1721. fi
  1722. instalarVirtualHost "/var/www/$dominioMoodle" $virtualHost $dominioMoodle
  1723. unset virtualHost wwwroot dbtype
  1724. else
  1725. comprobarError 1 603 $dominioMoodle
  1726. fi
  1727. }
  1728. crearDBMoodle() {
  1729. # Crear base de datos para Moodle
  1730. dbDir="./etc/db"
  1731. if [ ! -d $dbDir ];then
  1732. mkdir $dbDir 2>/dev/null
  1733. comprobarError $? 603
  1734. fi
  1735. dbFile=$dbDir"/moodle.sql"
  1736. echo -en "CREATE DATABASE IF NOT EXISTS $nombreDBMo DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n" > $dbFile
  1737. echo -en "GRANT ALL PRIVILEGES ON $nombreDBMo.* TO '$userDBMo'@'localhost' IDENTIFIED BY '$sqlPasswd';\n" >> $dbFile
  1738. mysql -u root --password=$sqlPasswd < $dbFile > /dev/null 2>&1
  1739. control=$?
  1740. rm -f $dbFile 2>/dev/null
  1741. comprobarError $? 603
  1742. comprobarError $control 603
  1743. unset control dbFile dbDir
  1744. }
  1745. configurarSELinux() {
  1746. # Configurar SELinux
  1747. which semanage >/dev/null 2>&1
  1748. if [ $? -ne 0 ];then
  1749. case $ID in
  1750. centos)
  1751. result=$(yum -y install policycoreutils-python 2>&1)
  1752. comprobarError $? 605 $result
  1753. semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/moodledata(/.*)?" > /dev/null 2>&1
  1754. comprobarError $? 604
  1755. restorecon -R /var/www/ > /dev/null 2>&1
  1756. comprobarError $? 604
  1757. ;;
  1758. fedora)
  1759. result=$(yum -y install policycoreutils-python-utils 2>&1)
  1760. comprobarError $? 605 $result
  1761. ;;
  1762. *)
  1763. comprobarError 1 604
  1764. ;;
  1765. esac
  1766. fi
  1767. }
  1768. instalarPHPInfo() {
  1769. # Instalar fichero info.php y su VirtualHost
  1770. if [ ! -f /etc/$webServerName/sites-enabled/phpinfo.conf ];then
  1771. infoFile="./var/www/info.php"
  1772. if [ ! -f $infoFile ];then
  1773. comprobarError 1 4 "$infoFile"
  1774. fi
  1775. mkdir /var/www/localhost 2>/dev/null
  1776. comprobarError $? 106
  1777. cp -f $infoFile /var/www/localhost/ 2>/dev/null
  1778. comprobarError $? 106
  1779. chown -R $webServerUser:$webServerGroup /var/www/localhost 2>/dev/null
  1780. comprobarError $? 106
  1781. unset infoFile
  1782. # Instalar VirtualHost
  1783. if [ $apacheOn = true ] && [ $sslOn = false ];then
  1784. virtualHost="./etc/apache2/sites-available/phpinfo.conf"
  1785. elif [ $nginxOn = true ] && [ $sslOn = false ];then
  1786. virtualHost="./etc/nginx/sites-available/phpinfo.conf"
  1787. elif [ $apacheOn = true ] && [ $sslOn = true ];then
  1788. virtualHost="./etc/apache2/sites-available/phpinfo-ssl.conf"
  1789. elif [ $nginxOn = true ] && [ $sslOn = true ];then
  1790. virtualHost="./etc/nginx/sites-available/phpinfo-ssl.conf"
  1791. else
  1792. comprobarError 1 106
  1793. fi
  1794. instalarVirtualHost "/var/www/localhost" $virtualHost "localhost"
  1795. unset virtualHost
  1796. return 0
  1797. else
  1798. return 1
  1799. fi
  1800. }
  1801. configurarBackups() {
  1802. # Instala backup-server y lo configura
  1803. letsEncryptCode=""
  1804. if [ ! -d "./var" ];then
  1805. comprobarError 1 5 "./var"
  1806. fi
  1807. which backup-server > /dev/null 2>&1
  1808. if [ $? -ne 0 ];then
  1809. # Descargamos backup-server
  1810. curl "https://code.castanedo.es/guzman/backup-server/archive/2.1.tar.gz" 2>/dev/null | tar -xz -C "./var/"
  1811. if [ $? -ne 0 ];then
  1812. comprobarError 1 701
  1813. return 1
  1814. fi
  1815. # Copiamos backup-server y logrotate
  1816. cp -f ./var/backup-server/backup-server /usr/bin/backup-server
  1817. if [ $? -ne 0 ];then
  1818. comprobarError 1 701
  1819. return 1
  1820. fi
  1821. cp -f ./var/backup-server/scripts/logrotate.d/backup-server /etc/logrotate.d/backup-server
  1822. if [ $? -ne 0 ];then
  1823. comprobarError 1 701
  1824. return 1
  1825. fi
  1826. # Creamos carpeta de backups
  1827. if [ ! -d /var/backup ];then
  1828. mkdir /var/backup 2>/dev/null
  1829. comprobarError $? 5 "/var/backup"
  1830. fi
  1831. # Configuramos cron.d
  1832. if [ $letsEncryptOn = false ];then
  1833. letsEncryptCode="--no-letsencrypt "
  1834. fi
  1835. echo -en "# /etc/cron.d/backup-server: crontab for $hostname\n\n" > /etc/cron.d/backup-server
  1836. echo -en "# Backup every day at 4:00 am\n" >> /etc/cron.d/backup-server
  1837. echo -en "0 4 * * * root [ -x /usr/bin/backup-server ] && /usr/bin/backup-server --xz -u $webServerUser -g $webServerGroup --no-encryption --sql-user root --sql-pass $sqlPasswd --nginx-dir /etc/$webServerName $letsEncryptCode--no-mail --no-gogs --no-postfix --no-dovecot --no-opendkim --no-spf --no-opendmarc --no-amavis --no-spamassassin /var/backup >> /var/log/backup-server.log 2>&1" >> /etc/cron.d/backup-server
  1838. comprobarError $? 701
  1839. fi
  1840. unset letsEncryptCode
  1841. }
  1842. # Comprobación del sistema e inicialización
  1843. horaInicial=$(date +"%s")
  1844. inicializarVariables
  1845. comprobarRoot
  1846. OSInfo
  1847. comprobarDependencias
  1848. # Bienvenida
  1849. mostrarBienvenida
  1850. # Selección de componentes (express vs avanzada)
  1851. mostrarExpress
  1852. # Instalación Servidor Web
  1853. if [ $apacheOn = true ]; then
  1854. echo -en "Instalando Servidor Web Apache..." | tee -a $logFile
  1855. instalarApache
  1856. echo -en " OK.\n" | tee -a $logFile
  1857. elif [ $nginxOn = true ]; then
  1858. echo -en "Instalando Servidor Web Nginx..." | tee -a $logFile
  1859. instalarNginx
  1860. echo -en " OK.\n" | tee -a $logFile
  1861. fi
  1862. # Instalación Base de Datos
  1863. if [ $mySQLOn = true ]; then
  1864. echo -en "Instalando Base de Datos MySQL..." | tee -a $logFile
  1865. instalarMySQL
  1866. echo -en " OK.\n" | tee -a $logFile
  1867. elif [ $mariaDBOn = true ]; then
  1868. echo -en "Instalando Base de Datos MariaDB..." | tee -a $logFile
  1869. instalarMariaDB
  1870. echo -en " OK.\n" | tee -a $logFile
  1871. fi
  1872. # Instalación PHP-7
  1873. if [ $phpOn = true ]; then
  1874. echo -en "Instalando PHP-7..." | tee -a $logFile
  1875. instalarPHP
  1876. echo -en " OK.\n" | tee -a $logFile
  1877. fi
  1878. # Instalación Let's Encrypt
  1879. if [ $letsEncryptOn = true ];then
  1880. echo -en "Instalando Let's Encrypt..." | tee -a $logfile
  1881. instalarLetsEncrypt
  1882. echo -en " OK.\n" | tee -a $logFile
  1883. fi
  1884. # Configuración Web Server
  1885. if [ $apacheOn = true ];then
  1886. echo -en "Configurando Servidor Web Apache..." | tee -a $logFile
  1887. configurarApache
  1888. echo -en " OK.\n" | tee -a $logFile
  1889. elif [ $nginxOn = true ]; then
  1890. echo -en "Configurando Servidor Web Nginx..." | tee -a $logFile
  1891. configurarNginx
  1892. echo -en " OK.\n" | tee -a $logFile
  1893. fi
  1894. # Configuración Database (Configurar Ficheros, Arrancar Base de Datos, establecer contraseña y configuración segura)
  1895. if [ $mySQLOn = true ] || [ $mariaDBOn = true ];then
  1896. echo -en "Configurando Base de Datos..." | tee -a $logFile
  1897. configurarSQL
  1898. echo -en " OK.\n" | tee -a $logFile
  1899. habilitarServicio "$sqlServerName"
  1900. echo -en "Estableciendo contraseña de la Base de Datos..." | tee -a $logFile
  1901. establecerSQLPasswd
  1902. echo -en " OK.\n" | tee -a $logFile
  1903. fi
  1904. # Configuración PHP (cgi.fix_pathinfo=0 y Configurar máximo de subida de archivos)
  1905. if [ $phpOn = true ];then
  1906. echo -en "Configurando PHP-7..." | tee -a $logFile
  1907. configurarPHP
  1908. echo -en " OK.\n" | tee -a $logFile
  1909. fi
  1910. # Configuración SSL/TLS
  1911. # Generar certificados auto-firmados
  1912. if [ $sslOn = true ];then
  1913. echo -en "Generando claves de intercambio Diffie-Hellman (puede llevar un largo tiempo)..." | tee -a $logFile
  1914. generarDHParam
  1915. echo -en " OK.\n" | tee -a $logFile
  1916. echo -en "Generando Certificados Auto-Firmados..." | tee -a $logFile
  1917. if [ $mediaWikiOn = true ];then
  1918. generarCertAutofirmado $dominioMediaWiki
  1919. fi
  1920. if [ $moodleOn = true ];then
  1921. generarCertAutofirmado $dominioMoodle
  1922. fi
  1923. if [ $infoPHPOn = true ];then
  1924. generarCertAutofirmado "localhost"
  1925. fi
  1926. echo -en " OK.\n" | tee -a $logFile
  1927. fi
  1928. # Arrancar y habilitar todos los servicios (SystemD, Service o SystemV)
  1929. echo -en "Habilitando todos los servicios..." | tee -a $logFile
  1930. if [ $apacheOn = true ] || [ $nginxOn = true ];then
  1931. habilitarServicio $webServerName
  1932. fi
  1933. if [ $nginxOn = true ] && [ $phpOn = true ];then
  1934. habilitarServicio $phpFPMName
  1935. fi
  1936. echo -en " OK.\n" | tee -a $logFile
  1937. # Instalar MediaWiki
  1938. if [ $mediaWikiOn = true ]; then
  1939. # Descargar MediaWiki
  1940. echo -en "Descargando MediaWiki-1.31.0..." | tee -a $logFile
  1941. descargarMediaWiki
  1942. echo -en " OK.\n" | tee -a $logFile
  1943. # Configurar MediWiki
  1944. echo -en "Configurando MediaWiki..." | tee -a $logFile
  1945. configurarMediaWiki
  1946. echo -en " OK.\n" | tee -a $logFile
  1947. fi
  1948. # Instalar Moodle
  1949. if [ $moodleOn = true ]; then
  1950. # Descargar Moodle
  1951. echo -en "Descargando Moodle-3.5.1..." | tee -a $logFile
  1952. descargarMoodle
  1953. echo -en " OK.\n" | tee -a $logFile
  1954. # Configuración Moodle
  1955. echo -en "Configurando Moodle (puede llevar un tiempo largo)..." | tee -a $logFile
  1956. configurarMoodle
  1957. echo -en " OK.\n" | tee -a $logFile
  1958. fi
  1959. # Instalar info.php
  1960. if [ $infoPHPOn = true ];then
  1961. echo -en "Configurando 'info.php'..." | tee -a $logFile
  1962. instalarPHPInfo
  1963. if [ $? -eq 0 ];then
  1964. echo -en " OK.\n" | tee -a $logFile
  1965. else
  1966. echo -en " Ya se encuentra configurado.\n" | tee -a $logFile
  1967. fi
  1968. fi
  1969. # Añadir reglas del cortafuegos
  1970. echo -en "Configurando Cortafuegos..." | tee -a $logFile
  1971. configurarCortafuegos
  1972. echo -en " OK.\n" | tee -a $logFile
  1973. # Generar certificados Let's Encrypt
  1974. if [ $letsEncryptOn = true ];then
  1975. echo -en "Generando Certificados Válidos Let's Encrypt..." | tee -a $logFile
  1976. generarCertLetsEncrypt
  1977. echo -en " OK.\n" | tee -a $logFile
  1978. fi
  1979. # Configurar Backup
  1980. if [ $backupOn = true ]; then
  1981. echo -en "Configurando Backup Automático..." | tee -a $logFile
  1982. configurarBackups
  1983. echo -en " OK.\n" | tee -a $logFile
  1984. fi
  1985. # Finalizamos
  1986. horaFinal=$(date +"%s")
  1987. echo -en "\n--------------------------------------------------\n" | tee -a $logFile
  1988. echo -en "\t¡INSTALACION FINALIZADA CON EXITO! \n" | tee -a $logFile
  1989. echo -en "\tDuración: "$((horaFinal-horaInicial))" seg." | tee -a $logFile
  1990. echo -en "\n--------------------------------------------------\n" | tee -a $logFile