【CentOS】iptables設定

  • iptablesのテーブル概念
    iptablesは下記3つのテーブルを持つ。

    filter:パケットフィルタリング
    nat:IPアドレスの変換
    mangle:IPヘッダの書き換え
    

    filterはssh、http、https等のポートを通したり。
    natはRaspberry PiのOpenVPNでも設定しましたが、
    「10.8.0.0」を「192.168.XXX.XXX」に変換したり。
    mangleは利用したことありませんので、説明省略。

  • iptableの設定確認
    各々の設定内容は下記コマンドで確認する。

    iptables -t テーブル -L
    

    「-t テーブル」を省略すると自動的に「-t filter」と同義になる。

    または下記コマンドで確認する。
    対象テーブルはfilterのみとなる。

    /etc/init.d/iptables status
    
  • iptableの起動

    /etc/init.d/iptables start
    

    または

    service iptables start
    
  • iptableの停止

    /etc/init.d/iptables stop
    

    または

    service iptables stop
    
  • iptablesの設定追加
    現状のままだと外からWordPressにアクセスできないため、
    ルーターのポート80、443をNUCのIPに向け、
    さらにiptablesのfilterテーブルに設定を追加します。

    iptables -I INPUT 5 -p tcp -m tcp --dport 80 -j ACCEPT
    iptables -I INPUT 6 -p tcp -m tcp --dport 443 -j ACCEPT
    service iptables save
    
    -I:チェインの任意の行番号に挿入
    -p:プロトコルを指定(icmp、 tcp、udp、all)
    -m:マッチング条件拡張(今回の例でいうと「tcp --dport」でパケットの送信先ポートを指定)
    -j:マッチングした際のターゲット(ACCEPT、 DROP、QUEUE、RETURN、LOG、REJECT等)
    

    チェインはテーブルごとに利用できる範囲があります。

    filter:FORWARD、INPUT、OUTPUT
    nat:PREROUTING、POSTROUTING、OUTPUT
    

    5行目と6行目に設定を追加した理由は、今回の設定を追加前の最終行目(5行目)に
    全ての通信を拒否する設定があるからです。
    iptablesは上から順に設定内容を評価するため、
    全ての通信を拒否する設定より前に許可したいポートの設定をしておく必要があります。

【NTPサーバ】NTPd導入

  • 既にインストール済か確認

    yum list installed | grep ntp
    
    fontpackages-filesystem.noarch
    ntp.x86_64              4.2.6p5-1.el6.centos
    ntpdate.x86_64          4.2.6p5-1.el6.centos
    
  • NTPd設定

    cp -pi /etc/ntp.conf /etc/ntp.conf.org
    vi /etc/ntp.conf
    
    下記内容を「#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap」のすぐ下に追加
    restrict 192.168.XXX.0 mask 255.255.255.0 nomodify notrap
    
    下記内容をコメントアウト
    #server 0.centos.pool.ntp.org iburst
    #server 1.centos.pool.ntp.org iburst
    #server 2.centos.pool.ntp.org iburst
    #server 3.centos.pool.ntp.org iburst
    
    下記内容を追加
    server ntp1.jst.mfeed.ad.jp
    server ntp2.jst.mfeed.ad.jp
    server ntp3.jst.mfeed.ad.jp
    
  • 手動で時刻設定(初回のみ)

    ntpdate ntp.nict.jp
    
  • NTPd自動起動

    /etc/rc.d/init.d/ntpd start
    chkconfig ntpd on
    chkconfig --list ntpd
    

    ランレベルが2~5でonを確認する。

