Sunday, October 5, 2014

Initial Server Setup for Linux Server

This was made on Ubuntu 14.04 LTS.

It's my personal list. I'll probably update this once in a while when I see a good practice for an initial server setup.

Your goals basically are:
1) to add a user and use it instead of the root
2) change the ssh port to add protection on random hackers attacking at the default ssh port

Ssh on the server:
$ ssh root@<server ip address>

Change your password.
$ passwd

Add a user.
$ adduser <sample user: bujo>

Add root priveleges to bujo via "sudo" command.
$ visudo
This opens up a file using the 'nano' editor.
Find the lines with the following notes

#User privilege specification
root ALL=(ALL:ALL) ALL

Add in 'bujo'. Copy the line with the 'root' and 'ALL...' value and paste it in the next line and just replace 'root' with 'bujo' on the copied line.

bujo ALL=(ALL:ALL) ALL

Save.

Next, configure your ssh.
$ vim /etc/ssh/sshd_config

Change ssh port.
Look at the line with:
Port 22

Just change 22 with any number between 1025 to 65536.

Next you can disable root login. Just change the value of the
PermitRootLogin yes
to 'no'.

But I don't recommend that. If you have a difficult password for the root user and it is securely stored, like using a key manager, for example keepassx, you can leave your server able to login root. That admin power may one day be needed and you might need it fast.

Now it's time to permit the user/s you defined. Add in to your ssh_config
AllowUsers bujo root

AllowUsers is a directive. 'bujo' and 'root' are the allowed users to login in your system.
Save.

Restart your ssh.
$ service ssh restart

You're done with the configs. Next TEST the config. :)
Remember, DO NOT EXIT FROM YOUR CURRENT TERMINAL SESSION.
Sometimes, you do mistakes in your config changes and you make your server out of reach to anyone even to yourself. For example, you have put "PermitRootLogin no" and added "AllowUsers" with no value after it, or misspelled your new user name. With this mistakes, you can only correct them with the current session you have in your server. So you don't log out of that current session until you finished testing your changes from another terminal.

Open a new terminal. Try logging in using your old 'root'. Remember, your ssh is now assigned to a new port in your server. So logging in will have to consider that.
$ ssh -p <new port assignment> root@<server ip>
Then exit and try to login again using 'bujo'. When there seems to be a problem, you can check and change the config, and restart sshd_config by the original terminal session you still have.

And when you are done logging in, you have now a solid setup to install your applications in your server. You can now leave the 'root' user behind and use it only when you need speed (not needing to do 'sudo') on very important server setups that is safe and you are comfortable doing.

Thursday, September 18, 2014

Palindrome Algorithm

This is my personal attempt to identify if a word (or series of words) is a palindrome.
Written in java, it's an optimal solution.

public class PalindromeTest {

    public static void main(String[] args) {

        for (String input : args) {

            if (isValid(input)) {
                int mid = input.length()/2;
                String firstHalf = input.substring(0, mid);
                String secondHalf = input.substring(getProperSecondHalfMid(input.length()), input.length());

                System.out.println(input + " is a palindrome: " + areStringsEqual(firstHalf, getReverseString(secondHalf)));
            } else {
                System.out.println("Sorry, input: " + input + ", is not valid!");
            }

        }

    }

    private static int getProperSecondHalfMid(int length) {
        return length%2 == 0 ? length/2: length/2 + 1;
    }

    private static String getReverseString(String secondHalf) {
        return (new StringBuffer(secondHalf).reverse().toString());
    }

    private static String areStringsEqual(String firstHalf, String secondHalf) {
        return Boolean.toString(firstHalf.equalsIgnoreCase(secondHalf));
    }

    private static boolean isValid(String input) {
        return !input.isEmpty();
    }

}



Friday, September 12, 2014

Sample Template on Starting a Single Java Project Using Gradle

Skip to the steps

You always want to program or create your own implementation of anything in your mind or anything out there.
So you always do a simple project.
This article reveals my template on how I always start my simple projects.

For some, creating one file and running it is enough for just experimenting a functionality, like
$ python Starter.py
$ php Starter.php
$ java Starter

That's it, one class and their done.

But for me, I like to keep my projects on industry standard even if they are just small or simple. I create a template structure before I start my implementation classes. Well you just don't know when you want to scale up any of your projects. Plus, you know how it is today with companies they throw words around like clean code, coverage, automation and cloud repos (like github). With these you must have by default structure, unit tests, source control manager (scm) and a build tool in your project. Java language has always been my strongest suit, so i'll create this starter demo project using Java. Also, gradle for my build tool and git for my scm.

Just before we start, I just want to address why I chose gradle and git.

For build tool, maven and gradle are, hands down, the most recommended. But after seeing their dependency declarations

Maven
---------













Gradle
--------




It's kinda obvious now right? Readability and maintainability, gradle it is. I never went back to Maven.

For SCM. Distributed repository is the way to go. Keeping them locally in your computer gives you a lot more power and more efficiency in execution of usual tasks like branching and managing your commits and merges. I never wanted to go back to CVS or SVN. I got caught up with the git bandwagon so it's what I stuck into as the scm for distributed repository. There is also that github mania. Maybe later on I will try others like Mercurial.

Creating a project from an advance IDE like Eclipse, Netbeans or Intellij is fine, but don't you want to know how it is done under the hood? Also, do you notice there are a lot of files added to your project upon creation? You can keep those auto generated files to a minimum by creating your project manually from scratch.

Okay... let's start.
Should just take about 10 mins for Mac or Linux, without the downloads.
I made this through Mac, but the principle should be similar to any Linux and Windows OS.

1) If you don't have them yet, install your tools: java, gradle, git
- for java, use JEM, the Java Environment Manager to install and manage your java versions: https://www.npmjs.org/package/jem
For windows, just download and install a version you like.
- for gradle, I used a package manager called homebrew
$ brew install gradle
- for git
$ brew install git

2) Create a standard folder structure.

$ mkdir gradle-starter-project
$ cd gradle-starter-project
$ mkdir -p src/main/java
$ mkdir -p src/test/java
$ mkdir -p src/main/resources
$ mkdir -p src/test/resources

3) Create 'build.gradle' file in your root project directory, it's your simplified pom.xml.
$ touch build.gradle

4) Add the java plugin on build.gradle
$ vim build.gradle
apply plugin: 'java'

Then save your file.
:wq

(This plugin is all you need to clean, execute your test classes, compile, download dependencies and bundle your project into a jar file with one simple command:
'$ gradle build')

5) Lastly, activate source control management for your project.
$ git init

This transforms your project directory into a git repository.

And voila, you're done!
This is how I start whatever single project I have, even the simple ones.

This is how your project folder structure should end up:













There, the template where I start my simple java projects.