PHP 7.1から7.2へのバージョンアップ (2)

続き、
あまり使わないかもだけど,PHPMyAdminが入ってないので入れます

#yum -y install --enablerepo=remi,remi-php72 phpMyAdmin

/etc/httpd/conf.d/phpMyAdmin.conf を編集します。
8 Alias /phpMyAdminMoji /usr/share/phpMyAdmin ← URLを推測しにくいように文字列を追加する
9 Alias /phpmyadminMoji /usr/share/phpMyAdmin ← URLを推測しにくいように文字列を追加する
10
11
12 AddDefaultCharset UTF-8
13 SSLRequireSSL ← 追加
14 Require local → all granted
15

16
17
18 Require local → all granted
19

20

mod_sslをインストール
#yum install mod_ssl

httpdを再起動して、↑で設定したURLにアクセスすると。。。

インストールできてました!

Posted in PHP

PHP 7.1から7.2へのバージョンアップ (1)

外出禁止令の間にそっとメンテナンスします。。。。

#cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

環境はCentOS 7です。

PHPのバージョンアップ

要は一度消去して再インストールすることになります。。。

# php -v
PHP 7.1.30 (cli) (built: May 28 2019 12:49:01) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

現在7.1なのでこれをいったん7.2にします。
拡張モジュールの確認

# yum list installed | grep php
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
php.x86_64 7.1.30-1.el7.remi @remi-php71
...

php.iniをバックアップしておきます。

cp /etc/php.ini /etc/php.ini_old

remiレポジトリを更新します

# yum install epel-release
読み込んだプラグイン:fastestmirror, langpacks
Determining fastest mirrors
epel/x86_64/metalink | 8.8 kB 00:00
* base: mirrors.cat.net
* epel: nrt.edge.kernel.org
* extras: mirrors.cat.net
* remi-safe: ftp.riken.jp
* updates: mirrors.cat.net
...
更新:
epel-release.noarch 0:7-12
完了しました!

レポジトリを確認します。

# ls /etc/yum.repos.d/ | grep remi-
remi-glpi91.repo
remi-glpi92.repo
...

削除します
# yum remove php-*

削除が終了すると
# php -v
-bash: /usr/bin/php: そのようなファイルやディレクトリはありません

消えてる。。。

モジュールと一緒にインストールします

# yum -y install --enablerepo=remi,remi-php72 php php-mbstring php-xml php-xmlrpc php-gd php-pdo php-pecl-mcrypt php-mysqlnd php-pecl-mysql

# php -v
PHP 7.2.31 (cli) (built: May 12 2020 09:23:38) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

# yum list installed | grep php
php.x86_64 7.2.31-1.el7.remi @remi-php72
...

これでいったん確認します。

サイトを開こうとすると

Class 'PDO' not found

とエラーが!!

php.ini へ次を追加してモジュールを有効にする必要がありました。

extension=pdo.so

参考サイト:
https://forum.matomo.org/t/you-need-to-enable-the-pdo-and-pdo-mysql-extensions-in-your-php-ini-file/261

Posted in PHP

CentOSにnode.jsをインストールする

$ node --version

-bash: node: コマンドが見つかりません

$ curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash -
[sudo] パスワード:

## Installing the NodeSource Node.js 8.x LTS Carbon repo…

## Inspecting system…

+ rpm -q –whatprovides redhat-release || rpm -q –whatprovides centos-release || rpm -q –whatprovides cloudlinux-release || rpm -q –whatprovides sl-release
+ uname -m

## Confirming “el7-x86_64” is supported…

+ curl -sLf -o /dev/null ‘https://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm’

## Downloading release setup RPM…

+ mktemp
+ curl -sL -o ‘/tmp/tmp.XOyX3qzAtS’ ‘https://rpm.nodesource.com/pub_8.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm’

## Installing release setup RPM…

+ rpm -i –nosignature –force ‘/tmp/tmp.XOyX3qzAtS’

## Cleaning up…

+ rm -f ‘/tmp/tmp.XOyX3qzAtS’

## Checking for existing installations…

+ rpm -qa ‘node|npm’ | grep -v nodesource

## Run `sudo yum install -y nodejs` to install Node.js 8.x LTS Carbon and npm.
## You may also need development tools to build native addons:
sudo yum install gcc-c++ make
## To install the Yarn package manager, run:
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install yarn

