Create Service For Apache Tomcat

cd /etc/systemd/system
sudo vi tomcat.service 
then insert following lines - 


[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

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

#User=tomcat
#Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target




sudo systemctl daemon-reload
Start tomcat
sudo systemctl start tomcat.service

Stop tomcat
sudo systemctl stop tomcat.service

Restart tomcat
sudo systemctl restart tomcat.service

Enable service after restart automatic tomcat start
sudo systemctl enable tomcat.service


Install apache tomcat 9 in ubuntu 18.04

  • cd /tmp
  • curl -O https://downloads.apache.org/tomcat/tomcat-9/v9.0.37/bin/apache-tomcat-9.0.37.zip
  • sudo mkdir -p /opt/tomcat
  • unzip apache-tomcat-9.0.37.zip
  • rm -rf apache-tomcat-9.0.37.zip
  • mv apache-tomcat-9.0.37/* /opt/tomcat/
  • cd /opt/tomcat
  • sudo chmod -R 0777 /opt/tomcat
  • sudo update-java-alternatives -l
  • OUTPUT – java-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64

  • Then Create a system service
  • Click Here

Send email In java using smtp

//import jar file from https://mvnrepository.com/artifact/javax.mail/mail/1.4.7
package com.mail.api.controller;

import com.sun.mail.smtp.SMTPTransport;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail {
public static boolean sendMail(Email email){
EmailTemplate template = email.getEmailTemplate();
int SMTP_PORT = 465;
final String EMAIL_TO = "";
final String EMAIL_TO_CC = "";
final String EMAIL_TO_BCC = "";
final String EMAIL_SUBJECT = "";
final String EMAIL_TEXT = "";
Properties prop = System.getProperties();
//prop.put("mail.debug", "true");
prop.put("mail.smtp.host", SMTP_SERVER); //optional, defined in SMTPTransport
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.socketFactory.port",SMTP_PORT);
prop.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
prop.put("mail.smtp.socketFactory.fallback", "false");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.ssl","true");
prop.put("mail.smtp.port", SMTP_PORT); // default port 25

Session session = Session.getInstance(prop, null);
Message msg = new MimeMessage(session);

try {
msg.setFrom(new InternetAddress(EMAIL_FROM));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(EMAIL_TO, false));
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(EMAIL_TO_CC, false));
msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(EMAIL_TO_BCC, false));

msg.setSubject(EMAIL_SUBJECT);
msg.setContent(EMAIL_TEXT,"text/html");
msg.setSentDate(new Date());
// Get SMTPTransport
SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
t.connect(SMTP_SERVER, SMTP_USERNAME, SMTP_PASSWORD);
t.sendMessage(msg, msg.getAllRecipients());
System.out.println("Response: " + t.getLastServerResponse());
t.close();
return true;
} catch (MessagingException e) {
e.printStackTrace();
}
return false;
}
}

Solve phpmyadmin problem in ubuntu 18.04

1. sudo vi /usr/share/phpmyadmin/libraries/sql.lib.php
approx line 613
replace this code
((empty($analyzed_sql_results['select_expr'])) || (count($analyzed_sql_results['select_expr'] == 1) && ($analyzed_sql_results['select_expr'][0] == '*')))

with this code

((empty($analyzed_sql_results['select_expr'])) || (count($analyzed_sql_results['select_expr']) == 1) && ($analyzed_sql_results['select_expr'][0] == '*'))

2. sudo vi /usr/share/phpmyadmin/libraries/plugin_interface.lib.php
approx line 532
replace this code
$no_options = true;
if($options != null && count($options) > 0) {
foreach($options->getProperties() as $propertyMainGroup) {

with this code

$no_options = true;
if($options != null && count((array)$options) > 0) {
foreach($options->getProperties() as $propertyMainGroup) {

3. sudo systemctl restart apache2

How to Reset mysql root password without known old password For Centos and Fedora

1. service mysqld stop
2. mysqld_safe --skip-grant-tables &
3. if error comes then create mkdir -p /var/run/mysqld and then give permission chown mysql:mysql /var/run/mysqld
and then again run 2 step
4. mysql
5. UPDATE mysql.user SET authentication_string=PASSWORD("password") WHERE User='root';
6. FLUSH PRIVIlEGES;
7. quit
8. then stop mysqld_safe command
9. service mysqld start

How to Reset mysql root password without known old password For Debian and Ubuntu

1. service mysql stop
2. mysqld_safe --skip-grant-tables &
3. if error comes then create mkdir -p /var/run/mysqld and then give permission chown mysql:mysql /var/run/mysqld
and then again run 2 step
4. mysql
5. UPDATE mysql.user SET authentication_string=PASSWORD("password") WHERE User='root';
6. FLUSH PRIVIlEGES;
7. quit
8. then stop mysqld_safe command
9. service mysql start