2018年3月28日 星期三

VirtualBox 安裝CentOS-6.9

使用VirtualBox(5.2.8 -> 4.3.10) 安裝CentOS 6.9
PHP 5.5 Redis Memcache Supervisor Beanstalk
MySQL 5.6(略,裝在Windows7 host機上 )

開機自動啟動eth0網路
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
ONBOOT=no
ONBOOT=yes

6.9 yum預設安裝的php是5.3.3版本
因為CentOS通常提供7-10年的生命週期,所以這很難讓php(和其他packages)維持在最新的版本,你能這麼做:
1. 裝5.3.3版
2. 使用源碼(source)去編譯安裝(build)
3. 使用有提供可選擇的三方源( 3rd party repository )

我選擇#3

使用webtatic.com源
https://webtatic.com/packages/php55/
Webtatic has now ended support for PHP 5.5
Webtatic 已終止PHP 5.5的支持

使用remirepo源
https://blog.remirepo.net/post/2016/09/10/PHP-5.5-is-dead
安裝remirepo源
# wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# rpm -Uvh remi-release-6*.rpm
warning: remi-release-6.rpm: Header V4 DSA/SHA1 Signature, key ID 00f97f56: NOKEY
Preparing...                ########################################### [100%]
   1:remi-release           ########################################### [100%]
查remirepo源版本
# yum info remi-release
...
Name        : remi-release
Version     : 6.9
如果你要最新的php 5.5.x,必須這樣做
# yum --enablerepo=remi,remi-php55 update -y
安裝PHP 5.5
# yum install php55-php

因為安裝的是remi源的php 5.5,所以命令行直接輸入php會找不到指令,要輸入php55
解法
# source /opt/remi/php55/enable

安裝php-fpm(yum預設的php-fpm是5.3.3的,所以要照下面這樣裝)
# yum install php55-php-fpm

安裝nginx
# yum install nginx

安裝memcached
# yum install memcached

安裝redis
# yum install redis

安裝supervisor
# yum install supervisor
啟動supervisor
# supervisord -c /etc/supervisord.conf
只有supervisor比較特殊,如果用service supervisor start的話,只會執行php artisan queue:listen tube-name 但不會生成 php artisan queue:work tube-name

安裝beanstalk
# yum install beanstalkd

