Fedora 安装 Apache HTTP Server

前言:由于要用到一些 Web 应用,虽然已经有了 Tomcat 服务器,可是 Tomcat 是用来开发时用的,常常须要重启,因此想要安装一个一直运行的服务器,以方便其余静态的 Web 应用正常使用。Apache HTTP Server 是最经常使用的 Web 服务器之一,因此安装来试试。这次操做环境是 Fedora 27。html

目录

一. 安装 HTTPD

1.安装 HTTPDweb

# Fedora 22 及以上版本
[xx@xxx ~]# dnf install httpd -y

# Fedora 21 及更早版本
[xx@xxx ~]# yum install httpd -y

2.启动 HTTPD 服务数据库

[xx@xxx ~]# systemctl start httpd.service

3.设置开机时自动启动 HTTPD 服务(可选)apache

[xx@xxx ~]# systemctl enable httpd.service

此时已经安装完成了,在浏览器访问 http://localhost (或 http://127.0.0.1/) 能够查看 Apache HTTP 服务器的测试界面,若能看到正常的页面,说明服务器已经正常工做。可是此时可能没法从任何其余主机访问该服务器,所以若要从其余主机访问服务器,请参阅后面打开防火墙端口的说明。浏览器

二. 保护 Apache HTTPD

要启用 TLS / SSL 支持,请下载并安装如下软件包之一:安全

使用 mod_sslruby

1.安装 mod_ssl
使用如下命令安装mod_ssl包,mod_ssl 包将在安装后自动启用 :服务器

[xx@xxx ~]# dnf install mod_ssl -y

2.生成新证书
要生成新证书,请参阅使用OpenSSL建立证书网络

3.安装现有证书
若是在另外一台计算机上生成过证书,请执行如下操做:app

# 将证书和密钥文件移动到正确的文件夹
[xx@xxx ~]# mv key_file.key /etc/pki/tls/private/myhost.com.key
[xx@xxx ~]# mv certificate.crt /etc/pki/tls/certs/myhost.com.crt

# 确保如下参数正确:
# SELinux contexts
[xx@xxx ~]# restorecon /etc/pki/tls/private/myhost.com.key
[xx@xxx ~]# restorecon /etc/pki/tls/certs/myhost.com.crt
# 拥有者
[xx@xxx ~]# chown root.root /etc/pki/tls/private/myhost.com.key
[xx@xxx ~]# chown root.root /etc/pki/tls/certs/myhost.com.crt
# 权限
[xx@xxx ~]# chmod 0600 /etc/pki/tls/private/myhost.com.key
[xx@xxx ~]# chmod 0600 /etc/pki/tls/certs/myhost.com.crt

安装现有证书后,使用 mod_ssl 配置设置证书。

4.mod_ssl 配置
默认的 TLS / SSL 配置包含在文件 /etc/httpd/conf.d/ssl.conf 中。在 ssl.conf 文件中,如下是指定 TLS / SSL 证书和密钥所在位置的指令:

SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key

这些指令包含在定义虚拟主机的块中:

<VirtualHost _default_:443>
...
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
...
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
...
</VirtualHost>

要为这些文件定义其余位置,请执行如下操做:

  • 建立 /etc/httpd/conf.d/ssl.conf 文件的副本,并将文件名改成 z-ssl-local.conf
  • 编辑 z-ssl-local.conf 文件中的如下行:
<VirtualHost _default_:443>
SSLCertificateFile /etc/pki/tls/certs/www.myhost.org.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.myhost.org.key
</VirtualHost>

此文件将覆盖 default:443 虚拟主机的两个设置;而来自 ssl.conf 的全部其余设置将被保留。

5.单个虚拟主机的设置
要将特定虚拟主机的 SSL / TLS 用做默认的其余证书,请执行如下操做:

  • 打开该虚拟主机的配置文件 /etc/httpd/conf.d/hostname.conf。
  • 在 <VirtualHost hostname:port> 和 <VirtualHost> 之间插入这些行:
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/hostname.crt
SSLCertificateKeyFile /etc/pki/tls/private/hostname.key</pre>

三. 配置 Apache HTTPD

