Kamis, 31 Januari 2019

Deploy java API as service in linux CentOS 6


For sample i use uereka microservices

  1. Create user for running API

    # useradd api
    # passwd api

  2. login as api create directory eureka for put jar file, copy jar file to /home/api/eureka

    # mkdir eureka

  3. Create script wrapper for system V init in /home/api/eureka


    #!/bin/sh
    ### BEGIN INIT INFO
    # chkconfig: 345 98 01
    # description: Startup Script for Eureka API
    # Provides:          Eureka
    ### END INIT INFO

    SCRIPT="nohup java -jar -DSpring.profiles.active=cloud1 /home/api/eureka/eureka-service.jar"
    RUNAS=api

    PIDFILE=/home/api/eureka/eureka-api.pid
    LOGFILE=/home/api/eureka/eureka-api.log

    start() {
      if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then
        echo 'Eureka already running' >&2
        return 1
      fi
      cd /home/api/eureka
      echo 'Starting service… Eureka' >&2
      local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
      su -c "$CMD" $RUNAS > "$PIDFILE"
        
      # Try with this command line instead of above if not workable
      # su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
      # or
      # nohup java -jar -DSpring.profiles.active=cloud1 /home/api/eureka/eureka.jar &> /home/api/eureka/eureka-api.log & echo $! > $PIDFILE

      PID=$(cat $PIDFILE)
        if pgrep -u $RUNAS -f eureka-service.jar > /dev/null
        then
          echo "API Eureka is now running, the PID is $PID"
        else
          echo ''
          echo "Error! Could not start Eureka !"
        fi
    }
      
    stop() {
      if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
        echo 'Service Eureka not running' >&2
        return 1
      fi
      echo 'Stopping service…' >&2
      kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
      echo 'Service Eureka stopped' >&2
    }
      
    uninstall() {
      echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
      local SURE
      read SURE
      if [ "$SURE" = "yes" ]; then
        stop
        rm -f "$PIDFILE"
        echo "Notice: log file was not removed: $LOGFILE" >&2
        update-rc.d -f eureka remove
        rm -fv "$0"
      fi
    }
      
    status() {
        printf "%-50s" "Checking Eureka ..."
        if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
           PID=$(cat $PIDFILE)
                if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
                    printf "%s\n" "The process appears to be dead but pidfile still exists"
                else  
                    echo "Running, the PID is $PID"
                fi
        else
            printf "%s\n" "Service Eureka not running"
        fi
    }
      
      
    case "$1" in
      start)
        start
        ;;
      stop)
        stop
        ;;
      status)
        status
        ;;
      uninstall)
        uninstall
        ;;
      restart)
        stop
        start
        ;;
      *)
        echo "Usage: $0 {start|stop|status|restart|uninstall}"
    esac
      

  4. Enable service in system V and start eureka

    # sudo ln -s /home/api/eureka/eureka /etc/init.d/eureka
    # sudo chkconfig --add eureka
    # sudo chkconfig eureka on
    # sudo service eureka start

  5. Firewall

    # iptables -I INPUT -p tcp --destination-port 8761 -j ACCEPT
    # service iptables save

Rabu, 30 Januari 2019

Install Tomcat 8 on a CentOS 6


In the following tutorial you will learn how to install and set-up Apache Tomcat 8 on your CentOS 6

What is Tomcat?

Apache Tomcat (before known as Jakarta Tomcat) is an application server developed by the Apache Software Foundation that executes Java servlets and renders Web pages that include Java Server Page coding.


UPDATE THE SYSTEM


Make sure you are in a your CentOS 6 based Linux Server is up-to-date by running:

# yum update


INSTALL JAVA 8


Download the latest JAVA 8 from  here or use the following command to download JAVA JDK

# wget --no-cookies \
--no-check-certificate \
--header "Cookie: oraclelicense=accept-securebackup-cookie" \
"https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.rpm" \
-O /tmp/jdk-8u201-linux-x64.rpm

once the JAVA package has been downloaded, install it using rpm as follows:

# rpm -Uvh /tmp/jdk-8u201-linux-x64.rpm

CONFIGURE JAVA

configure the newly installed JAVA package using alternatives as in:

# alternatives --install /usr/bin/java java /usr/java/jdk1.8.0_201-amd64/jre/bin/java 20000
# alternatives --install /usr/bin/jar jar /usr/java/jdk1.8.0_201-amd64/bin/jar 20000
# alternatives --install /usr/bin/javac javac /usr/java/jdk1.8.0_201-amd64/bin/javac 20000
# alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.8.0_201-amd64/jre/bin/javaws 20000
# alternatives --set java /usr/java/jdk1.8.0_201-amd64/jre/bin/java
# alternatives --set javaws /usr/java/jdk1.8.0_201-amd64/jre/bin/javaws
# alternatives --set javac /usr/java/jdk1.8.0_201-amd64/bin/javac
# alternatives --set jar /usr/java/jdk1.8.0_201-amd64/bin/jar

