install 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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 openssl 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 openssl 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 openssl 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 openssl 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. phpConfFile="./etc/php/php-fpm.d/www.conf"
  913. phpDest="/etc/php-fpm.d/www.conf"
  914. if [ ! -f $phpConfFile ];then
  915. comprobarError $? 4 "$phpConfFile"
  916. fi
  917. cp -f $phpConfFile $phpDest 2>/dev/null
  918. comprobarError $? 305
  919. fi
  920. unset phpConfFile phpDest
  921. }
  922. establecerMaxUpload() {
  923. control=false
  924. error=""
  925. while [ $control = false ];do
  926. 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)
  927. comprobarError $? 1
  928. if [ -z $maxUpload2 ];then
  929. error="ERROR: NO PUEDES DEJAR ESTE PARÁMETRO VACIO.\n"
  930. else
  931. control=true
  932. fi
  933. done
  934. maxUpload=$maxUpload2
  935. unset control maxUpload2
  936. }
  937. instalarPHPInfo() {
  938. # Instalar fichero php
  939. if [ ! -f /etc/$webServerName/sites-enabled/phpinfo.conf ];then
  940. infoFile="./var/www/info.php"
  941. if [ ! -f $infoFile ];then
  942. comprobarError 1 4 "$infoFile"
  943. fi
  944. cp -f $infoFile /var/www/html/ 2>/dev/null
  945. comprobarError $? 106
  946. chown -R $webServerUser:$webServerGroup /var/www/html 2>/dev/null
  947. comprobarError $? 106
  948. unset infoFile
  949. # Instalar VirtualHost
  950. if [ $debianOS = true ];then
  951. virtualHost="./etc/$webServerName-debian/sites-available/phpinfo.conf"
  952. elif [ $rhelOS = true ];then
  953. virtualHost="./etc/$webServerName-rhel/sites-available/phpinfo.conf"
  954. fi
  955. instalarVirtualHost $virtualHost
  956. unset virtualHost
  957. return 0
  958. else
  959. return 1
  960. fi
  961. }
  962. habilitarServicio() {
  963. # Arrancamos y habilitamos el servicio (con SystemD, Upstart o SystemV)
  964. # Intentamos con systemctl (SystemD)
  965. #echo -en "Habilitando Servicio '$1'" | tee -a $logFile
  966. if [ $# -le 0 ];then
  967. comprobarError 1 900
  968. fi
  969. which systemctl > /dev/null 2>&1
  970. if [ $? -eq 0 ]; then
  971. #echo -en " (SystemD)..." | tee -a $logFile
  972. systemctl start $1 > /dev/null 2>&1
  973. comprobarError $? 901 $1
  974. systemctl enable $1 > /dev/null 2>&1
  975. comprobarError $? 902 $1
  976. else
  977. # Intentamos con service (Upstart)
  978. which service > /dev/null 2>&1
  979. if [ $? -eq 0 ]; then
  980. #echo -en " (Upstart)..." | tee -a $logFile
  981. service $1 start > /dev/null 2>&1
  982. comprobarError $? 901 $1
  983. else
  984. # Intentamos con init.d (SystemV)
  985. #echo -en " (SystemV)..." | tee -a $logFile
  986. /etc/init.d/$1 start > /dev/null 2>&1
  987. comprobarError $? 901 $1
  988. fi
  989. # Intentamos habilitar en el arranque (Upstart)
  990. which update-rc.d > /dev/null 2>&1
  991. if [ $? -eq 0 ];then
  992. update-rc.d $1 enable
  993. comprobarError $? 902 $1
  994. else
  995. # Intentamos habilitar en el arranque (SystemV)
  996. which chkconfig > /dev/null 2>&1
  997. if [ $? -eq 0 ];then
  998. chkconfig $1 on
  999. comprobarError $? 902 $1
  1000. else
  1001. # ¿Qué mas opciones nos quedan?
  1002. comprobarError 1 902 $1
  1003. fi
  1004. fi
  1005. fi
  1006. # Mejora: comprobar si el servicio está parado
  1007. #echo -en " OK.\n" | tee -a $logFile
  1008. }
  1009. deshabilitarServicio() {
  1010. # Paramos y deshabilitamos el servicio (con SystemD, Upstart o SystemV)
  1011. # Intentamos con systemctl (SystemD)
  1012. #echo -en "Deshabilitando Servicio '$1'" | tee -a $logFile
  1013. if [ $# -le 0 ];then
  1014. comprobarError 1 903
  1015. fi
  1016. which systemctl > /dev/null 2>&1
  1017. if [ $? -eq 0 ]; then
  1018. #echo -en " (SystemD)..." | tee -a $logFile
  1019. systemctl stop $1 > /dev/null 2>&1
  1020. comprobarError $? 904 $1
  1021. systemctl disable $1 > /dev/null 2>&1
  1022. comprobarError $? 905 $1
  1023. else
  1024. # Intentamos con service (Upstart)
  1025. which service > /dev/null 2>&1
  1026. if [ $? -eq 0 ]; then
  1027. #echo -en " (Upstart)..." | tee -a $logFile
  1028. service $1 stop > /dev/null 2>&1
  1029. comprobarError $? 904 $1
  1030. else
  1031. # Intentamos con init.d (SystemV)
  1032. #echo -en " (SystemV)..." | tee -a $logFile
  1033. /etc/init.d/$1 stop > /dev/null 2>&1
  1034. comprobarError $? 904 $1
  1035. fi
  1036. # Intentamos habilitar en el arranque (Upstart)
  1037. which update-rc.d > /dev/null 2>&1
  1038. if [ $? -eq 0 ];then
  1039. update-rc.d $1 disable
  1040. comprobarError $? 905 $1
  1041. else
  1042. # Intentamos habilitar en el arranque (SystemV)
  1043. which chkconfig > /dev/null 2>&1
  1044. if [ $? -eq 0 ];then
  1045. chkconfig $1 off
  1046. comprobarError $? 905 $1
  1047. else
  1048. # ¿Qué mas opciones nos quedan?
  1049. comprobarError 1 905 $1
  1050. fi
  1051. fi
  1052. fi
  1053. # Mejora: comprobar si el servicio está funcionando
  1054. #echo -en " OK.\n" | tee -a $logFile
  1055. }
  1056. recargarServicio() {
  1057. # Recargamos el servicio (con SystemD, Upstart o SystemV)
  1058. # Intentamos con systemctl (SystemD)
  1059. #echo -en "Recargando Servicio '$1'" | tee -a $logFile
  1060. if [ $# -le 0 ];then
  1061. comprobarError 1 912
  1062. fi
  1063. which systemctl > /dev/null 2>&1
  1064. if [ $? -eq 0 ]; then
  1065. #echo -en " (SystemD)..." | tee -a $logFile
  1066. systemctl reload $1 > /dev/null 2>&1
  1067. comprobarError $? 913 $1
  1068. else
  1069. # Intentamos con service (Upstart)
  1070. which service > /dev/null 2>&1
  1071. if [ $? -eq 0 ]; then
  1072. #echo -en " (Upstart)..." | tee -a $logFile
  1073. service $1 reload > /dev/null 2>&1
  1074. comprobarError $? 913 $1
  1075. else
  1076. # Intentamos con init.d (SystemV)
  1077. #echo -en " (SystemV)..." | tee -a $logFile
  1078. /etc/init.d/$1 reload > /dev/null 2>&1
  1079. comprobarError $? 913 $1
  1080. fi
  1081. fi
  1082. # Mejora: comprobar si el servicio está funcionando
  1083. #echo -en " OK.\n" | tee -a $logFile
  1084. }
  1085. configurarCortafuegos() {
  1086. # Configuramos cortafuegos (añadir reglas y encender)
  1087. # Comprobamos si estamos usando SSH
  1088. esSSH $PPID
  1089. if [ $debianOS = true ];then
  1090. ufw allow 80/tcp >> $logFile 2>&1
  1091. comprobarError $? 906 "80/tcp"
  1092. if [ $sshControl = true ];then
  1093. ufw allow 22/tcp >> $logFile 2>&1
  1094. comprobarError $? 906 "22/tcp"
  1095. fi
  1096. if [ $sslOn = true ];then
  1097. ufw allow 443/tcp >> $logFile 2>&1
  1098. comprobarError $? 906 "443/tcp"
  1099. fi
  1100. ufw --force enable >> $logFile 2>&1
  1101. comprobarError $? 907
  1102. elif [ $rhelOS = true ];then
  1103. firewall-cmd --add-port=80/tcp >> $logFile 2>&1
  1104. comprobarError $? 906 "80/tcp"
  1105. if [ $sshControl = true ];then
  1106. firewall-cmd --add-port=22/tcp >> $logFile 2>&1
  1107. comprobarError $? 906 "22/tcp"
  1108. fi
  1109. if [ $sslOn = true ];then
  1110. firewall-cmd --add-port=443/tcp >> $logFile 2>&1
  1111. comprobarError $? 906 "443/tcp"
  1112. fi
  1113. firewall-cmd --runtime-to-permanent >> $logFile 2>&1
  1114. comprobarError $? 907
  1115. habilitarServicio firewalld >> $logFile 2>&1
  1116. comprobarError $? 907
  1117. fi
  1118. unset sshControl
  1119. }
  1120. esSSH() {
  1121. p=${1:-$PPID}
  1122. #read pid name x ppid y < <( cat /proc/$p/stat )
  1123. read pid name ppid < <( ps -o pid= -o comm= -o ppid= -p $p)
  1124. [[ "$name" =~ sshd ]] && { sshControl=true; return 0; }
  1125. [ "$ppid" -le 1 ] && { sshControl=false; return 1; }
  1126. esSSH $ppid
  1127. }
  1128. descargarMediaWiki() {
  1129. # Descargar la versión 1.31.0 de MediaWiki
  1130. if [ ! -d ./var ];then
  1131. comprobarError 1 5 "./var"
  1132. fi
  1133. #echo -en "Descargando MediaWiki-1.31.0..." | tee -a $logFile
  1134. #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
  1135. curl "https://releases.wikimedia.org/mediawiki/1.31/mediawiki-1.31.0.tar.gz" 2>$logFile | tar -xz -C "./var/"
  1136. comprobarError $? 501
  1137. #echo -en " Ok.\n" | tee -a $logFile
  1138. }
  1139. configurarMediaWiki() {
  1140. # Configuramos MediaWiki
  1141. # Copiamos archivos y permisos
  1142. cp -R ./var/mediawiki-1.31.0/ /var/www/wiki
  1143. comprobarError $? 502
  1144. chown -R $webServerUser:$webServerGroup /var/www/wiki
  1145. comprobarError $? 502
  1146. # Creamos DataBase
  1147. crearDBMediaWiki
  1148. # Configuramos LocalSettings.php
  1149. # Configuramos VirtualHost
  1150. if [ $nginxOn = true ] && [ $debianOS = true ] && [ $sslOn = false ]; then
  1151. virtualHost="./etc/nginx-debian/sites-available/mediawiki.conf"
  1152. elif [ $nginxOn = true ] && [ $debianOS = true ] && [ $sslOn = true ]; then
  1153. virtualHost="./etc/nginx-debian/sites-available/mediawiki-ssl.conf"
  1154. elif [ $nginxOn = true ] && [ $rhelOS = true ] && [ $sslOn = false ]; then
  1155. virtualHost="./etc/nginx-rhel/sites-available/mediawiki.conf"
  1156. elif [ $nginxOn = true ] && [ $rhelOS = true ] && [ $sslOn = true ]; then
  1157. virtualHost="./etc/nginx-rhel/sites-available/mediawiki-ssl.conf"
  1158. fi
  1159. if [ ! -f $virtualHost ];then
  1160. comprobarError $? 502
  1161. fi
  1162. instalarVirtualHost $virtualHost $dominioMediWiki
  1163. unset virtualHost
  1164. }
  1165. crearDBMediaWiki() {
  1166. # Creamos una base de datos para MediaWiki
  1167. dbDir="./etc/db"
  1168. if [ ! -d $dbDir ];then
  1169. mkdir $dbDir 2>/dev/null
  1170. comprobarError $? 503
  1171. fi
  1172. dbFile=$dbDir"/mediawiki.sql"
  1173. echo -en "CREATE DATABASE $dominioMediaWiki;\n" > $dbFile
  1174. echo -en "GRANT ALL PRIVILEGES ON $dominioMediaWiki.* TO '$dominioMediaWiki'@'localhost' IDENTIFIED BY '$sqlPasswd';\n" >> $dbFile
  1175. mysql -u root --password=$sqlPasswd < $dbFile
  1176. control=$?
  1177. rm -f $dbFile 2>/dev/null
  1178. comprobarError $? 503
  1179. comprobarError $control 503
  1180. unset control
  1181. }
  1182. descargarMoodle() {
  1183. # Descargar la versión 1.31.0 de MediaWiki
  1184. if [ ! -d ./var ];then
  1185. comprobarError 1 5 "./var"
  1186. fi
  1187. #echo -en "Descargando Moodle-3.5-1..." | tee -a $logFile
  1188. #curl -o "./var/moodle-3.5.1.tgz" "https://download.moodle.org/download.php/direct/stable35/moodle-latest-35.tgz" >> $logFile 2>&1
  1189. curl "https://download.moodle.org/download.php/direct/stable35/moodle-latest-35.tgz" 2>$logFile | tar -xz -C "./var/"
  1190. comprobarError $? 501
  1191. #echo -en " OK.\n" | tee -a $logFile
  1192. }
  1193. # Comprobación del sistema e inicialización
  1194. inicializarVariables
  1195. comprobarRoot
  1196. OSInfo
  1197. comprobarDependencias
  1198. # Bienvenida
  1199. mostrarBienvenida
  1200. # Selección de componentes (express vs avanzada)
  1201. mostrarExpress
  1202. # Instalación Servidor Web
  1203. if [ $apacheOn = true ]; then
  1204. echo -en "Instalando Servidor Web Apache..." | tee -a $logFile
  1205. # {
  1206. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1207. instalarApache
  1208. progreso=$((progreso + 1))
  1209. # } > >(whiptail --gauge "Instalando Web Server Apache..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1210. echo -en " OK.\n" | tee -a $logFile
  1211. elif [ $nginxOn = true ]; then
  1212. echo -en "Instalando Servidor Web Nginx..." | tee -a $logFile
  1213. # {
  1214. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1215. instalarNginx
  1216. progreso=$((progreso + 1))
  1217. # } > >(whiptail --gauge "Instalando Web Server Nginx..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1218. echo -en " OK.\n" | tee -a $logFile
  1219. fi
  1220. # Instalación Base de Datos
  1221. if [ $mySQLOn = true ]; then
  1222. echo -en "Instalando Base de Datos MySQL..." | tee -a $logFile
  1223. # {
  1224. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1225. instalarMySQL
  1226. progreso=$((progreso + 1))
  1227. # } > >(whiptail --gauge "Instalando Base de Datos MySQL..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1228. echo -en " OK.\n" | tee -a $logFile
  1229. elif [ $mariaDBOn = true ]; then
  1230. echo -en "Instalando Base de Datos MariaDB..." | tee -a $logFile
  1231. # {
  1232. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1233. instalarMariaDB
  1234. progreso=$((progreso + 1))
  1235. # } > >(whiptail --gauge "Instalando Base de Datos MariaDB..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1236. echo -en " OK.\n" | tee -a $logFile
  1237. fi
  1238. # Instalación PHP-7.2
  1239. if [ $phpOn = true ]; then
  1240. echo -en "Instalando PHP-7..." | tee -a $logFile
  1241. # {
  1242. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1243. instalarPHP
  1244. progreso=$((progreso + 1))
  1245. # } > >(whiptail --gauge "Instalando PHP-7..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1246. echo -en " OK.\n" | tee -a $logFile
  1247. fi
  1248. # Instalación SSL/TLS
  1249. if [ $sslOn = true ];then
  1250. echo -en "Instalando SSL/TLS..." | tee -a $logfile
  1251. # {
  1252. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1253. sleep 2
  1254. progreso=$((progreso + 1))
  1255. # } > >(whiptail --gauge "Instalando SSL/TLS..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1256. echo -en " OK.\n" | tee -a $logFile
  1257. fi
  1258. # Configuración Web Server
  1259. if [ $apacheOn = true ];then
  1260. echo -en "Configurando Servidor Web Apache..." | tee -a $logFile
  1261. # {
  1262. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1263. configurarApache
  1264. progreso=$((progreso + 1))
  1265. # } > >(whiptail --gauge "Configurando Servidor Web..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1266. echo -en " OK.\n" | tee -a $logFile
  1267. elif [ $nginxOn = true ]; then
  1268. echo -en "Configurando Servidor Web Nginx..." | tee -a $logFile
  1269. # {
  1270. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1271. configurarNginx
  1272. progreso=$((progreso + 1))
  1273. # } > >(whiptail --gauge "Configurando Servidor Web..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1274. echo -en " OK.\n" | tee -a $logFile
  1275. fi
  1276. # Configuración Database (Arrancar Base de Datos, establecer contraseña y configuración segura)
  1277. if [ $mySQLOn = true ] || [ $mariaDBOn = true ];then
  1278. habilitarServicio "$sqlServerName"
  1279. echo -en "Configurando Base de Datos MySQL..." | tee -a $logFile
  1280. # {
  1281. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1282. establecerSQLPasswd
  1283. progreso=$((progreso + 1))
  1284. # } > >(whiptail --gauge "Configurando Base de Datos MySQL..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1285. echo -en " OK.\n" | tee -a $logFile
  1286. fi
  1287. # Configuración PHP (cgi.fix_pathinfo=0 y Configurar máximo de subida de archivos)
  1288. if [ $phpOn = true ];then
  1289. echo -en "Configurando PHP-7..." | tee -a $logFile
  1290. # {
  1291. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1292. configurarPHP
  1293. progreso=$((progreso + 1))
  1294. # } > >(whiptail --gauge "Configurando PHP-7..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1295. echo -en " OK.\n" | tee -a $logFile
  1296. fi
  1297. # Configuración SSL/TLS
  1298. # Generar certificados
  1299. # Configurar certificados
  1300. if [ $sslOn = true ];then
  1301. echo -en "Configurando SSL/TLS..." | tee -a $logFile
  1302. # {
  1303. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1304. sleep 2
  1305. progreso=$((progreso + 1))
  1306. # } > >(whiptail --gauge "Configurando SSL/TLS..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1307. echo -en " OK.\n" | tee -a $logFile
  1308. fi
  1309. # Arrancar y habilitar todos los servicios (SystemD, Service o SystemV)
  1310. # {
  1311. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1312. if [ $apacheOn = true ];then
  1313. habilitarServicio $webServerName
  1314. elif [ $nginxOn = true ];then
  1315. habilitarServicio $webServerName
  1316. if [ $phpOn = true ];then
  1317. habilitarServicio $phpFPMName
  1318. fi
  1319. fi
  1320. progreso=$((progreso + 1))
  1321. # } > >(whiptail --gauge "Arrancando Servicios..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1322. # MediaWiki
  1323. if [ $mediaWikiOn = true ]; then
  1324. # Descargar MediaWiki
  1325. echo -en "Descargando MediaWiki-1.31.0..." | tee -a $logFile
  1326. # {
  1327. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1328. descargarMediaWiki
  1329. progreso=$((progreso + 1))
  1330. # } > >(whiptail --gauge "Instalando MediaWiki..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1331. echo -en " OK.\n" | tee -a $logFile
  1332. # Configurar MediWiki
  1333. echo -en "Configurando MediaWiki..." | tee -a $logFile
  1334. # {
  1335. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1336. configurarMediaWiki
  1337. progreso=$((progreso + 1))
  1338. # } > >(whiptail --gauge "Configurando MediaWiki..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1339. echo -en " OK.\n" | tee -a $logFile
  1340. fi
  1341. # Moodle
  1342. if [ $moodleOn = true ]; then
  1343. # Descargar Moodle
  1344. echo -en "Descargando Moodle-3.5.1..." | tee -a $logFile
  1345. # {
  1346. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1347. descargarMoodle
  1348. progreso=$((progreso + 1))
  1349. # } > >(whiptail --gauge "Instalando Moodle..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1350. echo -en " OK.\n" | tee -a $logFile
  1351. # Configuración Moodle
  1352. echo -en "Configurando Moodle..." | tee -a $logFile
  1353. # {
  1354. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1355. sleep 2
  1356. progreso=$((progreso + 1))
  1357. # } > >(whiptail --gauge "Configurando Moodle..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1358. echo -en " OK.\n" | tee -a $logFile
  1359. fi
  1360. # Configurar Virtual Hosts
  1361. # Instalar info.php
  1362. if [ $infoPHPOn = true ];then
  1363. echo -en "Configurando 'info.php'..." | tee -a $logFile
  1364. instalarPHPInfo
  1365. if [ $? -eq 0 ];then
  1366. echo -en " OK.\n" | tee -a $logFile
  1367. else
  1368. echo -en " Ya se encuentra configurado.\n" | tee -a $logFile
  1369. fi
  1370. fi
  1371. # Añadir reglas del cortafuegos
  1372. echo -en "Configurando Cortafuegos..." | tee -a $logFile
  1373. # {
  1374. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1375. configurarCortafuegos
  1376. progreso=$((progreso + 1))
  1377. # } > >(whiptail --gauge "Configurando Cortafuegos..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1378. echo -en " OK.\n" | tee -a $logFile
  1379. # Configurar actualizaciones
  1380. if [ $actualizacionesOn = true ]; then
  1381. echo -en "Configurando Actualizaciones Automáticas..." | tee -a $logFile
  1382. # {
  1383. # echo -en "%s\n" $((100 * progreso / progresoTotal))
  1384. sleep 2
  1385. progreso=$((progreso + 1))
  1386. # } > >(whiptail --gauge "Configurando actualizaciones automáticas..." $((ALTO * 4 / 10)) $((ANCHO * 9 / 10)) $((100 * progreso / progresoTotal)))
  1387. echo -en " OK.\n" | tee -a $logFile
  1388. fi