顯示具有 php 標籤的文章。 顯示所有文章
顯示具有 php 標籤的文章。 顯示所有文章

2022-12-28

在Rocky9使用openssl_sign及openssl_verify失敗的原因

 

在Rocky 9 (RHEL 9),一些已被認為不安全的cipher suites 及 protocols預設是停用的,

因此像在php8使用openssl_sign或openssl_verify,若不加第四個參數,預設都是用不安全的OPENSSL_ALGO_SHA1,因此會簽章或驗證失敗,但從錯誤訊息(-1)是看不出原因的(openssl_verify 來說,1=驗證成功,0=驗證失敗,-1/false=發生錯誤)。

還有使用ssh連線到別台主機時,可能別台主機還是用舊的sha1簽章,因此連線交握過程會失敗。

若在Rocky 9(RHEL 9)要啟用不安全的湊雜及加密演算法,必須下指令:
update-crypto-policies --set LEGACY

update-crypto-policies --set DEFAULT:SHA1 #只增加SHA1

以上指令會修改以下檔案(其實還會修改其它backend設定檔,如java/openssh/nss/gnutls等,以下單指openssl):
/etc/pki/tls/openssl.cnf:
[ crypto_policy ]
.include = /etc/crypto-policies/back-ends/opensslcnf.config

/etc/crypto-policies/back-ends/opensslcnf.config
[evp_properties]
rh-allow-sha1-signatures = yes

openssl-libs所包含的設定檔:
dnf repoquery -l openssl-libs | grep etc

ref:
https://bugs.launchpad.net/ubuntu/+source/crypto-policies/+bug/1926664/comments/6
https://gitlab.com/redhat-crypto/fedora-crypto-policies
https://forums.rockylinux.org/t/php-8-0-13-openssl-verify-fails-on-rocky-9/7478
https://github.com/php/php-src/issues/9686
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/security_hardening/using-the-system-wide-cryptographic-policies_security-hardening
https://www.redhat.com/en/blog/rhel-security-sha-1-package-signatures-distrusted-rhel-9

2022-10-03

php-fpm + nginx + codeigniter 使用 virtual host設定調校

 當使用nginx + php-fpm建立virtual host (server block)時,要注意權限問題,最好不同的site用不同的使用者去存取php-fpm的pool,以下以建立site1及site2二個網站為例:

1.建立使用者,並記得修改/etc/passwd為nologin
sudo groupadd site1
sudo useradd -g site1 site1
sudo groupadd site2
sudo useradd -g site2 site2

2021-05-19

apache + php 連線過多卡住

 apache + php 連線過多卡住,若是資料庫問題,可考慮使用proxysql,畢竟php沒有sql thread pool,就算是用 pconnect() 設定上也是很麻煩,也且一個連線也只能給同一個process的thread共用,只能說是半套,那不如直接用connect()連完就drop即可。

若不是資料庫而是apache撐不住,可以調整 prefork模組的設定(可用apachectl -V或httpd -V查看是不是prefork模式),

譬如 apache 2.4的/etc/apache2/mods-available/mpm_prefork.conf:

<IfModule mpm_prefork_module>

        StartServers            4

        MinSpareServers         15

        MaxSpareServers         75

        ServerLimit             3000

        MaxRequestWorkers       3000

        MaxConnectionsPerChild  9000

</IfModule>

或apacpe 2.0 的 /etc/httpd/conf/httpd.conf

<IfModule prefork.c>

StartServers       8

MinSpareServers    15

MaxSpareServers   75

ServerLimit      3000

MaxClients       3000

MaxRequestsPerChild  9000

</IfModule>


2021-03-17

phpmyadmin在innodb表格資料超過50000筆時,無法正確抓到筆數並切換頁

 

從phpmyadmin 4.8.0後,因效能考量,預設50000筆內是用select count取得資料列數,超過50000筆時才用show table status,但常會造成取的筆數不對,因此可以把phpmyadmin的$cfg['MaxExactCount']參數加大:

