Friday, July 28, 2017

Running php-fpm in Docker based on CentOS 7

Running php-fpm in Docker based on CentOS 7

Dockerfile:

FROM centos:centos7

RUN curl 'https://setup.ius.io/' -o setup-ius.sh \
 && bash setup-ius.sh \
 && rm -f setup-ius.sh \
 && yum -y update \
 && yum -y install \
  php71u-common \
  php71u-cli \
  php71u-fpm \
  php71u-opcache \
  php71u-xml \
  php71u-json \
  php71u-pdo \
  php71u-mysqlnd \
  php71u-intl \
  php71u-mbstring \
  php71u-mcrypt \
  php71u-gd \
  php71u-soap \
  php71u-process \
  php71u-pecl-redis \
  php71u-pecl-xdebug \
  php71u-fpm-httpd

EXPOSE 9000

CMD ["php-fpm", "-F"]

Build the docker image:

# docker build -t junhsieh/php7.1-fpm:0.0.0 .

Some important settings to be changed:

# vim /etc/php-fpm.d/www.conf

; Change ownership:

user = php-fpm
group = php-fpm

; Note: Ubuntu uses www-data user. Add php-fpm user to www-data group if the other container used it.
; # groupadd -g 33 www-data
; # useradd www-data -m -c 'web user' -u 33 -g 33
; # usermod -a -G www-data php-fpm
; # id php-fpm
; Now, restart this container to ensure php-fpm user is in www-data group.

; Bind port 9000 to the all interfaces:

listen = 9000

; Comment out the following line:

;listen.allowed_clients = 127.0.0.1

; Note: "listen.allowed_clients = any" will not work.
; Note: "listen.allowed_clients = other-container-name" will not work. IP address only.

; Uncomment the following line to debug the issue:

catch_workers_output = yes

; Note: Comment it out on production.


xdebug setting:

### xdebug setting
###
; Enable xdebug extension module
zend_extension=xdebug.so
;zend_extension=/usr/lib64/php/modules/xdebug.so

xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9009
; Note: php-fpm uses port 9000 as well.
xdebug.remote_log=/tmp/xdebug.log
xdebug.remote_connect_back=0
xdebug.remote_autostart=0
xdebug.remote_mode=req

xdebug.max_nesting_level=1000

Reference:

https://developers.redhat.com/blog/2014/12/29/running-php-fpm-in-docker/

Tuesday, July 25, 2017

Drupal 8 note

Drupal 8 note

Modules

Devel
https://www.drupal.org/project/devel

Examples for Developers
https://www.drupal.org/project/examples

Drush
https://www.drupal.org/project/drush

Drupal Console
https://drupalconsole.com/
===
# svn propedit svn:ignore /www/drupal8_public/sites

sites.php
*.com
*.ca
*.cn
*.local
===
# curl -sS https://getcomposer.org/installer | php
# mv composer.phar /usr/local/bin/composer

# composer self-update
# composer update
===
Install Drupal 8

# DRUPAL_VERSION=8.2.4; export DRUPAL_VERSION
# DRUPAL_MD5=288aa9978b5027e26f20df93b6295f6c; export DRUPAL_MD5
# curl -fSL "https://ftp.drupal.org/files/projects/drupal-${DRUPAL_VERSION}.tar.gz" -o drupal.tar.gz \
&& echo "${DRUPAL_MD5} *drupal.tar.gz" | md5sum -c - \
&& tar -xz --strip-components=1 -f drupal.tar.gz \
&& rm drupal.tar.gz \
&& chown -R www-data:www-data sites modules themes

# unset DRUPAL_VERSION
# unset DRUPAL_MD5
# env
===
Drush

# wget http://files.drush.org/drush.phar
# php drush.phar core-status
# chmod +x drush.phar
# mv drush.phar /usr/local/bin/drush
# chown root:wheel /usr/local/bin/drush

# drush help

# drush pm-download drupal-8 --select --destination=/www/drupal8 --drupal-project-rename=drupal8
# cd example
# mkdir modules/contrib
# mkdir modules/custom

# drush site-install minimal --account-mail=admin@example.com --account-name=admin --account-pass=admin --site-mail=admin@example.com --site-name=MySiteName --sites-subdir=mytest.local --db-url='mysql://[db_user]:[db_pass]@localhost/[db_name]'

# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local core-status
# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local pm-releases
# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local pm-projectinfo
# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local pm-info

# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local pm-refresh
# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local pm-updatestatus
# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local pm-updatecode
# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local updatedb-status
# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local updatedb

# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local pm-download devel --destination=modules/contrib
# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local pm-projectinfo devel
# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local pm-info devel
# drush --root=/www/test_dru8/example --uri=simplestore.cent-exp.local pm-enable devel

# drush --root=/www/drupal-8.0.5 --uri=simplestore.cent-exp.local cache-rebuild

# drush --root=/www/drupal-8.0.5 --uri=simplestore.cent-exp.local php-script test.php
# drush --root=/www/drupal-8.0.5 --uri=simplestore.cent-exp.local --debug --verbose php-script test.php
===
# drupal init --override
# drupal check

# drupal list
# drupal self-update

# drupal generate:module
===
Disable Drupal 8 caching during development

# cp sites/example.settings.local.php sites/simplestore.cent-exp.local/settings.local.php

Uncomment these lines:

# vim sites/simplestore.cent-exp.local/settings.php

if (file_exists(__DIR__ . '/settings.local.php')) {
include __DIR__ . '/settings.local.php';
}

Uncomment these lines in settings.local.php to Disable the render cache and Disable Dynamic Page Cache:

# vim sites/simplestore.cent-exp.local/settings.local.php

$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

Open development.services.yml in the sites folder and add the following block (to disable twig cache):

# vim sites/development.services.yml

parameters:
twig.config:
debug : true
auto_reload: true
cache: false

Afterwards you have to rebuild the Drupal cache. Otherwise your website will encounter an unexpected error on page reload. This can be done by visiting the following URL from your Drupal 8 website:
http://yoursite/core/rebuild.php

https://www.drupal.org/node/2598914

===
Drupal Console - a CLI tool to generate boilerplate code, interact and debug Drupal 8.
https://drupalconsole.com/

Evaluate Drupal projects online
http://simplytest.me/project/examples/8.x-1.x

===
Generates a token based on $value, the user session, and the private key:
\Drupal::csrfToken()->get()
\Drupal::csrfToken()->validate()

===
1. find the proper way to deal with database query error during form_submit try .. catch (rollback)

2. db_transaction

https://api.drupal.org/api/drupal/core%21includes%21database.inc/function/db_transaction/8
http://dcycleproject.org/blog/27/dont-perform-logic-your-hookformsubmit-use-api

===
vim *.yml src/*/*.php templates/*.twig css/*.css js/*.js
===
$outArr = [];
$sql = "SELECT name FROM {variable} WHERE name LIKE :name";
$arg = [
':name' => db_like('csm_node_temp_') . '%',
];

foreach (db_query($sql, $arg) as $obj) {
$outArr[] = $obj->name;
}
===
Goodbye Drush Make, Hello Composer!
https://www.lullabot.com/articles/goodbye-drush-make-hello-composer
===
files/config_*/sync

This directory contains configuration to be imported into your Drupal site. To make this configuration active, visit admin/config/development/configuration/sync. For information about deploying configuration between servers, see https://www.drupal.org/documentation/administer/config
===
Working With Twig Templates

https://www.drupal.org/node/2186401
https://www.drupal.org/node/2354645

Sunday, July 23, 2017

Execute mongo commands through shell scripts

Method 1:

# mongo DBName --quiet --eval 'db.users.find().pretty()' | less

Method 2:

# echo -e 'use DBName\ndb.users.find().pretty()' | mongo --quiet

Monday, July 10, 2017

Build OpenJDK 8 on Ubuntu 14.04

# apt-get update \
&& apt-get install build-essential mercurial zip openjdk-7-jdk libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev libcups2-dev libfreetype6-dev libasound2-dev ccache

# cd /usr/local/src \
&& hg clone http://hg.openjdk.java.net/jdk8u/jdk8u

# cd jdk8u/ \
&& bash ./get_source.sh

# bash ./configure -with-freetype-include=/usr/include/freetype2 -with-freetype-lib=/usr/lib/x86_64-linux-gnu/

# make all

# make install

Thursday, July 6, 2017

PHP http query

<?php

$header = "Content-type: application/json\r\n"
. "Content-Length: " . strlen($data) . "\r\n"
;


$context = stream_context_create([
        'http' => [
                'method'  => 'POST',
                'ignore_errors' => true,
                'header'=> $header,
                'content' => $data,
        ],
        'ssl' => [
                // set some SSL/TLS specific options
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
        ],
]);

$result = file_get_contents('http://example.com/', false, $context);