install 67 KB

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