/etc/httpd/conf/httpd.conf 是 Apache 的主要配置文件。自定义配置文件在 /etc/httpd/conf.d/*.conf 中。若是在 /etc/httpd/conf/httpd.conf/etc/httpd/conf.d/*.conf 文件中指定了相同的设置,则以 /etc/httpd/conf.d/*.conf 文件中的设置为准。

/etc/httpd/conf.d/ 文件夹中的文件将会按字母顺序读取:/etc/httpd/conf.d/z-foo.conf 中的设置将会覆盖 /etc/httpd/conf.d/foo.conf 中的设置。一样,/etc/httpd/conf.d/99-foo.conf 中的设置将会覆盖 /etc/httpd/conf.d/00-foo.conf 中的设置。

为了更好的体验效果,请不要直接修改 /etc/httpd/conf/httpd.conf 或 Fedora 软件包附带的任何 /etc/httpd/conf.d 文件。 若是对这些文件进行任何本地更改,则不会直接应用在较新软件包版本中对它们进行的任何更改。 相反,这些修改将建立 .rpmnew 文件,致使必须手动合并更改。

建议在 /etc/httpd/conf.d 中建立一个新文件,设置该文件优先于要修改的文件,而后哦编辑所需的设置。例如,要更改 /etc/httpd/conf.d/foo.conf 中指定的设置,能够建立文件 /etc/httpd/conf.d/z-foo-local.conf,并将新设置写在该文件中。

# 注意:
# 对服务器配置进行任何更改后,请执行如下命令: 
[xx@xxx ~]# apachectl reload 

# 某些更改可能须要 Apache 彻底从新启动。要彻底从新启动Apache,请执行如下命令: 
[xx@xxx ~]# systemctl restart httpd.service 

启用对 Web 应用程序的访问

默认状况下,Fedora 打包的 Web 应用程序一般配置为只容许从 localhost 访问。 这是由文件 /etc/httpd/conf.d/webapp.conf 中定义的,文件中包含如下设置:

<Directory /usr/share/webapp>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require local
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        Allow from ::1
    </IfModule>
</Directory>

在容许通常访问webapp以前,请确保执行如下操做:

  • Webapp 已正确配置
  • 没有适当的身份验证,没法访问管理界面和其余敏感区域
  • 若是应用程序使用数据库,则数据库配置是安全的

若是只是要放宽对应用程序的访问,请建立文件 /etc/httpd/conf.d/z-webapp-allow.conf。容许访问本地网络上的全部系统时,请将如下行添加到文件中:

<Directory /usr/share/webapp>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require local
        Require ip 192.168.1
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
        Allow from ::1
        Allow from 192.168.1
    </IfModule>
</Directory>

正确配置应用程序后,添加如下配置以容许从任何主机进行访问:

<Directory /usr/share/webapp>
    <IfModule mod_authz_core.c>
        # Apache 2.4
        Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
        # Apache 2.2
        Order Deny,Allow
        Allow from all
    </IfModule>
</Directory>

打开防火墙端口

注意:这会将您的计算机暴露给Internet和潜在的攻击者。在将服务器暴露给Internet以前,请正确保护系统和Apache安装。

默认状况下,Apache 将端口 80 用于普通 http 链接,将端口 443 用于 TLS / SSL 链接。要从其余计算机或 Internet 提供此服务,请使用如下任一命令容许 Apache 经过防火墙:

服务器启动时自动容许 Apache 经过防火墙:

# 对于普通 HTTP 链接:
[xx@xxx ~]# firewall-cmd --permanent --add-service=http

# 对于 TLS/SSL 链接:
[xx@xxx ~]# firewall-cmd --permanent --add-service=https

每次手动容许 Apache 当即经过防火墙:

# 对于普通 HTTP 链接:
[xx@xxx ~]# firewall-cmd --add-service=http

# 对于 TLS/SSL 链接:
[xx@xxx ~]# firewall-cmd --add-service=https

注意:若是您的服务器是在 NAT 路由器的网络中运行,想要容许从本地网络外部进行访问的话,则还须要配置路由器以将 HTTP 和 HTTPS 端口转发到您的服务器。

四. 故障排除

暂无

本文为转载翻译文章,有些内容已经被我忽略掉了,想要看详细内容,能够查看原文。固然我建议仍是去看一下原文,那里说明的更好一点。