網頁

2020年3月19日 星期四

install tomcat at ubuntu

參考 How to Install Tomcat 9 on Ubuntu 18.04

$ sudo apt update
$ sudo apt install default-jdk
$ sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
$ cd /tmp
$ wget http://ftp.tc.edu.tw/pub/Apache/tomcat/tomcat-9/v9.0.33/bin/apache-tomcat-9.0.33.tar.gz -P /tmp
$ sudo tar xf /tmp/apache-tomcat-9.0.33.tar.gz -C /opt/tomcat
$ sudo ln -s /opt/tomcat/apache-tomcat-9.0.33/ /opt/tomcat/latest
$ sudo chown -RH tomcat: /opt/tomcat/latest
$ sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'
$ sudo vi /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat 9 servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"

Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

$ sudo systemctl daemon-reload
$ sudo systemctl start tomcat
$ sudo systemctl status tomcat
$ sudo systemctl enable tomcat

$ sudo vi /opt/tomcat/latest/conf/tomcat-users.xml
<tomcat-users>
  <role rolename="admin-gui"/>
  <role rolename="manager-gui"/>
  <user username="admin" password="YourPassword" roles="admin-gui,manager-gui"/>
</tomcat-users>

$ sudo systemctl restart tomcat
網頁開啟 http://localhost:8080

參考 Nginx + Tomcat 使用 Https
$ vi /etc/nginx/sites-available/default
upstream tomcat_server {
        server 127.0.0.1:8080 weight=1;
}
server {
        listen 8443 ssl default_server;
        listen [::]:8443 ssl default_server;
        ssl_certificate /home/user1/Data/webapi/openssl/web1/server.crt;
        ssl_certificate_key /home/user1/Data/webapi/openssl/web1/server.key;

        location ^~ /test/ {
                proxy_redirect off;
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                # for https
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_pass http://tomcat_server;
        }
}

$ sudo systemctl reload nginx.service

$ sudo vi /opt/tomcat/latest/conf/server.xml
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Value className="org.apache.catalina.valves.RemoteIpValve"
            remoteIpHeader="X-Forwarded-For"
            protocolHeader="X-Forwarded-Proto"
            protocolHeaderHttpsValue="https"
            httpsServerPort="8443"
        />
      </Host>

$ sudo systemctl restart tomcat

$ sudo mkdir /opt/tomcat/laest/webapps/test
$ sudo vi /opt/tomcat/latest/webapps/test/index.jsp
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <title>React App</title>
  </head>
  <body>
    <div id="root">
        Hello world
    </div>
  </body>
</html>

$ sudo mkdir /opt/tomcat/laest/webapps/test/WEB-INF
$ sudo vi /opt/tomcat/latest/webapps/test/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
</web-app>


沒有留言:

張貼留言