How to add Respond Json function in OPENX Without plugin

1.add folder in {documnet_root}/www/{yourname}/

ex. adserver/json/

2.add php file ex.api.php

3.add content like this


//xid param from your url like {yourdoamin}/www/json/api.php?xid=10
//xid eq zoneid
//set your AD like banner type


$_GET['zoneid'] =$_GET['xid'];




// Require the initialisation file
require_once '../../init-delivery.php';

// Required files
require_once MAX_PATH . '/lib/max/Delivery/adSelect.php';
require_once MAX_PATH . '/lib/max/Delivery/flash.php';

// No Caching
MAX_commonSetNoCacheHeaders();

//Register any script specific input variables
MAX_commonRegisterGlobalsArray(array('refresh', 'resize', 'rewrite', 'n'));

// Initialise any afr.php specific variables
if (!isset($rewrite))   $rewrite = 1;
if (!isset($refresh))   $refresh = 0;
if (!isset($resize))    $resize = 0;
 
// Get the banner

$banner = MAX_adSelect($what, $campaignid, $target, $source, $withtext, $charset, $context, true, $ct0, $loc, $referer);
// Send cookie if needed
if (!empty($n)) {
    if (!empty($banner['html'])) {
        // Send bannerid headers
        $cookie = array();
        $cookie[$conf['var']['adId']] = $banner['bannerid'];
        // Send zoneid headers
        if ($zoneid != 0) {
            $cookie[$conf['var']['zoneId']] = $zoneid;
        }
        // Send source headers
        if (!empty($source)) {
            $cookie[$conf['var']['channel']] = $source;
        }
        // Set the cookie
        MAX_cookieAdd($conf['var']['vars'] . "[$n]", json_encode($cookie, JSON_UNESCAPED_SLASHES));
    } else {
        MAX_cookieUnset($conf['var']['vars'] . "[$n]");
    }
}

MAX_cookieFlush();

MAX_commonSendContentTypeHeader('text/html', $charset);

// Rewrite targets in HTML code to make sure they are
// local to the parent and not local to the iframe
if (isset($rewrite) && $rewrite == 1) {
    $banner['html'] = preg_replace('#target\s*=\s*([\'"])_parent\1#i', "target='_top'", $banner['html']);
    $banner['html'] = preg_replace('#target\s*=\s*([\'"])_self\1#i', "target='_parent'", $banner['html']);
}

// Build HTML
$outputHtml = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n";
$outputHtml .= "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>\n";
$outputHtml .= "<head>\n";
$outputHtml .= "<title>".(!empty($banner['alt']) ? $banner['alt'] : 'Advertisement')."</title>\n";

// Include the FlashObject script if required
if (isset($banner['contenttype']) && $banner['contenttype'] == 'swf') {
    $outputHtml .= MAX_flashGetFlashObjectExternal();
}

// Add refresh meta tag if $refresh is set and numeric
if (isset($refresh) && is_numeric($refresh) && $refresh > 0) {
    $dest = MAX_commonGetDeliveryUrl($conf['file']['frame']).'?'.$_SERVER['QUERY_STRING'];
    parse_str($_SERVER['QUERY_STRING'], $qs);
    $dest .= (!array_key_exists('loc', $qs)) ? "&loc=" . urlencode($loc) : '';

    $refresh = (int)$refresh;
    // JS needs to be escaped twice: the setTimeout argument is evaluated at runtime
    $jsDest = addcslashes(addcslashes($dest, "\0..\37\"\\"), "'\\");
    $htmlDest = htmlspecialchars($dest, ENT_QUOTES);

    // Try to use JS location.replace since browsers deal with this and history much better than meta-refresh
    $outputHtml .= "
    <script type='text/javascript'><!--// <![CDATA[
        setTimeout('window.location.replace(\"{$jsDest}\")', " . ($refresh * 1000) . ");
    // ]]> --></script><noscript><meta http-equiv='refresh' content='".$refresh.";url={$htmlDest}'></noscript>
    ";
}

