Linux

Laravel - Homestead

Laravel Homestead 介紹

Laravel 為了讓PHP開發整體體驗都能完善,在local開發的環境設定也提供了相關解決方案

也就是這裡要介紹的 Homestead

Laravel Homestead 是官方所維護的 Vagrant box pre-package

讓你可以直接透過這個環境開發,而不必再獨立安裝PHP、Web server…等軟體

並且不必擔心忘記系統設定,因為 Vagrant boxes 是一次性設定安裝完畢,

如果在安裝流程有發現錯誤,隨時都可以重新安裝環境,並且只需要幾分鐘的時間就能完成

Continue Reading

Docker - Get Start Part 3. Services

這裡記錄 Services 實作流程

在之前,要先透過 part2 建立了 image gordon/get-started:part2

在這裡,建立 docker-compose.yml ,透過 image 來產生五個重複的 container

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: gordon/get-started:part2
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "4000:80"
    networks:
      - webnet
networks:
  webnet:

接著先執行 swarm 初始化(這部分會在 part 4 介紹)

docker swarm init

接著,執行建立 service app 名稱為 getstartedlab

Continue Reading

Docker - Get Start Part 2. Containers

這裡記錄 Containers 實作流程

在這裡按照下方流程就能快速建立出一個 python demo web

預計將專案放置在 docker_project/get_started/

建立檔案: Dockerfile

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

requirements.txt

Continue Reading

Docker - 基本指令紀錄

Docker 基本操作常用的指令:

docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

Continue Reading

Docker - TMPFS

Docker Mount 下圖清楚了說明Docker host的 Volume with bind mounts 以及tmpfs mount 機制

這裡針對 tmpfs 部分進行介紹:

tmpfs mount

tmpfs mounts 只會儲存於host system的記憶體,不會寫入 host system的 filesystem。

因此,tmpfs mount 資料不會保存在硬碟、Docker host或 container 裡,只會存在container 執行的期間

通常會把 tmpfs mount 應用在非敏感或不必持續保留的狀態資料,舉例來說,swarm service 就是使用 tmpfs mount 去 mount secrets 到 service的 container。

在 Docker 17.06 以上版本,建議使用 –mount flag 來進行 bind mounts, volumes, 或者 tmpfs mount。

tmpfs 使用案例

tmpfs mounts 最適合使用於不必持續保留的資料,或者當你的應用程式需要寫入大量的非持續保留的資料時。在 host machine 或者 container,適當的採用 tmpfs mount 幾可以讓container執行效能提升。

Continue Reading

Docker - Bind Mounts

Docker Mount 下圖清楚了說明Docker host的 Volume with bind mounts 以及tmpfs mount 機制

這裡針對 Bind Mounts 部分進行介紹:

Bind Mounts

Bind mounts 在早期 Docker 版本就已經存在,相對於 volumes ,使用 Bind mounts 有一些功能限制:

當 container 在host machine 中的檔案或目錄使用 bind mount

host machine 是以完整的路徑來參照這些檔案或目錄

因此,在Docker host 中不需要存在這些檔案或目錄

若參照的目錄或檔案不存在,則會自動建立。

透過 Bind Mounts 的方式是非常有效率的方式,但使用 bind mounts 需要 host machine 有特定的 filesystem 結構才能運作

如果你正開發一個新的 Docker 應用程式,考慮改用 Volumes 時,就不能直接透過Docker CLI 來管理 bind mounts。

Bind mounts 可以指定在敏感的檔案

使用 bind mounts 其中一個特點就是,

Continue Reading

Docker - Volumes 介紹

Docker Mount

下圖清楚了說明Docker host的 Volume with bind mounts 以及tmpfs mount 機制

這裡針對 Volumes 部分進行介紹:

Continue Reading

Docker - 資料管理簡介

在 Docker 管理資料

在 container 所建立的檔案,預設都會存放在可容器寫入層(writable container layer)

這表示:

  • 當container不沒有運行時,資料就會跟著消失
  • container中的數據很難從外部取得
  • 可寫入容器層的資料,與container高度耦合,很難將資料搬移到其他地方
  • 需要透過 storage driver 來管理filesystem

當你建立一個新的 container,會同時在最上層建立一個新的可寫入層(writable layer),也稱為 container layer。

在 container內所有的改變都會在這裡面,例如: 建立新檔案,編輯檔案,刪除檔案,都會寫入這個容器可寫入層。

Continue Reading