check the JAVA version running on your system:

# java -version

INSTALL TOMCAT 8

Create a separate user which will run the Tomcat server:

# useradd tomcat8

Download the latest Tomcat 8 version from here or use the following command to download Tomcat

# wget https://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.37/bin/apache-tomcat-8.5.37.tar.gz -P /tmp

Extract the contents of the Tomcat archive to /opt using the following command:

# tar zxf /tmp/apache-tomcat-8.5.37.tar.gz -C /opt

make symbolic link

# ln -s /opt/apache-tomcat-8.5.37 /opt/tomcat8

change permission

# chown -hR tomcat8: /opt/tomcat8 /opt/apache-tomcat-8.5.37
# cd /opt/tomcat8/bin
# chmod +x *.sh


START THE TOMCAT 8 SERVICE

Create the following init script in /etc/init.d/tomcat8

#!/bin/sh
### BEGIN INIT INFO
# chkconfig: 345 84 16
# description: Tomcat Jakarta JSP Server
# Provides: Tomcat
### END INIT INFO

JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export JAVA_HOME

## IF NEEDED 
#JAVA_OPTS="-Dfile.encoding=UTF-8 \
#  -Dnet.sf.ehcache.skipUpdateCheck=true \
#  -XX:+UseConcMarkSweepGC \
#  -XX:+CMSClassUnloadingEnabled \
#  -XX:+UseParNewGC \
#  -XX:MaxPermSize=128m \
#  -Xms512m -Xmx512m"
# export JAVA_OPTS

PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/opt/tomcat8

STARTSCRIPT="/opt/tomcat8/bin/startup.sh"
STOPSCRIPT="/opt/tomcat8/bin/shutdown.sh"
RUNAS=tomcat8

PIDFILE=/opt/tomcat8/tomcat8.pid
LOGFILE=/opt/tomcat8/tomcat8.log

start() {

  TOMCATLIVE=$(pgrep -u tomcat8 -f bootstrap.jar | wc -l)
  if [ "$TOMCATLIVE" -eq 0 ]; then
    echo 'Starting Jakarta Tomact service …' >&2
    local CMD="$STARTSCRIPT &> \"$LOGFILE\" & echo \$!"
    su -l $RUNAS -c "$CMD" > "$PIDFILE"
  sleep 2
  PID=$(cat $PIDFILE)

    if pgrep -u $RUNAS -f bootstrap.jar > /dev/null
    then
      echo "Jakarta Tomcat is now running, the PID is $PID"
    else
      echo ''
      echo "Error! Could not start Jakarta Tomcat!"
    fi

  else
    echo 'Service Jakarta Tomcat all ready running' >&2
    return 1
  fi
  
}

stop() {

  TOMCATLIVE=$(pgrep -u tomcat8 -f bootstrap.jar | wc -l)
  if [ "$TOMCATLIVE" -eq 0 ]; then
    echo 'Service Jakarta Tomcat not running' >&2
    return 1
  fi
  echo 'Stopping Jakarta Tomcat service …' >&2
  su -l $RUNAS -c $STOPSCRIPT
  rm -f "$PIDFILE"
  echo 'Service Jakarta Tomcat stopped' >&2

}

uninstall() {
  echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
  local SURE
  read SURE
  if [ "$SURE" = "yes" ]; then
    stop
    rm -f "$PIDFILE"
    echo "Notice: log file was not removed: $LOGFILE" >&2
    update-rc.d -f tomcat8 remove
    rm -fv "$0"
  fi
}

status() {

TOMCATLIVE=$(pgrep -u tomcat8 -f bootstrap.jar | wc -l)
TOMCATPIDLIVE=$(pgrep -u tomcat8 -f bootstrap.jar )
if [ "$TOMCATLIVE" -eq 0 ]; then
  echo -n "Jakarta Tomcat Stopped"
  echo
else
  echo "Jakarta Tomcat Running, the PID is $TOMCATPIDLIVE "
fi

}


case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  uninstall)
    uninstall
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|uninstall}"
esac

make the script executable using chmod

# chmod +x /etc/init.d/tomcat8

Add the Tomcat 8 service to system startup:

# chkconfig --add tomcat8
# chkconfig tomcat8 on

Start the Tomcat 8 server using:

# service tomcat8 start

Configuring Firewall

# iptables -I INPUT -p tcp --destination-port 8080 -j ACCEPT
# service iptables save
# ip6tables -I INPUT -p tcp --destination-port 8080 -j ACCEPT
# service ip6tables save

Access your newly installed Tomcat at http://YOUR_IP:8080

Minggu, 27 Januari 2019