【WordPress】WordPress移行

  • WordPress移行
    Raspberry PiのWordPressをNUCのWordPressに移行します。
    移行元の管理画面から「ツール」-「エクスポート」を選択し、xmlファイルでエクスポートしておきます。

    移行先の管理画面から「ツール」-「インポート」を選択すると
    インポート元のシステムを選択する画面になるので、「WordPress」を選択します。
    「WordPress Importer」というプラグインをインストールし、
    先ほどエクスポートしておいたxmlファイルを指定して移行完了です。

    ・・・となるはずが、画面が変わらず反応がありません。

  • 試したこと1
    ダッシュボードの設定をある程度同じにしておかないとエラーになるのかと思い、
    設定変更後に挑戦しましたが、変わらず。

  • 試したこと2
    xmlファイルを読めていないのかと思い、phpのプラグインをインストール。

    yum -y install php-xml
    

    だめでした。

  • 試したこと3
    本家のBBSに下記のような記事がありました。
    https://wordpress.org/support/topic/wordpress-importer-04-not-importing

    Jon Caveさんの解決方法を試してみました。

    cd /var/www/html/任意ディレクトリ名/wp-content/plugins/wordpress-importer
    cp -pi parsers.php parsers.php.org
    vi parsers.php
    
    下記内容に変更
    if ( extension_loaded( 'simplexml' ) ) {
    ↓
    if ( false && extension_loaded( 'simplexml' ) ) {
    

    修正したif文の上でparseメソッドを呼び出しており、
    戻り値がfalseになってしまうので、falseが返ってきても
    強制的に処理を進めてしまうよう変更しています。

    かなり強引ですが、この解決方法で無事インポートできました。
    プラグインの設定を行い、ドキュメントルートからのパスは一緒なので、画像は再配置のみで
    NUCへの移行が完了しました。
    管理画面はNUCの方が性能が良いからなのか体感で早く感じます。

【WordPress】WordPress導入

  • WordPress導入

    mysql -u root -pデータベースパスワード
    
    create database データベース名;
    grant all privileges on データベース名.* to データベースユーザ名@localhost identified by "データベースパスワード";
    exit
    
    cd /var/www/html
    wget http://ja.wordpress.org/latest-ja.zip
    unzip latest-ja.zip
    mv wordpress/ 任意ディレクトリ名
    chown -R apache.apache 任意ディレクトリ名/
    cd 任意ディレクトリ名
    cp -pi wp-config-sample.php wp-config.php
    vi wp-config.php
    
    # 下記内容を変更
    define('DB_NAME', 'database_name_here');
    ↓
    define('DB_NAME', 'データベース名');
    
    define('DB_USER', 'username_here');
    ↓
    define('DB_USER', 'データベースユーザ名');
    
    define('DB_PASSWORD', 'password_here');
    ↓
    define('DB_PASSWORD', 'データベースパス');
    
    # 下記内容を「編集が必要なのはここまで(略)」より上に追加
    /* リビジョン無効 */
    define('WP_POST_REVISIONS', false);
    
    /* ログイン・管理画面SSL強制 */
    define('FORCE_SSL_LOGIN', true);
    define('FORCE_SSL_ADMIN', true);
    
    cd ../
    rm -f latest-ja.zip
    /etc/rc.d/init.d/iptables stop
    

    http://z-area.net/任意ディレクトリ名/wp-admin/install.php

    手順にそってインストールを行います。
    インストール後にログイン画面に飛ぶと自動でSSLに遷移もします。

【PHP】php5導入

php5インストール

yum -y install php php-mbstring php-mysql

拡張モジュールはmbstringとmysqlの2つ。

php5設定

vi /etc/httpd/conf/httpd.conf
下記内容を変更
DirectoryIndex index.html index.html.var
↓
DirectoryIndex index.html index.htm index.cgi index.php

下記内容を追加(「AddType application/x-gzip .gz .tgz」の下)
AddType application/x-httpd-php .php
vi /etc/php.ini
short_open_tag = Off
↓
short_open_tag = On

expose_php = On
↓
expose_php = Off

max_execution_time = 30
↓
max_execution_time = 300

;default_charset = "iso-8859-1"
↓
default_charset = "UTF-8"

post_max_size = 8M
↓
post_max_size = 20M

upload_max_filesize = 2M
↓
upload_max_filesize = 20M

;date.timezone =
↓
date.timezone = Asia/Tokyo

;mbstring.language = Japanese
↓
mbstring.language = Japanese

;mbstring.internal_encoding = EUC-JP
↓
mbstring.internal_encoding = UTF-8

;mbstring.http_input = auto
↓
mbstring.http_input = UTF-8

;mbstring.http_output = SJIS
↓
mbstring.http_output = pass

;mbstring.encoding_translation = Off
↓
mbstring.encoding_translation = On

;mbstring.detect_order = auto
↓
mbstring.detect_order = auto

;mbstring.substitute_character = none;
↓
mbstring.substitute_character = none;
/etc/rc.d/init.d/httpd restart
  • php5の動作確認

    echo "<?php phpinfo(); ?>" > /var/www/html/info.php
    /etc/rc.d/init.d/iptables stop
    

    http://192.168.11.30/info.php

    php5の情報が表示されれば正常です。

    /etc/rc.d/init.d/iptables start
    rm -f /var/www/html/info.php
    

【DBサーバ】MySQL導入

  • MySQLインストール

    yum -y install mysql-server
    
  • MySQL設定

    vi /etc/my.cnf
    
    # 下記内容を追加([mysqld]に)
    # クライアントの文字コードに依存しない
    skip-character-set-client-handshake
    # 文字コード
    character-set-server=utf8
    
    下記内容を追加
    [mysql]
    default-character-set=utf8
    
    /etc/rc.d/init.d/mysqld start
    chkconfig mysqld on
    chkconfig --list mysqld
    

    ランレベルが2~5でonを確認する。

  • rootユーザのパスワード設定とUTF-8の確認

    mysql -u root
    
    SET PASSWORD FOR root@localhost=password('データベースパスワード');
    

    character_set_filesystemとcharacter_sets_dir以外がUTF-8であることを確認する。

    show variables like 'char%';
    

    Variable_nameValue
    character_set_clientutf8
    character_set_connectionutf8
    character_set_databaseutf8
    character_set_filesystembinary
    character_set_resultsutf8
    character_set_serverutf8
    character_set_systemutf8
    character_sets_dir/usr/share/mysql/charsets/
  • 初期ユーザと初期データベースの削除

    mysql -u root -pデータベースパスワード
    

    ユーザ名が空のユーザを確認し、削除する。

    SELECT user,host FROM mysql.user;
    
    userhost
    root127.0.0.1
    localhost
    rootlocalhost
    z-area.net
    rootz-area.net
    DELETE FROM mysql.user WHERE user='';
    

    データベースを確認しデータベースtestを削除する。

    SHOW DATABASES;
    
    Database
    informationschema
    mysql
    test
    DROP DATABASE test;
    exit
    

【WEBサーバ】Apache2SSL導入

  • 秘密鍵作成

    cd /etc/pki/tls/certs
    openssl genrsa -aes256 1024 > server.key
    
    Generating RSA private key, 1024 bit long modulus
    ......................++++++
    ............++++++
    e is 65537 (0x10001)
    Enter pass phrase:パスワード入力
    Verifying - Enter pass phrase:パスワード再入力
    
  • 公開鍵作成

    openssl req -new -key server.key > server.csr
    
    Enter pass phrase for server.key:パスワード入力
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    略
    Country Name (2 letter code) [XX]:JP
    State or Province Name (full name) []:Tokyo
    Locality Name (eg, city) [Default City]:Akihabara
    Organization Name (eg, company) [Default Company Ltd]:OreOreCA
    Organizational Unit Name (eg, section) []:z-area
    Common Name (eg, your name or your server's hostname) []:z-area.net
    Email Address []:空欄
    A challenge password []:空欄
    An optional company name []:空欄
    
  • 証明書作成

    openssl x509 -in server.csr -days 36500 -req -signkey server.key > server.crt
    
    Signature ok
    subject=/C=JP/ST=Tokyo/L=Akihabara/O=OreOreCA/OU=z-area/CN=z-area.net
    Getting Private key
    Enter pass phrase for server.key:パスワード入力
    
  • 秘密鍵パスワード解除
    現状のままだとApache2の再起動のたびにパスワードを求められるので、
    秘密鍵のパスワードを解除しておきます。

    mv server.key server.key.org
    openssl rsa -in server.key.org > server.key
    
    Enter pass phrase for server.key.org:パスワード入力
    writing RSA key
    
  • Apache2のSSL導入

    yum install mod_ssl
    
  • Apache2のSSL設定

    cp -pi /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.org
    vi /etc/httpd/conf.d/ssl.conf
    
    下記内容に変更
    #DocumentRoot "/var/www/html"
    ↓
    DocumentRoot "/var/www/html"
    
    #ServerName www.example.com:443
    ↓
    ServerName z-area.net:443
    
    
    ErrorLog logs/ssl_error_log
    ↓
    ErrorLog /var/log/httpd/ssl_error_log
    
    TransferLog logs/ssl_access_log
    ↓
    #TransferLog logs/ssl_access_log
    
    CustomLog logs/ssl_request_log \
              "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
    ↓
    CustomLog /var/log/httpd/ssl_access_log \
              "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
    
    
    # POODLE SSLv3.0 脆弱性問題対処
    SSLProtocol all -SSLv2
    ↓
    SSLProtocol all -SSLv2 -SSLv3
    
    SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    ↓
    SSLCertificateFile /etc/pki/tls/certs/server.crt
    
    SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
    ↓
    SSLCertificateKeyFile /etc/pki/tls/certs/server.key
    
  • Apache2SSLの動作確認

    echo '<h1>It works!</h1>' > /var/www/html/index.html
    /etc/rc.d/init.d/iptables stop
    /etc/rc.d/init.d/httpd restart
    

    https://192.168.XXX.YYY/

    証明書の確認が表示された後、「It works!」が表示されれば正常です。

    /etc/rc.d/init.d/iptables start
    rm -f /var/www/html/index.html
    

【WEBサーバ】Apache2ログ設定

  • Apache2のログ設定

    vi /etc/httpd/conf/httpd.conf
    
    # エラーログ出力先
    ErrorLog /var/log/httpd/error_log
    
    # 通常ログからwormのログを除外
    SetEnvIf Request_URI "^/_mem_bin/" nolog
    SetEnvIf Request_URI "^/_vti_bin/" nolog
    SetEnvIf Request_URI "^/c/" nolog
    SetEnvIf Request_URI "^/d/" nolog
    SetEnvIf Request_URI "^/msadc/" nolog
    SetEnvIf Request_URI "^/MSADC/" nolog
    SetEnvIf Request_URI "^/scripts/" nolog
    SetEnvIf Request_URI "^/default.ida" nolog
    
    # 通常ログから画像、CSS、JavaScript参照ログを除外
    SetEnvIf Request_URI "\.(jpg|png|gif|css|js)$" nolog
    
    # 通常ログから自身の接続ログを除外
    SetEnvIf Remote_Addr 192.168.XX. nolog
    SetEnvIf Remote_Addr 10.8.0. nolog
    
    # 通常ログからロボットログを除外
    SetEnvIf User-Agent "Mediapartners-Google" nolog
    SetEnvIf User-Agent "msnbot" nolog
    SetEnvIf User-Agent "Yahoo! Slurp" nolog
    SetEnvIf User-Agent "Googlebot" nolog
    SetEnvIf User-Agent "Ask Jeeves" nolog
    SetEnvIf User-Agent "Gigabot" nolog
    SetEnvIf User-Agent "BecomeBot" nolog
    SetEnvIf User-Agent "Baiduspider" nolog
    SetEnvIf User-Agent "TurnitinBot" nolog
    SetEnvIf User-Agent "BecomeJPBot" nolog
    SetEnvIf User-Agent "Yeti" nolog
    SetEnvIf User-Agent "IRLbot" nolog
    SetEnvIf User-Agent "ia_archiver" nolog
    
    # 除外していない参照ログを通常ログとしてcombinedレベル(詳細)で出力
    CustomLog /var/log/httpd/access_log combined env=!nolog
    
  • Apache2の動作確認

    echo '<h1>It works!</h1>' > /var/www/html/index.html
    /etc/rc.d/init.d/iptables stop
    /etc/rc.d/init.d/httpd start
    

    http://192.168.XXX.YYY/

    「It works!」が表示されれば正常です。

    /etc/rc.d/init.d/iptables start
    rm -f /var/www/html/index.html
    
  • Apache2起動設定

    chkconfig httpd on
    chkconfig --list httpd
    

    ランレベルが2~5でonを確認する。

【WEBサーバ】Apache2設定

  • Apache2のドキュメントルート設定
    基本的な設定は完了したので、次にドキュメントルートの設定を行います。
    Apache2の設定はRaspberry Pi(Debian系)と違って1ファイルにまとまってます。

    vi /etc/httpd/conf/httpd.conf
    
    下記の設定となるように変更(変更部分または重要部分のみ記載)
    # ドキュメントルート
    DocumentRoot "/var/www/html"
    
    # 最上位の制御
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    
    # ドキュメントルートの制御
    <Directory "/var/www/html">
        Options -Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    
    # アクセス制御ファイル名
    AccessFileName .htaccess
    
    # ファイル名を指定しない場合の検索優先順位(index.html)
    <Files ~ "^\.ht">
        Order allow,deny
        Deny from all
        Satisfy All
    </Files>
    
    # iconsルートの制御
    <Directory "/var/www/icons">
        Options -Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
    
  • welcome.confの無効化

    mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.org
    
  • ドキュメントルートの権限変更

    chown -R ユーザ名 /var/www/html
    

【WEBサーバ】Apache2導入

  • Apache2導入

    yum -y install httpd
    yum list installed | grep httpd
    
    インストールした内容
    httpd.x86_64            2.2.15-39.el6.centos
    httpd-tools.x86_64      2.2.15-39.el6.centos
    
    cp -pi /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
    vi /etc/httpd/conf/httpd.conf
    
    下記内容を変更
    # サーバ情報表示内容
    ServerTokens OS
    ↓
    ServerTokens ProductOnly
    
    # サーバ署名表示内容
    ServerSignature On
    ↓
    ServerSignature Off
    
    # サーバー名
    #ServerName www.example.com:80
    ↓
    ServerName z-area.net:80