CentOS 7 + Nginx + rep2 その3

Distributionのレポジトリだと、古いNginxのPkgしかないので
Nginexのレポジトリを登録して、そこから導入する


標準のレポジトリだと、現時点では 1.12.2のStableパッケージだけど
Nginxのサイトによると基本は Mainlineのパッケージ(1.13.12)使用を推奨しているので
Nginxのレポジトリを登録してそちらからインストールするように設定する


Nginxのサイトに従って
-> https://nginx.org/en/linux_packages.html#mainline

# cat > /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
<ここで Ctrl+D で出力終了>
# chmod 644 nginx.repo
#
# yum info nginx.x86_64
Available Packages
Name        : nginx
Arch        : x86_64
Epoch       : 1
Version     : 1.13.12
Release     : 1.el7_4.ngx
Size        : 750 k
Repo        : nginx/x86_64
Summary     : High performance web server
URL         : http://nginx.org/
License     : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
            : a mail proxy server.



無事最新のパッケージを引っ張ってこれるようになったのでインストール

# yum install nginx



サービス登録を実施して、起動

# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# systemctl start nginx



http:///をブラウザで確認した所、Welcomページが表示されたので問題なく起動
Nginxのログローテートを設定する /etc/logrotate.d/nginx がパッケージのインストール時に導入されるのでこれを修正

# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
        daily
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}
#

上記を修正して、毎日実行から毎週に、過去ログは4week(一ヶ月分)保持するように変更した

# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
        weekly
        missingok
        rotate 4
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}
#

CentOS 7 + Nginx + rep2 その4

というわけで、順番が前後したけど Nginx の設定を変更


configファイルとしては、/etc/nginx/nginx.conf なのだが
個々の設定は ./conf.d/配下の *.conf を読み込む用になっているので
/etc/nginx/conf.d/以下にある、default.conf を修正しても良いのだが
VirtualHostの設定やら考えるとサイト毎にconfファイルを作成したほうが良いので今回は別ファイルで構築

# cat /etc/nginx/conf.d/<自ドメイン>.conf
server {
    listen       <Port No>;
    server_name  <Domain Name>;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /var/www/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}



ドメイン設定やポート番号を変更した後に

# /sbin/nginx -t

でconfig checkを実行して問題なければ restart

# systemctl restart nginx

CentOS 7 + Nginx + rep2 その5

rep2が要求する PHPは 7.xなので、標準レポジトリの 5.xだと使用できない為
remiレポジトリを追加する
-> http://rpms.famillecollet.com/


事前にEPELレポジトリが追加されていないとダメだが、既に追加済みの為この手順はスキップ
サイトにある、remiレポジトリのURLからインストールを実施

# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
#
# ls -l /etc/yum.repos.d/
total 64
-rw-r--r--. 1 root root 1664 Aug 31  2017 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Aug 31  2017 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Aug 31  2017 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 Aug 31  2017 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Aug 31  2017 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Aug 31  2017 CentOS-Sources.repo
-rw-r--r--. 1 root root 3830 Aug 31  2017 CentOS-Vault.repo
-rw-r--r--  1 root root  951 Oct  3  2017 epel.repo
-rw-r--r--  1 root root 1050 Oct  3  2017 epel-testing.repo
-rw-r--r--  1 root root  108 Apr 15 21:17 nginx.repo
-rw-r--r--  1 root root  456 Mar 21 22:28 remi-php54.repo
-rw-r--r--  1 root root 1314 Mar 21 22:28 remi-php70.repo
-rw-r--r--  1 root root 1314 Mar 21 22:28 remi-php71.repo
-rw-r--r--  1 root root 1314 Mar 21 22:28 remi-php72.repo
-rw-r--r--  1 root root 2605 Mar 21 22:28 remi.repo
-rw-r--r--  1 root root  750 Mar 21 22:28 remi-safe.repo
#



PHP7.2のrepoである、remi-php72.repoの設定をenableに変更

# cat /etc/yum.repos.d/remi-php72.repo
# This repository only provides PHP 7.2 and its extensions
# NOTICE: common dependencies are in "remi-safe"

