LoginSignup
2

More than 3 years have passed since last update.

全文検索サーバを構築した話 その4(webサーバ,sftp環境構築編)

Posted at

全文検索するサービスを構築する

全文検索サーバと検索する文書はできたのですが、ファイルをどのような形で
ユーザに提供するのかも検討する必要があります。
今回はWebサーバで提供することにしました。
Sambaにすると閲覧ユーザーとファイルアップロード更新ユーザが同じサービスで
管理できるのでメリットもあるのですが、ここはあえて分けたほうが後々の
セキュリティを担保しやすいのではないかと思い、以下のようにサービスを
分けました。

ユーザ向けサービス

  • Fessによる全文検索サービス (サービスポート:80)
  • apacheによる文書閲覧サービス (サービスポート:8080)

管理者向けサービス

  • sftpによる文書作成更新 (sshでsftp専用ユーザを作成してファイルを更新する)

Fessに関しましては細かい内容をその2で記載しているので、ここではapacheとsftpの
設定について記載いたします。

httpd(apache)構築

インストール
$ sudo yum install httpd

インストール:
  httpd.x86_64 0:2.4.6-89.el7.centos

依存性関連をインストールしました:
  apr.x86_64 0:1.4.8-3.el7_4.1        apr-util.x86_64 0:1.5.2-6.el7       httpd-tools.x86_64 0:2.4.6-89.el7.centos
  mailcap.noarch 0:2.1.41-2.el7

完了しました!
httpd.conf編集
# httpd.confをバックアップする
$ sudo cp -p /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org

# httpdのリッスンポートを80→8080に変更する
$ sudo sed -i 's/Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf

# diffで確認
$ diff /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
42c42
< Listen 8080
---
> Listen 80

# welcomeページを表示しないように修正する
$ sudo vi /etc/httpd/conf.d/welcome.conf

# This configuration file enables the default "Welcome" page if there
# is no default index page present for the root URL.  To disable the
# Welcome page, comment out all the lines below.
#
# NOTE: if this file is removed, it will be restored on upgrades.
#
<LocationMatch "^/+$">
    Options -Indexes
    ErrorDocument 403 /.noindex.html
</LocationMatch>

↓

LocationMatchタグをすべてコメントアウトする
# This configuration file enables the default "Welcome" page if there
# is no default index page present for the root URL.  To disable the
# Welcome page, comment out all the lines below.
#
# NOTE: if this file is removed, it will be restored on upgrades.
#
#<LocationMatch "^/+$">
#    Options -Indexes
#    ErrorDocument 403 /.noindex.html
#</LocationMatch>

httpd.confのデフォルト設定、rootディレクトリのOptions Indexesはあえて残します。
文書が保存されているディレクトリををWebディレクトリとして利用するためです。

サービス設定と起動
$ sudo systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

$ sudo systemctl start httpd

$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 金 2019-06-07 18:14:22 JST; 12s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 1905 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
 Main PID: 2250 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    Tasks: 6
   Memory: 2.8M
   CGroup: /system.slice/httpd.service
           tq2250 /usr/sbin/httpd -DFOREGROUND
           tq2251 /usr/sbin/httpd -DFOREGROUND
           tq2252 /usr/sbin/httpd -DFOREGROUND
           tq2253 /usr/sbin/httpd -DFOREGROUND
           tq2254 /usr/sbin/httpd -DFOREGROUND
           mq2255 /usr/sbin/httpd -DFOREGROUND

sftp環境設定

PDFアップロード用ディレクトリ作成
$ sudo chmod 775 /var/www/html/pdf/
$ sudo chown -R root:apache /var/www/html/pdf/
PDFアップロード用ユーザ作成
$ sudo useradd -s /sbin/nologin -G apache -d /var/www/html/ -M ftpuser
sftp専用ユーザ設定をsshd_configに追記する
$ sudo cat << EOS >> /etc/ssh/sshd_config
Match User ftpuser
  ChrootDirectory ~
  ForceCommand internal-sftp
EOS
sshd再起動で反映
$ sudo systemctl restart sshd

sshdはreloadでも反映されると思いますが、どこまで反映されるのかしっかり把握していないので
いつも再起動してしまいます。

完成

これで一通りの環境が完成しました。
なお、HTTPS化していないのは社内限定サービスのため暗号化を重要視していないためです。
証明書の設定とかもめんどくさいですからね…

利用者は、httpで80ポートに接続してFessにアクセスして検索、検索結果のファイルを
httpで8080ポートに接続してapacheから取得して閲覧するという形です。

文書管理者は、sftpでwebサーバのドキュメントルート以下に文書を格納していきます。
Chrootしているので他のディレクトリにはアクセスできません。

最後のまとめ

こんな感じで、テキスト無PDFをOCRしてテキスト埋め込みPDFを作成し、そのPDFを
全文検索サーバに載せて公開するまでのシナリオを書いてみました。

今回の内容はほぼ備忘録的に書いていますが、同じようなお悩みを抱えている方に
活用して頂けたらと思います。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2