How to Disable SELinux on CentOS


By default in CentOS 7, SELinux is enabled and in enforcing mode.

It is recommended to keep SELinux in enforcing mode, but in some cases, you may need to set it to permissive mode or disable it completely.

In this tutorial, we will show you how to disable SELinux on CentOS 7 systems.


Disable SELinux

You can temporarily change the SELinux mode from targeted to permissive with the following command:

# setenforce 0

However, this change will be valid for the current runtime session only.

To permanently disable SELinux on your CentOS 7 system, follow the steps below:

Open the /etc/selinux/config file and set the SELINUX mod to disabled:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#       targeted - Targeted processes are protected,
#       mls - Multi Level Security protection.
SELINUXTYPE=targeted

Save the file and reboot your CentOS system with:

# shutdown -r now

Once the system boots up, verify the change with the sestatus command:

# sestatus

The output should look like this:

SELinux status:                 disabled

Conclusion

In this tutorial, you learned how to permanently disable SELinux on a CentOS 7 machine.
You may wish to visit the CentOS SELinux guide and learn more about the powerful features of SELinux.


Kamis, 17 Januari 2019

Quit Bash Shell Without Saving Bash History


Sometimes it is very useful to delete / remove Bash history partially or completely when log out. Here is my favourite methods howto log out / quit / exit Bash shell without saving Bash history.

Remove Only Current Session Bash History and Leave Older History Untouched

  1. Quit Bash Shell Without Saving History: Unset HISTFILE

  2. # unset HISTFILE && exit

  3. Quit Bash by Killing you Current Consolel Without Saving History: Kill Console

  4. # kill -9 $$

  5. Quit Bash Shell Without Saving History: Clear History Option

  6. #history -c && exit

  7. Set your Bash Shelll history to Zero(0):  Set HISTSIZE 0

  8. #HISTSIZE=0 && exit


Remove/Delete Bash History Completely

  1. Quit Bash Shell Without Saving History: Delete HISTFILE and Unset HISTFILE

  2. #rm -f $HISTFILE && unset HISTFILE && exit


If you want make these commands more permanent then these commands could be added on ~/.bash_logout file or used with aliases.

echo "alias nohistory='history -c && exit'" >> ~/.bash_logout


Run the remote linux graphics application locally


Overview

The X Window System (also known as X11, or just X) is a software package and network protocol that lets you interact locally,
using your personal computer's display, mouse, and keyboard, with the graphical user interface (GUI) of an application running on a remote networked computer.

Requirements

For X forwarding in SSH to work your personal computer must be running an X server program.
The X server program manages the interaction between the remote application (the X client) and your computer's graphics hardware and input devices.

Most Linux distributions have the X server installed,
but if your personal computer is running Windows, you will most likely need to install and run an X server application, for example:

  • Xming, download and install Xming. For X forwarding to work, you'll need to start Xming before connecting to the remote system with your SSH client (for example, PuTTY).

  • MobaXTerm, download and install MobaXTerm

but now we will only focus Xming

Additionally, your personal computer's SSH terminal application must have X11 forwarding enabled:

  • In Linux, the SSH terminal supports X forwarding by default.

  • if not you can edit the sshd_config file

vi /etc/ssh/sshd_config

change for the lines below:

X11Forwarding yes
X11UseLocalhost yes

Restart the ssh srever, if you have made changes:

service sshd restart

  • In PuTTY for Windows, you can enable X forwarding new or saved SSH sessions by selecting Enable X11 forwarding in the "PuTTY Configuration" window ( Connection  > SSH > X11).
Also, the remote computer's SSH application must be configured to accept X server connections.


Use SSH with X forwarding

Linux

To use SSH with X forwarding from your Linux personal computer to run an X client application installed on remote server:

  1. Open the SSH terminal client.

  2. On the command line, enter (replacing username with your username):

  3. ssh -Y username@host

    note
    The -Y option turns on trusted X forwarding. You should use it only when connecting to secure systems.

  4. Log in with your password

  5. To test if X forwarding is working, try running xclock; on the command line, enter:

    xclock

    If X forwarding is working, the xclock graphical clock will appear on your personal computer's desktop.

PuTTY for Windows

To use SSH with X forwarding in PuTTY for Windows:

  1. Launch your X server application (for example, Xming).

  2. Make sure your connection settings for the remote system have Enable X11 forwarding selected; in the "PuTTY Configuration" window, see Connection > SSH > X11

     

  3. Open an SSH session to the desired remote system:

  4. Log in normally with your username and password.

To test if X forwarding is working, try running xclock; on the command line, enter:

If X forwarding is working, the xclock graphical clock will appear on your personal computer's desktop.

 How to start X application from SSH [simple way]
 
A short command

ssh -X username@host xapplication

note
The -X  is Enable X11 Forwarding.