Category Archives: Debian

How to Install Tomcat 10 on Debian 11

How to Install Tomcat 10 on Debian 11

Apache Tomcat is an open-source Java HTTP web server developed by the Apache Software Foundation. Tomcat helps to deploy the Java Servlet and the JavaServer Pages and serves them like an HTTP web server.

In this tutorial, we will show you the complete steps to install Tomcat 10 on Debian 11.

Update Operating System

Update your Debian 11 operating system to make sure all existing packages are up to date:

$ sudo apt update && sudo apt upgrade -y

Install Java 17 (OpenJDK 17) on Debian 11

Java 17 packages are available on Debian 11 repositories and you can install it with the following command:

$ sudo apt install openjdk-17-jdk

Check Java version after installation:

$ java -version
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-Debian-1deb11u2)
OpenJDK 64-Bit Server VM (build 17.0.1+12-Debian-1deb11u2, mixed mode, sharing)

Create Tomcat User

It is recommended to have tomcat user for tomcat services.

To create a new account, type:

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

The above command will create a user and group with the name “tomcat” in your system.

Download and Install Tomcat

You can download the Tomcat 10 directly from the official webpage of the Tomcat, or use the wget command:

$ sudo wget https://downloads.apache.org/tomcat/tomcat-10/v10.0.16/bin/apache-tomcat-10.0.16.tar.gz

Extract the downloaded file to /opt/tomcat directory using tar command:

$ sudo tar xpvf apache-tomcat-10.0.16.tar.gz -C /opt/tomcat --strip-components=1

Set the correct permissions on files and directories:

$ sudo chown tomcat:tomcat /opt/tomcat/ -R
$ sudo chmod u+x /opt/tomcat/bin -R

Create a Systemd service file

By default, we won’t have a Systemd unit file for Tomcatto run it in the background and to easily stop, start and enable its services.

Create systemd unit for tomcat to start / stop and restart service.

nano /etc/systemd/system/tomcat.service

Paste following lines:

[Unit]
Description=Tomcat
After=syslog.target network.target
[Service]
Type=forking

User=tomcat
Group=tomcat

Environment=JAVA_HOME=/usr/lib/jvm/java-1.17.0-openjdk-amd64
Environment='JAVA_OPTS=-Djava.awt.headless=true'

Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid

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

[Install]
WantedBy=multi-user.target 

Reload system daemon and start Tomcat Service:

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

To confirm everything is working normally, check the status of service:

systemctl status tomcat

Output:

 tomcat.service - Tomcat
Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: enabled)
Active: active (running)
Main PID: 5605 (java)
Tasks: 37 (limit: 2301)
Memory: 77.1M
CPU: 5.700s
CGroup: /system.slice/tomcat.service
└─5605 /usr/lib/jvm/java-1.17.0-openjdk-amd64/bin/java -Djava.util.logging.config.fil>

Note: In case firewall is enable and running on your Debian 11 system then allow 8080 tcp port:

$ sudo ufw allow 8080/tcp

Now open your web browser and type https://yourIP:8080. You will see the default tomcat page.

Apache Tomcat

Add Roles and Admin username and password

To do it, you need to edit the configuration file /opt/tomcat/conf/tomcat-users.xml. You can edit it with nano by executing the following command:

$ sudo nano /opt/tomcat/conf/tomcat-users.xml

Add following lines before context </tomcat-users>:

<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="manager"/>
<role rolename="manager-gui"/>

<user username="linuxtuto" password="Pa$$word" roles="admin,admin-gui,manager,manager-gui"/>

save and exit the file.

Allow Remote Access of Tomcat

By default, you won’t be able to access your installed Tomcat Manager sections (web interface) outside the local system.

In case you want to access tomcat applications from outside then edit the context.xml file for manager & host-manager and comment out the remote access section:

$ sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
........
<!--      <Valve className="org.apache.catalina.valves.RemoteAddrValve"
                 allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
  -->
........
$ sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
........
<!--      <Valve className="org.apache.catalina.valves.RemoteAddrValve"
                 allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
  -->
........

Restart Tomcat service:

$ sudo systemctl restart tomcat

To Access Tomcat Web Manager page type http://yourIP:8080/manager/html

It will prompt for username and password, Use username and password that you specify in the ‘/opt/tomcat/conf/tomcat-users.xml’ file:

