Tuesday, 31 December 2024

How to Install Tomcat on Ubuntu 24.04 LTS

 

How to Install Tomcat on Ubuntu 24.04 LTS

In This Article We Will Learn How to  install  Tomcat on Ubuntu 24.04 LTS.

Tomcat is a free and open-source Java Servlet container, a software application that executes Java Server Pages (JSP) and Java servlets. It is widely used for deploying Java-based web applications. We will walk you through the process step-by-step, including installing Java, creating a dedicated Tomcat user, downloading and extracting the Tomcat archive, configuring systemd, and verifying the installation.

Table of Contents

Prerequisites

  • AWS Account with Ubuntu 24.04 LTS EC2 Instance.
  • Java Development Kit (JDK) installed.

Step #1:Install Java on Ubuntu 24.04 LTS

To install Tomcat you should have Java installed. Tomcat relies on Java to function.

First update the package repository.

sudo apt update
How to Install Tomcat on Ubuntu 24.04 LTS 1

Install Openjdk (Java).

sudo apt install openjdk-17-jdk
How to Install Tomcat on Ubuntu 24.04 LTS 2

Step #2:Create a Tomcat User

For security reasons, it’s best not to run Tomcat as the root user. Let’s create a dedicated user and group.

sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat

This command creates a user named “tomcat” with a home directory at /opt/tomcat. The /bin/false shell prevents login access.

How to Install Tomcat on Ubuntu 24.04 LTS 3

Step #3:Install Tomcat on Ubuntu 24.04 LTS

Now lets install Tomcat. First navigate to the Tomcat official website and download the latest version of Tomcat 10. You can use wget for this purpose.

sudo wget https://www-eu.apache.org/dist/tomcat/tomcat-10/v10.1.24/bin/apache-tomcat-10.1.24.tar.gz -P /tmp
How to Install Tomcat on Ubuntu 24.04 LTS 4

extract it to the /opt/tomcat directory.

sudo tar -xvf /tmp/apache-tomcat-10.1.24.tar.gz -C /opt/tomcat
How to Install Tomcat on Ubuntu 24.04 LTS 5

Step #4:Update Permissions of Tomcat

Change ownership of the Tomcat directory to the tomcat user and group:

sudo chown -R tomcat:tomcat /opt/tomcat
How to Install Tomcat on Ubuntu 24.04 LTS 6

Step #5:Configure Tomcat as a Service

A systemd unit file tells systemd how to manage a service. Create a file named tomcat.service under the /etc/systemd/system directory using a text editor.

navigate to the /etc/systemd/system.

cd /etc/systemd/system
How to Install Tomcat on Ubuntu 24.04 LTS 7

Create a tomcat.service file using nano command.

sudo nano tomcat.service
How to Install Tomcat on Ubuntu 24.04 LTS 8

add the following content into it.

[Unit]
Description=Tomcat Server
After=network.target