Laravel Migrate-Generate

安裝擴充

composer require --dev "xethron/migrations-generator"

檢查laravel 版本

php artisan --version

Continue Reading

Docker - Compose 介紹 (上)

Docker-Compose 是一個可以用來定義且執行多個 Container 應用程式的工具

可以很簡單的透過 Compose 的 yaml 來設定你的container應用程式

並且透過一個指令,就可以建立及啟動所有yaml設定的應用程式

Compose 可以在產品、階段產品、開發或測試項目使用

在使用上大致可以區分為三個過程:

Continue Reading

Docker - 安裝 MySQL

MySQL 是一個廣泛被使用,開源的關聯式資料庫管理系統( relational database management system, RDBMS)

目前的MySQL執行效能不斷提升,可靠且易於使用,

因此經常被作為網路應用程式開發資料庫的首選.

其中包括 Facebook, Twitter, YouTube, Yahoo 等公司都有使用。

詳細說明可參考官網: www.mysql.com.

這裡將說明如何在 Docker 安裝即執行 MySQL 環境

Continue Reading

Docker - 執行 Nginx Webserver

執行 Nginx Container

-d 表示在背景( Detached )執行,Docker 預設前景( foreground )執行

-p 表示將本機 8080 port 的來源轉發到 container 的 80 port

–name 表示為 container 的名稱

–rm 表示當 exit container 時,會移除 container( incompatible with -d )

docker run -d -p 80:80 --name mywebserver nginx

//if need custom port (ex 3000) can do like this

docker run -d -p 3000:80 --name mywebserver nginx

由於 local 還沒有 nginx 的 image,因此Docker 會先執行檢查及下載 nginx image

Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
...下載安裝
Status: Downloaded newer image for nginx:latest

安裝完畢之後,就會直接對 nginx image 進行 instance 為 nginx container,並執行

Continue Reading

Docker - MAC安裝與執行 Hello World

Docker 在 Mac 環境可以直接透過 Install Docker for Mac 來安裝

前往 Docker for Mac 下載 Docker.dmg 執行安裝

檢查版本

透過下列方式來檢查 docker 版本,確定安裝完畢

docker --version

docker-compose --version

docker-machine --version

Continue Reading

Docker - Remove all images and container

Delete all docker containers

docker rm $(docker ps -a -q)

Delete one containers by name

docker rm mycontainername

Delete all docker images

docker rmi $(docker images -q)

Delete images by name

docker rmi images_name

Check Image and containers

//all container (include un-run container)
docker ps -a
//on running container
docker ps

//all images
docker images

Remove Laradock file

rm -rf laradock/

Continue Reading

Docker - 常用指令介紹

##【 Docker 】

關閉 Docker

docker-compose down

【 Container 】

建立container

docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]

OPTION:

-d 表示在背景( Detached )執行,Docker 預設前景( foreground )執行

-p 表示將本機 8080 port 的來源轉發到 container 的 80 port

–name 表示為 container 的名稱

–rm 表示當 exit container 時,會移除 container( incompatible with -d )

Continue Reading

16 進位簡介

16 進位簡介

電腦世界處理的基礎是 0 跟 1,我們稱之為二進位(binary)

二進位可以在組成十進位 0~9

不同的系統或程式對於十六進位都有不同的描述方式,但是基本概念如下:

十六進位則是由 09 AF 組成

十六進位剛好是 4^2 ,轉二進位相當容易

例如: 二進位 01011110101101010010 可直接轉十六進位 5EB52

另外是 Byte 共 8 Bit

其中將 4 個 Bit 剛好可儲存一個16進位

各種場合帶入16進位方式

環境 格式 備註
URL %hex
XML,XHTML &#xhex
HTML,CSS #hex 6位,用於表示顏色
Unicode U+hex 6位,表示字符編碼
MIME =hex
Modula-2 #hex
Smalltalk,ALGOL 68 16rhex
Common Lisp #xhex或#16rhex
IPv6 8個hex用:分隔

Continue Reading

ASCII 編碼說明

ASCII 編碼說明

ASCII(American Standard Code for Information Interchange,美國標準資訊交換碼)是由美國國家標準局(ANSI)訂定的字元標準

並且已經被國際標準組織(ISO)公認為國際標準,現今多數語系編碼都會都會參考 ASCII 的格式

接下來介紹如何從2進位轉10進位來取得參照值

1 Byte = 8 Bit

每一個 Bit 都可以存取一個二進位 (即 0, 1)