$cfg['MaxExactCountViews'] = 0;//disable trying to count the number of rows in any view
$cfg['MaxExactCount'] = 0;//disable correcting the InnoDB estimates

或者去點擊table總表的row count的數字去更新也行 (沒試過)

ref:
https://docs.phpmyadmin.net/en/latest/config.html#cfg_MaxExactCount
https://docs.phpmyadmin.net/en/latest/faq.html#faq3-11
https://stackoverflow.com/questions/10811405/phpmyadmin-and-big-tables
https://www.kavoir.com/2012/07/make-phpmyadmin-show-exact-number-of-records-for-innodb-tables.html

2021-01-21

iItelliJ 設定PHP XDEBUG環境

 一、安裝composer、php-cgi(注意,必須是cgi,不能用cli或fpm)、php-xdebug
sudo apt-get install php-cgi php-xdebug
sudo curl -s https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chown root.root /usr/local/bin/composer
sudo chmod 755 /usr/local/bin/composer

二、修改xdebug設定
sudo vi /etc/php/7.2/cgi/conf.d/20-xdebug.ini
加上
xdebug.remote_enable=1

php-cgi --version //須出現xdebug字眼
with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans

三、chrome或firefox安裝xdebug plugin
https://www.jetbrains.com/help/idea/2020.3/browser-debugging-extensions.html
擴充功能選項的IDE Key選擇PHPSTORM

四、修改IntelliJ的language & frameworks / PHP 設定
新增 /usr/bin/php-cgi

五、執行選單 Run / Web Server Debug Validation,看是否能正常debug

若使用intellij內建的web server + php-cgi,可能會沒有$_SERVER['SERVER_NAME'] 造成無法正常透過瀏覽器debug,下面的ref有解法但不一定有用,似乎是intellij的bug,似乎還是得換成nginx或apache才能解決,若用外部server,須到 language & frameworks / PHP / Servers及build,excution,deployment->Deployment去新增對應的server


ref:
https://www.jetbrains.com/help/idea/2020.3/configuring-xdebug.html
https://www.jetbrains.com/help/idea/2020.3/zero-configuration-debugging.html
https://youtrack.jetbrains.com/issue/WI-54542
https://stackoverflow.com/questions/23344106/php-storm-debugging-creates-empty-server-definitions
https://www.cnblogs.com/Renyi-Fan/p/9117895.html

2018-01-22

mac安裝composer & php-cs-fixer

安裝 php 7.2
brew update
brew tap homebrew/dupes
brew tap homebrew/php
brew install php72 (需php >= 7)
mv /usr/bin/php /usr/bin/php55
sudo ln -s /usr/local/bin/php /usr/bin/php

which php
# mac原本php
/usr/bin/php
# Homebrew的php
/usr/local/bin/php

# 重新安裝 PHP 7.2
brew reinstall php@7.2
# 測試重建鏈
brew link --overwrite --force --dry-run php@7.2
# 重建鏈結
brew link --overwrite --force php@7.2

安裝composer (php >= 7)
brew install composer

安裝xdebug
pecl install xdebug

安裝 php-cs-fixer
composer global require friendsofphp/php-cs-fixer
修改~/.profile
加入
export PATH="$PATH:$HOME/.composer/vendor/bin"
source ~/.profile
(或用brew: brew install homebrew/php/php-cs-fixer)

Bitnami Nginx Stack 取消 php opache

Bitnami Nginx Stack 預設是有PHP Opcache的,
因此修改php時無法即時看到修改結果,
當開發機使用時,讓人覺得很鬧。

修改
/Applications/nginxstack-1.10.1-2/php/etc/php.ini

opcache.enable=0

若安裝的是LAMP,需再修改
/Applications/nginxstack-1.10.1-2/apache2/conf/httpd.conf

#Include conf/pagespeed.conf
#Include conf/pagespeed_libraries.conf
給註解起來


