zsh, oh-my-zsh and powerlevel9k

Cli

installation

1
2
3
4
5
6
7
8
9
10
# zsh
brew install zsh zsh-completions
chsh -s $(which zsh)

# oh-my-zsh
git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh

# powerlevel9k
git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k
echo 'ZSH_THEME="powerlevel9k/powerlevel9k"' >> ~/.zshrc

edit ~/.zshrc

1
2
3
4
5
6
7
8
9
export ZSH=$HOME/.oh-my-zsh
ZSH_THEME="powerlevel9k/powerlevel9k"
plugins=(git)
source $ZSH/oh-my-zsh.sh
source /usr/local/share/zsh-history-substring-search/zsh-history-substring-search.zsh

POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD=0
POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION=2
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status command_execution_time time)

useful plugin

reference

https://github.com/robbyrussell/oh-my-zsh/wiki/Installing-ZSH
https://github.com/robbyrussell/oh-my-zsh
https://github.com/bhilburn/powerlevel9k/wiki/Install-Instructions


UTF-8 Multibyte String Encoding

Decode in php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Hexadecimal
#
# utf-8 (ASCII)
# \x2a
#
# utf-8 (2-byte)
# \xC3\xa9
#
# utf-8 (3-byte)
# \xe5\x8f\x82
#
# utf-8 (4-byte)
# \xF0\x9F\x98\x81
#



# decode in php
$str = "\xe5\x8f\x82";
echo mb_convert_encoding($str, 'utf-8');
# output: 参
echo mb_convert_encoding($str, mb_detect_encoding($str));




# Decimal
#
# \ud83d\ude01
#
# decode in php
$str = "\ud83d\ude01";
echo json_decode('"' . '\ud83d\ude01' . '"');
# output: [smile]




# HTML Entities encoding and UTF-16
#
# ¼
# \x10\x00
#
# decode in php
echo mb_convert_encoding('¼', 'UTF-8', 'HTML-ENTITIES');
# output: ¼
echo mb_convert_encoding("\x10\x00", 'UTF-8', 'UTF-16BE');
# output: က
.

Reference

https://stackoverflow.com/questions/6058394/unicode-character-in-php-string


IT automation 2 - Ansible Playbook

Ansible

Quick start

1
2
3
4
5
6
7
8
apt-get install ansible

# docker pip and docker role
ansible-galaxy install geerlingguy.pip geerlingguy.docker

# create and run a playbook
vim docker.playbook.yaml
ansible-playbook docker.playbook.yaml

Sample playbook yaml - install pip and docker, docker-compose

1
2
3
4
5
6
7
- hosts: all
vars:
pip_install_packages:
- name: docker
roles:
- geerlingguy.pip
- geerlingguy.docker

Reference

https://galaxy.ansible.com/geerlingguy/docker
https://galaxy.ansible.com/docs/


IT automation - Ansible

Ansible

Requirement

Quick start

1
2
apt-get install ansible
ansible localhost -m ping

Run task at remote host

  • setup password-less ssh connection
    1
    2
    3
    4
    5
    # add remote servers
    echo "[webservers]" >> /etc/ansible/hosts
    echo "www[01:50].example.com" >> /etc/ansible/hosts
    # test remoteservers
    ansible remoteservers -a "/bin/echo hello"

Other usage

1
2
3
4
5
6
7
8
9
10
# run as python2
ansible localhost -m ping -e 'ansible_python_interpreter="/usr/bin/env python"'
# run as python3
ansible localhost -m ping -e 'ansible_python_interpreter="/usr/bin/env python3"'

# run apt install screen
ansible 172.17.0.3 -m apt -a "name=screen"

# run `uptime`
ansible client -m shell -a "uptime"

Reference

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html



Cusomte a PHP docker image

Php

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FROM php:7-apache

RUN apt-get update && \
apt-get install -y --no-install-recommends \
curl \
libmemcached-dev \
libz-dev \
libpq-dev \
libjpeg-dev \
libpng-dev \
libfreetype6-dev \
libssl-dev \
libmcrypt-dev

RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install gd

.htaccess

1
2
3
4
5
6
7
php_flag  log_errors on
php_value error_log /var/www/html/PHP_errors.log
php_value error_reporting -1

php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on

Docker Build

1
2
docker build -t localhost-custom-php $PWD
docker run --name localhost-custom-php-instance --rm -p 8080:80 -v `pwd`:/var/www/html:ro localhost-custom-php

Basic usage of PHP Composer docker image

Cli

Basic usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# initial composer project
# --no-interaction (-n)
# --profile
docker run --rm -it \
-v $PWD:/app \
composer init -n


# add new library
docker run --rm -it \
-v $PWD:/app \
composer require slim/slim "^3.0"


# install a exist composer projects dependents
docker run --rm --interactive --tty \
--volume $PWD:/app \
composer install

# create index.php, run test
docker run --rm -p 8082:80 -v `pwd`:/var/www/html:ro php:7-apache

index.php

1
2
3
<?php
require('vendor/autoload.php');
echo "done";

Reference

https://getcomposer.org/doc/
https://hub.docker.com/_/composer/