[upla@os3-364-15302 ~]$ sudo yum install nodejs
読み込んだプラグイン:fastestmirror, langpacks
Determining fastest mirrors
epel/x86_64/metalink | 4.9 kB 00:00
* base: mirrors.cat.net
* epel: ftp.jaist.ac.jp
* extras: ty1.mirror.newmediaexpress.com
* remi-safe: ftp.riken.jp
* updates: ty1.mirror.newmediaexpress.com
base | 3.6 kB 00:00
epel | 5.4 kB 00:00
extras | 2.9 kB 00:00
nodesource | 2.5 kB 00:00
remi-safe | 3.0 kB 00:00
updates | 2.9 kB 00:00
(1/8): extras/7/x86_64/primary_db | 152 kB 00:00
(2/8): base/7/x86_64/group_gz | 165 kB 00:00
(3/8): nodesource/x86_64/primary_db | 48 kB 00:00
(4/8): remi-safe/primary_db | 1.6 MB 00:00
(5/8): epel/x86_64/updateinfo | 1.0 MB 00:01
(6/8): base/7/x86_64/primary_db | 6.0 MB 00:01
(7/8): updates/7/x86_64/primary_db | 1.9 MB 00:01
(8/8): epel/x86_64/primary_db | 6.8 MB 00:02
依存性の解決をしています
–> トランザクションの確認を実行しています。
—> パッケージ nodejs.x86_64 2:8.16.2-1nodesource を インストール
–> 依存性解決を終了しました。

依存性を解決しました

================================================================================
Package アーキテクチャー
バージョン リポジトリー 容量
================================================================================
インストール中:
nodejs x86_64 2:8.16.2-1nodesource nodesource 16 M

トランザクションの要約
================================================================================
インストール 1 パッケージ

総ダウンロード容量: 16 M
インストール容量: 49 M
Is this ok [y/d/N]: y
Downloading packages:
警告: /var/cache/yum/x86_64/7/nodesource/packages/nodejs-8.16.2-1nodesource.x86_64.rpm: ヘッダー V4 RSA/SHA512 Signature、鍵 ID 34fa74dd: NOKEY
nodejs-8.16.2-1nodesource.x86_64.rpm の公開鍵がインストールされていません
nodejs-8.16.2-1nodesource.x86_64.rpm | 16 MB 00:02
file:///etc/pki/rpm-gpg/NODESOURCE-GPG-SIGNING-KEY-EL から鍵を取得中です。
Importing GPG key 0x34FA74DD:
Userid : “NodeSource
Fingerprint: 2e55 207a 95d9 944b 0cc9 3261 5ddb e8d4 34fa 74dd
Package : nodesource-release-el7-1.noarch (installed)
From : /etc/pki/rpm-gpg/NODESOURCE-GPG-SIGNING-KEY-EL
上記の処理を行います。よろしいでしょうか? [y/N]y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告: RPMDB は yum 以外で変更されました。
インストール中 : 2:nodejs-8.16.2-1nodesource.x86_64 1/1
検証中 : 2:nodejs-8.16.2-1nodesource.x86_64 1/1

インストール:
nodejs.x86_64 2:8.16.2-1nodesource

完了しました!

$ node -v
v8.16.2

node.jsのこと

PHPと同じところ
・サーバーサイド言語
違うところ
・非同期処理を行う
・単純な処理を同時に高速に行える

テストアプリ(my-app)を作ってみます

$ npx create-react-app my-app
npx: 91個のパッケージを7.62秒でインストールしました。

Creating a new React app in /[directory]/my-app.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts…

> core-js@2.6.10 postinstall /[directory]/my-app/node_modules/babel-runtime/node_modules/core-js
> node postinstall || echo “ignore”

> core-js@3.2.1 postinstall /[directory]/my-app/node_modules/core-js
> node scripts/postinstall || echo “ignore”

+ react@16.10.2
+ react-dom@16.10.2
+ react-scripts@3.2.0
added 1496 packages from 687 contributors and audited 904959 packages in 72.04s
found 0 vulnerabilities

Success! Created my-app at /[directory]/my-app
Inside that directory, you can run several commands:

npm start
Starts the development server.

npm run build
Bundles the app into static files for production.

npm test
Starts the test runner.

npm run eject
Removes this tool and copies build dependencies, configuration files
and scripts into the app directory. If you do this, you can’t go back!

We suggest that you begin by typing:

cd my-app
npm start

Happy hacking!

Laravel いろいろメモ

バージョン確認
php artisan --version
Laravel Framework 5.4.36
5.4です。

タグをエスケープさせずに出力する際は
bladeのなかで
{{ }} で囲む代わりに {{!! !!}}で囲う

シーダーを使う
DBにダミーデータを入れることをシーダーというそうです。

php artisan make:seeder DummysTableSeeder

/root/database/seeds/ に DummysTableSeeder.php ができます。


use Illuminate\Database\Seeder;
class DummysTableSeeder extends Seeder
{

/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
DB::table('contacts')->insert([
'name' => str_random(10),
'tel'=> 99999999,
]);
}
}

あとは同じディレクトリ にある DatabaseSeeder.php に先ほどのファイルを追加します。
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(DummysTableSeeder::class);
}
}

記述の仕方はLaravelのバージョンによって異なります。↑ は5.4

コマンドラインで
$php artisan db:seed

+—-+————+———-+————+————+
| id | name | tel | created_at | updated_at |
+—-+————+———-+————+————+
| 1 | qf9wdpx5a3 | 99999999 | NULL | NULL |
+—-+————+———-+————+————+