[remi-php72]
name=Remi's PHP 7.2 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php72/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/7/php72/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/php72/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php72-debuginfo]
name=Remi's PHP 7.2 RPM repository for Enterprise Linux 7 - $basearch - debuginfo
baseurl=http://rpms.remirepo.net/enterprise/7/debug-php72/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php72-test]
name=Remi's PHP 7.2 test RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/test72/$basearch/
#mirrorlist=https://rpms.remirepo.net/enterprise/7/test72/httpsmirror
mirrorlist=http://cdn.remirepo.net/enterprise/7/test72/mirror
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

[remi-php72-test-debuginfo]
name=Remi's PHP 7.2 test RPM repository for Enterprise Linux 7 - $basearch - debuginfo
baseurl=http://rpms.remirepo.net/enterprise/7/debug-test72/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
[root@rep2 conf.d]# cat /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1



remiレポジトリをenableにするとそっちが優先されるのね
php72パッケージじゃなくて、phpパッケージが全部remiからインストールされるようになってた

# yum info php php-common php-cli php-json php-mbstring php-pdo php-process php-xml
Available Packages
Name        : php
Arch        : x86_64
Version     : 7.2.4
Release     : 1.el7.remi
Size        : 10 M
Repo        : remi-php72
Summary     : PHP scripting language for creating dynamic web sites
URL         : http://www.php.net/
License     : PHP and Zend and BSD and MIT and ASL 1.0
Description : PHP is an HTML-embedded scripting language. PHP attempts to make it
            : easy for developers to write dynamically generated web pages. PHP also
            : offers built-in database integration for several commercial and
            : non-commercial database management systems, so writing a
            : database-enabled webpage with PHP is fairly simple. The most common
            : use of PHP coding is probably as a replacement for CGI scripts.
            :
            : The php package contains the module (often referred to as mod_php)
            : which adds support for the PHP language to Apache HTTP Server.

Name        : php-cli
Arch        : x86_64
Version     : 7.2.4
Release     : 1.el7.remi
Size        : 15 M
Repo        : remi-php72
Summary     : Command-line interface for PHP
URL         : http://www.php.net/
License     : PHP and Zend and BSD and MIT and ASL 1.0
Description : The php-cli package contains the command-line interface
            : executing PHP scripts, /usr/bin/php, and the CGI interface.

Name        : php-common
Arch        : x86_64
Version     : 7.2.4
Release     : 1.el7.remi
Size        : 12 M
Repo        : remi-php72
Summary     : Common files for PHP
URL         : http://www.php.net/
License     : PHP and BSD
Description : The php-common package contains files used by both the php
            : package and the php-cli package.

Name        : php-json
Arch        : x86_64
Version     : 7.2.4
Release     : 1.el7.remi
Size        : 88 k
Repo        : remi-php72
Summary     : JavaScript Object Notation extension for PHP
URL         : http://www.php.net/
License     : PHP
Description : The php-json package provides an extension that will add
            : support for JavaScript Object Notation (JSON) to PHP.

Name        : php-mbstring
Arch        : x86_64
Version     : 7.2.4
Release     : 1.el7.remi
Size        : 620 k
Repo        : remi-php72
Summary     : A module for PHP applications which need multi-byte string handling
URL         : http://www.php.net/
License     : PHP and LGPLv2 and BSD and OpenLDAP
Description : The php-mbstring package contains a dynamic shared object that will add
            : support for multi-byte string handling to PHP.

Name        : php-pdo
Arch        : x86_64
Version     : 7.2.4
Release     : 1.el7.remi
Size        : 390 k
Repo        : remi-php72
Summary     : A database access abstraction module for PHP applications
URL         : http://www.php.net/
License     : PHP
Description : The php-pdo package contains a dynamic shared object that will add
            : a database access abstraction layer to PHP.  This module provides
            : a common interface for accessing MySQL, PostgreSQL or other
            : databases.

Name        : php-process
Arch        : x86_64
Version     : 7.2.4
Release     : 1.el7.remi
Size        : 188 k
Repo        : remi-php72
Summary     : Modules for PHP script using system process interfaces
URL         : http://www.php.net/
License     : PHP
Description : The php-process package contains dynamic shared objects which add
            : support to PHP using system interfaces for inter-process
            : communication.