ref:
https://docs.bitnami.com/general/infrastructure/lamp/#how-to-disable-the-cache-in-the-server

2018-01-17

WAMP使用PHP7

依WAMP的需求安裝Visual C++ Redistributable 2015
https://www.microsoft.com/en-us/download/details.aspx?id=48145

下載PHP 7
http://windows.php.net/download/
選擇x86或x64(依WAMP版本決定)Thread Safe版本

建立 wamp\bin\php\php7.2 資料夾
從 wamp\bin\php\php5.x 複製 php.ini 及 wampserver.conf 到 wamp\bin\php\php7.2

修改 php.ini
將 extension_dir 修改成 php7.2路徑
將 track_errors = On 註解掉
將 extension=php_mysql.dll 註解掉
複製 php_xdebug-2.6.0beta1-7.2-vc15-x86_64.dll(TS)到 wamp\bin\php\php7.2\zend_ext
在[xdebug]新增 xdebug.remote_enable = on
在[xdebug]新增 xdebug.remote_autostart = on
複製一份 php.ini,改名為 phpForApache.ini (wamp\bin\apache\apache2.4.9\bin\php.ini會link到此檔,而且apache會載入apapche/bin/php.ini!)

※注意:執行wamp時,看一下phpinfo()中的php.ini路徑是否正確,以及xdebug模組是否順利載入。
若是跑php CLI要載入xdebug,跑一下php --ini,看一下載入的php.ini路徑是否正確,
或執行php -S localhost:80,看一下phpinfo()看載入的php.ini路徑是否正確

修改 wampserver.conf
phpConf['apache']['2.4']['LoadModuleName'] = 'php7_module';
$phpConf['apache']['2.4']['LoadModuleFile'] = 'php7apache2_4.dll';

啟動WAMP並選擇PHP版本
Start Wamp > PHP > Version > 7
(Apache 需 >=2.4 )

2015-09-30

PHP 的 stack trace

若是要追踨單支php的stack,可用如下做法:
$debug = debug_backtrace();
file_put_contents("/path/to/log.txt", date("Y-m-d H:i:s",time())." file:".$debug[1]['file']." php_thread:".getmypid().", mysql_thread:".mysql_thread_id(self::$Link_ID)."(".self::$Link_ID.")\r\n", FILE_APPEND|LOCK_EX);

若是要追踨整個php的stack,可能得用gdb,且前提是php在compile時有加入 --enable-debug 選項:
$ gdb -p $PHP_PID
(gdb) bt     # Get a system-level stacktrace, might already give some info
(gdb) source /path/to/php-src/.gdbinit # Load some useful macros
(gdb) dump_bt executor_globals.current_execute_data
            # Macro from PHP's .gbinit giving PHP stack trace
            # If you for whatever reason are using a thread-safe PHP build you have to do this:
(gdb) ____executor_globals
(gdb) dump_bt $eg.current_execute_data

2015-03-23

爬iPeen座標的小程式

昨天幫網友寫的一個爬iPeen座標的小程式。
http://goo.gl/R4EeIW

如果用網頁介面跑到一半會中斷,檢查Apache的 http.conf 及 httpd-mpm.conf
...
Timeout 300000
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 0
...

   ThreadsPerChild    350 #default 150
   MaxRequestsPerChild    10000 #default 0
   ThreadStackSize 8388608 #8MB <--- fmodule="" preg_match="">
...

ini_set('max_execution_time', 0);
ini_set('memory_limit', '2G');
@ob_end_flush();
@ob_implicit_flush();

$page = 2;
$regexp1 = '/(?:[\s\S]*?)]*>([\s\S]*?)<\/a>(?:[\s\S]*?)<\/h3>/i';
$regexp2 = '/(?:[\s\S]*?)longitude"\s+?content="(.*)"\s?\/?>(?:[\s\S]*?)]+?data-action="up_small_classify"[^>]*?>([\s\S]*?)<\/a>(?:[\s\S]*?)]+?data-action="up_address"[^>]*?>([\s\S]*?)<\/a>/i';
$www = 'http://www.ipeen.com.tw';
$www_page = $www.'/search/taiwan/000/4-7-0-0/?p=';

