Login to server using SSH key, without password

When login into server using SSH,  it’s more safe and easier using SSH key rather than password.

Before start, check my username on the computer
whoami
And use this username for easier setup

1. On remote server, switch to sudor

sudo su

2. Adding user and put password

adduser {user}

3. Switch to the created user

su {user}
cd ~/.ssh

4. Create key

ssh-keygen

5. Open ~/.ssh/id_rsa.pub from local computer and copy public key

ssh-rsa {some keys goes here}

6. Create authorized_keys file on remote server and paste public key

vi authorized_keys

7. Change Authorized key permission, make sure it has 600 permission

chmod 600 authorized_keys

8. Optionally, delete id_rsa, id_rsa.pub on remove server, it’s not used and not necessary to keep.

9. make login without password

sudo vi /etc/ssh/sshd_config

make sure PermitRootLogin line has like this:

PermitRootLogin without-password

10. Reload ssh

sudo service ssh restart

11. Try to login to remote server without password:

ssh {user}@{remove-server-ip}

12. Optionally, you can login without user name, just use ip address:
sudo su

vi /etc/sudoers.d/admin

Add like this

{user} ALL=(ALL) NOPASSWD:ALL

After change, give admin file permission to 400.

chmod 400 admin

13. If you want to allow login with password SSH or SFTP, change below.

sudo vi /etc/ssh/sshd_config

Then uncomment below

PasswordAuthentication yes

Delete git repository histories

One of my site git file became too big, and took long time to clone.
So I had to clean it.

1. Checkout

git checkout --orphan latest_branch

2. Add all the files

git add -A

3. Commit the changes

git commit -am "commit message"

4. Delete the branch

git branch -D master

5. Rename the current branch to master

git branch -m master

6. Finally, force update your repository

git push -f origin master

To be a good house keeper.

Upgrade PHP5 to PHP7

I have older server to upgrade to PHP7.

PHP7 has better performance and cleaner library.
But some of functions are depricated like ereg_replace, ereg and also mysql is not supported anymore. Instead, preg_replace, preg_match and mysqli are available.

So some of php programming may not work and need to be updated.

To upgrade, use below commands:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get install php7.0 libapache2-mod-php7.0
sudo a2dismod php5
sudo a2enmod php7.0
sudo apachectl restart

It’s quite simple, but I had to change those depricated functions.
Especially mysqli’s usage is slightly different than mysql.

To connect db:

$this->lid = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME);

Select DB:

mysqli_select_db($this->lid,DB_NAME)

Query:

$this->qid = mysqli_query($this->lid, $q);

Fetch Array:

$this->record = mysqli_fetch_array($this->qid);

Upgrading to PHP7 wasn’t night mare, it was quite simple and easy.

Get JSON data using CURL

There’s lots of communication using json, this is useful PHP function for getting JSON data using CURL function.

/*
E.g.  
  $args = '{"tid":"'.$taxonomy_id.'"}';
  $content = curl_request('http:/test.com/test.json', $args, 'POST');
*/
if (!function_exists('get_url_data_curl')) {
  function get_url_data_curl($url, $args='', $httpmethod){
    try{
      if (function_exists('curl_init') && function_exists('curl_setopt')){
      	$ch = curl_init();
      	curl_setopt($ch, CURLOPT_URL, $url);
      	curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type:application/json'));     
      	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      	if(strcmp($httpmethod, "POST") == 0){
      		curl_setopt($ch, CURLOPT_POST, true);
      	}else{
      		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpmethod); 
      	}
      	if(strcmp($httpmethod, "POST") == 0 || strcmp($httpmethod, "PUT") == 0 && !empty($args)){
      			curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
      	}
      	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      	$result = curl_exec($ch);
      
      	curl_close($ch);
      	return $result;
      }
    }catch(Exception $e){
      $error = array(
      	'status' => 'failed',
      	'errorMessage' => $e->getMessage(),
      	'errorCode' => '500'
      );
      return json_encode($error);
    } 
  }
}

How to use is simple.

$args = '{"tid":"'.$taxonomy_id.'"}';
$content = curl_request('http:/test.com/test.json', $args, 'POST');

 

Put $args variable what you want to pass through.

Method can be either GET or POST.

Installing Memcached PHP extension on CentOS 7

I have new server required to use PHP memcached.

But it showed this error.

PHP Fatal error: Class 'Memcached' not found

The server version is CentOS 7, so used below commands.

Check linux version: lsb_release -a
yum clean all
yum -y update
yum -y install memcached
systemctl start memcached.service
systemctl enable memcached.service
systemctl restart memcached.service

Run this to check memcached installed:

php -m | grep memcache

You should see like “memcache”, if it is installed correctly.

Restart apache:

systemctl restart httpd

 

 

 

Easy and fast git commit

I am sick and tired with typing git commands.

So I decided to use short and quick for git.

First create commit.sh file

#!/bin/bash -e
commit_message="$1"
git pull
git add . -A
git commit -m "$commit_message"
git push

Then add short command on .bash_profile

alias commit="~/path_to_sh_file/commit.sh"

Now, it’s ready to use. Restart terminal.

And just type like this,

commit 'this is test commit'

Works great, I can save 10 second per commit.

Usually I push the changes like 100 times, then I save 1000 seconds.

1000 seconds / 60 = 16 mins

I can save 16 mins per day!

Yay!