Name        : php-xml
Arch        : x86_64
Version     : 7.2.4
Release     : 1.el7.remi
Size        : 855 k
Repo        : remi-php72
Summary     : A module for PHP applications which use XML
URL         : http://www.php.net/
License     : PHP
Description : The php-xml package contains dynamic shared objects which add support
            : to PHP for manipulating XML documents using the DOM tree,
            : and performing XSL transformations on XML documents.



PHPの幾つかのパッケージは httpd をdependで要求するので、httpdは取り敢えずインストールして Disableにしておくか

# yum install php php-common php-cli php-json php-mbstring php-pdo php-process php-xml



NginxでPHPを動かす為に、CGI型のPHP-FPMを導入する

# yum install php-fpm



PHP-FPMはデフォルトが Apacheの設定になっているので修正

# cd /etc/php-fpm.d
# vi www.conf

user と group を、 apache -> nginx に変更


PHP-FPMを自動起動設定

# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
# systemctl start php-fpm
# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2018-04-16 00:57:56 JST; 1s ago
 Main PID: 7723 (php-fpm)
   Status: "Ready to handle connections"
   CGroup: /system.slice/php-fpm.service
           tq7723 php-fpm: master process (/etc/php-fpm.conf)
           tq7724 php-fpm: pool www
           tq7725 php-fpm: pool www
           tq7726 php-fpm: pool www
           tq7727 php-fpm: pool www
           mq7728 php-fpm: pool www

Apr 16 00:57:56 rep2 systemd[1]: Starting The PHP FastCGI Process Manager...
Apr 16 00:57:56 rep2 systemd[1]: Started The PHP FastCGI Process Manager.



NginxがPHPを扱えるように設定変更を実施

# cat /etc/nginx/conf.d/<自ドメイン>.conf
server {
    listen       <Port No>;
    server_name  <Domain Name>;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /var/www/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}



Nginxの設定ファイルを変更したので、再起動

# systemctl restart nginx



phpinfoファイルを作成してphpの動作検証

# cat /var/www/html/phpinfo.php
<?php
        phpinfo();
?>

phpinfo.phpにアクセスして、PHPの動作状況が表示されればOK
(セキュリティの為には、動作チェック終わった後はファイルを削除しておいたほうが良い)

CentOS 7 + Nginx + rep2 その6

rep2をインストールする為に gitコマンドが必要なので
gitのインストール

# yum install git



Open774版のrep2をインストール

# cd /var/www/html
# git clone git://github.com/open774/p2-php.git



依存ライブラリをインストール

# cd p2-php
# curl -O http://getcomposer.org/composer.phar
# php -d detect_unicode=0 composer.phar install



アクセス権の変更

# chmod 0777 data/* rep2/ic



チェックコマンドの実行

# php scripts/p2cmd.php check
PHP Version:
  7.2.4: OK
PHP Extensions:
  curl: OK
  dom: OK
  json: OK
  libxml: OK
  mbstring: OK
  pcre: OK
  pdo: OK
  pdo_sqlite: OK
  phar: OK
  session: OK
  spl: OK
  zlib: OK
php.ini directives:
  safe_mode = : OK
  register_globals = : OK
  magic_quotes_gpc = : OK
  mbstring.encoding_translation = 0: OK
  session.auto_start = 0: OK

全部OKが出たので問題無し!

CentOS 7 + Nginx + rep2 その7

(ちなみに過去データ移行してるのでrep2の設定周りは省略)


無事rep2のログイン画面が見える所まで来たら
rep2 が直接 2ch(今は5chだけど)のDATファイルにはアクセスにいくと蹴られるので
Local Proxyを設置して、間接的にアクセスできるようにする


と、その前に bzip2とかwgetとか入ってなかったのでインストール

# yum install bzip2 wget



今までも使っていた 2chproxy を使用

# cd /usr/local/bin
# wget https://github.com/yama-natuki/2chproxy.pl/raw/master/2chproxy.pl
# chown nginx:nginx 2chproxy.pl
# chmod 755 2chproxy.pl



2cgproxyの以下を修正
DEDICATED_BROWSER => "rep2",
DAT_DIRECTORY => "/var/www/html/p2-php/data/",
LISTEN_HOST => "127.0.0.1",


で、実行して見事蹴られる
過去ログみたら、全く同じ事やってた _no

[root@rep2 bin]# /usr/local/bin/2chproxy.pl
Can't locate HTTP/Daemon.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/bin/2chproxy.pl line 48.
BEGIN failed--compilation aborted at /usr/local/bin/2chproxy.pl line 48.



