2009年3月22日 星期日

使用 Xdebug + Vim 對 PHP 逐行除錯

[Vim]
檢查 python 與 sign 前面是否為 + 號

# vim
:version


若為 - 號的話,就需要重裝 Vim

# /usr/ports/editors/vim/make WITH_PYTHON=yes install clean


接下來要安裝 Xdebug Client 在 Vim 上。
下載:http://www.vim.org/scripts/script.php?script_id=1929

解開後將 debugger.py 與 debugger.vim 複製到 /usr/local/share/vim/vim72/plugin/

PS.後來發現有人放出改過得 debugger.py,解決了原版只等待 5 秒的問題,並修改了一些切割視窗的配置。

文章在此:
http://www.workingsoftware.com.au/index.php?h=WebLog&author_id=11&entry_id=1
下在位址在此:
http://www.workingsoftware.com.au/debugger.py





[Xdebug]
安裝 Xdebug for PHP

# cd /usr/ports/devel/php-xdebug
# make install clean


修改 php.ini,加入 Xdebug 設定

# vim /usr/local/etc/php.ini


找到 Module Settings,在下面加入:

zend_extension=/usr/local/lib/php/20060613/xdebug.so

[debug]
; Remote settings
xdebug.remote_autostart=off
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000

; General
xdebug.auto_trace=off
xdebug.collect_includes=on
xdebug.collect_params=off
xdebug.collect_return=off
xdebug.default_enable=on
xdebug.extended_info=1
xdebug.manual_url=http://www.php.net
xdebug.show_local_vars=0
xdebug.show_mem_delta=0
xdebug.max_nesting_level=100
;xdebug.idekey=

; Trace options
xdebug.trace_format=0
xdebug.trace_output_dir=/tmp
xdebug.trace_options=0
xdebug.trace_output_name=crc32

; Profiling
xdebug.profiler_append=0
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=0
xdebug.profiler_output_dir=/tmp
xdebug.profiler_output_name=crc32


重開 apache,並檢查 phpinfo 中有 Xdebug 資訊即為安裝成功。






[使用方法]

從瀏覽器啟動 Debug:

在你的網站上隨便開一個 PHP 網址,並在後面加上 ?XDEBUG_SESSION_START=1 參數,這樣就會啟動 Xdebug。

接下來用 Vim 開啟你要 Debug 的 PHP 檔,開啟後按下 F5。
這時候 Vim 畫面下方會出現一行字:
waiting for a new connection on port 9000 for 5 seconds

看到這行字後請在 5 秒鍾內用覽器開啟你正在編輯的這個 PHP 網頁,這樣 Vim 就會進入 Debug 模式了。




從 Command Line(CLI) 啟動 Debug:


# setenv "XDEBUG_CONFIG=vim=1"

打開 Vim,按下 F5。

# php -f debug_target.php

再回到 Vim 裡面去看,Debug 就已經啟動了!





如果要 Debug 用 system 開出來的 CLI PHP,則需要在 system 之前用 putenv 來設定環境變數

putenv("XDEBUG_CONFIG=vim=1");
system("php -f debug_target.php");




使用 Vim 進行 Debug 時會出現很多 畫面切割的小視窗,要其中移動必須按下 Ctrl + w,之後再按 上下左右,進行視窗的切換。

要改變視窗的大小則是按 Ctrl + w,之後再按 + 是變大,而 - 則是變小,按 = 則是回到預設值。最後按 F1 就會把目前視窗放到最大。

為瞭解決因為 Vim 共用 temp 檔而開啟多個 Vim 進行 debug 時每次都會說 暫存檔已經存在,只需要在 Vim 中下這個指令,並將每個 Vim 的 dir 都設成不一樣就可以了。

:set dir=/tmp



參考資料:
我同事 JohnPuPu的 Blog:http://blog.johnpupu.tw/?p=208


2009年3月11日 星期三

SU 與 SUDO 權限設定

[SU]
設定可以使用 su 的群組,之後把要用的人加入群組中。

# vim /etc/pam.d/su
設定可以使用 su 的群組
auth requisite pam_group.so no_warn group=wheel

把使用者加入群組中
# vim /etc/group

wheel:*:0:root,使用者帳號



[SUDO]
跟 SU 一樣設定可以使用的群組,之後把要用的人加入群組中。

# vim /usr/local/etc/sudoers