if (isset($resize) && $resize == 1) {
    // If no banner found, use 0 as width and height
    $bannerWidth = empty($banner['width']) ? 0 : $banner['width'];
    $bannerHeight = empty($banner['height']) ? 0 : $banner['height'];

    $outputHtml .= "<script type='text/javascript'>\n";
    $outputHtml .= "<!--// <![CDATA[ \n";
    $outputHtml .= "\tfunction MAX_adjustframe(frame) {\n";
    $outputHtml .= "\t\tif (document.all) {\n";
    $outputHtml .= "\t\t\tparent.document.all[frame.name].width = ".$bannerWidth.";\n";
    $outputHtml .= "\t\t\tparent.document.all[frame.name].height = ".$bannerHeight.";\n";
    $outputHtml .= "\t\t}\n";
    $outputHtml .= "\t\telse if (document.getElementById) {\n";
    $outputHtml .= "\t\t\tparent.document.getElementById(frame.name).width = ".$bannerWidth.";\n";
    $outputHtml .= "\t\t\tparent.document.getElementById(frame.name).height = ".$bannerHeight.";\n";
    $outputHtml .= "\t\t}\n";
    $outputHtml .= "\t}\n";
    $outputHtml .= "// ]]> -->\n";
    $outputHtml .= "</script>\n";
}

$outputHtml .= "<style type='text/css'>\n";
$outputHtml .= "body {margin:0; height:100%; background-color:transparent; width:100%; text-align:center;}\n";
$outputHtml .= "</style>\n";
$outputHtml .= "</head>\n";

$jsonArr['html'] =  ($banner['html']);

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials true');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,token,Access-Token');
header('Access-Control-Max-Age: 1728000');
header("Content-type: application/json; charset=utf-8");

echo json_encode($jsonArr);
exit;

Laravel 5/7 Redis的分庫設定

在此之前,有一些預備工作要做的

Redis driver

按照官方建議 用phpredis

redis是可以設定他在他一個db內的,例如我的全站設定檔是在db3,我的客戶暫存是在db1,預設是在db0

當我在連線設定沒有指定,預設會跑到db0,那為什麼要分呢?因為redis是沒有辦法做特徵全部刪除的,例如他無法做到刪除key name 為abc_開頭的資料 ,我必須要知道keyname的完整名稱,這樣如果我的網站站存檔要全部更新的話,就很麻煩,你可能還要跑資料庫迴圈把相關資料叫出來,不能爽快的flushdb…,但如果你今天把資料分門別類,那你就可以對db3下達flush指令,這樣效率會高一點,而且你要改小部分資料的時候,在沒有看code的情況下,透過工具也比較好找,例如我只要改一個設定檔案,我只要透過Medis之類的GUI工具就可以修改,不需要半夜打電話叫工程師起床尿尿 叫他看一下他設定的key name是什麼

在config/database.php 內 記錄了redis的設定


'redis' => [

        'client' => env('REDIS_CLIENT', 'phpredis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
        ],

        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_DB', '0'),
        ],
        'db1' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' =>'1',
        ],
        'db2' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' =>'2',
        ],
        'db3' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' =>'3',
        ],
        'cache' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_CACHE_DB', '1'),
        ],

    ],
這裡的陣列中,我設定了幾個索引名稱,這個在後來的連線上會用到

設定連線




        $setobj = Redis::connection('db3');

        $rf=[];
        if($setobj->exists($key)){
            $rf['exist']=1;
            $rf['rs']=$setobj->get($key);

        }else{
            $rf['exist']=0;

        }

對應到上面的索引設定,我就是從db3裡面撈取資料,而不是從預設的db0撈取,這樣不同的程式甚至是不同的功能,就可以共用同一個redis,而不會互相影響到

PHP JENKINS 設定紀錄

原始碼管理

Repository URL

這裡是填入Repository URL 的地方 這裡的設定用http

Credentials

因為用的是http,所以輸入git 帳號密碼

Branches to build

*/master