Perlの http-daemon モジュールを追加でインストール

# yum install perl-HTTP-Daemon-SSL



これで解決と思いきや、次は LWP/UserAgent のモジュールが足らんようで・・・

# /usr/local/bin/2chproxy.pl
Can't locate LWP/UserAgent.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/bin/2chproxy.pl line 50.
BEGIN failed--compilation aborted at /usr/local/bin/2chproxy.pl line 50.
#
# yum install perl-LWP-UserAgent-Determined

で、見事起動\( ̄∇ ̄)/

# /usr/local/bin/2chproxy.pl
[0|1675] PROCESS: listen to http://127.0.0.1:8080/

CentOS 7 + Nginx + rep2 その8

2chproxy が無事起動するようになったので、今まで同様サービス化を実施


/etc/systemd/system 以下にUnit定義ファイルを設置
中身はこんな感じ

2018/09/15 追記
NetworkManagerを停止した所、Network I/Fが起動する(IP取得)前に
2chproxyが起動しようとしてコケテいたので Unit項目に以下追記
Wants=network-online.target
After=network-online.target

上記の詳細はこちら NetworkManager-wait-online.serviceはどのような場合にenableにすれば良いか、を理詰めで考える

# cat /etc/systemd/system/2chproxy.service
[Unit]
Description = 2ch proxy daemon
Wants=network-online.target
After=network-online.target

[Service]
ExecStart = /usr/local/bin/2chproxy.pl
Restart = always
Type = simple

[Install]
WantedBy = multi-user.target



UnitがServiceとして認識されているか確認する

# systemctl list-unit-files --type=service | grep 2chproxy
2chproxy.service                              disabled



認識されていたので、EnableしてStartする

# systemctl enable 2chproxy
Created symlink from /etc/systemd/system/multi-user.target.wants/2chproxy.service to /etc/systemd/system/2chproxy.service.
# systemctl start 2chproxy
# systemctl status 2chproxy
● 2chproxy.service - 2ch proxy daemon
   Loaded: loaded (/etc/systemd/system/2chproxy.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2018-04-16 02:31:14 JST; 6s ago
 Main PID: 1519 (2chproxy.pl)
   CGroup: /system.slice/2chproxy.service
           mq1519 /usr/bin/perl /usr/local/bin/2chproxy.pl

Apr 16 02:31:14 rep2 systemd[1]: Started 2ch proxy daemon.
Apr 16 02:31:14 rep2 systemd[1]: Starting 2ch proxy daemon...



OS再起動しても、問題なくアクセスできることが確認できたので今日はここまで


この後はLet's Encryptを使用してhttps化を実行しよう(何時になることやら


参考情報1 Mozilla SSL Configuration Genarator
-> https://mozilla.github.io/server-side-tls/ssl-config-generator/


参考情報2 rep2用 Nginx設定サンプル

server {
    listen 443 ssl http2;

    # certs sent to the client in SERVER HELLO are concatenated in ssl_certificate
    ssl_certificate /path/to/signed_cert_plus_intermediates;
    ssl_certificate_key /path/to/private_key;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;


    # modern configuration. tweak to your needs.
    ssl_protocols TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
    add_header Strict-Transport-Security max-age=15768000;

    # OCSP Stapling ---
    # fetch OCSP records from URL in ssl_certificate and cache them
    ssl_stapling on;
    ssl_stapling_verify on;

    ## verify chain of trust of OCSP response using Root CA and Intermediate certs
    ssl_trusted_certificate /path/to/root_CA_cert_plus_intermediates;

    location / {
        root    /path/to/p2-php/rep2;
        index   index.php index.html;

        location ~ \.php$ {
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            include fastcgi_params;
            fastcgi_param HTTPS             on;
            fastcgi_param SSL_PROTOCOL      $ssl_protocol;
            fastcgi_param SSL_CIPHER        $ssl_cipher;
            fastcgi_param SSL_SESSION_ID    $ssl_session_id;
            fastcgi_param SSL_CLIENT_VERIFY $ssl_client_verify;
            fastcgi_param SCRIPT_FILENAME   /path/to/p2-php/rep2$fastcgi_script_name;
            break;
        }
}