配置nignx
/etc/nginx/conf.d/default.conf
server {
    ...

    location / {
        index index.php  index.html index.htm;
    }
    ...
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
s

啟動php-fpm、nginx、memcached、redis、beanstalkd
# service php55-php-fpm start
# service nginx start
# service memcached start
# service redis start
# service beanstalkd start

開啟80 port
# iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
# service iptables save
如不開啟 Windows chrome訪問 VM會報錯 ERR_CONNECTION_TIMED_OUT
telnet 會不通,但是在VM上 curl http://localhost 能訪問
配置後,記得換瀏覽器試試,不然chrome配置前訪問過,配置後仍然會卡住
看iptables有哪些規則
# iptables -S
# iptables --line -vnL

開機自動啟動php-fpm、nginx、memcached、redis、beanstalkd
# chkconfig nginx on
# chkconfig php55-php-fpm on
# chkconfig memcached on
# chkconfig redis on
# chkconfig beanstalkd on
檢查
#  chkconfig  --list nginx
nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off
0~6是linux的runlevel

Virtualbox降版本
原本使用最新版Virtualbox 5.2.8
因為重裝一台Virtualbox也會遇到開機卡在creating process for virtual machine (gui/qt) ... (1/2) 的問題
https://www.youtube.com/watch?v=20p-r5WzYXg
這邊說要降版本到4.3.10
降到4.3.10後打開虛擬機會有連不上網路的問題
# ifup eth0
Determining IP information for eth0... failed; no link present. Check cable?
解法
Virtualbox選你的虛擬機 => 設置 => 網路 => 選你的網卡 => 高級 => 勾選"接入網線"
降版本後開機報錯HostAudioNotResponding
解法(因為當虛擬機當web server用不需要聲音)
Virtualbox選你的虛擬機 => 設置 =>聲音 => 取消勾選"啟用聲音"

安裝php所需擴展
# yum install php55-php-mcrypt
# yum install php55-php-pdo
# yum install php55-php-pecl-memcached
# yum install php55-php-gd  //防止圖片驗證碼報錯:Call to undefined function Johntaa\Captcha\imagecreatefrompng()
# yum install php55-php-pecl-xdebug
# service php55-php-fpm restart


laravel 連不上redis
網頁報錯 Permission denied [tcp://127.0.0.1:6379]
但是 telnet 127.0.0.1 6379 和 redis-cli 可以通
因為
# getsebool httpd_can_network_connect
httpd_can_network_connect --> off
解法
# /usr/sbin/setsebool httpd_can_network_connect=1
Use -P option makes the change permanent. Without this option, the boolean would be reset to 0 at reboot.
使用-P選項使其永久更改,否則重開機設定會被重置
# /usr/sbin/setsebool -P httpd_can_network_connect=1  => VM下實測跑不過
解法2
把它寫到 /etc/rc.d/rc.local 下 => 解決

laravel虛擬域名千萬不能配置有底線(_) 否則會被轉跳到
http://x_web.centos/http://x_web.centos/auth/signin

讓php能在command line使用
因為用remi裝的php55,所以命令行輸入php會找不到,只能用php55
解法:
# source /opt/remi/php55/enable
因為supervisor執行時吃不到這句,所以command路徑要寫完全
command = /opt/remi/php55/root/usr/bin/php /path/artisan queue:listen ga2-xxx

查remi裝的php.ini在哪
# php -i | grep "Loaded Configuration File"
Loaded Configuration File => /opt/remi/php55/root/etc/php.ini

解決 could not find driver 錯誤
# php -m | grep pdo
pdo_sqlite  // 只有這個,沒有pdo_mysql所以出錯
安裝php55-php-mysqlnd(其實我安裝的是php55-php-pecl-mysqlnd-qc,但是會一起裝php55-php-mysqlnd)
# yum install php55-php-pecl-mysqlnd-qc
為什麼會裝php55-php-pecl-mysqlnd-qc?
因為
# yum search pdo_mysql
...
php55-php-pecl-mysqlnd-qc.x86_64 : A query cache plugin for mysqlnd


參考資料:
https://unix.stackexchange.com/questions/138019/php-5-5-13-on-centos-6-5-with-yum  PHP 5.5.13 on CentOS 6.5 with yum
https://webtatic.com/packages/php55/  PHP 5.5 on CentOS/RHEL 7.2, 6.8 and 5.11 via Yum
https://www.binarytides.com/open-http-port-iptables-centos/  Open http port ( 80 ) in iptables on CentOS
https://zh.wikipedia.org/wiki/%E8%BF%90%E8%A1%8C%E7%BA%A7%E5%88%AB  執行級別
https://www.shellhacks.com/ubuntu-centos-enable-disable-service-autostart-linux/  Ubuntu, CentOS – Enable or Disable Service Autostart in Linux
https://stackoverflow.com/questions/27982024/php-command-not-found-after-yum-install-php55-php-cli  php command not found after yum install php55-php-cli
https://stackoverflow.com/questions/8765848/troubleshooting-permission-denied-when-attempting-to-connect-to-redis-from-php  Troubleshooting “Permission denied” when attempting to connect to Redis from PHP script
https://stackoverflow.com/questions/27982024/php-command-not-found-after-yum-install-php55-php-cli  php command not found after yum install php55-php-cli
https://stackoverflow.com/questions/2852748/pdoexception-could-not-find-driver  PDOException “could not find driver”
https://stackoverflow.com/questions/17850787/where-is-the-php-ini-file-on-a-linux-centos-pc  Where is the php.ini file on a Linux/CentOS PC? [duplicate]
https://stackoverflow.com/questions/36019111/pdo-mysql-is-not-available-on-centos-6-7  pdo_mysql is not available on centos 6.7
https://serverfault.com/questions/240015/how-do-i-allow-mysql-connections-through-selinux  how do I allow mysql connections through selinux





2017年8月26日 星期六

PHP的get_called_class()

在以下例子中,我們可以使用get_called_class()來知道$result是用哪個Class的_test()函數生成的
class B extends A
{
 public static function _test(){
  return 'B_result';
 }
}

class C extends A
{
 public static function _test(){
  return 'C_result';
 }
}

class A
{
 public static function test(){
  $result = static::_test();
  echo get_called_class();
  echo $result;
 }
}

$sClass = 'C';
$sClass::test();
output:
C
$result:C_result

雖然看似簡單,但是如果_test()算出來的結果不是那麼的規律,而且$sClass這邊又不是寫的這麼直觀。可以用get_called_class()來找到A::test()的static::_test()是呼叫哪個Class的_test()

參考資料:
https://stackoverflow.com/questions/506705/how-can-i-get-the-classname-from-a-static-call-in-an-extended-php-class  How can I get the classname from a static call in an extended PHP class?



2017年8月16日 星期三

laravel 4.2 心得

安裝PHP memcached(非memcache)

錯誤:
Class 'Memcached' not found
# yum install php-pecl-memcached

PHP 5.4升級5.5

環境:centOS 7.1
http://www.techoism.com/how-to-upgrade-php-version-5-4-to-5-5-on-centosrhel/
移除舊的php
# yum remove php-common
安裝PHP 5.5和其套件
# yum -y install php55w php55w-opcache
# yum -y install php55w-xml php55w-mcrypt php55w-gd php55w-devel php55w-mysql php55w-intl php55w-mbstring
安裝其餘必要擴展
# yum install php55w-fpm
# yum install php55w-pecl-memcache
# yum install php55w-pecl-memcached
重啟php-fpm
# service php-fpm restart

安裝redis

https://www.linode.com/docs/databases/redis/install-and-configure-redis-on-centos-7
# yum install redis
啟動redis
# systemctl start redis
開機自動啟動redis(可選)
# systemctl enable redis
測試redis是否安裝成功
# redis-cli ping
PONG

安裝supervisor

# yum install python-setuptools
# yum install supervisor
配置
# vim /etc/supervisord.conf
[include]
files = supervisord.d/*.ini
files = supervisord.d/*.conf
# vim /etc/supervisord.d/your.conf
[program:ga2-main]
command = php /laravel_path/artisan queue:listen ga2-main
process_name = %(program_name)s_%(process_num)s
numprocs = 1
autostart=true
autorestart=true
stdout_logfile=/var/log/supervisor/ga2-main.log

啟動supervisor
# service supervisord start
停用supervisor
# service supervisord stop

檢查是否有跑
# ps aux | grep supervisord
root     14983  0.3  0.2 223064  2696 ?        Ss   12:25   0:01 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
# ps aux | grep ga2-main
root     16471  0.1  2.1 411032 22236 ?        S    12:26   0:00 php /laravel_path/artisan queue:listen ga2-main

勿用pip或 easy_install 安裝,測試安裝後啟動不了supervisor
移除用pip或easy_install安裝的supervisor
# pip uninstall supervisor
列出systemd的unit files
# systemctl list-unit-files




2017年7月27日 星期四

javascript 正則批配取內容(同PHP的 preg_match_all)

https://stackoverflow.com/questions/3291289/preg-match-in-javascript
使用()取出匹配的內容
ex.
var text = '||||||03';
var matches = text.match(/\|\|\|\|\|\|(\d{2})/);
console.log(matches[1]); // 03

b
(\d{2})
括號() 在這邊非常好用

2017年7月22日 星期六

Google Driver 下載發生noncesigner錯誤

https://superuser.com/questions/836345/why-is-my-chrome-failing-to-download-some-files
解法:
google chrome
ctrl + shift + del 
清除 快取圖片和檔案  和 Cookie(網站需要重新登陸)

重開chrome登陸google drive,即可下載

2017年4月22日 星期六

Windows - CentOS 遠端XDebug調試PHP

服務器CentOS
本機Window 7 + XAMPP

0. 在CentOS上安裝XDebug

0.1 查nginx 跑的php安裝在哪

因網頁phpinfo()顯示PHP版本是5.5.36、但php --version卻顯示是5.3.3
$ ps aux | grep php-fpm
root     14028  0.0  0.1 179816  9556 ?        Ss   20:30   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)

0.2 安裝Xdebug

# yum install php-devel  #需先安裝php-devel、gcc、gcc-c++、autoconf、automake
# /usr/local/php/bin/pecl install Xdebug
勿直接 pecl install Xdebug,因為which pecl的位置在/usr/bin/pecl,非nginx跑的php

0.3 檢查是否安裝成功

重啟php-fpm
# service php-fpm restart
打開phpinfo(),發現安裝成功

1. 配置php.ini

#vim /usr/local/php/etc/php.ini
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "192.168.1.173" ;你Windows本機的IP
xdebug.remote_port = 9000
xdebug.remote_log="/tmp/xdebug_log"

remote_host  => Xdebug是服務器去連你本機client開的服務

2. 打開Windows的port

讓CentOS主機能連到Windows上XDebug的服務
開始 => 控制台 => 搜尋並點擊"Windows 防火牆" => 高級設置 => 入站規則 => 新建規則 => 端口 => (TCP) 特定端口:9000 => 允許連接 => (規則全勾)下一步 =>名稱:xdebug-client
如果不做這步CentOS上/tmp/xdebug_log 會一直報錯
Log opened at 2017-04-22 11:05:08
I: Connecting to configured address/port: 127.0.0.1:9500.
W: Creating socket for '127.0.0.1:9500', poll success, but error: Operation now in progress (25).
E: Could not connect to client. :-(
Log closed at 2017-04-22 11:05:08
在/tmp/下會產生大量的 cachegrind.out.xxxxx.xxxxxx 最後把你的硬碟吃完,讓Server CPU跑滿,所以要非常注意

3. 檢查Windows上端口是否有通

3.1 在Windows的Sublime 先打開XDebug服務

Sublime -> Tools -> Xdebug -> Start Debugging

3.2 檢查Sublime Xdebug開在windows上哪個port

打開XAMPP Control Panel => Netstat => 看有沒有9000的port被plugin_host.exe執行

3.3  在CentOS上執行

# telnet 192.168.1.173 9000
Trying 192.168.1.173...
Connected to 192.168.1.173.
Escape character is '^]'.
出現這個代表這個port有通

完成1.2.3後在/tmp/xdebug_log 中看到 下列類似XML的訊息,就代表快成功了
Log opened at 2017-04-22 13:04:58
<- feature_set -i 1 -v 1 -n show_hidden
-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" command="feature_set" transaction_id="1" feature="show_hidden" success="1"></response>

4. 設定Sublime

與只在本機上使用XDebug不同的是,遠端使用必須加入path_mapping設定
Sublime -> Project -> Edit Project 打開這個專案的.sublime-project檔,並加入以下設定
{
"folders":
[
{
"path": "D:\\bear\\www\\admin168"
}
],
"settings": {
"xdebug": {
"url": "http://bear.admin.web.com/",
"path_mapping": {
"/var/wwwroot/bearadmin/web/" : "D:\\bear\\www\\admin168",
    "/www/wwwroot/codes/": D:\\bear\\www\\admin168\\codes"  // 可配置多個目錄(如果專案code不在同一個目錄下)
},
}
}
}
"/var/wwwroot/bearadmin/web/" : "D:\\bear\\www\\admin168" => "遠端專案位置":"本機專案位置"
本機專案位置為FTP地址也OK
"/var/www/bear/www/" : "\\\\192.168.1.xx\\linuxsir\\bear\\www"

https://github.com/martomo/SublimeTextXdebug
path_mapping
For remote debugging to resolve the file locations it is required to configure the path mapping with the server path as key and local path as value.
在遠端debug時,解決檔案路徑是必要的,使用path_mapping設定服務器路徑和本地路徑


How a debugging session works?
debugging session如何工作的?
The client software (Sublime with the Xdebug package in your case) starts listening on port 9000 of localhost, waiting for the server to start the connection. It probably doesn't listen on the port all the time but only when the developer tells it so.
Windows本機上的Xdebug client開始聽9000的port,等待服務器PHP Xdebug的連線。

多個開發者同時使用
1. php.ini 註解掉 xdebug.remote_host
2. 新配置xdebug.remote_connect_back = 1,當xdebug.remote_connect_back開啟時,xdebug.remote_host不作用。
3. 註解掉 xdebug.remote_autostart=1 ,避免/tmp/xdebug_log往死裡跑
4. (可略)因為我主機ip換過,所以在xdebug_log中會顯示
Log opened at 2018-03-13 10:51:18
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Checking header 'REMOTE_ADDR'.
I: Remote address found, connecting to 192.168.1.125:9000.
E: Time-out connecting to client. :-(
Log closed at 2018-03-13 10:51:19
此時我ip的9000 port也通不了($ telnet 192.168.1.125 9000)
這時候要在Windows系統防火牆重新建立入站規則,Sublime也要關掉重新開你電腦上的Xdebug服務
5. 在CLI模式下斷點
$ export XDEBUG_CONFIG="remote_host=192.168.1.125 idekey=sublime.xdebug"

Xdebug v2.6.1 (PHP 7.1.7) Xdebug Context 和Xdebug Breakpoint空白
https://stackoverflow.com/questions/17070818/xdebug-not-showing-stack-and-trace-in-sublime-text-2-on-window
https://github.com/martomo/SublimeTextXdebug/issues/172
解法:
Sublime => Prefeerences => Package Settings => Xdebug => Settings - User ( Data/Packages/User/Xdebug.sublime-settings )
加入以下配置
"super_globals": false
"max_depth": 1  => 必須改回1
以上是用 Package Control 安裝的 Xdebug Client才需要這麼做
完美解法:
https://github.com/martomo/SublimeTextXdebug/issues/172#issuecomment-454400985
1. 移除 Package Control 安裝的 Xdebug Client
2. Preferences => Browse Packages.. =>  手動把XDebug Client安裝到這個目錄下(注意:必須使用meengit的SublimeTextXdebug )
$ git clone https://github.com/meengit/SublimeTextXdebug "Xdebug Client"
2.5 刪除 .git 和 .github 資料夾避免啟動時報錯
3. 重啟Sublime

參考資料:
https://github.com/martomo/SublimeTextXdebug  SublimeTextXdebug
https://wiki.mcneel.com/zh-tw/zoo/window7firewall  從Windows防火牆開啟TCP Port 80
https://gist.github.com/kramarama/9695033  install xdebug on centos
http://stackoverflow.com/questions/39027686/xdebug-unable-to-connect-to-client-where-do-i-start-debugging-the-debugger  Xdebug unable to connect to client, where do I start debugging the debugger?
https://xdebug.org/docs/all_settings#remote_connect_back  xdebug.remote_connect_back




2017年1月15日 星期日

wordpress 在cloudflare上http轉https

前言:
兩個多月沒寫文章了,也代表兩個多月沒工作了。

掛上cloudflare CDN的網址要強制走https,因為cloudflare到原始伺服器中間的連線是HTTP,所以傳統在nginx上將http 轉成https的作法在chrome上會報錯 ERR_TOO_MANY_REDIRECTS
傳統nginx設法:
server {
       listen         80;
       server_name    my.domain.com;
       return         301 https://$server_name$request_uri;
}
server {
       listen         443 ssl;
       server_name    my.domain.com;
       # add Strict-Transport-Security to prevent man in the middle attacks
       add_header Strict-Transport-Security "max-age=31536000"; 
       [....]
}
解法:
必須在cloudflare後台Page Rules 新增規則
將 http://your.domain.com/* 增加規則 Always Use HTTPS

wordpress用HTTPS訪問,chrome會造成css, js mixed-content錯誤,因為只有主頁走https,其他css, js, 圖片檔案還是走http
解法:
wordpress 後台安裝 HTTP / HTTPS Remover 外掛,並啟用。即可

BUG:
開啟Always Use HTTPS後台無法登入,前台無法留言.. 
wordpress 後台安裝 CloudFlare Flexible SSL 外掛
最後即可用https訪問掛在cloudflare上的wordpress

參考資料:
http://serverfault.com/questions/67316/in-nginx-how-can-i-rewrite-all-http-requests-to-https-while-maintaining-sub-dom  In Nginx, how can I rewrite all http requests to https while maintaining sub-domain?
http://serverfault.com/questions/653976/redirect-loop-using-cloudflares-flexible-ssl   Redirect loop using cloudflare's Flexible ssl