↑のように挿入されます

コメントデータの取得を操作する comments_template_query_args

wp-inclludes/comment-template.phpのなかにコメントの取得とソートに関する変数 &comment_argsがあります。

$comment_args = array(
 'orderby'                   => 'comment_date_gmt',
 'order'                     => 'DSC',
 'status'                    => 'approve',
 'post_id'                   => $post->ID,
 'no_found_rows'             => false,
 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
);

このあといろいろごにょごにょして。
コメントのデータを取得しているようです。

フックの一覧を見るとcomments_template_query_argsというのがあるので(version4.5~)ここにフックしてorderbyやorderを変更できそうです。
あと未承認のコメントの表示とかもできそうです。

wp-inclludes/comment-template.phpの該当箇所が(↓)
現在のバージョンは5.1なので1444行目にありますね。

 $comment_args  = apply_filters( 'comments_template_query_args', $comment_args );
 $comment_query = new WP_Comment_Query( $comment_args );
 $_comments     = $comment_query->comments;

コメントを表示する場所で↑を変更します。

//コメント取得のための変数を納めた配列
function edit_comment_query( $comment_args ) {
 $comment_args['order'] = 'ASC';
  return $comment_args;
}
//comments_template_query_argsへフックする
add_filter( 'comments_template_query_args', 'edit_comment_query' );

とりあえず今日はここまで。。。

PHP(PDO)でMySQL(MariaDB)に接続してDBからデータを取得する

今さらながらPHP7対応。
なぜか500エラーを連発してハマってしまったのでうまくいった書き方をメモしときます。

    function DBconnect()
    {
        $DBlink = mysqli_connect('yourdbhost.work', 'username', 'password');

        if (!$DBlink) {
            die('MYSQL接続エラー' . mysqli_error($DBlink));
        }

        mysqli_set_charset($DBlink, 'utf8');

        return $DBlink;
    }

    function DBselect($selectDB, $DBlink)
    {
        $db_selected = mysqli_select_db($DBlink, $selectDB);

        if (!$db_selected) {
            die($selectDB . 'データベース選択エラー' . mysqli_error($DBlink));
        }
    }

    function RunSQL($DBlink, $sql)
    {
        $result = mysqli_query($DBlink, $sql);

        if (!$result) {
            die('クエリーのエラー' . mysqli_error($DBlink));
        }

        return $result;
    }


    function h(string $string): string
    {
        return $string;
    }


    function myfunction()
    {

            $DBlink = DBconnect();
            DBselect('database', $DBlink);
            $records = RunSQL(
                $DBlink,
                "SELECT " .
                "*" .
                "FROM `table`" .
                "ORDER BY `key`;"
            );

            $result = [];

            while ($row = mysqli_fetch_array($records)) {
                //ここに処理を
            }
            return $result;

            mysqli_close($RDBlink);
    };

懐かしい感じですね。
これを↓のように変えさせてもらいました。

define('DSN', 'mysql:host=yourdbhost.work;dbname=dbname;charset=utf8');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');

try{
    $dbh = new PDO(DSN, DB_USERNAME, DB_PASSWORD,
     array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_EMULATE_PREPARES => false,
        )
    );

    $prepare = $dbh->prepare('SELECT * FROM table ORDER BY key');
    $prepare->execute();

    $result = $prepare->fetchAll(PDO::FETCH_ASSOC);

    foreach($result as $r){

      //ここで処理を

    }


} catch (PDOException $e){
    $error = $e->getMessage();
}

macへnodeをインストール

brew install node

ところが

node already installed, it's just not linked

インストールはできたけどリンクされてないから使えないよ!と。。
なおかつリンクを上書きするといいよ!

と言われるので

brew link --overwrite node

とするも、弾かれてしまう。。

下記で解決!

 

brew uninstall node

 いったんアンインストール

brew update
brew upgrade
 Homebrewを最新に
brew cleanup
 いらないものを消す
brew install node
 もっかいインストール
sudo chown -R $(whoami) /usr/local/
 リンクが書いてあるファイルを上書きできるように権限を変更
 
brew link --overwrite node
 リンクを上書き
brew postinstall node
 これは何かよくわからない

OSアップデートしてsassが使えなくなったら

masomi792017-11-02

症状::

新規案件に着手する前にOSXをHigh Sierraにアップデートしたらsass が使えない!急いでるのに!

sass -v

バージョン確認しようとしただけで。。。

-bash: /usr/local/bin/sass: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby: bad interpreter: No such file or directory

とおこられる。

対策::

1.Xcodeをアップデート
App Storeから行います。

2.ターミナルからRubyを再インストール

brew install rbenv ruby-build

3.sass とcompassを /usr/local/binへインストール

sudo gem install -n /usr/local/bin sass
sudo gem install -n /usr/local/bin compass

というわけでアップデートは慎重に。。。

Posted in mac