Turning a laptop into a Java application server in 10 steps

This is just my own proposition of a list of things needed to be done to turn a laptop into a Java application server based on Apache Tomcat under control of a Linux operating system, optionally using PostgreSQL as a database.

  1. Download and install Long-Term-Support (LTS) version of some Linux distribution, preferably a server variant. Example: Ubuntu Server 16.04 LTS. When selecting packages/services mark following (these options are available on Ubuntu Server installer):
    1. Open SSH server
    2. PostgreSQL – if the server is going to run a SQL database as well
  2. Configure the system so closing the laptop lid will not suspend the system.
    1. Edit the file /etc/systemd/logind.conf (sudo nano /etc/systemd/logind.conf) by adding line:
      HandleLidSwitch=ignore
    2. Restart the systemd daemon:
      sudo service systemd-logind restart
  3. Install a Server JRE (Apache Tomcat no longer needs JDK). Example: server-jre-8u101-linux-x64.tar.gz from Oracle Java SE download page. If installing manually it’s a common approach to put it into /opt directory. Example: /opt/jdk1.8.0_101
    1. You can use tar xf file.tar.gz command to extract the package
    2. Then move the whole extracted JRE directory to /opt
    3. Then change ownership of the JRE directory, example:
      sudo chown -R root.root /opt/jdk1.8.0_101
  4. Install Apache Tomcat. If installing manually I propose to put it into /home/tomcat directory. Thanks to this our Java applications will be stored on /home disk partition.
  5. Create a system user “tomcat” and configure Apache Tomcat to be run as a system service under this user. I followed this guide more or less: How To Install Apache Tomcat 8 on Ubuntu 16.04. Most important steps:
    1. Create a new user “tomcat” with commands:
      sudo groupadd tomcat
      sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
    2. Change directory and file permissions such that user “tomcat” has read access to all under /home/tomcat and has write access only to following subdirectories: conf, logs, temp, webapps, work.
    3. Create a system service configuration file /etc/systemd/system/tomcat.service (for example by command: sudo nano /etc/systemd/system/tomcat.service) and put the below into the file:
      [Unit]
      Description=Apache Tomcat 8.5
      After=network.target
      
      [Service]
      Type=forking
      
      Environment=JAVA_HOME=/opt/jdk1.8.0_101
      Environment=CATALINA_PID=/home/tomcat/temp/tomcat.pid
      Environment=CATALINA_HOME=/home/tomcat
      Environment=CATALINA_BASE=/home/tomcat
      
      ExecStart=/home/tomcat/bin/startup.sh
      ExecStop=/home/tomcat/bin/shutdown.sh
      
      User=tomcat
      Group=tomcat
      RestartSec=10
      Restart=always
      
      [Install]
      WantedBy=multi-user.target
    4. Reload system services configuration, run and enable the service with below commands – thanks to this Apache Tomcat will be automatically started with the OS:
      sudo systemctl daemon-reload
      sudo systemctl start tomcat
      sudo systemctl enable tomcat
    5. Check Tomcat service status with command:
      sudo systemctl status tomcat
      

      It should present similar information:

         Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
         Active: active (running) since sob 2016-09-24 13:45:44 CEST; 1h 17min ago
        Process: 1063 ExecStart=/home/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
       Main PID: 1195 (java)
          Tasks: 26
         Memory: 130.4M
            CPU: 16.798s
         CGroup: /system.slice/tomcat.service
                 └─1195 /opt/jdk1.8.0_102/bin/java -Djava.util.logging.config.file=/home/tomcat/conf/logging.properties -
  6. Configure an account to access Apache Tomcat console by editing the file /home/tomcat/conf/tomcat-users.xml (sudo nano /home/tomcat/conf/tomcat-users.xml) and adding following entries:
      <role rolename="manager-gui"/>
      <role rolename="admin-gui"/>
      <user username="you-user" password="your-password" roles="manager-gui,admin-gui"/>
  7. Configure PostgreSQL:
    1. Run the below command to stop PostgreSQL:
      sudo systemctl stop postgresql
    2. Change the default location of data held by PostgreSQL and it’s listening IP address by editing postgresql.conf file (in case of PostgreSQL 9.5 which is part of Ubuntu Server 16.04: sudo nano /etc/postgresql/9.5/main/postgresql.conf):
      1. data_directory variable should point to a directory of your choice, like /home/postgresql
      2. listen_addresses variable should be set to the value '*'
    3. Allow remote TCP connections to be accepted by editing pg_hba.conf file (sudo nano /etc/postgresql/9.5/main/pg_hba.conf):
      1. Modify line starting with “host”. To allow any connection it can be set to:
        host    all    all    0.0.0.0/0    md5
    4. Create the new data directory and change ownership to PostgreSQL user:
      sudo mkdir /home/postgresql
      sudo chown postgres.postgres /home/postgresql
    5. PostgreSQL is very sensitive in case of access rights to it’s data directory. I found it started to working after no access for group and others was set up:
      drwx------
    6. Run below commands to run & initialize PostgreSQL in the new directory and check its status (all statues should be “active”):
      sudo systemctl start postgresql
      sudo -u postgres /usr/lib/postgresql/9.5/bin/initdb -D /home/postgresql
      sudo systemctl status postgresql
      sudo systemctl -a|grep postgresql
  8. Create database, tables and user. I assume that you have SQL script for creation of tables and user. The second command below is based on the Running SQL scripts with psql article. The SQL script file must be accessible by “postgres” user.
    sudo -u postgres createdb your-db-name
    sudo -u postgres PGOPTIONS='--client-min-messages=warning' psql -X -q -v ON_ERROR_STOP=1 -d your-db-name -f path-to-sql-script
  9. Deploy your Java application WAR (assuming it has properly configured database connection inside the WAR). You can use Apache Tomcat console for this.
  10. Additional: check laptop’s battery and power status with a command-line tool so it can be done remotely later via ssh.
    1. Command for installation:
      sudo apt-get install acpi
    2. Command for checking:
      acpi -V

Now close the laptop lid and enjoy having the compact and silent server running your application!

Advertisements

About krzysztoftomaszewski

I've got M.Sc. in software engineering. I graduated in 2005 at Institute of Computer Science, Warsaw University of Technology, Faculty of Electronics and Information Technology. I'm working on computer software design and engineering continuously since 2004.
This entry was posted in database, Java, Linux, Tomcat. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s