Tomcat Web Application Manager

To access Tomcat Host Manager Web Page page type http://yourIP:8080/host-manager/html

It will prompt for username and password, Use username and password that you specify in the ‘/opt/tomcat/conf/tomcat-users.xml’ file:

Tomcat Virtual Host Manager

Comments and Conclusion

That’s it. You have successfully installed Tomcat 10 on Debian 11. If you have any questions please leave a comment below.

How to Install MariaDB 10.7 on Debian 11

MariaDB is an open-source relational database management system developed by the developers of MySQL as an enhanced drop-in replacement to the MySQL server. MariaDB is focused on reliability, stability, security, and performance.

In this tutorial, we will show you the complete steps to install MariaDB 10.7 on Debian 11 from an APT repository.

Update Operating System

Update your Debian 11 operating system to make sure all existing packages are up to date:

$ sudo apt update && sudo apt upgrade -y

Install Dependencies

Install the required packages for the installation of MariaDB 10.7 with the following command:

$ sudo apt install curl software-properties-common dirmngr gnupg2

Add MariaDB 10.7 Repository

MariaDB 10.5 is the currently available version on the default Debian 11 repositories. To be able to install MariaDB 10.7 on Debian 11 you need to add the MariaDB 10.7 APT repository:

$ curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash -s -- --mariadb-server-version=10.7 --skip-maxscale --skip-tools

Install MariaDB 10.7

Once the repository has been added to the system, You are now ready to install MariaDB 10.7 with the following command:

$ sudo apt install mariadb-server mariadb-client

You can verify the installed version of MariaDB with the following command:

$ mariadb --version

Output:

mariadb Ver 15.1 Distrib 10.7.1-MariaDB, for debian-linux-gnu (x86_64) using readline EditLine wrapper

Start the database server daemon, and also enable it to start automatically at the next boot with the following commands:

$ systemctl start mariadb
$ systemctl enable mariadb

Check the status of the service:

$ systemctl status mariadb

Example output:

$  mariadb.service - MariaDB 10.7.1 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf
Active: active (running) 
Docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
 Process: 3274 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
Process: 3276 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 3278 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`cd /usr/bin/..; /usr/bin/galera_recovery`; [ $? -eq 0 ] && systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, stat>
Process: 3340 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 3342 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
Main PID: 3326 (mariadbd)
Status: "Taking your SQL requests now..."
Tasks: 13 (limit: 2301)
Memory: 68.1M
CPU: 345ms
CGroup: /system.slice/mariadb.service
└─3326 /usr/sbin/mariadbd

Secure MariaDB

Run mariadb-secure-installation script which helps you secure your MariaDB database server:

$ sudo mariadb-secure-installation

You can set a root password for MariaDB along with removing empty databases, restricting remote access except for localhost, removing anonymous users, and more:

Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

MariaDB Create Database

We need to login to MariaDB with the following command:

$ sudo mysql -u root -p

Note: Use -p if you have set the root password.

To create a database in MariaDB you need to run the following command:

MariaDB [(none)]> CREATE DATABASE your_db;

View the new database:

SHOW DATABASES;

To create a new MariaDB user, run the following command:

MariaDB [(none)]> CREATE USER 'your_user'@localhost IDENTIFIED BY 'password';

You should replace password with a secure password.

View the new user:

SELECT User FROM mysql.user;

The newly created user does not have privileges to manage databases.

Create a new user (only with local access) and grant privileges to this user on the database:

MariaDB [(none)]> GRANT ALL PRIVILEGES ON your_db.* TO 'your_user'@'localhost' IDENTIFIED BY 'password';

Create a new user (with remote access) and grant privileges to this user on the database:

MariaDB [(none)]> GRANT ALL PRIVILEGES ON your_db.* TO 'your_user'@'%' IDENTIFIED BY 'password';

Flush the privileges:

MariaDB [(none)]> FLUSH PRIVILEGES;

Exit the MariaDB shell:

MariaDB [(none)]> exit

Reset the MariaDB Root Password

If you forget your root MariaDB password, it can be reset.

Stop the MariaDB server:

$ sudo systemctl stop mariadb

Start the database without loading the grant tables or enabling networking:

$ sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"

Restart MariaDB:

$ sudo systemctl start mariadb

Now you can connect to the database as the root user,:

$ sudo mysql -u root