可以試想,1Byte提供了8個空間 [][][][][][][][]

,每一個空間都可以塞一個2進位值,例如: [0][0][0][0][0][0][0][0]

接下來,計算從2進位轉10進位方式:

例如 n 表示為 0或1

[2^7n]+[2^6n]+[2^5n]+[2^4n]+[2^3n]+[2^2n]+[2^1n]+[2^0n]

其 [0][0][0][0][0][0][0][1] 就可以計算程 [2^70]+[2^60]+[2^50]+[2^40]+[2^30]+[2^20]+[2^10]+[2^01] = 1 其 [0][0][0][0][0][0][1][0] 就可以計算程 [2^70]+[2^60]+[2^50]+[2^40]+[2^30]+[2^20]+[2^11]+[2^00] = 2 其 [0][0][0][0][0][0][1][1] 就可以計算程 [2^70]+[2^60]+[2^50]+[2^40]+[2^30]+[2^20]+[2^11]+[2^01] = 3

根據這些每一個10進位狀態,可以對應到一個字母

可直接參考下方 ASCII 編碼表

Continue Reading

資料傳輸方式

資料傳輸方式紀錄

資料傳輸常見Json, XML 的方式傳送,但這裡我們要說明的是在細節一點的格式傳輸方式

Byte 格式傳輸

一般各種格式的資料,傳輸前都必須轉為 byte 格式

例如,數字、字串… 轉為 byte ,再用 byte 格式傳送

FIX/Fast 資料格式傳輸

證券即時行情、各類統計資訊傳輸,都相當要求速度及品質

因此國內證交所即採用 FIX(Financial Information eXchange) 傳輸協定搭配FAST資料流壓縮技術,來達到一定的效率

例如,證交所資料伺服器(MDS, Market Data Server) 主要負責Fix/Fast資料處理

處理後會透過內網分送到不同 VM DAP-RT (Data Access Point Real-Time)

每一個界接資訊的用戶,都可以連線到專屬的 DAP-RT

FIX 基本格式為 Tag=Value Tag=Value….

FIX 雖然可讀性高,但是純字串格式傳輸效能並不好

因此,會再將此字串透過 FAST 壓縮成二進位,提升資料傳輸速度

除此外,FAST 資料格式會在第一個 bit 作為判別是否還有其他資料(1:有, 0:結束)

Byte + FIX

前面提到的 byte 轉換方式,可以結合FIX 的格式來建立資料

將FIX字串轉換為 byte 格式,接著就可以透過 Socket…各種方式將資料傳送出去

//傳送byte資料
string transferData = "001=Adam 002=0999888777 003=Male 004=Tw";
byte[] buff = Encoding.Default.GetBytes(transferData);

接收方 byte 格式資料後,可將轉換為字串 在依照兩方溝通的FIX對照方式,參照資料意義

Continue Reading

MAC 與 Linux 安裝 composer 方式

這裡記錄幾種安裝composer的方式

  • 官方安裝方式說明
  • brew安裝方式

Composer 官方

透過下方幾個指令,安裝 composer.phar 到本機

Continue Reading

MAC 如何升級PHP版本

通常,MACOS Sierra都會預設安裝PHP5.6版本

這裡針對升級到更高階PHP版本流程進行說明:

可以透過brew 或者 curl 兩種方式來安裝 Brew

brew update && brew upgrade
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
brew unlink php56 
brew install php72

(如果要從7.1升級到7.2,作法如下:)

brew tap homebrew/homebrew-php
brew unlink php71
brew install php72
brew install php72-xdebug

在這過程中,如果php5.6不是透過 brew 安裝,很可能會發生錯誤

這時仍可以繼續透過 CRUL的方式來安裝

CURL

curl -s http://php-osx.liip.ch/install.sh | bash -s 7.2

安裝完畢後,可以透過下方語法檢查PHP版本

php -v

如果還無法取得到PHP7版本,可以透過輸入下面的指令來更新路徑,來取得正確版本

vim ~/.profile

# 將下面這行加入~/.profile 最後一行
export PATH=/usr/local/php5/bin:$PATH

# 離開後,接著執行
source ~/.profile

上方的路徑,可能要依照你的環境來進行調整 如果上方指令仍無法運作,最後可以再試試這個方式

export PATH="$(brew — prefix homebrew/php/php72)/bin:$PATH"

祝安裝順利,也別忘了幫我按個讚

Continue Reading