Friday, January 10, 2020

Install Java on Ubuntu

Java is a programming language used to build Android apps and real life web applications like Gmail and Google Docs. This ubiquitous language can be installed onto an Ubuntu server and its what we’ll be teaching in this tutorial today.

Pre-flight

  • Open the terminal and log in as root. If you are logged in as another user, you will need to add sudo before each command.
  • Working on a Linux Ubuntu 18.04 server
  • No installations of previous Java versions

Install Java

Step 1: Updating your system

apt-get update && apt-get upgrade

Step 2: Install the Repo

First, install the repository, its provided by a third party and you’ll need to press enter to continue downloading. The Oracle Java (JDK) Installer automatically downloads and installs Oracle JDK8.
add-apt-repository ppa:webupd8team/java
Update your system once more
apt-get update

Step 3: Install Java 8

Using the command below will kickstart the Java Installer which will assist in our task. Press Y when prompted to continue.
sudo apt install openjdk-8-jdk

Step 4: Verify the Installation of Java 8

java -version
Example Output:
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

Set Java’s Home Environment

You may be looking to set Java’s home environment once we’ve installed it onto our server. To do that, lets first, let’s find Java’s path.
update-alternatives --config java
There is 1 choice for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 auto mode
* 1 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode
Take the highlighted path and add the variable into your /etc/environment file. Add the JAVA_HOME path after any line that is already present.
vim /etc/environment
Once you are in the file type `i` to insert the needed line:
JAVA_HOME="/usr/lib/jvm/java-8-oracle/jre/bin/java"
Save and exit the file by typing `ESC` followed by `:wq` and press `Enter`. Afterward, use the source command to have the system recognize the changes in your file.
source /etc/environment
You’ll know the variable has been set correctly by calling the variable.
echo $JAVA_HOME
Example Output:
root@1804:~$ echo $JAVA_HOME
/usr/lib/jvm/java-8-oracle/jre/bin/java

Reference:




Thursday, January 9, 2020

How to Install, Start, Stop & Restart PostgreSQL Server on Windows, Linux, MacOS / OSX


In this post, we are going to figure out how to start, stop, and restart a PostgreSQL server on macOS, Linux, and Windows.
1. On macOS
If you installed PostgreSQL via Homebrew:
  • To start manually:
pg_ctl -D /usr/local/var/postgres start
  • To stop manually:
pg_ctl -D /usr/local/var/postgres stop
  • To start PostgreSQL server now and relaunch at login:
brew services start postgresql
  • And stop PostgreSQL:
brew services stop postgresql

If you want a hassle-free way to manage the local PostgreSQL database servers, use DBngin. It’s just one click to start, another click to turn off. No dependencies, no command line required, multiple drivers, multiple versions and multiple ports. And it’s free.

2. On Windows
Download and install PostgreSQL with Installer or Portable.
First, you need to find the PostgreSQL database directory, it can be something like C:\Program Files\PostgreSQL\10.4\data. Then open Command Prompt and execute this command:
pg_ctl -D "C:\Program Files\PostgreSQL\9.6\data" start
  • To stop the server
pg_ctl -D "C:\Program Files\PostgreSQL\9.6\data" stop
  • To restart the server:
pg_ctl -D "C:\Program Files\PostgreSQL\9.6\data" restart
Another way:
  • Open Run Window by Winkey + R
  • Type services.msc
  • Search Postgres service based on version installed.
  • Click stop, start or restart the service option.

3. On Linux
Update and install PostgreSQL 10.4
sudo apt-get update
sudo apt-get install postgresql-10.4
By default, the postgres user has no password and can hence only connect if ran by the postgres system user. The following command will assign it:
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
sudo -u postgres psql -c "CREATE DATABASE testdb;"
  • Start the PostgreSQL server
sudo service postgresql start
  • Stop the PostgreSQL server:
sudo service postgresql stop

Need a good GUI tool for PostgreSQL on MacOS and Windows? TablePlus is a modern, native tool with an elegant GUI that allows you to simultaneously manage multiple databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server and more.


Reference :


Sunday, December 15, 2019

Install Java on CentOS

In this tutorial, we’ll be showing you how to install Oracle’s Java 8 programming language specifically onto a CentOS 7 server. This simple object-oriented language is used for many of the applications and websites you come across today.  Let’s jump right in!

Pre-flight

  1. Open the terminal and login as root.  If you are logged in as another user, you will need to add sudo before each command.
  2. Working on a Linux CentOS 7 server
  3. No installations of previous Java versions

Installing Java 8 on CentOS 7

Step 1: Update

As a matter of best practice we’ll update before installing any new programs:
yum -y update

Step 2: Install Java 8

yum install java-1.8.0-openjdk

Step 3:  Verify Java is Installed

java -version
Example Output:
java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

Set Java’s Home Environment

Step 1: Find Java’s Path

Let’s set the JAVA_HOME variable, using the following command will give us a path so we can set the variable.
update-alternatives --config java
You’ll see a prompt to “Enter to keep the current selection[+], or type selection number:”, if you had multiple Java version you could set the default here, but all we need is the path of Java so we can exit pressing enter.  The highlighted area is the path we will need to copy/paste into our .bash_profile file.
There is 1 choice for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 auto mode
* 1 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode

Step 2: Setting Java’s Path in Your Environment

After copying your Java’s path, open the .bash_profile with your text editor.
vim .bash_profile
Export your Java path into the .bash_profile by adding the following to the bottom of the file. (Your path may look different from mine, and it’s not important that they vary.)
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre/bin/java
Refresh the File:
source .bash_profile
When you use the JAVA_HOME variable you’ll now be able to see the path you set.
echo $JAVA_HOME
Example Output:
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre/bin/java

Reference:

Monday, November 4, 2019

Java execute shell commands on SSH Unix using JSch



Today we will look into the JSch example tutorial. We can use JSch for creating SSH connection in java. Earlier I wrote a program to connect to remote database on SSH server. Today, I am presenting a program that can be used to connect to the SSH-enabled server and execute shell commands. I am using JSch to connect to remote ssh server from java program.

JSch Example

You can download JSch jar from its official website. You can also get the JSch jars using below maven dependency.

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.53</version>
</dependency>
Below is a simple JSch example program.

import java.io.InputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;


public class JSchExampleSSHConnection {

 /**
  * JSch Example Tutorial
  * Java SSH Connection Program
  */
 public static void main(String[] args) {
     String host="ssh.journaldev.com";
     String user="sshuser";
     String password="sshpwd";
     String command1="ls -ltr";
     try{
      
      java.util.Properties config = new java.util.Properties(); 
      config.put("StrictHostKeyChecking", "no");
      JSch jsch = new JSch();
      Session session=jsch.getSession(user, host, 22);
      session.setPassword(password);
      session.setConfig(config);
      session.connect();
      System.out.println("Connected");
      
      Channel channel=session.openChannel("exec");
         ((ChannelExec)channel).setCommand(command1);
         channel.setInputStream(null);
         ((ChannelExec)channel).setErrStream(System.err);
         
         InputStream in=channel.getInputStream();
         channel.connect();
         byte[] tmp=new byte[1024];
         while(true){
           while(in.available()>0){
             int i=in.read(tmp, 0, 1024);
             if(i<0)break;
             System.out.print(new String(tmp, 0, i));
           }
           if(channel.isClosed()){
             System.out.println("exit-status: "+channel.getExitStatus());
             break;
           }
           try{Thread.sleep(1000);}catch(Exception ee){}
         }
         channel.disconnect();
         session.disconnect();
         System.out.println("DONE");
     }catch(Exception e){
      e.printStackTrace();
     }

 }

}
Let me know if you face any problem with the execution of the JSch example program. It’s pretty straight forward example of JSch to create SSH connection in java program.
You can download JSch jar file from its official website.

Reference :

Saturday, October 19, 2019

Create Valid SSL / HTTPS Certificate for Locally using mkcert


The web is moving to HTTPS, preventing network attackers from observing or injecting page contents. But HTTPS needs TLS certificates, and while deployment is increasingly a solved issue thanks to the ACME protocol and Let's Encrypt, development still mostly ends up happening over HTTP because no one can get an universally valid certificate for localhost.

This is a problem because more and more browser features are being made available only to secure origins, and testing with HTTP hides any mixed content issues that can break a production HTTPS website. Developing with HTTPS should be as easy as deploying with HTTPS.

That's what mkcert is for.



mkcert is a simple by design tool that hides all the arcane knowledge required to generate valid TLS certificates. It works for any hostname or IP, including localhost, because it only works for you.

Here's the twist: it doesn't generate self-signed certificates, but certificates signed by your own private CA, which your machine is automatically configured to trust when you run mkcert -install. So when your browser loads a certificate generated by your instance of mkcert, it will show up with a green lock!


Quickstart

1. Download mkcert at GitHub FiloSottile

2. Install mkcert
$ mkcert -install
Install the local CA in the system trust store.

3.1 Create Certificate
$ mkcert example.org
Generate "example.org.pem" and "example.org-key.pem".
$ mkcert example.com myapp.dev localhost 127.0.0.1 ::1
Generate "example.com+4.pem" and "example.com+4-key.pem".


3.2 Create Wildcard Ceritificate
$ mkcert '*.example.com'
Generate "_wildcard.example.com.pem" and "_wildcard.example.com-key.pem".


3.3 Create PKCS #12 Certificate (Advanced Options)
$ mkcert -pkcs12 example.com
Generate "example.com.p12" instead of a PEM file.


4. Uninstall Certificate
$ mkcert -uninstall
Uninstall the local CA (but do not delete it).


Reference:



Run SpringBoot as a Service on Linux


Please note that since Spring Boot 1.3.0.M1, you are able to build fully executable jars using Maven and Gradle.

For Maven, just include the following in your pom.xml:
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>
For Gradle add the following snippet to your build.gradle:
springBoot {
    executable = true
}
The fully executable jar contains an extra script at the front of the file, which allows you to just symlink your Spring Boot jar to init.d or use a systemd script.
init.d example:
$ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
This allows you to start, stop and restart your application like:
$/etc/init.d/yourapp start|stop|restart
Or use a systemd script:
[Unit]
Description=yourapp
After=syslog.target

[Service]
ExecStart=/var/yourapp/yourapp.jar
User=yourapp
WorkingDirectory=/var/yourapp
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target
More information at the following links:

Reference: https://stackoverflow.com/a/31654838/9278668