在 Sublime Text 裡的格式化 PHP 代碼

由於某些原因,需要處理一堆比較舊的PHP代碼,
而且編程風格也不統一,所以需要先統一後再處理會比較方便.
不過於由 Pear 的東西有點參差不齊.所以整合到 Editor 內的過程也不太順利.
來記錄一下.

  1.  # 更新已有的 Pear 套件
  2.  # 之後安裝 PHP_Beautifier
  3.  # 再列出使用方法
  4.  cd /Applications/XAMPP/xamppfiles/bin
  5.  sudo ./pear update-channels
  6.  sudo ./pear upgrade-all
  7.  sudo ./pear install --alldeps PHP_Beautifier-0.1.15
  8.  ./php_beautifier --help
  9.  
  10.  # 修正出現 Cannot redeclare class Config 錯誤
  11.  # 切進 PHP_Beautifier 的 Filter 目錄
  12.  cd /Applications/XAMPP/xamppfiles/lib/php/pear/PHP/Beautifier/Filter
  13.  
  14.  # 打開下面兩個檔案
  15.  - Pear.filter.php
  16.  - phpBB.filter.php
  17.  
  18.  # 注解掉下句這句
  19.  require_once ('PEAR/Config.php');
  20.  
  21.  # 安裝 Sublime Text 2 的 PhpBeautifier 套件
  22.  cd ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/
  23.  git clone https://github.com/SublimeText/PhpBeautifier.git PhpBeautifier
  24.  cd PhpBeautifier/
  25.  vim php_beautifier.py
  26.  
  27.      indent = "-t"
  28.      cmd = "/Applications/XAMPP/xamppfiles/bin/php_beautifier"
  29.  
  30.  # 最後在需要美化/格式化的代碼編輯界面裡就可以格式化了
  31.  Open File > Command+Shift+P > Format PHP > Enter

在 Nginx 中設定子目錄內的 Slim Framework 應用網址

簡單記錄一下在 nginx 裡面,處理 Slim Framework 的 URL Rewrite 方法

  1.  server {
  2.      listen      80;
  3.      charset     utf-8;
  4.      server_name example.com www.example.com;
  5.      root        /path/to/your/document_root;
  6.      index       index.html index.htm index.php;
  7.  
  8.      location ~ \.php$ {
  9.          fastcgi_pass  127.0.0.1:9000;
  10.          fastcgi_index index.php;
  11.          fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  12.          include       fastcgi_params;
  13.      }
  14.  
  15.      location /sub-directory-app {
  16.          try_files $uri $uri/ /sub-directory-app/index.php?$request_uri;
  17.      }
  18.  
  19.      location ~ /\.ht {
  20.          deny all;
  21.      }   
  22.  }

修正升級後找不到 php.ini 檔案

補充一下關於文章 升級到 PHP5.3.6
會出現關於 php.ini 找不到的問題,解決方法如下

  1.  # 找出查找 php.ini 的指定目錄
  2.  /usr/local/php5-cgi/bin/php -i | grep php.ini
  3.  
  4.  # 得到他的位置(如我的)
  5.  /usr/local/php5-cgi/lib
  6.  
  7.  # 之後再進入到這目錄裡
  8.  cd /usr/local/php5-cgi/lib
  9.  
  10.  # 建立一個連接
  11.  ln -s /usr/local/php5-cgi/etc/php.ini
  12.  
  13.  # 查看是否建立成功
  14.  ls -l
  15.  
  16.  # 重新載入
  17.  php-fpm usr/local/php5-cgi/php-fpm.sh reload
  18.  
  19.  # 再確定是否已經成功指定 (此時應該會出現了兩行結果)
  20.  Configuration File (php.ini) Path => /usr/local/php5-cgi/lib
  21.  Loaded Configuration File => /usr/local/Zend/etc/php.ini

Nginx 反向代理自己的 Dropbox 網址