如果是別的branch 就寫branch名稱 當然 這個branch是在線上的 不是你本地端的

建置環境

Delete workspace before build starts

建置bash

#遠端主機 ,你可以設定多個 這個是單主機設定
HOST="{your_host_ip}"
#多主機 中間空白
HOST="{ip1} {ip2} {ip3}" 

#PHP 安裝 composer
#主機要先裝好composer
COMPOSER=/usr/local/sbin/composer
COMPOSERINSTALL=".composer.sh"

#遠端主機 的HTM資料來 設定擁有者 這裡設計上是讓nginx掌管php目錄
HTM=/var/html/blog
HTMOWNER="nginx:nginx"
 

#遠端主機 的 SSH 登入設定
RUSER="{user}"

# SSH 連線參數
#登入遠端主機要用的key,這裡的位址是指jenkins的位置
OPT="-i ~/.ssh/key.pem"
OPT="$OPT -o StrictHostKeyChecking=no"
OPT="$OPT -o UserKnownHostsFile=/dev/null"
OPT="$OPT -o LogLevel=ERROR"
OPT="$OPT -o ConnectTimeout=20"

SCP="scp $OPT -r -p "

SRC=$WORKSPACE

FAILHOST=""
FAILTOTAL=0
TOTAL=0
for IP in $HOST
do
    TOTAL=$((TOTAL+1))
done

IPSEQ=0
# 上傳到 遠端主機
for IP in $HOST
  
do
	IPSEQ=$(($IPSEQ+1))
    echo "Update Server : $IP ($IPSEQ/$TOTAL/$FAILTOTAL)"
	SSH="ssh $OPT $RUSER@$IP "
	SCP="scp $OPT -r -p "
    RSYNC="rsync -a"
    # 測試連線
    if ! $SSH /bin/true; then
        echo "Update Fail $IP"
        FAILHOST="$FAILHOST $IP"
        FAILTOTAL=$((FAILTOTAL+1))
        continue 
    fi
    
    # 先建資料夾
    $SSH sudo mkdir -p $HTM
    sleep 1
	# 複製 HTM
    $RSYNC --chown=$HTMOWNER -e "ssh $OPT" $SRC/ $RUSER@$IP:$HTM/
    sleep 1
    # 更換設定檔
   #把正式的.env檔設定上去 這裡設定檔叫做.prod
    $RSYNC --chown=$HTMOWNER -e "ssh $OPT" $SRC/.prod $RUSER@$IP:$HTM/.env
    # 檢查 composer 安裝
    $SSH sudo sh $HTM/$COMPOSERINSTALL $HTM
    # 用 composer 安裝
    $SSH sudo $COMPOSER i -d $HTM
    #LARAVEL  清緩存 三指令
    CMD="cd $HTM;
    php artisan cache:clear;
    php artisan config:clear;
    php artisan route:clear;
    "
    $SSH $CMD
done

if [ ! -z "$FAILHOST" ];then
    echo "FAIL IP TOTAL $FAILTOTAL / $TOTAL"
	echo $FAILHOST
    /bin/false
fi

使用choco安裝LxRunOffline 在wsl2的CentOs

這個標題真的是有點深奧難懂XD

wsl2是沒有CentOS,而Ubuntu在18以後的hosts 與local dns的設定令我嫌棄,所以來安裝CentOS.
LxRunOffline是來控制wsl2安裝非原廠套件的一個東西,而choco(chocolatey的簡稱)這個windows版本的套件管理工具是很方便的安裝LxRunoffline
所以要先安裝choco,我是在cmder的as admin環境下安裝的,還不錯用

1.install lxrunoffline
choco install lxrunoffline
這樣就把lxrunoffline裝好了
2.test lxrunoffline
LxRunOffline.exe 執行後可以看到所有相關指令
3.下載CentOS image file
這裡用的方式是走docker image的方式當作他的主要檔案,github上的build.sh編譯需要安裝很多環境,就直接給下載網址比較快

