7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Squid 6.13をインストールする(ソースからコンパイル) for RockyLinux9 and UbuntuLinux22.x (SSLインスペクション対応)

Last updated at Posted at 2017-01-17

Proxyサーバーとして有名なSquidをRockyLinux9.3/UbuntuLinux 22.xにインストール(ソースからコンパイル)する方法をご紹介します。
AlmaLinuxやそのほかRHEL互換OSであれば、同様に以下の手順で導入可能だと思います。
Squidは、2025年2月現在、6.13が最新版となります。
今は、ソースがGitHubに置かれています。GitHubのBootstrapedのソースをダウンロードします

また、本インストールではあらかじめOpenSSLのインストールが必要となります。
OpenSSLのインストール方法は以下を参考にしてください。

RockyLinux 9.xとUbunt22.xにOpenSSL 3.2.xをインストールする(ソースからビルド)

インストール

bash
cd /usr/local/src
wget https://github.com/squid-cache/squid/releases/download/SQUID_6_13/squid-6.13.tar.gz
tar xfzv squid-6.13.tar.gz
cd squid-6.13/
./configure \
--with-large-files \
--enable-ssl-crtd \
--with-openssl=/usr/local/ssl \
--enable-security-cert-generators \
--enable-security-cert-validators\
--enable-linux-netfilter \
--with-default-user=daemon
make -j 8
make install

権限設定を変更する

bash
chown -R daemon:daemon /usr/local/squid

起動ファイルを作成する

起動ファイルは、CentOSのRPMを利用し、若干カスタマイズします。

squid.service
cat > /usr/lib/systemd/system/squid.service << EOF
[Unit]
Description=Squid caching proxy
After=syslog.target network.target nss-lookup.target

[Service]
Type=forking
LimitNOFILE=16384
#EnvironmentFile=/etc/sysconfig/squid
#EnvironmentFile=/usr/local/squid/etc/squid.conf
Environment="SQUID_CONF=/usr/local/squid/etc/squid.conf"
ExecStartPre=/usr/local/squid/libexec/cache_swap.sh
ExecStart=/usr/local/squid/sbin/squid \$SQUID_OPTS -f \$SQUID_CONF
ExecReload=/usr/local/squid/sbin/squid \$SQUID_OPTS -k reconfigure -f $SQUID_CONF
ExecStop=/usr/local/squid/sbin/squid -k shutdown -f \$SQUID_CONF
TimeoutSec=0

[Install]
WantedBy=multi-user.target
EOF

cache_swap.shを作成します。

bash:cache_swap.sh
cat > /usr/local/squid/libexec/cache_swap.sh << EOF
#!/bin/bash
if [ -f /etc/sysconfig/squid ]; then
        . /etc/sysconfig/squid
fi

SQUID_CONF=${SQUID_CONF:-"/usr/local/squid/etc/squid.conf"}