[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64"
WorkingDirectory=/opt/tomcat/apache-tomcat-10.1.24
ExecStart=/opt/tomcat/apache-tomcat-10.1.24/bin/startup.sh

[Install]
WantedBy=multi-user.target
How to Install Tomcat on Ubuntu 24.04 LTS 9

Save and close the file.

Adjust the JAVA_HOME environment variable path based on your OpenJDK installation location.

Step #6:Reload systemd and Start Tomcat

Reload the systemd daemon to apply the changes.

sudo systemctl daemon-reload
How to Install Tomcat on Ubuntu 24.04 LTS 10

Start the Tomcat service.

sudo systemctl start tomcat
How to Install Tomcat on Ubuntu 24.04 LTS 11

Enable Tomcat to start on boot.

sudo systemctl enable tomcat
How to Install Tomcat on Ubuntu 24.04 LTS 12

Now you can verify if your service is running properly or not by running your public ip address with port 8080 which is default port for Tomcat in url. You should see the Tomcat welcome page if everything is set up correctly.

How to Install Tomcat on Ubuntu 24.04 LTS 13

Conclusion:

In conclusion, installing Apache Tomcat on Ubuntu 24.04 is a straightforward process that involves updating your system, installing Java, creating a Tomcat user, downloading and configuring Tomcat, setting up a systemd service. By following these steps, you can easily install Tomcat and set up a robust environment for deploying Java-based web applications on your server.


SOURCE

Tuesday, 5 November 2024

Fedora linux creating sudo user

 

Step 1: Create a Regular Standard User

The first order of business is to create a standard login user. So, right off the bat, log into your server and run the `adduser` command with the username as the command-line argument. Here, we are creating a login user called ‘jumpcloud’.

# adduser jumpcloud

Next, assign a password to the user using the `passwd` command as shown. Be sure to provide a strong password and confirm it.

# passwd jumpcloud

In the end, you’ll get a confirmation indicating that the operation was successful.

code

By default, once a user is created, they are placed in a primary group named after the username. In this case, the user jumpcloud is placed in a group called jumpcloud. To confirm this, run the groups command.

# groups jumpcloud

code

Now we are going to check if the regular user we just created can run commands with elevated privileges. To verify this, we will switch to the user.

 # su – jumpcloud

Next, we will try to upgrade system packages to their latest versions.

$ sudo dnf update

Upon providing the password, you will get a notification that the user is not in the sudoers file. This implies that the user does not have elevated privileges to run administrative or root commands.

code

The next step is to grant sudo privileges to the user. Note that there should be a limited set of privileges granted and no alias commands allowed. 

Step 2: Add the Regular User to the Sudoers Group

To grant sudo privileges to the user, we will add the user to a secondary group called wheel. This is a special user group in Arch and Red Hat-based systems that provides administrative access to a regular user in order to masquerade as the root user.

To add the user to the group, run the usermod command as shown.

 # usermod -aG wheel jumpcloud

Or

 # usermod -a -G wheel jumpcloud

The -a option appends the user to the group, while the -G option specifies the group, in this case, wheel.Now run the command to confirm that the user is now a member of the group.

# groups jumpcloud

code

In addition, you can view the user accounts that belong to the wheel group as follows.

# cat /etc/group | grep wheel

code

Step 3: Test the Sudo User

It’s now time to put our sudo user to the test and verify its ability to run administrative privileges which are a preserve of the root user.

So, let’s now switch to the user.

# su – jumpcloud

Once again, we will attempt to upgrade all the packages to their latest versions as shown.

$ sudo dnf update

This time around, the command executes without any issues. This is confirmation that we have successfully added the user to the sudo group and can execute elevated privileges.

code

Step 4: How to Remove a Sudo User (Optional)

If you no longer need a sudo user on your server, you can easily remove them using the gpasswd command-line utility. This is a tool for managing entries in the /etc/shadow and /etc/groups files.

To remove a sudo user, invoke the gpasswd utility as shown to remove the user from the “wheel” group.

$ gpasswd -d jumpcloud wheel

code

From the removal of the user from the “wheel” group, the user regains its initial primary group which is the default group that the user belongs to during creation.

Sunday, 16 June 2024

Create a systemd service script for running Gunicorn to serve your application

 


To create or edit a gunicorn.service file in Linux for running a Flask application, you need to create a systemd service unit file. This service unit file will define how Gunicorn should run your Flask application as a service. Here's a step-by-step guide:

Create or Edit the Gunicorn Service File:

Open a terminal on your Linux system and use a text editor to create or edit the gunicorn.service file. You can use editors like nano or vi:

   sudo nano /etc/systemd/system/gunicorn.service

Or with vi:

   sudo vi /etc/systemd/system/gunicorn.service

To create a systemd service script for running Gunicorn to serve your application, you'll need to create a file named gunicorn.service with the following contents and place it in the appropriate directory on your server:

[Unit]
Description=Gunicorn instance to serve application
After=network.target

[Service]
User=your_username
Group=your_groupname
WorkingDirectory=/path/to/your/app
Environment="PATH=/path/to/venv/bin"
ExecStart=/path/to/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:5003 web_dynamic.2-hbnb:app
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Replace the placeholders with your actual values:

  • your_username: Your username on the system.
  • your_groupname: Your primary group name on the system.
  • /path/to/your/app: The absolute path to your application's root directory.
  • /path/to/venv/bin: The absolute path to your virtual environment's bin directory.
  • web_dynamic.2-hbnb:app: The Python import path to your Gunicorn app object.

Username and Primary Group Name on a Unix-like system

To find your username and primary group name on a Unix-like system, you can use the id command. Open a terminal and type the following commands:

To get your username:

   id -un

To get your primary group name:

   id -gn

These commands will display your username and primary group name respectively. You can then use these values to replace the placeholders in the gunicorn.service file.

After creating the gunicorn.service file, you need to place it in the appropriate directory for systemd unit files. Typically, this directory is /etc/systemd/system/.

Then, follow these steps:

Reload the systemd manager configuration to make it aware of the new service file:

   sudo systemctl daemon-reload

Enable the service to start on boot:

   sudo systemctl enable gunicorn

Start the service:

   sudo systemctl start gunicorn

Verify that the service is running without errors:

   sudo systemctl status gunicorn

You can also restart, stop, or check the logs of the service using systemd commands:

   sudo systemctl restart gunicorn
   sudo systemctl stop gunicorn
   journalctl -u gunicorn

Finally, test your application to ensure it's serving content as expected using curl commands similar to what you mentioned in your instructions.

Remember that the gunicorn.service script must be tailored to your specific environment and application setup. Make sure to adjust paths, usernames, and other parameters accordingly.

Also, note that systemd services require administrative privileges to manage. Be sure to use sudo as needed while performing these steps.

 

SOURCE