https://raw.githubusercontent.com/CentOS/sig-cloud-instance-images/CentOS-7-x86_64/docker/centos-7-x86_64-docker.tar.xz

4.安裝CentOS
LxRunOffline.exe install -n CentOS -d D:\wsl\CentOS -f D:\wsl_img\centos-7-x86_64-docker.tar.xz

啟動wsl的CentOS
輸入wsl -l 可以看到CetnOS 安裝起來了
輸入wsl -d CentOS 就會直接進入到CentOs內
剩下的就可以操作了

laravel 5 增加一組route group 設定

我今天要增加一組route controller與 route檔案
名字叫ajax
要到 class RouteServiceProvider 修改
1.增加namespace
protected $cust_namespace = ‘App\Http\Controllers\ajax’;
2.
去map route增加一組設定
$this->mapAjaxRoutes();
3.增加method mapAjaxRoutes

protected function mapAjaxRoutes()
{
Route::prefix(‘ajax’)
->middleware(‘ajax’)
->namespace($this->cust_namespace)
->group(base_path(‘routes/ajax.php’));
}

 

完整設定會是這樣

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php 
namespace App\Providers; 
use Illuminate\Support\Facades\Route; 
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; 
class RouteServiceProvider extends ServiceProvider { 
/** * This namespace is applied to your controller routes. * * In addition, it is set as the URL generator's root namespace. * * @var string */ 
    protected $namespace = 'App\Http\Controllers'; 
    protected $cust_namespace = 'App\Http\Controllers\ajax'; /** * Define your route model bindings, pattern filters, etc. * * @return void */ 
 
     public function boot() { // parent::boot(); } 
 
     /** * Define the routes for the application. * * @return void */ 
    public function map() { $this->mapApiRoutes();
        $this->mapAjaxRoutes();
        $this->mapWebRoutes();
 
        //
    }
 
    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }
 
    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }
    /**
     * Define the "Ajax" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapAjaxRoutes()
    {
        Route::prefix('ajax')
            ->middleware('ajax')
            ->namespace($this->cust_namespace)
            ->group(base_path('routes/ajax.php'));
    }
}

apache php-fpm Primary script unknown\n

一直遇到這個問題 AH01071: Got error ‘Primary script unknown\n

搞了很久

基本上大致解決方向有三種

1.document_root路徑設定錯誤

2.php-fpm 的權限問題

-查php-fpm的user 與 group是否與apache相同

3.selinux || firwall等系統權限問題

4.ProxyPassMatch

這個坑真的很坑….

apache 2.4設定php-fpm

可以用的設定應該只有

ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://0.0.0.0:9000/webroot/phptest/$1

大部分的網上文件多半說明需要配合此設定檔的DocumentRoot
but…並不完全是這樣的
apache的DocumentRoot是給apache用的
但是給fcgi後面的root,是要看你fcgi機器上的路徑
這件事情通常會是在docker上面發生
舉個例子
我已經在host 上面跑了apache+php 5.4 ,但我今天有一個網站要跑php 7.1
我考慮使用docker 跑php-fpm ,讓apache對連
這時候fcgi://host:port/{path} 裏頭的{path}是docker container內的路徑
如果你在host沒有共享DocumentRoot給docker內的container使用
你就顆顆了
ProxyPassMatch後面的路徑設定如果跟DocumentRoot不一致,那就會以ProxyPassMatch後面的路徑為主
你根本可不用設定DocumentRoot…

vmware在nat下的靜態ip設定-以centos7為例

1.找到Virtual Network Editor

在vmwere下找 vmnetcfg.exe

2.看dhcp與nat的範圍與他的geteway, 注意 geteway 的ip並非是你想的那個,看工具最準

或者用指令

ip route show

看geteway ip
再來 以centos 7 為例

1.檢查 Network Manager 是否啟動

systemctl status NetworkManager.service 

2.檢查使用的網卡

ip route show 

3.進入 nmtui 設定網路,若沒有則安裝

yum install NetworkManager-tui

 

4.重啟網路

systemctl restart network.service

5.ping google.com