簡單記錄一下,不用再記長長的ID和找網址.

  1.  server {
  2.      listen 80;
  3.      server_name dropbox.example.com;
  4.  
  5.      location / {
  6.          set $index index.html;
  7.  
  8.          proxy_set_header X-Real_IP $remote_addr;
  9.          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  10.          proxy_set_header Host $http_host;
  11.          proxy_set_header X-NginX-Proxy true;
  12.          proxy_pass http://dl.dropbox.com/u/[==Your Dropbox ID==]/;
  13.          proxy_redirect off;
  14.     
  15.          rewrite ^.*\/$ $fastcgi_script_name$index last;
  16.      }
  17.  }

Sybase 中找出函數,視圖,觸發器,資料表

簡單記錄一下,版本不同可能有差別.

  1.  # 只知道部份字眼
  2.  SELECT * FROM sysobjects WHERE name LIKE '%關鍵字%'
  3.  
  4.  # 列出所有函數 (FUNCTION)
  5.  SELECT * FROM sysobjects WHERE type = 'P'
  6.  
  7.  # 列出所有視圖 (VIEW)
  8.  SELECT * FROM sysobjects WHERE type = 'V'
  9.  
  10.  # 列出所有觸發器 (TIGGER)
  11.  SELECT * FROM sysobjects WHERE type = 'TR'
  12.  
  13.  # 列出所有觸發器 (USER TABLE)
  14.  SELECT * FROM sysobjects WHERE type = 'U'

FreeBSD 安裝 Node.js + MongoDB 與部署 App

簡單記錄一下

  1.  # 先更新 index 和 ports
  2.  cd /usr/ports && make fetchindex
  3.  cvsup -L 2 -h cvsup.tw.FreeBSD.org /usr/share/examples/cvsup/ports-supfile
  4.  
  5.  # 通過 ports 安裝 node
  6.  cd /usr/ports/www/node
  7.  make install clean
  8.  
  9.  # 通過 ports 安裝 mongodb (需等待)
  10.  cd /usr/ports/databases/mongodb
  11.  make install clean
  12.  
  13.  (選擇 V8 引擎取代 SpiderMonkey)
  14.  (之後的用預設)
  15.  (之後再用預設)
  16.  
  17.  # 將安裝 mongodb 時新增的 mongodb 用戶 shell 改為 nologin
  18.  chsh -s /usr/sbin/nologin mongodb
  19.  
  20.  # 通過 ports 安裝 supervisor
  21.  # Python 寫的,用來管理 node app
  22.  cd /usr/ports/sysutils/py-supervisor
  23.  make install clean
  24.  
  25.  # 重新進入 root 環境,使上面安裝的指令可作用
  26.  su -
  27.  
  28.  # 接下來,設定 supervisor
  29.  # 編輯 supervisord.conf
  30.  # ports 預設位置是這裡.
  31.  # 可複製到 /etc/supervisord.conf
  32.  vim /usr/local/etc/supervisord.conf
  33.  
  34.  # 裡面的內容如下
  35.  # ================
  36.  [unix_http_server]
  37.  file=/var/run/supervisor/supervisor.sock
  38.  chmod=0700
  39.  chown=www:www
  40.  
  41.  [supervisord]
  42.  user=root
  43.  
  44.  ;最後在最下面加入 (前面加 ; 代表註解)
  45.  [program:node_app]
  46.  command=/usr/local/bin/node /home/[用戶]/node-app.example.com/app.js
  47.  directory=/home/[用戶]/node-app.example.com
  48.  autostart=true
  49.  startsecs=5
  50.  user=[執行者]
  51.  ;redirect_stderr=true
  52.  ;stdout_logfile = /var/log/supervisord/node_note.log
  53.  # ================
  54.  
  55.  # 將 sock 存放目錄設為 www 用戶和用戶組才可讀取
  56.  chown -Rf www:www /var/run/supervisor
  57.  
  58.  # 之後修改 nginx 加入 proxy 轉向到內部端口
  59.  server {
  60.      listen      80;
  61.      charset     utf-8;
  62.      server_name node-app.example.com;
  63.      root        /home/[用戶]/node-app.example.com;
  64.  
  65.      location / {
  66.          proxy_pass http://127.0.0.1:[端口]/;
  67.      }
  68.  }
  69.  
  70.  # 運行 MongoDB
  71.  mongod --dbpath /var/db/mongodb/ --logpath /var/log/mongodb.log --fork
  72.  
  73.  # 進入命令窗口,測試操作資料庫(或者加權限)
  74.  mongo 127.0.0.1:27017
  75.  
  76.  # 簡單記錄一下權限設置,
  77.  # 重新運行 MongoDB 後,再用 admin 時就需要權限了
  78.  use admin
  79.  db.auth("username", "password")
  80.  exit
  81.  
  82.  killall mongod
  83.  mongod --dbpath /var/db/mongodb/ --logpath /var/log/mongodb.log --fork --auth
  84.  
  85.  # 為確保開發環境和上線環境內的依賴套件一致使用如下指令
  86.  # 更新自機中的 NPM 確保可使用 npm shrinkwrap 指令 (>= 1.1.2)
  87.  # 生成 npm-shrinkwrap.json
  88.  npm update npm -g
  89.  npm shrinkwrap
  90.  
  91.  # 再到主機中更新 npm
  92.  # 再安裝 app (應用) 的依賴套件
  93.  npm update npm -g
  94.  npm install
  95.  
  96.  # 之後兩邊再比對一下 npm list 是否一樣
  97.  npm list
  98.  
  99.  # 最後通過 supervisor 執行 node app 應用
  100.  # 使 node app 應用打開 127.0.0.1:[端口] 的端口
  101.  supervisord -c /usr/local/etc/supervisord.conf
  102.  supervisorctl start node_app
  103.  
  104.  # 當 supervisord 運行後,以後就可經 supervisorctl 操作
  105.  supervisorctl stop [node_app],停止某個 supervisord.conf 中的 [program:(應用)]
  106.  supervisorctl start node_app,運行某個應用
  107.  supervisorctl restart programxxx,重新運行某個應用
  108.  supervisorctl stop all,停止重部應用
  109.  supervisorctl reload,重新載入最新的配置文件,並按照配置文件執行每個應用
  110.  
  111.  *** 註 ***
  112.  當修改 supervisord.conf 後,需使用 reload 重新加載,start,stop,restart都不會加新文件
  113.  
  114.  # 最後將 supervisor 和 mongod 都放到 /etc/rc.local
  115.  # ===========
  116.  # Supervisord (因不是預設的檔案位置,所以不用前者)
  117.  #supervisord_enable="YES"
  118.  supervisord -c /usr/local/etc/supervisord.conf
  119.  
  120.  # MongoDB
  121.  mongod --dbpath /var/db/mongodb/ --logpath /var/log/mongodb.log --fork --auth

