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中可用的网络服务器是Apache HTTP服务器的版本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和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 运行的用户不应具有 shell 访问权限:

# 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 jail中。这意味着本地用户将无法访问其对应主目录之外的任何文件。

最后,为了允许ftp读取用户主目录中的文件,设置以下SELinux布尔值:

# setsebool -P ftp_home_dir on

现在你可以使用像Filezilla这样的客户端连接到ftp服务器:

Check FTP Connection

请注意/var/log/xferlog日志记录下载和上传,与上述目录列表相符:

Monitor FTP Download and Upload

阅读也: 使用Trickle在Linux系统中限制应用程序使用的FTP网络带宽

摘要

在本教程中,我们已经解释了如何设置Web和FTP服务器。由于这些主题的广泛性,不可能涵盖所有方面(例如虚拟Web主机)。因此,我建议您还应该查看本网站上关于Apache的其他优秀文章。

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