發表文章

[PHP] Lumen 沒有 php artisan serve 但又想要有類似的功能怎辦?

來源: https://stackoverflow.com/questions/47385324/laravel-controller-error-when-using-request-in-function 解法: php -S 127.0.0.1:8000 -t ./public

[PHP] 在 MacOS 上安裝 php mongodb driver

圖片
因為用mac透過 pecl 去做一定會狂出問題 所以自己編譯會比較方便 $ git clone https://github.com/mongodb/mongo-php-driver.git $ cd mongo-php-driver $ git submodule update --init $ phpize $ ./configure $ make all $ sudo make install 假如出現 Security/Security.h file not found 可以參考這篇 https://stackoverflow.com/questions/53531008/pecl-install-mongodb-on-mac-fatal-error-security-security-h-file-not-found cd mongo-php-driver/include ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Security.framework/Versions/A/Headers/ Security ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/A/Headers/ CoreFoundation brew install pcre make 最後記得編輯 php.ini vi /etc/php.ini 在裡面新增 extension=mongodb.so 可以先用 php -i | grep extension_dir 得知extension directory在哪 大概像是這樣: 之後進去該目錄,檢查 mongodb.so 是否在裡面 然後用 php -m | grep mongo 檢查是不是有進到設定檔中 這篇也有參考價值 https://www.jianshu.com/p/1fd7c1927ae5 brew install autoconf 如果出現找不...
ifconfig eth0:0 192.168.2.4

[Linux] CentOS python2.6 升級 2.7 但不影響yum, 安裝pip

Ref: https://snippetinfo.net/media/1955 https://www.itread01.com/articles/1498939033.html yum groupinstall "Development tools" yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel cd /opt wget --no-check-certificate https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz tar xf Python-2.7.6.tar.xz cd Python-2.7.6 ./configure --prefix=/usr/local make && make altinstall 其實主要需要解決的是系統 Python soft link 指向 Python2.7 版本後 因為yum是不兼容 Python 2.7的 所以yum不能正常工作 我們需要指定 yum 的Python版本 vi /usr/bin/yum 將文件頭部的 !/usr/bin/python 改成 !/usr/bin/python2.6 (此處為系統內原本python2.6的位置) 然後把新裝好的 python 用 soft link 指回去 /usr/bin/python 就可以囉! ln -s /usr/bin/python2.7 /usr/local/bin/python 另外不想用yum install python-pip的話 可以用: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py # pip安裝好之後可以使用 pip -V 看版本 # 要安裝套件就直接 pip install 套件 pip install flask pip install redis pip install scrapy

[Nginx] 使用轉址做負載平衡

Ref: http://blog.51cto.com/smileyouth/1623766 https://serverfault.com/questions/597671/load-balancing-in-nginx-with-redirect-rather-than-proxy https://stackoverflow.com/questions/46515842/nginx-proxy-pass-same-protocol-http-https 有個需求是這樣的 現在前端使用某一個 url, 假設是 http://example.com/test 然後希望前端載入這個網址的時候, 可以讓這個 url 隨機導向N個不同的網域, 假設是 a.example.com, b.example.com, c.example.com 但是後面路徑要是一樣的 也就是 http://example.com/test?t=1 -> http://c.example.com/test?t=1 這樣該怎麼做呢? 先假設 nginx 路徑是安裝在 /usr/local/nginx 先建立一個 /usr/local/nginx_module的目錄 安裝這個 module 要先下載另一個 ngx_devel_kit 先到  https://github.com/simplresty/ngx_devel_kit/tags 找個版本下載 cd ~ && wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz tar zxfv v0.3.1rc1.tar.gz #把解壓縮出來的目錄去掉版本號 mv ngx_devel_kit-0.3.1rc1 ngx_devel_kit && mv ngx_devel_kit /usr/local/nginx_module/ 然後接著下載  set-misc  這個 module 先到  https://github.com/openresty/set-misc-nginx-module/tags 找個版本下載 c...

[Nginx] 幫 nginx 特定網址加上 basic auth

Ref: http://www.ttlsa.com/nginx/nginx-basic-http-authentication/ https://serverfault.com/questions/505482/nginx-auth-only-for-given-location 假如說有需要對特定的網址加上簡單的驗證保護 又懶得去新增比較複雜的會員機制 就可以考慮簡單的 Nginx Basic Auth 這個方法 一般情況下, ngx_http_auth_basic_module 是已經被包含在預設安裝內了 所以假設我需要對 /abc 這個網址去做驗證 可以新增以下語法(以PHP為例) location = /abc { auth_basic "basic http auth for this site"; auth_basic_user_file /etc/nginx/conf.d/htpasswd; try_files $uri $uri/ /index.php?$args; } 然後可以使用 htpasswd 來驗證 以上面的例子而言, 可以將帳號密碼新增到 /etc/nginx/conf.d/htpasswd內 假設新增帳號: abc, 密碼: 123456 printf "abc:$(openssl passwd -crypt 123456)\n" >>/etc/nginx/conf.d/htpasswd 接著可以確認自己 nginx 有沒有出錯 可以使用以下指令並 reload nginx nginx -t nginx -s reload

mysql study

ALTER table 是鎖住整個 table # 1. 檢查支票帳戶餘額是否大於等於200 # 2. 從支票帳戶餘額扣200轉出 # 3. 從存款帳戶餘額加200轉入 簡單的transaction START TRANSACTION; select balance from checking where customer_id = 1 and balace >= 200; update checking set balance  = balance - 200 where where customer_id = 1; update savings set balance = balance + 200 where customer_id = 1; COMMIT; 隔離級別 READ_UNCOMMITTED (未提交讀) 一個transaction中修改的東西, 在其他 transaction中是可見的, 也就其他事務可以讀取另一個事務尚未提交 (commit) 的東西, 也被稱作 dirty read, 容易造成很多問題, 效能也不一定會好 READ_COMMITTED 一個transaction在committed之前所做的任何改變都不會被其他transaction看到 REPEATABLE READ 保證了一個transaction讀取同樣的record會是一樣的結果, 理論上無法解決Phantom Read (幻讀)的問題, 幻讀是某個transaction A在讀取某個範圍的資料時, 別的transaction剛好在這個範圍內新增一筆資料, 造成這個transaction A會產生Phantom Row, InnoDB透過多版本併發控制 (Multi Version Concurrency Control) 解決幻讀的問題 SERIALIZABLE 最高的隔離級別, 強迫所有transaction一個接一個執行, 就不會有Phantom Read的問題, 也就是在讀取的每一行都加上read lock(讀鎖), 會導致大量超時與搶鎖的問題,只有在非常強調資料一致性與可以接受非併發環境才可以考慮使用 MySQL預設開啟 autocommit show variables like '...