# 參考

http://cuppster.com/2011/05/12/diy-node-js-server-on-amazon-ec2/

http://serverfault.com/questions/134179/how-do-i-automatically-start-supervisor-on-boot-in-freebsd

http://feilong.me/2011/03/monitor-processes-with-supervisord

http://www.aiezu.com/Article/FreeBSD_MongoDB.aspx

Node.js 遇見 Not a string or buffer

在開發環境部署到線上環境時,遍上了下面兩句東西
可以不停地向大神發問都得到不一個比較有用的解決方法
最後一再查證下發現應該是由於線上環境中的套件依賴版本不同導致的

問題:

  1.  > node (new Hmac).init(hmac, key);
  2.  > Not a string or buffer

解決:

  1.  # 先更新開發環境中的 npm 版本
  2.  # 同時也更新線上環境的 npm 版本
  3.  # 確保兩者都 (>= 1.1.2)
  4.  npm update npm -g
  5.  
  6.  # 之後在開發環境中輸入指令建立新的依賴版本描述
  7.  npm shrinkwrap
  8.  
  9.  # 之後將生成出來的 npm-shrinkwrap.json
  10.  # 放到線上環境中,再安裝依賴套件
  11.  npm install
  12.  
  13.  # 最後兩邊通過比較套件列表確保大家都用同版本就解決了
  14.  npm list

FreeBSD Git 安裝筆記 (2)

