Squid là một phần mềm máy chủ proxy và bộ nhớ đệm được nhiều người sử dụng. Nó được sử dụng chủ yếu như một proxy chuyển tiếp, nhưng nó cũng có thể được sử dụng như một proxy ngược. Squid cung cấp rất nhiều tính năng và thường xuyên được sử dụng trên các mạng lớn.
1. Cài đặt các phụ thuộc
Chỉnh sửa tệp danh sách kho lưu trữ trong trình soạn thảo văn bản yêu thích của bạn.
$ sudo nano /etc/apt/sources.list
Bỏ ghi chú dòng sau để bật kho lưu trữ nguồn gói.
# deb-src http://archive.ubuntu.com/ubuntu/ focal main restricted
Lưu và thoát khỏi tệp.
Cập nhật danh sách gói.
$ sudo apt update
Cài đặt htpasswd
tiện ích và các phụ thuộc xây dựng cho Squid.
$ sudo apt install apache2-utils
$ sudo apt build-dep squid
2. Biên dịch Squid từ mã nguồn
Sao chép kho lưu trữ Squid GitHub chính thức vào một thư mục có tên squid
.
$ git clone https://github.com/squid-cache/squid.git squid
Điều hướng đến kho lưu trữ nhân bản.
$ cd squid
Kiểm tra kho lưu trữ đến nhánh V5
(phiên bản 5).
$ git checkout v5
Chạy tập lệnh cài đặt phần phụ thuộc để cài đặt phần phụ thuộc bổ sung.
$ ./bootstrap.sh
Định cấu hình bản dựng cho các vị trí hệ thống tệp Ubuntu.
$ ./configure --prefix=/usr --localstatedir=/var --libexecdir=${prefix}/lib/squid --datadir=${prefix}/share/squid --sysconfdir=/etc/squid --with-default-user=proxy --with-logdir=/var/log/squid --with-pidfile=/var/run/squid.pid
Biên dịch mã nguồn. Vui lòng không đóng phiên SSH của bạn cho đến khi quá trình biên dịch kết thúc.
$ make
Cài đặt các tệp nhị phân đã biên dịch.
$ sudo make install
Cấp quyền truy cập cho Squid vào thư mục nhật ký.
$ sudo chown -R proxy:proxy /var/log/squid
3. Tạo một dịch vụ Squid
Chỉnh sửa một tệp mới có tên squid.service
trong thư mục /etc/systemd/system
.
$ sudo nano /etc/systemd/system/squid.service
Điền vào tệp với các nội dung sau.
[Unit]
Description=Squid Web Proxy Server
Documentation=man:squid(8)
After=network.target network-online.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/squid.pid
ExecStartPre=/usr/sbin/squid --foreground -z
ExecStart=/usr/sbin/squid -sYC
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
[Install]
WantedBy=multi-user.target
Lưu và thoát khỏi tệp.
Tải lại hệ thống
$ sudo systemctl daemon-reload
Bắt đầu dịch vụ Squid.
$ sudo systemctl start squid
Cho phép dịch vụ bắt đầu khi khởi động máy chủ.
$ sudo systemctl enable squid
4. Cấu hình Squid
Chỉnh sửa tệp cấu hình Squid trong trình soạn thảo văn bản yêu thích của bạn.
$ sudo nano /etc/squid/squid.conf
Chỉ định cấu hình proxy để cho phép kết nối từ những người dùng đã xác thực và che địa chỉ IP của họ. Nối các dòng sau vào đầu tệp.
auth_param basic program /usr/lib/squid/basic_ncsa_auth /etc/squid/squid_passwd
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
forwarded_for off
request_header_access Allow allow all
request_header_access Authorization allow all
request_header_access WWW-Authenticate allow all
request_header_access Proxy-Authorization allow all
request_header_access Proxy-Authenticate allow all
request_header_access Cache-Control allow all
request_header_access Content-Encoding allow all
request_header_access Content-Length allow all
request_header_access Content-Type allow all
request_header_access Date allow all
request_header_access Expires allow all
request_header_access Host allow all
request_header_access If-Modified-Since allow all
request_header_access Last-Modified allow all
request_header_access Location allow all
request_header_access Pragma allow all
request_header_access Accept allow all
request_header_access Accept-Charset allow all
request_header_access Accept-Encoding allow all
request_header_access Accept-Language allow all
request_header_access Content-Language allow all
request_header_access Mime-Version allow all
request_header_access Retry-After allow all
request_header_access Title allow all
request_header_access Connection allow all
request_header_access Proxy-Connection allow all
request_header_access User-Agent allow all
request_header_access Cookie allow all
request_header_access All deny all
Lưu và thoát khỏi tệp.
5. Thiết lập xác thực Squid
Tạo tệp thông tin xác thực Squid Proxy.
$ sudo touch /etc/squid/squid_passwd
Tạo người dùng mới trên proxy của bạn. Thay thế youruser
trong lệnh tiếp theo bằng tên người dùng mong muốn của bạn. Lệnh này sẽ nhắc bạn nhập mật khẩu hai lần. Bạn có thể sử dụng lệnh tiếp theo nhiều lần để tạo thêm người dùng.
$ sudo htpasswd /etc/squid/squid_passwd youruser
Khởi động lại dịch vụ Squid.
$ sudo service squid restart
6. Định cấu hình Tường lửa
Mở cổng Squid Proxy ( 3128/UDP
) bằng UFW.
$ sudo ufw allow 3128/udp
7. Sử dụng Proxy của bạn
Bây giờ bạn có thể sử dụng Squid làm proxy chuyển tiếp. Thiết lập “cài đặt internet” của trình duyệt web để sử dụng proxy của bạn, bao gồm tên người dùng và mật khẩu mà bạn đã tạo trước đó. Truy cập trang web phát hiện địa chỉ IP, chẳng hạn như trang này , để đảm bảo rằng proxy đang hoạt động chính xác.