拿掉此行的註解
%wheel ALL=(ALL) ALL


把使用者加入群組中
# vim /etc/group

wheel:*:0:root,使用者帳號


CSH 與 VIM 環境設定

[CSH]
到 /etc/passwd 確認自己的 SHELL 是用 csh


修改
/etc/csh.cshrc (影響所有使用者) 或 ~/.cshrc (影響自己)


加入以下設定,完成後重新登入即生效:

# $FreeBSD: src/etc/csh.cshrc,v 1.3.52.1 2008/11/25 02:59:29 kensmith Exp $
#
# System-wide .cshrc file for csh(1).

set prompt = '[%B%n@%M%b] %B%~%b%#'
# color
setenv LSCOLORS ExGxFxdxCxegedabagExEx
setenv CLICOLOR yes
# autolist
set autolist

# bind keypad keys for console, vt100, vt102, xterm
bindkey "\e[1~" beginning-of-line # Home
bindkey "\e[2~" overwrite-mode # Ins
bindkey "\e[3~" delete-char # Delete
bindkey "\e[4~" end-of-line # End

# UTF8
setenv LC_ALL en_US.UTF-8
setenv LANG en_US.UTF-8




[VIM]
修改
~/.vimrc


加入以下設定,完成後重開 VIM 即生效:


syntax on
" 256 color
set t_Co=256
map tn :tabnext
map tp :tabprev
map to :tabnew
map tc :tabclose
nmap L
nmap H
set history=200
set ai
set ruler
set cin
set dir=/tmp
set hls
set ch=2
"set softtabstop=4
"set shiftwidth=4
set tabstop=4 shiftwidth=4 softtabstop=0
set ignorecase
"set expandtab
set bs=2 " allow backspacing over everything in insert mode
set ts=2
set ff=unix
set nobk
"set fileencodings=utf-8,big5,gbk,sjis,euc-jp,euc-kr,utf-bom,iso8859-1
"set encoding=utf-8
"set termencoding=utf-8
set encoding=utf-8
set fileencoding=utf-8
set fileencodings=utf-8,big5,euc-jp,gbk,euc-kr,utf-bom,iso8859-1
set termencoding=utf-8
set ambiwidth=double
"set =^U
"set =^B
"map :set enc=utf-8 :set termencoding=utf-8 :set fileencoding=utf-8
"map :set enc=big5 :set termencoding=big5 :set fileencoding=big5
set guifontset=-sgi-screen-medium-r-normal-*-14-*-*-*-*-*-iso8859-1,-*-mingliu-medium-r-normal-*-14-*-*-*-c-*-big5-0

hi clear
set background=dark
if exists("syntax_on")
syntax reset
endif
let g:colors_name = "koehler"
hi Normal guifg=white guibg=black
hi Scrollbar guifg=darkcyan guibg=cyan
hi Menu guifg=black guibg=cyan
hi SpecialKey term=bold cterm=bold ctermfg=darkred guifg=Blue
hi NonText term=bold cterm=bold ctermfg=darkred gui=bold guifg=Blue
hi Directory term=bold cterm=bold ctermfg=brown guifg=Blue
hi ErrorMsg term=standout cterm=bold ctermfg=grey ctermbg=blue guifg=White guibg=Red
hi Search term=reverse ctermfg=white ctermbg=red guifg=white guibg=Red
hi MoreMsg term=bold cterm=bold ctermfg=darkgreen gui=bold guifg=SeaGreen
hi ModeMsg term=bold cterm=bold gui=bold guifg=White guibg=Blue
hi LineNr term=underline cterm=bold ctermfg=darkcyan guifg=Yellow
hi Question term=standout cterm=bold ctermfg=darkgreen gui=bold guifg=Green
hi StatusLine term=bold,reverse cterm=bold ctermfg=lightblue ctermbg=white gui=bold guifg=blue guibg=white
hi StatusLineNC term=reverse ctermfg=white ctermbg=lightblue guifg=white guibg=blue
hi Title term=bold cterm=bold ctermfg=darkmagenta gui=bold guifg=Magenta
hi Visual term=reverse cterm=reverse gui=reverse
hi WarningMsg term=standout cterm=bold ctermfg=darkblue guifg=Red
hi Cursor guifg=bg guibg=Green
hi Comment term=bold cterm=bold ctermfg=cyan guifg=#80a0ff
hi Constant term=underline cterm=bold ctermfg=magenta guifg=#ffa0a0
hi Special term=bold cterm=bold ctermfg=red guifg=Orange
hi Identifier term=underline ctermfg=brown guifg=#40ffff
hi Statement term=bold cterm=bold ctermfg=yellow gui=bold guifg=#ffff60
hi PreProc term=underline ctermfg=darkblue guifg=#ff80ff
hi Type term=underline cterm=bold ctermfg=lightgreen gui=bold guifg=#60ff60
hi Error term=reverse ctermfg=darkcyan ctermbg=black guifg=Red guibg=Black
hi Todo term=standout ctermfg=black ctermbg=darkcyan guifg=Blue guibg=Yellow
hi link IncSearch Visual
hi link String Constant
hi link Character Constant
hi link Number Constant
hi link Boolean Constant
hi link Float Number
hi link Function Identifier
hi link Conditional Statement
hi link Repeat Statement
hi link Label Statement
hi link Operator Statement
hi link Keyword Statement
hi link Exception Statement
hi link Include PreProc
hi link Define PreProc
hi link Macro PreProc
hi link PreCondit PreProc
hi link StorageClass Type
hi link Structure Typedd
hi link Typedef Type
hi link Tag Special
hi link SpecialChar Special
hi link Delimiter Special
hi link SpecialComment Special
hi link Debug Special
" Hilight for tabline
hi TabLine guifg=#90fff0 guibg=#2050d0 ctermfg=black ctermbg=white
hi TabLineSel guifg=#90fff0 guibg=#2050d0 ctermfg=white ctermbg=LightMagenta
"set cursorline
"hi cursorline cterm=none ctermbg=gray
""------Restore cursor to file position in VIM
set viminfo='10,\"100,:20,%,n~/.viminfo
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif

"設定副檔名為 XXX 時,為什麼檔案
au BufNewFile,BufRead *.tpl :set ft=php " all my .tpl files ARE html
au BufNewFile,BufRead *.ini :set ft=php " all my .tpl files ARE html
au BufNewFile,BufRead *.phtml :set ft=php " all my .tpl files ARE html
au BufNewFile,BufRead *.php :set ft=php " all my .tpl files ARE html

au FileType javascript so ~/.vim/indent/javascript.vim

"for textmate, plugin
filetype on
filetype plugin on
filetype indent on
"change gf buffer open to tabedit
nmap gf :tabedit

"Folding
set foldenable

"Highlight SQL syntax in string
let php_sql_query=1
"Highlight HTML in string
let php_htmlInStrings=1
"Folding for classes & functions
"let php_folding=1
nnoremap @=((foldclosed(line('.')) < 0) ? 'zc' :'zo')

" for omni
set completeopt=longest,menu
"set completeopt=menu,menuone
imap
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
" Omni補完関連
" $VIMRUNTIME/autoload/htmlcomplete.vimの645行目をコメントアウントしておくとhtmlの補完が小文字になる

" TabでOmni補完及びポップアップメニューの選択
function InsertTabWrapper(is_shift)
if pumvisible()
return a:is_shift ? "\" : "\"
endif
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k\|<\|/' " htmlで補完できるように<,/でもOmni補完
return "\"
elseif exists('&omnifunc') && &omnifunc == ''
return a:is_shift ? "\" : "\"
else
return "\\"
endif
endfunction
inoremap =InsertTabWrapper(0)
" Shift-Tabはうまくいかないようだ
" inoremap =InsertTabWrapper(1)

" CRでOmni確定&改行
function InsertCrWrapper()
return pumvisible() ? "\\" : "\"
endfunction
inoremap =InsertCrWrapper()
"colorscheme yzlin256
"colorscheme zenburn



參考資料:
我同事 JohnPuPu 的設定檔


資料夾 與 MySQL 之備份/還原

[資料夾]
連同資料夾權限一起備份,還原時才不會有問題。


備份:
# tar pzcf 備份檔案.tar.gz 欲備份資料夾

還原:
# tar pzxvf 備份檔案.tar.gz




[MySQL]
這是 MySQL 正規的作法,比用 phpMyAdmin 匯出再匯入遇到的問題會少一些。


備份:
# mysqldump --user=root -p 資料庫名稱 > 備份檔案


還原:
# mysql -u root -p
mysql> create database 資料庫名稱;
mysql> quit

# mysql -u 使用者 -p 資料庫名稱 < 備份檔案