install 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. #!/bin/bash
  2. #################################################################
  3. # auto-mediawiki #
  4. # Instala un servidor LAMP (Linux+Apache+MySQL+PHP) e instala #
  5. # MediaWiki y lo configura. #
  6. # #
  7. # Guzman Castanedo Villalba (guzman@castanedo.es) Junio 2018 #
  8. # GPLv3 (https://www.gnu.org/licenses/gpl.html) #
  9. #################################################################
  10. comprobarError() {
  11. # comprobarError exitNum codeNum extraInfo
  12. # 1xx: Servidor Web
  13. # 2xx: Base de Datos
  14. # 3xx: PHP
  15. # 4xx: SSL/TLS
  16. # 5xx: MediaWiki
  17. # 6xx: Moodle
  18. # 7xx: Actualizaciones automáticas
  19. # 8xx: Dependencias
  20. # 9xx: Servicios & Firewall
  21. type="ERROR"
  22. exitNum=$1
  23. shift
  24. codeNum=$1
  25. shift
  26. extraInfo=$@
  27. case $codeNum in
  28. 1)
  29. error="$type $codeNum:\tInstalación interrumpida por el usuario.\n"
  30. ;;
  31. 2)
  32. error="$type $codeNum:\tError interno (selección express).\n"
  33. ;;
  34. 3)
  35. error="$type $codeNum:\tError interno (selección avanzada).\n"
  36. ;;
  37. 4)
  38. error="\n$type $codeNum:\tEl archivo '"$(realpath $extraInfo)"' no existe.\n"
  39. ;;
  40. 5)
  41. error="\n$type $codeNum:\tEl directorio '"$(realpath $extraInfo)"' no existe.\n"
  42. ;;
  43. 6)
  44. error="$type $codeNum:\tError interno (selección SSL/TLS).\n"
  45. ;;
  46. 100)
  47. error="$type $codeNum:\tError interno (selección del servidor web).\n"
  48. ;;
  49. 101)
  50. error="\n$type $codeNum:\tError al instalar Apache2.\nDetalles:\n$extraInfo\n"
  51. ;;
  52. 102)
  53. error="\n$type $codeNum:\tError al instalar Nginx.\nDetalles:\n$extraInfo\n"
  54. ;;
  55. 103)
  56. error="\n$type $codeNum:\tError al nstalar repositorio 'epel-release'.\nDetalles:\n$extraInfo\n"
  57. ;;
  58. 104)
  59. error="\n$type $codeNum:\tError interno (instalación Apache2).\n"
  60. ;;
  61. 105)
  62. error="\n$type $codeNum:\tError interno (instalación Nginx).\n"
  63. ;;
  64. 106)
  65. type="WARNING"
  66. error="\n$type $codeNum:\tImposible instalar 'info.php'.\n"
  67. ;;
  68. 107)
  69. error="\n$type $codeNum:\tError al configurar Nginx.\n"
  70. ;;
  71. 108)
  72. error="\n$type $codeNum:\tError al configurar Apache2.\n"
  73. ;;
  74. 200)
  75. error="$type $codeNum:\tError interno (selección de base de datos).\n"
  76. ;;
  77. 201)
  78. error="\n$type $codeNum:\tError al instalar MySQL.\nDetalles:\n$extraInfo\n"
  79. ;;
  80. 202)
  81. error="\n$type $codeNum:\tError al instalar MariaDB.\nDetalles:\n$extraInfo\n"
  82. ;;
  83. 203)
  84. error="\n$type $codeNum:\tError interno (instalación MySQL).\n"
  85. ;;
  86. 204)
  87. error="\n$type $codeNum:\tError interno (instalación MariaDB).\n"
  88. ;;
  89. 205)
  90. error="\n$type $codeNum:\t$DIST no incluye MySQL en sus repositorios.\n"
  91. ;;
  92. 206)
  93. type="WARNING"
  94. error="\n$type $codeNum:\tUsuario 'root' ya dispone de contraseña."
  95. ;;
  96. 207)
  97. error="\n$type $codeNum:\tImposible cambiar la contraseña de 'root'."
  98. ;;
  99. 208)
  100. error="\n$type $codeNum:\tImposible desactivar acceso 'root' desde el exterior."
  101. ;;
  102. 209)
  103. error="\n$type $codeNum:\tImposible eliminar usuarios anónimos."
  104. ;;
  105. 210)
  106. type="WARNING"
  107. error="\n$type $codeNum:\tImposible eliminar bases de datos de pruebas."
  108. ;;
  109. 211)
  110. type="WARNING"
  111. error="\n$type $codeNum:\tImposible eliminar los permisos de las bases de datos de pruebas."
  112. ;;
  113. 212)
  114. error="\n$type $codeNum:\tImposible recargar base de datos."
  115. ;;
  116. 300)
  117. error="\n$type $codeNum:\tError interno (instalación PHP-7.2).\n"
  118. ;;
  119. 301)
  120. error="\n$type $codeNum:\tError interno (Web Server no seleccionado).\n"
  121. ;;
  122. 302)
  123. error="\n$type $codeNum:\tError al instalar PHP-7.2.\nDetalles:\n$extraInfo\n"
  124. ;;
  125. 303)
  126. error="\n$type $codeNum:\tError al instalar Repositorio Remi.\nDetalles:\n$extraInfo\n"
  127. ;;
  128. 304)
  129. error="\n$type $codeNum:\tSistema Operativo no compatible con PHP-7.2 ($OS $DIST $REV).\n"
  130. ;;
  131. 305)
  132. error="\n$type $codeNum:\tImposible configurar PHP-7.2.\n"
  133. ;;
  134. 501)
  135. error="\n$type $codeNum:\tImposible descargar MediaWiki-1.31.0.\n"
  136. ;;
  137. 502)
  138. error="\n$type $codeNum:\tImposible configurar MediaWiki-1.31.0.\n"
  139. ;;
  140. 503)
  141. error="\n$type $codeNum:\tImposible configurar Base de Datos para MediaWiki-1.31.0.\n"
  142. ;;
  143. 601)
  144. error="\n$type $codeNum:\tImposible descargar Moodle-3.5.1.\n"
  145. ;;
  146. 800)
  147. error="$type $codeNum:\tEs necesario ser root ('sudo $0').\n"
  148. ;;
  149. 801)
  150. error="$type $codeNum:\t'whiptail' no instalado.\n"
  151. ;;
  152. 802)
  153. error="$type $codeNum:\t'hostnamectl' no instalado.\n"
  154. ;;
  155. 803)
  156. error="$type $codeNum:\t'apt-get' no instalado.\n"
  157. ;;
  158. 804)
  159. error="$type $codeNum:\t'yum' no instalado.\n"
  160. ;;
  161. 805)
  162. error="\n$type $codeNum:\tImposible actualizar repositorio\nDetalles:\n$extraInfo.\n"
  163. ;;
  164. 806)
  165. error="$type $codeNum:\t'ufw' no instalado.\n"
  166. ;;
  167. 807)
  168. error="$type $codeNum:\t'firewall-cmd' no instalado.\n"
  169. ;;
  170. 808)
  171. error="$type $codeNum:\t'tput' no instalado.\nDetalles:\n$extraInfo\n"
  172. ;;
  173. 809)
  174. error="$type $codeNum:\t'sed' no instalado.\nDetalles:\n$extraInfo\n"
  175. ;;
  176. 810)
  177. error="$type $codeNum:\t'curl' no instalado.\nDetalles:\n$extraInfo\n"
  178. ;;
  179. 811)
  180. error="$type $codeNum:\t'tar' no instalado.\nDetalles:\n$extraInfo\n"
  181. ;;
  182. 812)
  183. error="$type $codeNum:\t'gzip' no instalado.\nDetalles:\n$extraInfo\n"
  184. ;;
  185. 813)
  186. error="$type $codeNum:\t'bzip2' no instalado.\nDetalles:\n$extraInfo\n"
  187. ;;
  188. 814)
  189. error="$type $codeNum:\t'xz' no instalado.\nDetalles:\n$extraInfo\n"
  190. ;;
  191. 815)
  192. error="$type $codeNum:\t'openssl' no instalado.\nDetalles:\n$extraInfo\n"
  193. ;;
  194. 900)
  195. error="$type $codeNum:\tError interno (habilitar servicio).\n"
  196. ;;
  197. 901)
  198. error="$type $codeNum:\tImposible encender servicio '$extraInfo'.\n"
  199. ;;
  200. 902)
  201. error="$type $codeNum:\tImposible habilitar servicio '$extraInfo' durante el arranque.\n"
  202. ;;
  203. 903)
  204. error="$type $codeNum:\tError interno (deshabilitar servicio).\n"
  205. ;;
  206. 904)
  207. error="$type $codeNum:\tImposible parar servicio '$extraInfo'.\n"
  208. ;;
  209. 905)
  210. error="$type $codeNum:\tImposible deshabilitar servicio '$extraInfo' durante el arranque.\n"
  211. ;;
  212. 906)
  213. type="WARNING"
  214. error="\n$type $codeNum:\tImposible añadir regla al cortafuegos ('$extraInfo').\n"
  215. ;;
  216. 907)
  217. type="WARNING"
  218. error="\n$type $codeNum:\tImposible encender cortafuegos.\n"
  219. ;;
  220. 908)
  221. error="\n$type $codeNum:\tError interno (instalar Virtual Host).\n"
  222. ;;
  223. 909)
  224. error="\n$type $codeNum:\tVirtual Host '$extraInfo' no existe.\n"
  225. ;;
  226. 910)
  227. error="\n$type $codeNum:\tImposible copiar Virtual Host '$extraInfo'.\n"
  228. ;;
  229. 911)
  230. error="\n$type $codeNum:\tImposible activar Virtual Host '$extraInfo'.\n"
  231. ;;
  232. 912)
  233. error="\n$type $codeNum:\tError interno (recargar servicio).\n"
  234. ;;
  235. 913)
  236. error="\n$type $codeNum:\tImposible recargar servicio '$extraInfo'.\n"
  237. ;;
  238. *)
  239. error="ERROR 13:\tError interno (comprobación de errores)\n"
  240. exitNum=1
  241. codeNum=13
  242. ;;
  243. esac
  244. if [ $exitNum -ne 0 ];then
  245. echo -en "$error" | tee -a $logFile
  246. if [ $type = "ERROR" ];then
  247. exit $codeNum
  248. fi
  249. fi
  250. unset error exitNum codeNum type
  251. }
  252. OSInfo() {
  253. echo -en "Detectando SO..." >> $logFile
  254. OS=$(uname -s)
  255. if [ $OS = "Linux" ]; then
  256. OS="GNU/Linux"
  257. if [ -f /etc/os-release ]; then
  258. DIST=$(grep ^NAME= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)
  259. REV=$(grep ^VERSION= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)
  260. ID_LIKE=$(grep ^ID_LIKE= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)
  261. VERSION=$(grep ^VERSION_ID= /etc/os-release | cut -d = -f 2 | cut -d '"' -f 2)
  262. for i in $ID_LIKE; do
  263. #echo -en "$i\n"
  264. case $i in
  265. debian|ubuntu)
  266. debianOS=true
  267. break
  268. ;;
  269. rhel|fedora)
  270. rhelOS=true
  271. break
  272. ;;
  273. *)
  274. debianOS=false
  275. rhelOS=false
  276. ;;
  277. esac
  278. done
  279. elif [ -f /etc/debian-version ]; then
  280. # Familia Debian (Debian, Ubuntu, Linux Mint, ...)
  281. DIST="Debian"
  282. REV=""
  283. ID_LIKE="debian"
  284. VERSION=""
  285. debianOS=true
  286. elif [ -f /etc/redhat-release ]; then
  287. # Familia Red-Hat (RHEL, Fedora, CentOS, ...)
  288. DIST="Red-Hat"
  289. REV=""
  290. ID_LIKE="rhel"
  291. VERSION=""
  292. rhelOS=true
  293. else
  294. # Other Linux (No Soportado)
  295. DIST=""
  296. REV=""
  297. ID_LIKE=""
  298. VERSION=""
  299. fi
  300. else
  301. # UNIX, OS X, ... (No Soportado)
  302. DIST=$OS
  303. REV=""
  304. ID_LIKE=""
  305. VERSION=""
  306. fi
  307. echo -en " $OS $DIST $REV\n" >> $logFile
  308. HDInfo=$(df -h | head -1)"\n"$(df -h | grep ^/dev/sd)"\n"$(df -h | grep ^/dev/mapper)
  309. echo -en "$HDInfo\n" >> $logFile
  310. }
  311. comprobarRoot() {
  312. comprobarError $(id -u) 800
  313. }
  314. comprobarDependencias() {
  315. # Comprobamos whiptail
  316. which whiptail > /dev/null 2>&1
  317. comprobarError $? 801
  318. # Comprobamos hostnamectl
  319. which hostnamectl > /dev/null 2>&1
  320. comprobarError $? 802
  321. if [ $debianOS = true ];then
  322. # Comprobamos apt-get
  323. which apt-get > /dev/null 2>&1
  324. comprobarError $? 803
  325. # Actualizamos base de datos del repositorio
  326. echo -en "Actualizando repositorio APT..." | tee -a $logFile
  327. result=$(apt-get -q -y update 2>&1)
  328. comprobarError $? 805 $result
  329. echo -en " OK.\n" | tee -a $logFile
  330. # Comprobamos Firewall (ufw)
  331. which ufw > /dev/null 2>&1
  332. comprobarError $? 806
  333. # Comprobamos tput
  334. which tput > /dev/null 2>&1
  335. if [ $? -ne 0 ];then
  336. result=$(apt-get -q -y ncurses-bin 2>&1)
  337. comprobarError $? 808 $result
  338. fi
  339. # Comprobar sed
  340. which sed > /dev/null 2>&1
  341. if [ $? -ne 0 ];then
  342. result=$(apt-get -q -y sed 2>&1)
  343. comprobarError $? 809 $result
  344. fi
  345. # Comprobar curl
  346. which curl > /dev/null 2>&1
  347. if [ $? -ne 0 ];then
  348. result=$(apt-get -q -y curl 2>&1)
  349. comprobarError $? 810 $result
  350. fi
  351. # Comprobamos tar, gzip, bzip2 y xz
  352. which tar > /dev/null 2>&1
  353. if [ $? -ne 0 ];then
  354. result=$(apt-get -q -y install tar 2>&1)
  355. comprobarError $? 811 $result
  356. fi
  357. which gzip > /dev/null 2>&1
  358. if [ $? -ne 0 ];then
  359. result=$(apt-get -q -y install gzip 2>&1)
  360. comprobarError $? 812 $result
  361. fi
  362. which bzip2 > /dev/null 2>&1
  363. if [ $? -ne 0 ];then
  364. result=$(apt-get -q -y install bzip2 2>&1)
  365. comprobarError $? 813 $result
  366. fi
  367. which xz > /dev/null 2>&1
  368. if [ $? -ne 0 ];then
  369. result=$(apt-get -q -y install xz-utils 2>&1)
  370. comprobarError $? 814 $result
  371. fi
  372. which openssl > /dev/null 2>&1
  373. if [ $? -ne 0 ];then
  374. result=$(apt-get -q -y install openssl 2>&1)
  375. comprobarError $? 815 $result
  376. fi
  377. fi
  378. if [ $rhelOS = true ]; then
  379. # Comprobamos yum
  380. which yum > /dev/null 2>&1
  381. comprobarError $? 804
  382. # Actualizamos base de datos del repositorio
  383. echo -en "Actualizando repositorio YUM..." | tee -a $logFile
  384. result=$(yum -y makecache 2>&1)
  385. comprobarError $? 805 $result
  386. echo -en " OK.\n" | tee -a $logFile
  387. # Comprobamos Firewall (firewall-cmd)
  388. which firewall-cmd > /dev/null 2>&1
  389. comprobarError $? 807
  390. # Comprobamos tput
  391. which tput > /dev/null 2>&1
  392. if [ $? -ne 0 ];then
  393. result=$(yum -y ncurses 2>&1)
  394. comprobarError $? 808 $result
  395. fi
  396. # Comprobar sed
  397. which sed > /dev/null 2>&1
  398. if [ $? -ne 0 ];then
  399. result=$(yum -y sed 2>&1)
  400. comprobarError $? 809 $result
  401. fi
  402. # Comprobar curl
  403. which curl > /dev/null 2>&1
  404. if [ $? -ne 0 ];then
  405. result=$(yum -y curl 2>&1)
  406. comprobarError $? 810 $result
  407. fi
  408. # Comprobamos tar, gzip, bzip2 y xz
  409. which tar > /dev/null 2>&1
  410. if [ $? -ne 0 ];then
  411. result=$(yum -y install tar 2>&1)
  412. comprobarError $? 811 $result
  413. fi
  414. which gzip > /dev/null 2>&1
  415. if [ $? -ne 0 ];then
  416. result=$(yum -y install gzip 2>&1)
  417. comprobarError $? 812 $result
  418. fi
  419. which bzip2 > /dev/null 2>&1
  420. if [ $? -ne 0 ];then
  421. result=$(yum -y install bzip2 2>&1)
  422. comprobarError $? 813 $result
  423. fi
  424. which xz > /dev/null 2>&1
  425. if [ $? -ne 0 ];then
  426. result=$(yum -y install xz 2>&1)
  427. comprobarError $? 814 $result
  428. fi
  429. which openssl > /dev/null 2>&1
  430. if [ $? -ne 0 ];then
  431. result=$(yum -y install openssl 2>&1)
  432. comprobarError $? 815 $result
  433. fi
  434. fi
  435. }
  436. inicializarVariables() {
  437. debianOS=false
  438. rhelOS=false
  439. apacheOn=false
  440. nginxOn=false
  441. mySQLOn=false
  442. mariaDBOn=false
  443. phpOn=false
  444. sslOn=false
  445. letsEncryptOn=false
  446. mediaWikiOn=false
  447. moodleOn=false
  448. infoPHPOn=false
  449. actualizacionesOn=false
  450. progreso=0
  451. progresoTotal=0
  452. hostname=""
  453. logFile="./."$(basename $0)".log"
  454. maxUpload="100M"
  455. webServerName=""
  456. webServerUser=""
  457. webServerGroup=""
  458. sqlServerName=""
  459. phpFPMName=""
  460. dominioMediaWiki=""
  461. }
  462. instalacionExpress() {
  463. apacheOn=true
  464. mariaDBOn=true
  465. phpOn=true
  466. sslOn=true
  467. letsEncryptOn=true
  468. mediaWikiOn=true
  469. moodleOn=true
  470. actualizacionesOn=true
  471. progresoTotal=15
  472. # Preguntas mínimas
  473. establecerFQDN
  474. leerSQLPasswd
  475. }
  476. mostrarBienvenida() {
  477. ANCHO=$(tput cols)
  478. ALTO=$(tput lines)
  479. if [ $debianOS = false ] && [ $rhelOS = false ]; then
  480. whiptail --title "ERROR S.O. NO SOPORTADO" --msgbox "Este script automatiza la creación de una web MediaWiki SOLO para distribuciones Linux de la familia Debian (Ubuntu, Linux Mint, ...) y de la familia Red-Hat (CentOS, Fedora, ...).\n\nInformación del sistema:\nOS: $OS $DIST $REV\n$HDInfo" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Salir"
  481. exit 1
  482. fi
  483. whiptail --title "INSTALACION MEDIAWIKI" --yesno "Este script automatiza completamente la instalación de una wiki.\nPara ello instala un servidor LAMP, el software MediaWiki y lo configura todo.\n\nInformación del sistema:\nOS: $OS $DIST $REV\n$HDInfo" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --yes-button "Continuar" --no-button "Salir"
  484. comprobarError $? 1
  485. }
  486. mostrarExpress() {
  487. express=$(whiptail --title "INSTALACION EXPRESS" --radiolist "<ESPACIO>: seleccionar <TAB>: cambiar <FLECHAS>: moverse\n\nLa instalación express instala Apache2, MariaDB, PHP-7, Let's Encrypt, MediaWiki, Moodle y actualizaciones automáticas.\nSeleccione el tipo de instalación que desee:" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) 2 \
  488. "Express" "Instalación rápida" ON \
  489. "Avanzada" "Permite escoger todas las opciones disponibles" OFF \
  490. --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  491. comprobarError $? 1
  492. case $express in
  493. Express)
  494. instalacionExpress
  495. # DECIDIR QUÉ OPCIONES SON LAS MÍNIMAS
  496. ;;
  497. Avanzada)
  498. mostrarAvanzada
  499. ;;
  500. *)
  501. comprobarError 1 2
  502. ;;
  503. esac
  504. }
  505. mostrarAvanzada() {
  506. 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 \
  507. "LAMP" "GNU/Linux + Apache + MySQL/MariaDB + PHP-7" ON \
  508. "LEMP" "GNU/Linux + Nginx + MySQL/MariaDB + PHP-7" OFF \
  509. --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  510. comprobarError $? 1
  511. for i in $webServer; do
  512. case $i in
  513. LAMP)
  514. apacheOn=true
  515. ;;
  516. LEMP)
  517. nginxOn=true
  518. ;;
  519. *)
  520. comprobarError 1 100
  521. ;;
  522. esac
  523. done
  524. # Opciones comunes
  525. establecerFQDN
  526. mostrarDatabase
  527. phpOn=true
  528. establecerMaxUpload
  529. progresoTotal=$((progresoTotal + 6))
  530. mostrarComponentes
  531. }
  532. mostrarComponentes() {
  533. 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 \
  534. "SSL/TLS" "Instalar certificados para activar HTTPS" ON \
  535. "MediaWiki" "Instalar wiki con MediaWiki" ON \
  536. "Moodle" "Instalar campus virtual con Moodle" ON \
  537. "InfoPHP" "Instalar info.php (sólo para pruebas)" OFF \
  538. "Actualizaciones" "Programar actualizaciones automáticas" ON \
  539. --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  540. comprobarError $? 1
  541. # Mejora: autodetección de componentes ya instalados
  542. for i in $componentes; do
  543. case $i in
  544. \"SSL/TLS\")
  545. # 2 Opciones: Let's Encrypt o Autofirmado
  546. mostrarSSL
  547. progresoTotal=$((progresoTotal + 2))
  548. ;;
  549. \"MediaWiki\")
  550. mediaWikiOn=true
  551. mostrarMediaWiki
  552. progresoTotal=$((progresoTotal + 2))
  553. ;;
  554. \"Moodle\")
  555. moodleOn=true
  556. progresoTotal=$((progresoTotal + 2))
  557. ;;
  558. \"InfoPHP\")
  559. infoPHPOn=true
  560. progresoTotal=$((progresoTotal + 2))
  561. ;;
  562. \"Actualizaciones\")
  563. actualizacionesOn=true
  564. progresoTotal=$((progresoTotal + 1))
  565. ;;
  566. *)
  567. comprobarError 1 3
  568. ;;
  569. esac
  570. done
  571. progresoTotal=$((progresoTotal + 2))
  572. }
  573. mostrarSSL() {
  574. 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 \
  575. "LetsEncrypt" "Compatible con todos los navegadores" ON \
  576. "Auto-Firmado" "Certificados auto-firmados (sólo para pruebas)" OFF \
  577. --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  578. comprobarError $? 1
  579. for i in $ssl; do
  580. case $i in
  581. LetsEncrypt)
  582. sslOn=true
  583. letsEcnryptOn=true
  584. ;;
  585. Auto-Firmados)
  586. sslOn=true
  587. ;;
  588. *)
  589. comprobarError 1 6
  590. ;;
  591. esac
  592. done
  593. }
  594. mostrarMediaWiki(){
  595. # Introducir dominio
  596. while [ -z $dominioMediaWiki ]; do
  597. dominioMediaWiki=$(whiptail --title "CONFIGURACION MEDIAWIKI" --inputbox "Introduzca el dominio/subdominio para MediaWiki.\nTenga en cuenta que debes apuntar este a la dirección IP de esta máquina mediante un registro DNS tipo CNAME." $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) wiki.$hostname --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  598. comprobarError $? 1
  599. # Mejora: comprobar si el dominio ya está en uso
  600. done
  601. }
  602. establecerFQDN() {
  603. while [ -z $hostname ]; do
  604. hostname=$(whiptail --title "FQDN" --inputbox "El nombre de dominio principal (FQDN) de este servidor es:\n"$(hostname)"\n\nQuieres cambiarlo por otro?" $((ALTO * 9 / 10)) $((ANCHO * 9 / 10)) --ok-button "Cambiar" --cancel-button "No Cambiar" 3>&1 1>&2 2>&3)
  605. if [ $? -eq 0 ] && [ ! -z $hostname ]; then
  606. hostnamectl set-hostname $hostname
  607. else
  608. hostname=$(hostname)
  609. fi
  610. done
  611. echo -en "Hostname: $hostname\n" >> $logFile
  612. }
  613. instalarApache() {
  614. if [ $debianOS = true ];then
  615. webServerName="apache2"
  616. result=$(apt-get -q -y install apache2 2>&1)
  617. comprobarError $? 101 $result
  618. elif [ $rhelOS = true ]; then
  619. webServerName="httpd"
  620. result=$(yum -y install httpd 2>&1)
  621. comprobarError $? 101 $result
  622. else
  623. comprobarError 1 104
  624. fi
  625. deshabilitarServicio $webServerName
  626. }
  627. configurarApache() {
  628. if [ $debianOS = true ];then
  629. apacheConfFile="./etc/$webServerName-debian/apache2.conf"
  630. if [ ! -f $apacheConfFile ];then
  631. comprobarError 1 4 $apacheConfFile
  632. fi
  633. cp -f $apacheConfFile /etc/$webServerName/apache2.conf 2>/dev/null
  634. comprobarError $? 108
  635. webServerUser=$(grep ^User /etc/$webServerName/apache2.conf | cut -d ' ' -f 2)
  636. webServerGroup=$(grep ^Group /etc/$webServerName/apache2.conf | cut -d ' ' -f 2)
  637. elif [ $rhelOS = true ];then
  638. apacheConfFile="./etc/$webServerName-rhel/conf/httpd.conf"
  639. welcomeConfFile="/etc/$webServerName/conf.d/welcome.conf"
  640. if [ ! -f $apacheConfFile ];then
  641. comprobarError 1 4 $apacheConfFile
  642. fi
  643. cp -f $apacheConfFile /etc/$webServerName/conf/ 2>/dev/null
  644. comprobarError $? 108
  645. if [ -f $welcomeConfFile ];then
  646. sed -i -e 's/^/#/' $welcomeConfFile
  647. comprobarError $? 108
  648. fi
  649. webServerUser=$(grep ^User /etc/$webServerName/conf/httpd.conf | cut -d ' ' -f 2)
  650. webServerGroup=$(grep ^Group /etc/$webServerName/conf/httpd.conf | cut -d ' ' -f 2)
  651. unset apacheConfFile welcomeConfFile
  652. fi
  653. if [ ! -d "/etc/$webServerName/sites-available" ];then
  654. mkdir "/etc/$webServerName/sites-available" 2>/dev/null
  655. comprobarError $? 5 "/etc/$webServerName/sites-available"
  656. fi
  657. if [ ! -d "/etc/$webServerName/sites-enabled" ]; then
  658. mkdir "/etc/$webServerName/sites-enabled" 2>/dev/null
  659. comprobarError $? 5 "$webServerRoot/sites-enabled"
  660. fi
  661. }
  662. instalarNginx() {
  663. webServerName="nginx"
  664. if [ $debianOS = true ];then
  665. result=$(apt-get -q -y install nginx 2>&1)
  666. comprobarError $? 102 $result
  667. elif [ $rhelOS = true ]; then
  668. # Hay que instalar primero otro repositorio
  669. result=$(yum -y install epel-release)
  670. comprobarError $? 103 $result
  671. result=$(yum -y install nginx 2>&1)
  672. comprobarError $? 102 $result
  673. else
  674. comprobarError 1 105
  675. fi
  676. deshabilitarServicio $webServerName
  677. }
  678. configurarNginx() {
  679. # Configuramos Nginx
  680. rm -Rf /etc/$webServerName/* 2>/dev/null
  681. comprobarError $? 107
  682. if [ $debianOS = true ];then
  683. nginxConfFile="./etc/$webServerName-debian"
  684. elif [ $rhelOS = true ];then
  685. nginxConfFile="./etc/$webServerName-rhel"
  686. fi
  687. if [ -d "$nginxConfFile" ];then
  688. cp -Rf $nginxConfFile/* /etc/$webServerName/ 2>/dev/null
  689. comprobarError $? 107
  690. else
  691. comprobarError 1 5 "$nginxConfFile"
  692. fi
  693. if [ ! -d "/etc/$webServerName/sites-available" ];then
  694. mkdir "/etc/$webServerName/sites-available" 2>/dev/null
  695. comprobarError $? 5 "/etc/$webServerName/sites-available"
  696. fi
  697. if [ ! -d "/etc/$webServerName/sites-enabled" ]; then
  698. mkdir "/etc/$webServerName/sites-enabled" 2>/dev/null
  699. comprobarError $? 5 "$webServerRoot/sites-enabled"
  700. fi
  701. webServerUser=$(grep ^user /etc/$webServerName/nginx.conf | cut -d ' ' -f 2 | cut -d ';' -f 1)
  702. webServerGroup=$webServerUser
  703. unset nginxConfFile
  704. }
  705. instalarVirtualHost() {
  706. # Configuramos un Virtual Host para Apache o Nginx
  707. # Uso: instalarVirtualHost $virtualHost $virtualHostName
  708. webServerRoot=$(realpath "/etc/$webServerName/")
  709. if [ $# -eq 1 ];then
  710. virtualHost=$(realpath $1)
  711. virtualHostName=$(basename $virtualHost)
  712. elif [ $# -eq 2 ];then
  713. virtualHost=$(realpath $1)
  714. virtualHostName=$2
  715. else
  716. comprobarError 1 908
  717. fi
  718. if [ ! -f $virtualHost ];then
  719. comprobarError 1 909 $virtualHostName
  720. fi
  721. cp -f $virtualHost "$webServerRoot/sites-available/$virtualHostName" >> $logFile 2>&1
  722. comprobarError $? 910
  723. ln -s "$webServerRoot/sites-available/$virtualHostName" "$webServerRoot/sites-enabled/$virtualHostName" >> $logFile 2>&1
  724. comprobarError $? 911 $virtualHostName
  725. recargarServicio $webServerName
  726. unset webServerRoot virtualHost virtualHostName
  727. }
  728. mostrarDatabase() {
  729. if [ $debianOS = true ]; then
  730. 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 \
  731. "MariaDB" "Instalar la base de datos MariaDB (fork de MySQL)" ON \
  732. "MySQL" "Instalar la base de datos MySQL (uso no comercial)" OFF \
  733. --ok-button "Continuar" --cancel-button "Salir" 3>&1 1>&2 2>&3)
  734. comprobarError $? 1
  735. case $database in
  736. MariaDB)
  737. mariaDBOn=true
  738. ;;
  739. MySQL)
  740. mySQLOn=true
  741. ;;
  742. *)
  743. comprobarError 1 200
  744. ;;
  745. esac
  746. elif [ $rhelOS = true ]; then
  747. 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"
  748. comprobarError $? 1
  749. mariaDBOn=true
  750. else
  751. comprobarError 1 200
  752. fi
  753. # Otras opciones (contraseña)
  754. leerSQLPasswd
  755. }
  756. leerSQLPasswd() {
  757. control=false
  758. error=""
  759. # Leemos la contreseña (stdin) y confirmamos
  760. while [ $control = false ]; do
  761. 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)
  762. comprobarError $? 1
  763. 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)
  764. comprobarError $? 1
  765. #echo -en "SQL Password 1: $sqlPasswd\n"
  766. #echo -en "SQL Password 2: $sqlPasswd2\n"
  767. if [ -z $sqlPasswd ] || [ -z $sqlPasswd2 ]; then
  768. error="ERROR: LA CONTRASEÑA NO PUEDE ESTAR VACIA.\n"
  769. elif [ $sqlPasswd != $sqlPasswd2 ];then
  770. error="ERROR: LAS CONTRASEÑAS NO COINCIDEN.\n"
  771. else
  772. control=true
  773. fi
  774. done
  775. #echo -en "SQL Password: $sqlPasswd\n"
  776. unset control error sqlPasswd2
  777. }
  778. establecerSQLPasswd() {
  779. # Establecemos SQL root passwd y securizamos BD (mysql_secure_installation)
  780. # Comprobamos si ya tiene una contraseña asignada
  781. mysql -e "FLUSH PRIVILEGES" >> $logFile 2>&1
  782. if [ $? -eq 0 ];then
  783. # Establecemos contraseña del usuario root (y desactivamos posibles plugins para forzar login por contraseña)
  784. mysql -e "UPDATE mysql.user SET plugin = '', Password = PASSWORD('$sqlPasswd') WHERE User = 'root'" >> $logFile 2>&1
  785. comprobarError $? 207
  786. # Desactivamos acceso root desde el exterior (solo localhost)
  787. mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')" >> $logFile 2>&1
  788. comprobarError $? 208
  789. # Eliminamos todos los usuarios anónimos
  790. mysql -e "DELETE FROM mysql.user WHERE User=''" >> $logFile 2>&1
  791. comprobarError $? 209
  792. # Eliminamos bases de datos 'test'
  793. mysql -e "DROP DATABASE IF EXISTS test" >> $logFile 2>&1
  794. comprobarError $? 210
  795. # Eliminamos privilegios de la base de datos 'test'
  796. mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" >> $logFile 2>&1
  797. comprobarError $? 211
  798. # Aplicamos los cambios
  799. mysql -e "FLUSH PRIVILEGES" >> $logFile 2>&1
  800. comprobarError $? 212
  801. else
  802. comprobarError 1 206
  803. fi
  804. }
  805. instalarMySQL() {
  806. sqlServerName="mysql"
  807. if [ $debianOS = true ];then
  808. result=$(apt-get -q -y install mysql-server mysql-client 2>&1)
  809. comprobarError $? 201 $result
  810. elif [ $rhelOS = true ];then
  811. # MySQL no disponible en RHEL. 2 opciones:
  812. # 1) Instalar un repositorio adicional
  813. # 2) No instalar MySQL en distribuciones RHEL
  814. comprobarError 1 205
  815. else
  816. comprobarError 1 203
  817. fi
  818. }
  819. instalarMariaDB() {
  820. sqlServerName="mariadb"
  821. if [ $debianOS = true ];then
  822. result=$(apt-get -q -y install mariadb-server mariadb-client 2>&1)
  823. comprobarError $? 202 $result
  824. elif [ $rhelOS = true ];then
  825. result=$(yum -y install mariadb-server mariadb 2>&1)
  826. comprobarError $? 202 $result
  827. else
  828. comprobarError 1 204
  829. fi
  830. }
  831. instalarPHP() {
  832. # Instalamos PHP-7 de forma diferente si es junto a Apache o con Nginx
  833. if [ $debianOS = true ];then
  834. if [ $apacheOn = true ]; then
  835. result=$(apt-get -q -y install php libapache2-mod-php php-mysql php-intl php-mbstring php-xml 2>&1)
  836. comprobarError $? 302 $result
  837. elif [ $nginxOn = true ];then
  838. phpFPMName="php7.2-fpm"
  839. result=$(apt-get -q -y install php-fpm php-mysql php-intl php-mbstring php-xml 2>&1)
  840. comprobarError $? 302 $result
  841. deshabilitarServicio $phpFPMName
  842. else
  843. comprobarError 1 301
  844. fi
  845. elif [ $rhelOS = true ];then
  846. # Necesitamos un repositorio adicional para PHP-7 (EPEL-RELEASE)
  847. result=$(yum -y install epel-release yum-utils 2>&1)
  848. comprobarError $? 103 $result
  849. # Necesitamos un repositorio adicional para PHP-7 (REMI-RELEASE)
  850. if [ ! -f /etc/yum.repos.d/remi-php72.repo ];then
  851. if [ $VERSION = "7" ];then
  852. result=$(yum -y install http://remi.mirrors.cu.be/enterprise/remi-release-7.rpm 2>&1)
  853. comprobarError $? 303 $result
  854. elif [ $VERSION = "6" ];then
  855. result=$(yum -y install http://remi.mirrors.cu.be/enterprise/remi-release-6.rpm 2>&1)
  856. comprobarError $? 303 $result
  857. elif [ $VERSION = "28" ];then
  858. result=$(yum -y install http://remi.mirrors.cu.be/fedora/remi-release-28.rpm 2>&1)
  859. comprobarError $? 303 $result
  860. elif [ $VERSION = "27" ];then
  861. result=$(yum -y install http://remi.mirrors.cu.be/fedora/remi-release-27.rpm 2>&1)
  862. comprobarError $? 303 $result
  863. else
  864. comprobarError 1 304
  865. fi
  866. fi
  867. # Activamos repositorio remi-php72
  868. result=$(yum-config-manager --enable remi-php72 2>&1)
  869. comprobarError $? 303 $result
  870. # Instalamos PHP-7.2
  871. if [ $apacheOn = true ];then
  872. result=$(yum -y install php php-mysql php-intl php-mbstring php-mcrypt php-xml 2>&1)
  873. comprobarError $? 302 $result
  874. elif [ $nginxOn = true ];then
  875. phpFPMName="php-fpm"
  876. result=$(yum -y install php php-fpm php-mysql php-intl php-mbstring php-mcrypt php-xml 2>&1)
  877. comprobarError $? 302 $result
  878. deshabilitarServicio $phpFPMName
  879. else
  880. comprobarError 1 301
  881. fi
  882. else
  883. comprobarError 1 300
  884. fi
  885. }
  886. configurarPHP() {
  887. # Configura PHP para segurizarlo y establecer máximo de subida.
  888. if [ $debianOS = true ];then
  889. phpConfFile="./etc/php/php.ini.debian"
  890. phpDest="/etc/php/7.2/cli/php.ini"
  891. elif [ $rhelOS = true ];then
  892. phpConfFile="./etc/php/php.ini.rhel"
  893. phpDest="/etc/php.ini"
  894. fi
  895. if [ ! -f $phpConfFile ];then
  896. comprobarError 1 4 "$phpConfFile"
  897. fi
  898. # Copiamos configuración
  899. cp -f $phpConfFile $phpDest 2>/dev/null
  900. comprobarError $? 305
  901. # Configuramos cgi.fix_pathinfo
  902. sed -i '/cgi.fix_pathinfo=/c\cgi.fix_pathinfo=0' $phpDest
  903. comprobarError $? 305
  904. # Configuramos post_max_size
  905. sed -i '/post_max_size =/c\post_max_size = '$maxUpload $phpDest
  906. comprobarError $? 305
  907. # Configuramos upload_max_filesize
  908. sed -i '/upload_max_filesize =/c\upload_max_filesize = '$maxUpload $phpDest
  909. comprobarError $? 305
  910. # Configurar php-fpm (sólo en RHEL con Nginx)
  911. if [ $rhelOS = true ] && [ $nginxOn = true ];then
  912. # Configurar Socket UNIX
  913. phpConfFile="./etc/php/php-fpm.d/www.conf"
  914. phpDest="/etc/php-fpm.d/www.conf"
  915. if [ ! -f $phpConfFile ];then
  916. comprobarError $? 4 "$phpConfFile"
  917. fi
  918. cp -f $phpConfFile $phpDest 2>/dev/null
  919. comprobarError $? 305
  920. # Configurar Permisos /var/lib/php
  921. chown -R root:$webServerGroup /var/lib/php/
  922. comprobarError $? 305
  923. fi
  924. unset phpConfFile phpDest
  925. }
  926. establecerMaxUpload() {
  927. control=false
  928. error=""
  929. while [ $control = false ];do
  930. 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)
  931. comprobarError $? 1
  932. if [ -z $maxUpload2 ];then
  933. error="ERROR: NO PUEDES DEJAR ESTE PARÁMETRO VACIO.\n"
  934. else
  935. control=true
  936. fi
  937. done
  938. maxUpload=$maxUpload2
  939. unset control maxUpload2
  940. }
  941. instalarPHPInfo() {
  942. # Instalar fichero php
  943. if [ ! -f /etc/$webServerName/sites-enabled/phpinfo.conf ];then
  944. infoFile="./var/www/info.php"
  945. if [ ! -f $infoFile ];then
  946. comprobarError 1 4 "$infoFile"
  947. fi
  948. cp -f $infoFile /var/www/html/ 2>/dev/null
  949. comprobarError $? 106
  950. chown -R $webServerUser:$webServerGroup /var/www/html 2>/dev/null
  951. comprobarError $? 106
  952. unset infoFile
  953. # Instalar VirtualHost
  954. if [ $debianOS = true ];then
  955. virtualHost="./etc/$webServerName-debian/sites-available/phpinfo.conf"
  956. elif [ $rhelOS = true ];then
  957. virtualHost="./etc/$webServerName-rhel/sites-available/phpinfo.conf"
  958. fi
  959. instalarVirtualHost $virtualHost
  960. unset virtualHost
  961. return 0
  962. else
  963. return 1
  964. fi
  965. }
  966. habilitarServicio() {
  967. # Arrancamos y habilitamos el servicio (con SystemD, Upstart o SystemV)
  968. # Intentamos con systemctl (SystemD)
  969. #echo -en "Habilitando Servicio '$1'" | tee -a $logFile
  970. if [ $# -le 0 ];then
  971. comprobarError 1 900
  972. fi
  973. which systemctl > /dev/null 2>&1
  974. if [ $? -eq 0 ]; then
  975. #echo -en " (SystemD)..." | tee -a $logFile
  976. systemctl start $1 > /dev/null 2>&1
  977. comprobarError $? 901 $1
  978. systemctl enable $1 > /dev/null 2>&1
  979. comprobarError $? 902 $1
  980. else
  981. # Intentamos con service (Upstart)
  982. which service > /dev/null 2>&1
  983. if [ $? -eq 0 ]; then
  984. #echo -en " (Upstart)..." | tee -a $logFile
  985. service $1 start > /dev/null 2>&1
  986. comprobarError $? 901 $1
  987. else
  988. # Intentamos con init.d (SystemV)
  989. #echo -en " (SystemV)..." | tee -a $logFile
  990. /etc/init.d/$1 start > /dev/null 2>&1
  991. comprobarError $? 901 $1
  992. fi
  993. # Intentamos habilitar en el arranque (Upstart)
  994. which update-rc.d > /dev/null 2>&1
  995. if [ $? -eq 0 ];then
  996. update-rc.d $1 enable
  997. comprobarError $? 902 $1
  998. else
  999. # Intentamos habilitar en el arranque (SystemV)
  1000. which chkconfig > /dev/null 2>&1
  1001. if [ $? -eq 0 ];then
  1002. chkconfig $1 on
  1003. comprobarError $? 902 $1
  1004. else
  1005. # ¿Qué mas opciones nos quedan?
  1006. comprobarError 1 902 $1
  1007. fi
  1008. fi
  1009. fi
  1010. # Mejora: comprobar si el servicio está parado
  1011. #echo -en " OK.\n" | tee -a $logFile
  1012. }
  1013. deshabilitarServicio() {
  1014. # Paramos y deshabilitamos el servicio (con SystemD, Upstart o SystemV)
  1015. # Intentamos con systemctl (SystemD)
  1016. #echo -en "Deshabilitando Servicio '$1'" | tee -a $logFile
  1017. if [ $# -le 0 ];then
  1018. comprobarError 1 903
  1019. fi
  1020. which systemctl > /dev/null 2>&1
  1021. if [ $? -eq 0 ]; then
  1022. #echo -en " (SystemD)..." | tee -a $logFile
  1023. systemctl stop $1 > /dev/null 2>&1
  1024. comprobarError $? 904 $1
  1025. systemctl disable $1 > /dev/null 2>&1
  1026. comprobarError $? 905 $1
  1027. else
  1028. # Intentamos con service (Upstart)
  1029. which service > /dev/null 2>&1
  1030. if [ $? -eq 0 ]; then
  1031. #echo -en " (Upstart)..." | tee -a $logFile
  1032. service $1 stop > /dev/null 2>&1
  1033. comprobarError $? 904 $1
  1034. else
  1035. # Intentamos con init.d (SystemV)
  1036. #echo -en " (SystemV)..." | tee -a $logFile
  1037. /etc/init.d/$1 stop > /dev/null 2>&1
  1038. comprobarError $? 904 $1
  1039. fi
  1040. # Intentamos habilitar en el arranque (Upstart)
  1041. which update-rc.d > /dev/null 2>&1
  1042. if [ $? -eq 0 ];then
  1043. update-rc.d $1 disable
  1044. comprobarError $? 905 $1
  1045. else
  1046. # Intentamos habilitar en el arranque (SystemV)
  1047. which chkconfig > /dev/null 2>&1
  1048. if [ $? -eq 0 ];then
  1049. chkconfig $1 off
  1050. comprobarError $? 905 $1
  1051. else
  1052. # ¿Qué mas opciones nos quedan?
  1053. comprobarError 1 905 $1
  1054. fi
  1055. fi
  1056. fi
  1057. # Mejora: comprobar si el servicio está funcionando
  1058. #echo -en " OK.\n" | tee -a $logFile
  1059. }
  1060. recargarServicio() {
  1061. # Recargamos el servicio (con SystemD, Upstart o SystemV)
  1062. # Intentamos con systemctl (SystemD)
  1063. #echo -en "Recargando Servicio '$1'" | tee -a $logFile
  1064. if [ $# -le 0 ];then
  1065. comprobarError 1 912
  1066. fi
  1067. which systemctl > /dev/null 2>&1
  1068. if [ $? -eq 0 ]; then
  1069. #echo -en " (SystemD)..." | tee -a $logFile
  1070. systemctl reload $1 > /dev/null 2>&1
  1071. comprobarError $? 913 $1
  1072. else
  1073. # Intentamos con service (Upstart)
  1074. which service > /dev/null 2>&1
  1075. if [ $? -eq 0 ]; then
  1076. #echo -en " (Upstart)..." | tee -a $logFile
  1077. service $1 reload > /dev/null 2>&1
  1078. comprobarError $? 913 $1
  1079. else
  1080. # Intentamos con init.d (SystemV)
  1081. #echo -en " (SystemV)..." | tee -a $logFile
  1082. /etc/init.d/$1 reload > /dev/null 2>&1
  1083. comprobarError $? 913 $1
  1084. fi
  1085. fi
  1086. # Mejora: comprobar si el servicio está funcionando
  1087. #echo -en " OK.\n" | tee -a $logFile
  1088. }
  1089. configurarCortafuegos() {
  1090. # Configuramos cortafuegos (añadir reglas y encender)
  1091. # Comprobamos si estamos usando SSH
  1092. esSSH $PPID
  1093. if [ $debianOS = true ];then
  1094. ufw allow 80/tcp >> $logFile 2>&1
  1095. comprobarError $? 906 "80/tcp"
  1096. if [ $sshControl = true ];then
  1097. ufw allow 22/tcp >> $logFile 2>&1
  1098. comprobarError $? 906 "22/tcp"
  1099. fi
  1100. if [ $sslOn = true ];then
  1101. ufw allow 443/tcp >> $logFile 2>&1
  1102. comprobarError $? 906 "443/tcp"
  1103. fi
  1104. ufw --force enable >> $logFile 2>&1
  1105. comprobarError $? 907
  1106. elif [ $rhelOS = true ];then
  1107. firewall-cmd --add-port=80/tcp >> $logFile 2>&1
  1108. comprobarError $? 906 "80/tcp"
  1109. if [ $sshControl = true ];then
  1110. firewall-cmd --add-port=22/tcp >> $logFile 2>&1
  1111. comprobarError $? 906 "22/tcp"
  1112. fi
  1113. if [ $sslOn = true ];then
  1114. firewall-cmd --add-port=443/tcp >> $logFile 2>&1
  1115. comprobarError $? 906 "443/tcp"
  1116. fi
  1117. firewall-cmd --runtime-to-permanent >> $logFile 2>&1
  1118. comprobarError $? 907
  1119. habilitarServicio firewalld >> $logFile 2>&1
  1120. comprobarError $? 907
  1121. fi
  1122. unset sshControl
  1123. }
  1124. esSSH() {
  1125. p=${1:-$PPID}
  1126. #read pid name x ppid y < <( cat /proc/$p/stat )
  1127. read pid name ppid < <( ps -o pid= -o comm= -o ppid= -p $p)
  1128. [[ "$name" =~ sshd ]] && { sshControl=true; return 0; }
  1129. [ "$ppid" -le 1 ] && { sshControl=false; return 1; }
  1130. esSSH $ppid
  1131. }
  1132. descargarMediaWiki() {
  1133. # Descargar la versión 1.31.0 de MediaWiki
  1134. if [ ! -d ./var ];then
  1135. comprobarError 1 5 "./var"
  1136. fi
  1137. #echo -en "Descargando MediaWiki-1.31.0..." | tee -a $logFile
  1138. #curl -o "./var/mediawiki-1.31.0.tar.gz" "https://releases.wikimedia.org/mediawiki/1.31/mediawiki-1.31.0.tar.gz" >> $logFile 2>&1
  1139. curl "https://releases.wikimedia.org/mediawiki/1.31/mediawiki-1.31.0.tar.gz" 2>$logFile | tar -xz -C "./var/"
  1140. comprobarError $? 501
  1141. #echo -en " Ok.\n" | tee -a $logFile
  1142. }
  1143. configurarMediaWiki() {
  1144. # Configuramos MediaWiki
  1145. # Copiamos archivos y permisos
  1146. cp -R ./var/mediawiki-1.31.0/ /var/www/wiki
  1147. comprobarError $? 502
  1148. chown -R $webServerUser:$webServerGroup /var/www/wiki
  1149. comprobarError $? 502
  1150. # Creamos DataBase
  1151. crearDBMediaWiki
  1152. # Configuramos LocalSettings.php
  1153. # Configuramos VirtualHost
  1154. if [ $nginxOn = true ] && [ $debianOS = true ] && [ $sslOn = false ]; then
  1155. virtualHost="./etc/nginx-debian/sites-available/mediawiki.conf"
  1156. elif [ $nginxOn = true ] && [ $debianOS = true ] && [ $sslOn = true ]; then
  1157. virtualHost="./etc/nginx-debian/sites-available/mediawiki-ssl.conf"
  1158. elif [ $nginxOn = true ] && [ $rhelOS = true ] && [ $sslOn = false ]; then
  1159. virtualHost="./etc/nginx-rhel/sites-available/mediawiki.conf"
  1160. elif [ $nginxOn = true ] && [ $rhelOS = true ] && [ $sslOn = true ]; then
  1161. virtualHost="./etc/nginx-rhel/sites-available/mediawiki-ssl.conf"
  1162. fi
  1163. if [ ! -f $virtualHost ];then
  1164. comprobarError $? 502
  1165. fi
  1166. instalarVirtualHost $virtualHost $dominioMediWiki
  1167. unset virtualHost
  1168. }
  1169. crearDBMediaWiki() {
  1170. # Creamos una base de datos para MediaWiki
  1171. dbDir="./etc/db"
  1172. if [ ! -d $dbDir ];then
  1173. mkdir $dbDir 2>/dev/null
  1174. comprobarError $? 503
  1175. fi
  1176. dbFile=$dbDir"/mediawiki.sql"
  1177. echo -en "CREATE DATABASE $dominioMediaWiki;\n" > $dbFile
  1178. echo -en "GRANT ALL PRIVILEGES ON $dominioMediaWiki.* TO '$dominioMediaWiki'@'localhost' IDENTIFIED BY '$sqlPasswd';\n" >> $dbFile
  1179. mysql -u root --password=$sqlPasswd < $dbFile
  1180. control=$?
  1181. rm -f $dbFile 2>/dev/null
  1182. comprobarError $? 503
  1183. comprobarError $control 503
  1184. unset control
  1185. }
  1186. descargarMoodle() {
  1187. # Descargar la versión 1.31.0 de MediaWiki
  1188. if [ ! -d ./var ];then
  1189. comprobarError 1 5 "./var"
  1190. fi
  1191. #echo -en "Descargando Moodle-3.5-1..." | tee -a $logFile
  1192. #curl -o "./var/moodle-3.5.1.tgz" "https://download.moodle.org/download.php/direct/stable35/moodle-latest-35.tgz" >> $logFile 2>&1
  1193. curl "https://download.moodle.org/download.php/direct/stable35/moodle-latest-35.tgz" 2>$logFile | tar -xz -C "./var/"
  1194. comprobarError $? 501
  1195. #echo -en " OK.\n" | tee -a $logFile
  1196. }
  1197. # Comprobación del sistema e inicialización
  1198. inicializarVariables
  1199. comprobarRoot
  1200. OSInfo
  1201. comprobarDependencias
  1202. # Bienvenida
  1203. mostrarBienvenida
  1204. # Selección de componentes (express vs avanzada)
  1205. mostrarExpress
  1206. # Instalación Servidor Web
  1207. if [ $apacheOn = true ]; then
  1208. echo -en "Instalando Servidor Web Apache..." | tee -a $logFile
  1209. # {
  1210. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1211. instalarApache
  1212. progreso=$((progreso + 1))
  1213. # } > >(whiptail --gauge "Instalando Web Server Apache..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1214. echo -en " OK.\n" | tee -a $logFile
  1215. elif [ $nginxOn = true ]; then
  1216. echo -en "Instalando Servidor Web Nginx..." | tee -a $logFile
  1217. # {
  1218. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1219. instalarNginx
  1220. progreso=$((progreso + 1))
  1221. # } > >(whiptail --gauge "Instalando Web Server Nginx..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1222. echo -en " OK.\n" | tee -a $logFile
  1223. fi
  1224. # Instalación Base de Datos
  1225. if [ $mySQLOn = true ]; then
  1226. echo -en "Instalando Base de Datos MySQL..." | tee -a $logFile
  1227. # {
  1228. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1229. instalarMySQL
  1230. progreso=$((progreso + 1))
  1231. # } > >(whiptail --gauge "Instalando Base de Datos MySQL..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1232. echo -en " OK.\n" | tee -a $logFile
  1233. elif [ $mariaDBOn = true ]; then
  1234. echo -en "Instalando Base de Datos MariaDB..." | tee -a $logFile
  1235. # {
  1236. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1237. instalarMariaDB
  1238. progreso=$((progreso + 1))
  1239. # } > >(whiptail --gauge "Instalando Base de Datos MariaDB..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1240. echo -en " OK.\n" | tee -a $logFile
  1241. fi
  1242. # Instalación PHP-7.2
  1243. if [ $phpOn = true ]; then
  1244. echo -en "Instalando PHP-7..." | tee -a $logFile
  1245. # {
  1246. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1247. instalarPHP
  1248. progreso=$((progreso + 1))
  1249. # } > >(whiptail --gauge "Instalando PHP-7..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1250. echo -en " OK.\n" | tee -a $logFile
  1251. fi
  1252. # Instalación SSL/TLS
  1253. if [ $sslOn = true ];then
  1254. echo -en "Instalando SSL/TLS..." | tee -a $logfile
  1255. # {
  1256. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1257. sleep 2
  1258. progreso=$((progreso + 1))
  1259. # } > >(whiptail --gauge "Instalando SSL/TLS..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1260. echo -en " OK.\n" | tee -a $logFile
  1261. fi
  1262. # Configuración Web Server
  1263. if [ $apacheOn = true ];then
  1264. echo -en "Configurando Servidor Web Apache..." | tee -a $logFile
  1265. # {
  1266. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1267. configurarApache
  1268. progreso=$((progreso + 1))
  1269. # } > >(whiptail --gauge "Configurando Servidor Web..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1270. echo -en " OK.\n" | tee -a $logFile
  1271. elif [ $nginxOn = true ]; then
  1272. echo -en "Configurando Servidor Web Nginx..." | tee -a $logFile
  1273. # {
  1274. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1275. configurarNginx
  1276. progreso=$((progreso + 1))
  1277. # } > >(whiptail --gauge "Configurando Servidor Web..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1278. echo -en " OK.\n" | tee -a $logFile
  1279. fi
  1280. # Configuración Database (Arrancar Base de Datos, establecer contraseña y configuración segura)
  1281. if [ $mySQLOn = true ] || [ $mariaDBOn = true ];then
  1282. habilitarServicio "$sqlServerName"
  1283. echo -en "Configurando Base de Datos MySQL..." | tee -a $logFile
  1284. # {
  1285. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1286. establecerSQLPasswd
  1287. progreso=$((progreso + 1))
  1288. # } > >(whiptail --gauge "Configurando Base de Datos MySQL..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1289. echo -en " OK.\n" | tee -a $logFile
  1290. fi
  1291. # Configuración PHP (cgi.fix_pathinfo=0 y Configurar máximo de subida de archivos)
  1292. if [ $phpOn = true ];then
  1293. echo -en "Configurando PHP-7..." | tee -a $logFile
  1294. # {
  1295. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1296. configurarPHP
  1297. progreso=$((progreso + 1))
  1298. # } > >(whiptail --gauge "Configurando PHP-7..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1299. echo -en " OK.\n" | tee -a $logFile
  1300. fi
  1301. # Configuración SSL/TLS
  1302. # Generar certificados
  1303. # Configurar certificados
  1304. if [ $sslOn = true ];then
  1305. echo -en "Configurando SSL/TLS..." | tee -a $logFile
  1306. # {
  1307. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1308. sleep 2
  1309. progreso=$((progreso + 1))
  1310. # } > >(whiptail --gauge "Configurando SSL/TLS..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1311. echo -en " OK.\n" | tee -a $logFile
  1312. fi
  1313. # Arrancar y habilitar todos los servicios (SystemD, Service o SystemV)
  1314. # {
  1315. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1316. if [ $apacheOn = true ];then
  1317. habilitarServicio $webServerName
  1318. elif [ $nginxOn = true ];then
  1319. habilitarServicio $webServerName
  1320. if [ $phpOn = true ];then
  1321. habilitarServicio $phpFPMName
  1322. fi
  1323. fi
  1324. progreso=$((progreso + 1))
  1325. # } > >(whiptail --gauge "Arrancando Servicios..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1326. # MediaWiki
  1327. if [ $mediaWikiOn = true ]; then
  1328. # Descargar MediaWiki
  1329. echo -en "Descargando MediaWiki-1.31.0..." | tee -a $logFile
  1330. # {
  1331. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1332. descargarMediaWiki
  1333. progreso=$((progreso + 1))
  1334. # } > >(whiptail --gauge "Instalando MediaWiki..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1335. echo -en " OK.\n" | tee -a $logFile
  1336. # Configurar MediWiki
  1337. echo -en "Configurando MediaWiki..." | tee -a $logFile
  1338. # {
  1339. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1340. configurarMediaWiki
  1341. progreso=$((progreso + 1))
  1342. # } > >(whiptail --gauge "Configurando MediaWiki..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1343. echo -en " OK.\n" | tee -a $logFile
  1344. fi
  1345. # Moodle
  1346. if [ $moodleOn = true ]; then
  1347. # Descargar Moodle
  1348. echo -en "Descargando Moodle-3.5.1..." | tee -a $logFile
  1349. # {
  1350. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1351. descargarMoodle
  1352. progreso=$((progreso + 1))
  1353. # } > >(whiptail --gauge "Instalando Moodle..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1354. echo -en " OK.\n" | tee -a $logFile
  1355. # Configuración Moodle
  1356. echo -en "Configurando Moodle..." | tee -a $logFile
  1357. # {
  1358. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1359. sleep 2
  1360. progreso=$((progreso + 1))
  1361. # } > >(whiptail --gauge "Configurando Moodle..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1362. echo -en " OK.\n" | tee -a $logFile
  1363. fi
  1364. # Configurar Virtual Hosts
  1365. # Instalar info.php
  1366. if [ $infoPHPOn = true ];then
  1367. echo -en "Configurando 'info.php'..." | tee -a $logFile
  1368. instalarPHPInfo
  1369. if [ $? -eq 0 ];then
  1370. echo -en " OK.\n" | tee -a $logFile
  1371. else
  1372. echo -en " Ya se encuentra configurado.\n" | tee -a $logFile
  1373. fi
  1374. fi
  1375. # Añadir reglas del cortafuegos
  1376. echo -en "Configurando Cortafuegos..." | tee -a $logFile
  1377. # {
  1378. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1379. configurarCortafuegos
  1380. progreso=$((progreso + 1))
  1381. # } > >(whiptail --gauge "Configurando Cortafuegos..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1382. echo -en " OK.\n" | tee -a $logFile
  1383. # Configurar actualizaciones
  1384. if [ $actualizacionesOn = true ]; then
  1385. echo -en "Configurando Actualizaciones Automáticas..." | tee -a $logFile
  1386. # {
  1387. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1388. sleep 2
  1389. progreso=$((progreso + 1))
  1390. # } > >(whiptail --gauge "Configurando actualizaciones automáticas..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1391. echo -en " OK.\n" | tee -a $logFile
  1392. fi