Use the following commands to reset root’s password. Replace password with a strong password:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> UPDATE mysql.user SET password = PASSWORD('password') WHERE user = 'root';

Update the authentication methods for the root password:

MariaDB [(none)]> UPDATE mysql.user SET authentication_string = '' WHERE user = 'root';
MariaDB [(none)]> UPDATE mysql.user SET plugin = '' WHERE user = 'root';
exit;

Revert the environment settings to allow the database to start with grant tables and networking:

$ sudo systemctl unset-environment MYSQLD_OPTS

Then restart MariaDB:

$ sudo systemctl start mariadb

You should now be able to log into the database with your new root password:

$ sudo mysql -u root -p

Comments and Conclusion

That’s it. You have successfully installed MariaDB 10.7 on Debian 11. Also, In the tutorial, you have learned how to create database and reset your MariaDB root password.

For additional help or useful information, we recommend you to check the official MariaDB documentation.

If you have any questions please leave a comment below.

How to Install PHP 8.1 on Debian 11

“HyperText Processor”, commonly known as PHP, is the open-source programming language used for Web application development. It is a scripting language, mostly used for the front end with HTML. The latest PHP 8.1 version is officially released on November 25th, 2021.

In this tutorial, we will show you how to install PHP 8.1 on a Debian 11 system.

Update Operating System

Update your Debian 11 operating system to make sure all existing packages are up to date:

$ sudo apt update && sudo apt upgrade -y

Add PHP Repository

By default, PHP 8.1 is not included in the Debian 11 default repository. So you will need to add the DEB.SURY.ORG repository to APT.

First, install the required packages using the following command:

$ sudo apt-get install ca-certificates apt-transport-https software-properties-common -y

Once all the packages are installed, add a Sury repository to APT using the following command:

$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/sury-php.list

Next, download and add the GPG key with the following command:

$ wget -qO - https://packages.sury.org/php/apt.gpg | apt-key add -

Once you are done, update the repository with the following command:

$ apt-get update -y

Install PHP 8.1

Now, you can install the PHP 8.1 using the following command:

$ sudo apt-get install php8.1

Install PHP 8.1

Once the PHP is installed, you can check the PHP version on your system with the following command:

$ php8.1 --version

Output:

$ php8.1 --version
PHP 8.1.1 (cli) (built: Dec 20 2021 21:35:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies

Install PHP 8.1 FPM for Nginx

For the Nginx web server, you need to install the FPM service, you can install it using the following command:

$ sudo apt install php8.1-fpm

Once the installation has been completed, you can confirm that the PHP-FPM service has been installed correctly with the following command:

$ systemctl status php8.1-fpm

Output:

 php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
Active: active (running)
Docs: man:php-fpm8.1(8)
Process: 43762 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=exited, status=0>
Main PID: 43759 (php-fpm8.1)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 2303)
Memory: 8.9M
CPU: 194ms
CGroup: /system.slice/php8.1-fpm.service
├─43759 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
├─43760 php-fpm: pool www
└─43761 php-fpm: pool www

Install PHP Extension

Installing PHP extensions are simple with the below-mentioned syntax:

$ sudo apt install php8.1-[extension]

Replace [extension] with the extension you want to install, if you want to add multiple extensions then include them in braces:

$ sudo apt install php8.1-mysql php8.1-imap php8.1-ldap php8.1-xml php8.1-fpm php8.1-curl php8.1-mbstring php8.1-zip

To check loaded PHP modules use the command:

$ php8.1 -m

Example Output:


[PHP Modules]
..............
imap
json
ldap
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
..............

[Zend Modules]
Zend OPcache

Running PHP 8.1 with Other Versions

Instead of removing old PHP versions, it is also possible to run multiple PHP versions side-by-side.

Multiple PHP versions

The update-alternatives command provides an easy way to switch between PHP versions for PHP CLI.

$ sudo update-alternatives --config php

This brings up a prompt to interactively select the alternative PHP binary path that php points to.

There are 2 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.1   81        auto mode
  1            /usr/bin/php7.4   74        manual mode
  2            /usr/bin/php8.1   81        manual mode

To set the path without the interactive prompt:

$ update-alternatives --set php /usr/bin/php7.4

Comments and Conclusion

In the tutorial, you have learned how to install PHP 8.1 on Debian 11.

If you have any questions please leave a comment below.