RHCSA系列:安装、配置和保护Web和FTP服务器-第9部分

A web server (also known as a HTTP server) is a service that handles content (most commonly web pages, but other types of documents as well) over to a client in a network.

A FTP server is one of the oldest and most commonly used resources (even to this day) to make files available to clients on a network in cases where no authentication is necessary since FTP uses username and password without encryption.

RHEL 7 中可用的 Web 服务器是 Apache HTTP Server 的版本 2.4。至于 FTP 服务器,我们将使用 Very Secure Ftp Daemon(又称为 vsftpd)建立由 TLS 保护的连接。

RHCSA: Installing, Configuring and Securing Apache and FTP – Part 9

在本文中,我们将解释如何在 RHEL 7 中安装、配置和保护 Web 服务器和 FTP 服务器。

安装 Apache 和 FTP 服务器

在本指南中,我们将使用具有静态 IP 地址 192.168.0.18/24 的 RHEL 7 服务器。要安装 Apache 和 VSFTPD,请运行以下命令:

# yum update && yum install httpd vsftpd

安装完成后,这两个服务最初将被禁用,因此我们需要手动启动它们,并在下次引导时启用它们以自动启动:

# systemctl start httpd
# systemctl enable httpd
# systemctl start vsftpd
# systemctl enable vsftpd

此外,我们必须打开端口 8021,分别监听 Web 和 FTP 守护程序,以允许从外部访问这些服务:

# firewall-cmd --zone=public --add-port=80/tcp --permanent
# firewall-cmd --zone=public --add-service=ftp --permanent
# firewall-cmd --reload

要确认 Web 服务器正常工作,请启动您的浏览器并输入服务器的 IP。您应该会看到测试页面:

Confirm Apache Web Server

至于 FTP 服务器,我们将不得不进一步配置它,在确认它按预期工作之前,我们将在一分钟内完成。

配置和保护 Apache Web 服务器

Apache 的主配置文件位于 /etc/httpd/conf/httpd.conf,但它可能依赖于位于 /etc/httpd/conf.d 内的其他文件。

官方文件所述。

總是在編輯主配置文件之前備份一份副本:

# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.$(date +%Y%m%d)

然後用您首選的文本編輯器打開它,並尋找以下變量:

  1. ServerRoot:服務器配置、錯誤和日誌文件所在的目錄。
  2. Listen:指示Apache監聽特定的IP地址和/或端口。
  3. Include:允許包含其他配置文件,這些文件必須存在。否則,服務器將失敗,與IncludeOptional指令相反,如果指定的配置文件不存在,則會被默默忽略。
  4. User and Group:運行httpd服務的用戶/組名稱。
  5. DocumentRoot:Apache用於提供文檔的目錄。默認情況下,所有請求都來自這個目錄,但可以使用符號鏈接和別名指向其他位置。
  6. ServerName:此指令設置服務器用於識別自身的主機名(或IP地址)和端口。

第一個安全措施將包括創建專用使用者和組(即 tecmint/tecmint)來運行 Web 伺服器,並將默認端口更改為較高的端口(在此情況下為 9000):

ServerRoot "/etc/httpd"
Listen 192.168.0.18:9000
User tecmint
Group tecmint
DocumentRoot "/var/www/html"
ServerName 192.168.0.18:9000

您可以使用以下方式測試配置文件。

# apachectl configtest

如果一切正常,則重新啟動 Web 伺服器。

# systemctl restart httpd

別忘了在防火牆中啟用新端口(並禁用舊端口):

# firewall-cmd --zone=public --remove-port=80/tcp --permanent
# firewall-cmd --zone=public --add-port=9000/tcp --permanent
# firewall-cmd --reload

請注意,由於 SELinux 策略的原因,您只能使用由

# semanage port -l | grep -w '^http_port_t'

返回的端口來用於 Web 伺服器。

如果您想使用其他端口(例如 TCP 端口 8100),您將需要將其添加到 SELinuxhttpd 服務的端口上下文中:

# semanage port -a -t http_port_t -p tcp 8100
Add Apache Port to SELinux Policies

為了進一步保護您的 Apache 安裝,請按照以下步驟進行:

1. Apache 正在運行的使用者不應該有訪問外殼的權限:

# usermod -s /sbin/nologin tecmint

2. 禁用目錄列表,以防止瀏覽器顯示目錄的內容,如果該目錄中沒有 index.html

編輯 /etc/httpd/conf/httpd.conf(以及任何虛擬主機的配置文件,如果有的話),並確保在頂部和目錄塊級別的 Options 指令設置為 None

Options None

3. 在 HTTP 響應中隱藏有關 Web 伺服器和操作系統的信息。 編輯 /etc/httpd/conf/httpd.conf 如下所示:

ServerTokens Prod 
ServerSignature Off

現在您準備好從您的/var/www/html目錄開始提供內容。

配置和保護FTP服務器

與Apache一樣,Vsftpd的主配置文件(/etc/vsftpd/vsftpd.conf)有詳細的注釋,儘管默認配置對大多數應用程序來說應該足夠,但您應該熟悉文檔和man頁面(man vsftpd.conf)以更有效地運行ftp服務器(我無法再強調這一點了!)

在我們的情況下,使用了以下指令:

anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
chroot_local_user=YES
allow_writeable_chroot=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

通過使用chroot_local_user=YES,本地用戶(默認情況下)將在登錄後立即被放置在其家目錄中的chroot’ed監獄中。這意味著本地用戶將無法訪問其相應的家目錄之外的任何文件。

最後,要允許ftp讀取用戶的家目錄中的文件,設置以下SELinux布爾值:

# setsebool -P ftp_home_dir on

現在您可以使用Filezilla等客戶端連接到ftp服務器:

Check FTP Connection

請注意,/var/log/xferlog日志記錄了下載和上傳,這與上述目錄列表一致:

Monitor FTP Download and Upload

閱讀更多在Linux系統中使用Trickle限制應用程序使用的FTP網絡帶寬

總結

在本教程中,我們已經解釋了如何設置網絡和FTP伺服器。由於這個主題的廣泛性,不可能涵蓋所有這些主題的各個方面(即虛擬網絡主機)。因此,我建議您也查看本網站關於Apache的其他優秀文章。

Source:
https://www.tecmint.com/rhcsa-series-install-and-secure-apache-web-server-and-ftp-in-rhel/