# Setup: Tomcat

https://tomcat.apache.org/download-10.cgi

We suggest that you run the Apache Tomcat as user `tomcat`. You can follow the fairly standard installation and configuration instructions compiled by Justin Ellingwood at How To Install Apache Tomcat 10 on Ubuntu 20.04. (His site has instructions for earlier versions of Tomcat and for other Linux versions as well).

In particular, you'll need to create a Linux user group named `tomcat`, and a user of the same name:

    sudo groupadd tomcat
    sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Also, do not forget to do:

    chmod +x /opt/tomcat/bin/*.sh
    chown tomcat:tomcat /opt/tomcat/conf

(This is to avoid this problem:

      30-Jun-2020 01:58:03.276 SEVERE [main] org.apache.catalina.startup.HostConfig.beforeStart Unable to create directory for deployment: [/opt/apache-tomcat-9.0.36/conf/Catalina/localhost]

)

We recommend that you intall Tomcat in `/opt/tomcat/` (which usually is the default location). If you install it elsewhere, you may need to adjust some scripts and configuration files.

You can check whether tomcat is running with something like

        ps -auxw | grep tomcat

If it is not, you can start it with

    sudo systemctl start tomcat

(how we do it e.g. on the CAE host) or

    sudo service tomcat start

, depending on how your OS is set up.

Unless you do the installation just for a one-time test, you will probably want to set up the Tomcat to automatically restart when your computer is rebooted. For instructions, see the same web page linked above; e.g., on our CAE host it involves creating the file `/etc/systemd/system/tomcat.service` and "enabling" it with

        sudo systemctl enable tomcat

If the tomcat is running properly, you can see the tomcat's main page by going to an appropriate URL in your browser, such as http://localhost:8080/ . (The port number, of course, is based on how you have configured the Tomcat; you may have chosen to change 8080 to 80, which can be omitted in URLs).

## Extra libraries

You will need an extra JAR file installed to provide for connectivity between the Java applications (such as the Game Server) and your relational database. (If you don't, you will have a runtime error mentioning `com.mysql.cj.jdbc.Driver` when you try to use the Game Server).

If your database server is the MySQL server, then the necessary JAR file can be downloaded from the Connector/J site. At that site, choose the appropriate version for your OS.

On our system, we have put that JAR file into `/opt/w2020/lib` (earlier, in `/usr/share/java`), and sym-linked it to Tomcat's library directory, so that the web application could find it in run time:

        cd /opt/tomcat/lib
        sudo ln -s /opt/w2020/lib/mysql-connector-java-8.0.20.jar

As of 2023, the above web site offers you downloading in the form a *.deb file, e.g. mysql-connector-j_8.1.0-1ubuntu22.04_all.deb . Once downloaded, you can install it with

          dpkg -i mysql-connector-j_8.1.0-1ubuntu22.04_all.deb

To find out what files you have actually downloaded by doing this, try

    $ dpkg-deb -c mysql-connector-j_8.1.0-1ubuntu22.04_all.deb
    drwxr-xr-x root/root         0 2023-06-26 12:10 ./
    drwxr-xr-x root/root         0 2023-06-26 12:10 ./usr/
    drwxr-xr-x root/root         0 2023-06-26 12:10 ./usr/share/
    drwxr-xr-x root/root         0 2023-06-26 12:10 ./usr/share/doc/
    ...
    drwxr-xr-x root/root         0 2023-06-26 12:10 ./usr/share/java/
    -rw-r--r-- root/root   2485886 2023-06-26 12:10 ./usr/share/java/mysql-connector-j-8.1.0.jar
    lrwxrwxrwx root/root         0 2023-06-26 12:10 ./usr/share/java/mysql-connector-java-8.1.0.jar -> mysql-connector-j-8.1.0.jar

So this tells you where the JAR file gets installed, and you can simply sym-link it into Tomcat's lib directory, instead of copying it.

(Note: As of ver. 6.023, the above is not recommended anymore; instead, we pack the JAR file in question into the main WAR file. For that to happen, place the JAR file into ~/w2020/other-lib directory in your build tree, and when you build your WAR file, the Ant build process will pick the JAR).

## Running on port 80

By default, Tomcat is configured to run on port 8080. If you desire to run it on port 80 instead, you need to modify Tomcat's config file (server.xml), and to allow user `tomcat` to use port 80. (By default, only user `root` can use this port, and other ports with numbers before 2024). For instructions, see e.g. https://www.baeldung.com/tomcat-change-port

## MacOS notes

If your server runs on MacOS, rather than Linux, then it's likely that your Tomcat has been installed using Homebrew, and lives somewhere like /usr/local/opt/tomcat/libexec/ rather than /opt/tomcat. In this case you may need to either change the line in build.xml that refers to the tomcat library directory, or, for compatibility's sake, provide a few symlinks:

    	mkdir /opt
    	cd /opt
    	ln -s /usr/local/opt/tomcat/libexec tomcat