2018年6月21日 星期四

解決 307 Internal Redirect

某個網址原本有https被拿掉後,chrome輸入http的網址自動轉跳到https然後顯示 ERR_CONNECTION_REFUSED
開發者工具勾選Preserve Log看,發現是 307 Internal Redirect

如何解決?
https://stackoverflow.com/questions/25277457/google-chrome-redirecting-localhost-to-https/28586593#28586593
chrome瀏覽器打開 chrome://net-internals/#hsts 
先在 Query HSTS/PKP domain 查你被 307 Internal Redirect 的域名,如果是Not Found,這邊就不是你要的答案
如果有內容,如下圖
那就到 Delete domain security policies 輸入你被 307 Internal Redirect 的網址,然後Delete 他
現在你能使用HTTP訪問該網址了






2018年4月26日 星期四

解決JSON.stringify()報錯Converting circular structure to JSON

因為現在遇到一套程式一部分被js uglify過了。使用JSON.stringify()要看他的object全貌,報錯
JSON.stringify(gagame.Games.getCurrentGame().getCurrentGameMethod())
Uncaught TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at <anonymous>:1:6
解法:
使用 circular-json
將 circular-json.js  直接貼到console下就可以照下面方法用了
CircularJSON.stringify(gagame.Games.getCurrentGame().getCurrentGameMethod())
...// json string will show here
(然後複製貼到sublime後去美化JSON觀察這個object)



參考資料:
https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json/31817879#31817879  JSON.stringify, avoid TypeError: Converting circular structure to JSON
https://github.com/WebReflection/circular-json  CircularJSON




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,即可下載