GNU/Linux >> Linux Esercitazione >  >> Cent OS

Come installare Apache Tomcat 9.0/8.5 su CentOS 6/RHEL 6

Apache Tomcat è un server Web open source e un servlet container sviluppato dalla Apache Software Foundation (ASF).

Tomcat implementa le specifiche Java Servlet e JavaServer Pages (JSP) di Oracle e fornisce un ambiente server Web HTTP "puro Java" per l'esecuzione dei codici Java.

Apache Tomcat include strumenti per la configurazione e la gestione, ma può anche essere configurato modificando i file di configurazione XML.

Ecco la guida passo passo per installare Apache Tomcat 9.0 / 8.5 su CentOS 6 / RHEL 6.

Prerequisiti

Installa Java

Tomcat richiede che sulla macchina sia installata una versione stabile di Java 8 o successiva. Puoi installare Oracle JDK o OpenJDK sul tuo computer.

Qui userò OpenJDK.

yum install -y java-1.8.0 wget

Puoi anche verificare Java emettendo il seguente comando.

java -version

Risultato:

openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

Crea un account di servizio

Per la migliore pratica, Tomcat non dovrebbe mai essere eseguito come utente privilegiato (root). Quindi, crea un utente con privilegi bassi per l'esecuzione del servizio Tomcat.

useradd -d /opt/tomcat  tomcat

Installa Apache Tomcat

Scarica Apache Tomcat

Scarica Apache Tomcat dal sito ufficiale.

### Apache Tomcat 9.0 ###

wget https://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.22/bin/apache-tomcat-9.0.22.tar.gz

### Apache Tomcat 8.5 ###

wget https://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.43/bin/apache-tomcat-8.5.43.tar.gz

Configura Apache Tomcat

Estrarre il tomcat nel modo desiderato (/opt/tomcat ) directory.

tar -zxvf apache-tomcat-*.tar.gz