CACHE_SWAP=\`sed -e 's/#.*//g' $SQUID_CONF | grep cache_dir | awk '{ print $3 }'\`

for adir in $CACHE_SWAP; do
        if [ ! -d $adir/00 ]; then
                echo -n "init_cache_dir $adir... "
                squid -N -z -F -f $SQUID_CONF >> /usr/local/squid/var/logs/squid.out 2>&1
        fi
done
EOF
chmod +x /usr/local/squid/libexec/cache_swap.sh

設定ファイルの編集

デフォルトだと、ipv6でしかポートをlistenしないため、変更する

vi /usr/local/squid/etc/squid.conf

変更箇所
http_portに「0.0.0.0:ポート番号」とし、前にipv4の0.0.0.0を追加する

squid.confの変更箇所
http_port 0.0.0.0:3128

起動ユーザーをdaemonを指定する

squid.confの最下部に追加
cache_effective_user    daemon
cache_effective_group   daemon

保存する

起動と自動起動設定

bash
systemctl start squid
systemctl enable squid

設定ファイルに誤りが無いかをチェックする

bash
/usr/local/squid/sbin/squid -k check

SSLインスペクションに対応する

証明書を作成する

秘密鍵を作成

#Directory作成
export LD_LIBRARY_PATH=/usr/local/ssl/lib
mkdir /usr/local/squid/etc/cert/
#CSR+秘密鍵作成
/usr/local/ssl/bin/openssl req -new -key /usr/local/squid/etc/cert/squid.key -out /usr/local/squid/etc/cert/squid.csr

Windowsの証明期間で証明書を作成する

CAから証明書を作成する。
Windowsで証明書を発行する場合、証明書テンプレートは必ず「下位の証明期間」を選択します。
これがないと、ChromeやEdgeで証明書エラーが出ます。
追加属性には、入力は不要です。(san追加は不要)
image.png

Windowsの証明書サーバーで作成する場合は、必ず「下位の証明機関」で証明書を作成してください。通常のサーバー証明書で作成するとChrome等の場合エラーになりませんが、IEやIEのエンジンを使ったMicrosoft 365の認証などで証明書のエラーが表示され正常に動作しません。

証明書の配置

Windows CAで発行された証明書をダウンロードして、配置します。
さらに、Windows CA自体の証明書をもダウンロードし1つのファイルに記載します。
ここでは、squid.pemとします。
image.png

クライアントへインポートする証明書

先ほどの用意した証明書のベースであるCAの証明書とProxyサーバー用の証明書をインポートします。配置場所は、「信頼されたルート証明期間」を選択します。

/usr/local/ssl/bin/openssl req -new -newkey rsa:2048 -days 3650 -nodes -x509 -keyout squidCA.pem -out squidCA.pem

キャッシュ領域作成

bash
mkdir -p /usr/local/squid/var/lib
mkdir -p /usr/local/squid/var/spool

#キャッシュDB作成
/usr/local/squid/libexec/security_file_certgen -c -s /usr/local/squid/var/spool/ssl_db -M 4MB
chown -R daemon:daemon /usr/local/squid

設定ファイルを変更

vi /usr/local/squid/etc/squid/conf
/usr/local/squid/etc/squid/conf
#http_port 0.0.0.0:3128
# ↓
http_port 3128 ssl-bump generate-host-certificates=on dynamic_cert_mem_cache_size=4MB cert=/usr/local/squid/etc/cert/squid.pem key=/usr/local/squid/etc/cert/squid.key

下に追記
sslcrtd_program /usr/local/squid/libexec/security_file_certgen -s /usr/local/squid/var/spool/ssl_db -M 4MB
sslcrtd_children 5
ssl_bump server-first all
sslproxy_cert_error deny all

キャッシュDB作成+権限設定を変更する

bash
#/usr/local/squid/libexec/security_file_certgen -c -s /usr/local/squid/var/spool/ssl_db -M 4MB
chown -R daemon:daemon /usr/local/squid

起動と自動起動設定

bash
systemctl start squid
systemctl enable squid

トラブルシューティング

プロセスが落ちて起動しない場合

systemd[1]: Starting Squid caching proxy...
systemd[1]: Started Squid caching proxy.
squid[420873]: Squid Parent: will start 1 kids
squid[420873]: Squid Parent: (squid-1) process 420875 started
squid[420873]: Squid Parent: squid-1 process 420875 exited with status 1
squid[420873]: Squid Parent: (squid-1) process 420883 started
squid[420873]: Squid Parent: squid-1 process 420883 exited with status 1
squid[420873]: Squid Parent: (squid-1) process 420891 started
squid[420873]: Squid Parent: squid-1 process 420891 exited with status 1
squid[420873]: Squid Parent: squid-1 process 420907 will not be restarted for 3600 seconds due to repeated, frequent failures

とエラーが出る場合は、
/usr/local/squid/var/log/cache.log
を確認する。

おそらく、

sslcrtd_program| FATAL: Cannot generate certificates: Uninitialized SSL certificate database directory: /usr/local/squid/var/spool/ssl_db. To initialize, run "security_file_certgen -c -s /usr/local/squid/var/spool/ssl_db".

というエラーが出ている可能性が高い。

その場合は、「キャッシュ領域作成」のディレクトリ作成から作業を行う。

証明書エラーが出る場合

image.png
このような証明書エラーが出る場合、以下の点を確認してください。

1. 証明書を発行する際に、「下位の証明機関」で発行をしていない場合。

Webサーバーなどで発行するとこうなります
以下を参考に、CAの証明書の下に、Squid用に発行した証明書が表示されているかを確認してください。この多段になっていない場合、下位の証明機関で発行された証明書になっていない可能性が高いです。
image.png

この場合Windowsの証明期間で証明書を作成するを確認して、証明書を再発行してください。
再発行したら3. 証明書を設定した後に、Squid側のキャッシュを消していないの手順も必ず実行してください。

また、配置した証明書にCAの証明書が記載されているかも併せて確認してください。
証明書の配置

2.CAの証明書をPEMに追加していない

かならず、発行したProxyの証明書の下に、CAの証明書を追記してください。

3. 証明書を設定した後に、Squid側のキャッシュを消していない

これが一番はまるのですが、設定をミスして再度設定をした場合であっても事前に作った証明書が使いまわされるため、証明書を上記の対応で変更しても同一のサイトでは反映されません。

以下を実行し、アクセスした証明書キャッシュを一度消すと新しい証明書が作成されるため確認しやすいです。

bash
rm -rf /usr/local/squid/var/spool
mkdir -p /usr/local/squid/var/spool

#キャッシュDB作成
/usr/local/squid/libexec/security_file_certgen -c -s /usr/local/squid/var/spool/ssl_db -M 4MB
chown -R daemon:daemon /usr/local/squid

証明書を再発行したり入れ替えたら、必ず「3. 証明書を設定した後に、Squid側のキャッシュを消していない」の手順を実行してください。
そうしないと、古い証明書情報のキャッシュがそのまま利用され、新しい証明書の情報が反映されません。

7
4
0

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
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?