這篇主要是修正之前那篇 FreeBSD install Git and GitWeb 記錄的問題
解決了某些輸入密碼和 git shell 不能用的情況

  1.  # 切到 git 用戶測試
  2.  su git
  3.  
  4.  # 可能會輸出以下錯誤
  5.  fatal: Interactive git shell is not enabled.
  6.  hint: ~/git-shell-commands should exist and have read and execute access.
  7.  
  8.  # 解決方法是複製 git-shell-commands 目錄到 git 用戶目錄
  9.  cd /home/git
  10.  cp -Rf /usr/local/share/git-core/contrib/git-shell-commands .
  11.  
  12.  # 再來改變權限
  13.  chown -R git: git-shell-commands/
  14.  chmod +x git-shell-commands/help
  15.  chmod +x git-shell-commands/list
  16.  
  17.  # 找出 git-shell 位置
  18.  which git-shell
  19.  
  20.  # 之後修改 git 用戶的 shell 到剛才找出的位置
  21.  chsh -s /usr/local/bin/git-shell git
  22.  
  23.  # 注意
  24.  - 最後確保 /home/git/.ssh/authorized_keys 內的 key 是正確
  25.  - 再測試在自機測試 clone 遠端的 repo,應該就不需要密碼了
  26.  - 例如遠端目錄的樣子是: /home/git/work/core.git
  27.  
  28.  git clone git@example.com:work/core.git

JavaScript Prototype (原型鏈) 小記

事緣剛上線後不久..
就和網友討論到JavaScript的Prototype(原型鏈),
在討論中大家都寫了一小點的代碼來討論一些事情,
所以記錄一下相關的實現方法,
兩個版本都參考了jQuery的實現,感覺奇技淫巧