for($i=1;$i<=$page;$i++){
 preg_match_all($regexp1, get_html($www_page.$i), $t);
 foreach($t[1] as $key=>$url){
     preg_match($regexp2, get_html($www.$url), $info);
     echo trim($t[3][$key])."(".$t[2][$key].")\n".trim($info[3])."\n".trim($info[4])."\n".$info[1].", ".$info[2]."\n=================================".PHP_EOL;
    }
}

function get_html($url=''){
    if(empty($url)) return null;
 $opts = stream_context_create(['http'=>['method'=>'GET', 'max_redirects'=>200, 'header'=>"User-Agent: 安全衛士360+hao123\r\n"]]);
 return file_get_contents($url, false, $opts);
}

2015-02-03

APACHE & PHP 基本安全設定

.htaccess
http://www.socss.cn/2011/php_0902/167.html

php.ini
http://jamesbond0479.blog.163.com/blog/static/241475820115231759458/

在php的生產環境下建議做一些額外的安全措施:
enable_dl = Off
disable_functions = exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen,show_source

如果你建立的是虛擬主機那麼你需要一份更長的函數列表:
disable_functions = escapeshellarg, escapeshellcmd, exec, passthru, proc_close, proc_get_status, proc_open, proc_nice, proc_terminate, shell_exec, system, ini_restore, popen, dl, disk_free_space, diskfreespace, set_time_limit, tmpfile, fopen, readfile, fpassthru, fsockopen, mail, ini_alter, highlight_file, openlog, show_source, symlink, apache_child_terminate, apache_get_modules, apache_get_version, apache_getenv, apache_note,apache_setenv, parse_ini_file

[CI] 解決FastCGI 下無法獲得PATH_INFO的問題

http://codeigniter.org.cn/forums/thread-5884-1-4.html

http://codeigniter.com/forums/viewthread/68698/#389281

$_SERVER['PATH_INFO']:
http://php.net/manual/en/reserved.variables.server.php

[PHP] 加速方法 進程表

今天安裝了XCache來對php的opcode作緩存加速,再加上8 CPUs,果然有飛快的感覺。
但其實還有很多其它的加速方法,有空再來一個個加上去,先做一下note。

XCache: opcode caching (已安裝)
memcache: database calls, API calls, or page rendering caching
FastCGI: 將預設以ISAPI在run的PHP process改以FastCGI方式執行 (PHP-FPM)
Varnish: http proxy (比squid效能優秀,大量減少php運算及mysql查詢)
Nginx:http server (比apache效能優秀,增加效能10倍以上(據說...))

uploadify、swfupload的cookie問題

http://www.javaworld.com.tw/jute/post/view?bid=49&id=280398

http://mini.nidbox.com/diary/read/1323

Flash 帶的 USER_AGENT是 Shockwave Flash,
Flash不支援非IE核心的瀏覽器cookie送出,
FLASH的POST資料,會將+變為空白,
Falsh會修改上傳檔案的MIME TYPE 成為 application/octet-stream

WTF......

難怪這二天拿Uploadify做上傳相片功能,
使用Chrome時Cookie會抓不到,ADOBE是有收M$錢逆...
SWFupload也是一樣的問題。

Caching with Codeigniter

http://stevenbenner.com/2010/12/caching-with-codeigniter-zen-headaches-and-performance/

說明詳盡,很棒的文章!

2015-02-02

居留證號檢驗 PHP version

網路有JAVA、Javascript version的,沒有PHP version,只好自已刻一個...跟檢驗身份證號的方法意思是差不多的。

Content-Encoding: HTTP deflate在IE的issue

是的,又是萬惡的IE。

這是我之前處理壓縮檔在IE下載與解壓縮的過程中,所遇到的問題。