mv apache-tomcat-*/* /opt/tomcat/

chown -R tomcat:tomcat /opt/tomcat/

Crea script di inizializzazione

Apache Tomcat può essere avviato e arrestato manualmente dallo script fornito con il pacchetto. Ma qui useremo lo script init per gestirlo.

vi /etc/init.d/tomcat9

Usa le informazioni di seguito.

        
#!/bin/bash
# chkconfig: 2345 95 20
# description: This application was developed by me and is tested on this server
# processname: my_app
#
# Tomcat 8 start/stop/status init.d script
# Initially forked from: https://gist.github.com/valotas/1000094
# @author: Miglen Evlogiev <[email protected]>
#
# Release updates:
# Updated method for gathering pid of the current proccess
# Added usage of CATALINA_BASE
# Added coloring and additional status
# Added check for existence of the tomcat user
# Added termination proccess
 
#Location of JAVA_HOME (bin files)
export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk.x86_64/
 
#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH
 
#CATALINA_HOME is the location of the bin files of Tomcat  
export CATALINA_HOME=/opt/tomcat/
 
#CATALINA_BASE is the location of the configuration files of this instance of Tomcat
export CATALINA_BASE=/opt/tomcat/
 
#TOMCAT_USER is the default user of tomcat
export TOMCAT_USER=tomcat
 
#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;31mkill\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
 
#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20
 
tomcat_pid() {
        echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
}
 
start() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
  else
    # Start tomcat
    echo -e "\e[00;32mStarting tomcat\e[00m"
    #ulimit -n 100000
    #umask 007
    #/bin/su -p -s /bin/sh $TOMCAT_USER
        if [ `user_exists $TOMCAT_USER` = "1" ]
        then
                /bin/su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
        else
                sh $CATALINA_HOME/bin/startup.sh
        fi
        status
  fi
  return 0
}
 
status(){
          pid=$(tomcat_pid)
          if [ -n "$pid" ]; then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
          else echo -e "\e[00;31mTomcat is not running\e[00m"
          fi
}

terminate() {
	echo -e "\e[00;31mTerminating Tomcat\e[00m"
	kill -9 $(tomcat_pid)
}

stop() {
  pid=$(tomcat_pid)
  if [ -n "$pid" ]
  then
    echo -e "\e[00;31mStoping Tomcat\e[00m"
    #/bin/su -p -s /bin/sh $TOMCAT_USER
        sh $CATALINA_HOME/bin/shutdown.sh
 
    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
      sleep 1
      let count=$count+1;
    done
 
    if [ $count -gt $kwait ]; then
      echo -n -e "\n\e[00;31mkilling processes didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
      terminate
    fi
  else
    echo -e "\e[00;31mTomcat is not running\e[00m"
  fi
 
  return 0
}
 
user_exists(){
        if id -u $1 >/dev/null 2>&1; then
        echo "1"
        else
                echo "0"
        fi
}
 
case $1 in
	start)
	  start
	;;
	stop)  
	  stop
	;;
	restart)
	  stop
	  start
	;;
	status)
		status
	;;
	kill)
		terminate
	;;		
	*)
		echo -e $TOMCAT_USAGE
	;;
esac    
exit 0

Credito :Timothy Hutz.

Imposta lo script in modo che sia eseguibile.

chmod +x /etc/init.d/tomcat9

Avvia Apache Tomcat

Avvia il servizio.

service tomcat9 start

Puoi verificare il servizio in esecuzione, per impostazione predefinita Tomcat viene eseguito sulla porta n. 8080

netstat -antup | grep 8080

Uscita:

tcp        0      0 :::8080                     :::*                        LISTEN      1526/java

Abilita l'avvio automatico del servizio Tomcat all'avvio del sistema.

chkconfig --add tomcat9

chkconfig tomcat9 on

Firewall

Consenti richieste di applicazioni Web Tomcat tramite il firewall.

iptables -I INPUT -p tcp -m tcp --dport 8080 -j ACCEPT

/etc/init.d/iptables save

Configura l'interfaccia utente Web di Apache Tomcat

Tomcat può essere gestito tramite il web-manager e il virtual host manager. Sia il gestore Web che il gestore host sono protetti da password e richiedono nome utente e password per accedere.

L'utente con manager-gui e interfaccia utente il ruolo ha accesso rispettivamente al gestore dell'applicazione Web e al gestore host. Questi utenti e ruoli sono definiti in tomcat-users.xml.

vi /opt/tomcat/conf/tomcat-users.xml

Posiziona le due righe seguenti appena sopra l'ultima riga.

<role rolename="admin-gui,manager-gui"/>
<user username="admin" password="tomcat" roles="manager-gui,admin-gui"/>

Per motivi di sicurezza, Web Manager e Host Manager sono accessibili solo dal localhost, ovvero dal server stesso.

Per accedere ai gestori Web e host dal sistema remoto, è necessario aggiungere la rete di origine nell'elenco Consenti. Per farlo, modifica i due file sottostanti.

### Web Manager ###

vi /opt/tomcat/webapps/manager/META-INF/context.xml

### Host Manager ###

vi /opt/tomcat/webapps/host-manager/META-INF/context.xml

Aggiorna la riga seguente sui file sopra con l'IP di origine da cui accedi al Web e a Host Manager. .* consentirà a tutti di avere accesso a entrambi i gestori.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|.*" />

O

Puoi anche consentire solo parte della tua rete. Ad esempio:per consentire solo la rete 192.168.1.0/24, puoi utilizzare i valori seguenti.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|192.168.1.*" />

Riavvia il servizio Tomcat.

service tomcat9 restart

Accedi ad Apache Tomcat

Apri il browser web e punta a

http://ip.add.re.ss:8080

Otterresti la pagina predefinita di Tomcat.

Web Manager:– Accesso richiesto. Nome utente:admin, Password:tomcat.

Qui puoi distribuire una nuova applicazione, distribuire una nuova applicazione in un contesto specifico, elencare le applicazioni attive o inattive, avviare e arrestare le applicazioni Web.

Inoltre, puoi controllare lo stato del server .

Gestione host:– Accesso richiesto. Nome utente:admin, Password:tomcat.

Qui puoi gestire gli host virtuali di Tomcat.

Conclusione

È tutto. Spero che tu abbia imparato come installare Tomcat 9 su CentOS 6 / RHEL 6. Ora sei pronto per distribuire la tua prima applicazione web. Come consiglio di sicurezza, considera l'implementazione di SSL/TLS per Tomcat


Cent OS
  1. Come installare Apache Tomcat su Linux RHEL 8 / CentOS 8

  2. Come installare Apache Maven CentOS 7 / RHEL 7

  3. Come installare Apache Tomcat 8 su CentOS

  4. Come installare Apache Tomcat su CentOS 7

  5. Come installare Tomcat 7 su Centos?

Come installare Apache Maven CentOS 8 / RHEL 8

Come installare Apache Solr 8.9 su CentOS/RHEL 8

Come installare Apache Solr su CentOS e RHEL 8

Come installare Apache Tomcat su CentOS 8

Come installare Apache Kafka in CentOS/RHEL 7

Come installare Apache Tomcat 9 su CentOS 7