這是朋友在解決問後提供的版本

  1.  < !doctype html>
  2.  <html lang="zh-tw">
  3.  <head></head>
  4.  <body>
  5.  <p>Attr: [<em id="attr" title="huge, gigantic">large</em>].</p>
  6.  </body>
  7.  <script>
  8.  (function(){
  9.  
  10.      window.$=function(c,o){
  11.          console.log(this.init?'>> Init':'>> New Class');
  12.          return this.init?this.init(c,o):new $(c,o);
  13.      };
  14.  
  15.      $.fn=$.prototype={
  16.          init:function(c){
  17.              console.log('init');
  18.              this[0]=document.getElementById(/^[^< ]*(<(.|\s)+>)[^>]*$|^#(\w+)$/.exec(c)[3]);
  19.              return this;
  20.          },
  21.          html:function(c){
  22.              console.log('html');
  23.              return this[0].innerHTML;
  24.          }
  25.      };
  26.  })();
  27.  
  28.  alert($('#attr').html());
  29.  alert($('#attr').html());
  30.  </script>
  31.  </html>

之後是我修改和重寫後的faker版本

  1.  < !doctype html>
  2.  <html lang="zh-tw">
  3.  <head>
  4.  </head>
  5.  <body>
  6.  <p>Attr: [<em id="attr" title="huge, gigantic">large</em>].</p>
  7.  <p>Attr: [<em id="attr2" title="huge, gigantic">large2</em>].</p>
  8.  </body>
  9.  <script>
  10.  var faker = (function(){
  11.      var faker = function(id) {
  12.          console.log('New Class');
  13.          return new faker.fn.init(id);
  14.      };
  15.  
  16.      faker.fn = faker.prototype ={
  17.          init: function(id) {
  18.              console.log('init');
  19.              this.fakerObject = window.document.getElementById(id);
  20.              return this;
  21.          },
  22.         
  23.          html:function() {
  24.              console.log('html');
  25.              return this.fakerObject.innerHTML;
  26.          }
  27.      };
  28.     
  29.      faker.fn.init.prototype = faker.fn;
  30.  
  31.      return window.faker = window.$ = faker;
  32.  })();
  33.  
  34.  console.log("第一次"); alert(faker('attr').html());
  35.  console.log("第二次"); alert(faker('attr2').html());
  36.  </script>
  37.  </html>

CloudFoundry 中部署 Node.js+MongoDB

大約記錄一下部署到 CloudFoundry 的情況

  1.  # 建立一個獨立的環境來安裝需要的 VMC 指令
  2.  rvm gemset create cloudfoundry
  3.  rvm gemset use cloudfoundry
  4.  gem install vmc --no-rdoc --no-ri
  5.  
  6.  # 安裝完成後可以查看可用指令
  7.  vmc --help
  8.  
  9.  # 先設定 API 位置之後登入
  10.  vmc target api.cloudfoundry.com
  11.  vmc login
  12.  
  13.  # 登入時會出現以下介面
  14.  Email: [輸入註冊時的電郵]
  15.  Password: [輸入註冊時的位置]
  16.  Successfully logged into [http://api.cloudfoundry.com]
  17.  
  18.  # 之後進入到自己的專案位置
  19.  # 同時通過 NPM 安裝依賴套件
  20.  cd ~/Project/Path
  21.  npm install
  22.  
  23.  # 第一次新建的應用通過以下指令部署
  24.  vmc push
  25.  
  26.  # 第一次時會有以下問題出現
  27.  Would you like to deploy from the current directory? [yN]: y
  28.  Application Name: [ProjectName] (應用名字)
  29.  Application Deployed URL: '[ProjectName] (應用名字).cloudfoundry.com'?
  30.  Detected a Node.js Application, is this correct? [yN]: y
  31.  Memory Reservation [Default:64M] (64M, 128M, 256M, 512M, 1G or 2G)
  32.  Creating Application: OK
  33.  Would you like to bind any services to '[ProjectName] (應用名字)'? [yN]: y
  34.  The following system services are available
  35.  1: mongodb
  36.  2: mysql
  37.  3: postgresql
  38.  4: rabbitmq
  39.  5: redis
  40.  Please select one you wish to provision: 1
  41.  Specify the name of the service [mongodb-隨機數]:
  42.  Creating Service: OK
  43.  Binding Service [mongodb-隨機數]: OK
  44.  Uploading Application:
  45.    Checking for available resources: OK
  46.    Processing resources: OK
  47.    Packing application: OK
  48.    Uploading (2K): OK   
  49.  Push Status: OK
  50.  Staging Application: OK
  51.  Starting Application: OK
  52.  
  53.  # 當部署完成後可以通過以下網址查看
  54.  http://[ProjectName].cloudfoundry.com
  55.  
  56.  # 當需要更新整個應用時,進入相應目錄後用以下指令
  57.  vmc update [ProjectName]
  58.  
  59.  # 當需要查看相關應用文件時(應用的檔案,日誌記錄)
  60.  vmc files [ProjectName]
  61.  
  62.  # 上面的指令就會列出關於這個專案的目錄
  63.  - app (整個應用存放於此)
  64.  - logs (應用的日誌記錄)
  65.  
  66.  # 輸入以下指令就可以進入對應目錄查看有什麼檔案
  67.  vmc files [ProjectName] logs
  68.  - stdout.log
  69.  - stderr.log
  70.  
  71.  # 執行以下指令則會列出這檔案的內容
  72.  vmc files [ProjectName] logs/stdout.log
  73.  
  74.  # 當部署應用屬於 Node.js 時需要注意以下幾點
  75.  1. 應用的 port, host 需要由 ENV 中取得
  76.  2. 目前測試時 require("abc.json") 會發生錯 (可能版本不同)
  77.  
  78.  關於第一點可以通過以下方法修改 (以 expressjs 為例)
  79.  
  80.  var port = (process.env.VMC_APP_PORT || 3000);
  81.  var host = (process.env.VCAP_APP_HOST || 'localhost');
  82.  
  83.  app.listen(port, host);
  84.  
  85.  否則可能會出現以下情況
  86.  Application state is undetermined, not enough information available.
  87.  
  88.  如果還是解決不了可以再看上面的方法查看 stderr.log 了解
  89.  
  90.  # 再者當部署的資料庫是 Node.js 時需要注意連結部份
  91.  if(process.env.VCAP_SERVICES){
  92.      var env = JSON.parse(process.env.VCAP_SERVICES);
  93.      var dsnObject = env['mongodb-1.8'][0]['credentials'];
  94.      var mongoDSN = "mongodb://" + dsnObject.username + ":" + dsnObject.password + "@" + dsnObject.hostname + ":" + dsnObject.port + "/" + dsnObject.db;
  95.  }else{
  96.      var mongoDSN = "mongodb://localhost/database";
  97.  }
Page 1 of 4212345»102030...Last »
Fork me on GitHub