本指南适用于哪些人? 自托管 Web 内容与中小型公司、企业主、非营利组织和个人相关。 对于谁可以自我托管并没有真正的限制,但 将直接受益并看到立竿见影的效果。中小型公司将获得一个功能齐全的系统,该系统符合所有当前的加密和安全协议,尤其是 PCI 规范。 注重隐私的群体 为什么要自托管? 自托管有好处的原因有很多,以下是其中一些: 隐私 增强的安全性 客制化 降低成本 电子邮件托管 这篇文章是关于网络服务器托管的,如果您对电子邮件托管感兴趣,请阅读关于 的文章。 自托管您自己的电子邮件服务器 先决条件 一个新的服务器,专用的或虚拟的,可以访问互联网 基于 RHEL 的 Linux 发行版:Alma、Rocky、Oracle 或 Fedora Server 根访问权限 服务器主机名 请确保您的服务器具有完全限定的域名 (FQDN),并且 文件中的服务器 IP 指向 FQDN 而不是短名称。这是某些工具(如 OpenDKIM)的要求,如果没有 FQDN,这些工具将无法工作。 /etc/hosts 要测试您的 FQDN 是否设置正确,请运行: # hostname -f server.example.tld 如果您只看到服务器名称而不是 FQDN,则使用 systemd 命令设置您的 FQDN: hostnamectl # hostnamectl set-hostname server.example.tld 第 1 步 - 所需的软件包 第一步是安装一些基本包,使用你的包管理器: 更新版本的 EL8/EL9 的 dnf: # dnf install php php-cli php-pdo php-mbstring epel-release unzip wget EL7 的百胜: # yum install php php-cli php-pdo php-mbstring epel-release unzip wget 对于 Fedora,软件包略有不同: # dnf install php php-cli php-pdo php-mbstring unzip wget 第 2 步 - 安装 Aetolos 在第二步中,我们将安装 Aetolos 虚拟主机控制面板,它只生成配置文件,不会对系统进行任何其他修改: # wget -O /root/master.zip 'https://gitlab.com/noumenia/aetolos/-/archive/master/aetolos-master.zip' # unzip /root/master.zip -d /root/ # mv /root/aetolos-master /root/aetolos # rm -rf /root/master.zip 最新的开发代码可以从git仓库下载: git clone 'https://gitlab.com/noumenia/aetolos.git' 第 3 步 - 启用服务 现在是时候选择服务器将提供的服务了。使用状态参数运行 Aetolos 以了解所有可用服务: # ~/aetolos/aetolos --status +---------+--------------+---------+------------+--------------+ | Enabled | Module | SystemD | Repository | Dependencies | +---------+--------------+---------+------------+--------------+ | [ ] | apache | | | | | [ ] | clamav | | epel | postfix | | [ ] | dehydrated | | | | | [ ] | dovecot | | | | | [ ] | haproxy | | | | | [ ] | mariadb | | | | | [ ] | mtasts | | | apache | | [ ] | nsd | | epel | | | [ ] | opendkim | | epel | postfix | | [ ] | opendmarc | | epel | postfix | | [✓] | php | | | | | [ ] | postfix | | | | | [ ] | postgrey | | epel | postfix | | [ ] | spamassassin | | | postfix | | [✓] | virtualhost | | | | +---------+--------------+---------+------------+--------------+ 由于这将是一个自托管的网络服务器,我们将启用为我们提供一整套网络托管功能的服务: Apache,用于响应 ACME 请求的 Web 服务器 Dehydrated,Let's Encrypt 证书的 bash 脚本 MariaDB,数据库 如果您有一个单独的服务器用于使用 DKIM 签名的电子邮件,您还应该在此服务器中启用 OpenDKIM。然后在服务器之间共享密钥以进行正确签名。 让我们使用 Aetolos 启用这些服务: # ~/aetolos/aetolos --enable=apache # ~/aetolos/aetolos --enable=dehydrated # ~/aetolos/aetolos --enable=mariadb 现在我们可以重新运行带有状态参数的 Aetolos 来查看下表: +---------+--------------+---------+------------+--------------+ | Enabled | Module | SystemD | Repository | Dependencies | +---------+--------------+---------+------------+--------------+ | [✓] | apache | dead | | | | [ ] | clamav | | epel | postfix | | [✓] | dehydrated | | | | | [ ] | dovecot | | | | | [ ] | haproxy | | | | | [✓] | mariadb | dead | | | | [ ] | mtasts | | | apache | | [ ] | nsd | | epel | | | [ ] | opendkim | | epel | postfix | | [ ] | opendmarc | | epel | postfix | | [✓] | php | dead | | | | [ ] | postfix | | | | | [ ] | postgrey | | epel | postfix | | [ ] | spamassassin | | | postfix | | [✓] | virtualhost | | | | +---------+--------------+---------+------------+--------------+ 请注意 SystemD 列,它将大多数服务显示为 。这不是问题,因为我们还没有配置/启动这些服务。 dead 第 4 步 - 设置 最后,我们可以运行 Aetolos setup 来安装所需的 RPM 包并生成所有配置文件。我们更喜欢使用额外的 参数查看详细输出。 --verbose 您会注意到有关脱水脚本的警告,它告诉我们指定 Let's Encrypt 的注册电子邮件地址 # ~/aetolos/aetolos --verbose --setup [DEBUG] Check running system [DEBUG] Detected: AlmaLinux 8.7 (Stone Smilodon) [DEBUG] Check system memory [DEBUG] Detected: 460MiB [DEBUG] Check CPU cores [DEBUG] Detected: 2 [DEBUG] Check proxy [DEBUG] Loading Aetolos configuration [DEBUG] Starting operating system setup [DEBUG] Checking repository dependencies [DEBUG] Checking package dependencies [DEBUG] Installing package: php-fpm [DEBUG] Checking SELinux requirements [DEBUG] SELinux: enable httpd_can_network_connect [DEBUG] SELinux: enable httpd_can_sendmail [DEBUG] SELinux: enable httpd_read_user_content [DEBUG] SELinux: enable httpd_enable_homedirs [DEBUG] SELinux: enable httpd_home_tmp_aetolos.pp [DEBUG] Checking certificate requirements [DEBUG] Generating a self-signed certificate [DEBUG] Certificate: /etc/pki/tls/certs/localhost.crt [DEBUG] Fullchain: /etc/pki/tls/certs/localhost.fullchain [DEBUG] Key: /etc/pki/tls/private/localhost.key [DEBUG] Checking ACME client [DEBUG] Downloading ACME client: dehydrated 0.7.1 [DEBUG] Installing ACME client under: /root/dehydrated/ [WARNING] ACME registration email set to: postmaster [WARNING] Please change the ACME registration email by executing: '/root/aetolos/aetolos --verbose --module=dehydrated --registration-email=new-email' [DEBUG] Checking module dependencies [DEBUG] Save configuration: apache [DEBUG] Writing to file: /etc/httpd/conf/httpd.conf [DEBUG] Writing to file: /etc/httpd/conf.d/ssl.conf [DEBUG] Writing to file: /etc/sysconfig/htcacheclean [INFO] System memory: 460MB [INFO] Allocate 50% of system memory to MariaDB: 230MB [INFO] 70% of the above usage goes to total thread memory: 161MB [INFO] The rest 30% goes to server buffers: 69MB [INFO] MariaDB memory per connection: 18MB [INFO] MariaDB max connections: 10 [INFO] MariaDB temporary table size: 16MB [INFO] MariaDB query cache: 16MB [INFO] Allocate 40% of system memory to Apache/PHP-FPM: 184MB [INFO] Estimated average number of connections per CPU core: 9 [INFO] Apache StartServers: 1 [INFO] Apache ServerLimit: 2 [INFO] Apache ThreadsPerChild: 18 [INFO] Apache MinSpareThreads: 18 [INFO] Apache ThreadLimit: 22 [INFO] Apache MaxRequestWorkers: 36 [INFO] Apache maximum number of concurrent connections: 108 [DEBUG] Writing to file: /etc/logrotate.d/httpd [DEBUG] Save configuration: dehydrated [DEBUG] Writing to file: /root/dehydrated/config [DEBUG] Writing to file: /root/dehydrated/hook.sh [DEBUG] Writing to file: /etc/pki/letsencrypt/domains.txt [DEBUG] Writing to file: /etc/cron.daily/dehydrated.cron [DEBUG] Save configuration: mariadb [DEBUG] Writing to file: /etc/systemd/system/mariadb.service.d/limitnofile.conf [DEBUG] Writing to file: /etc/my.cnf.d/mariadb-server.cnf [DEBUG] Save configuration: php [DEBUG] Writing to file: /etc/php.ini [DEBUG] Writing to file: /etc/php.d/10-opcache.ini [DEBUG] Save configuration: virtualhost [DEBUG] Writing to file: /etc/tmpfiles.d/hometmp.conf [DEBUG] Reloading systemd 根据警告,我们为 Let's Encrypt 添加了一个电子邮件地址: # ~/aetolos/aetolos --verbose --module=dehydrated --registration-email=postmaster@example.tld 启动 MariaDB 与其他服务不同,MariaDB 需要启动并运行,Aetolos 才能添加虚拟主机和相应的数据库权限。因此,让我们启用并启动 MariaDB: # systemctl --now enable mariadb 此时我们可以通过运行以下命令来保护 MariaDB 实例: /usr/bin/mysql_secure_installation 这要求您还使用 root 的登录详细信息创建 文件: /root/.my.cnf [client] default-character-set="utf8mb4" user="root" password='the_password_set_in_the_previous_step' 添加虚拟主机 现在我们已准备好添加任意数量的虚拟主机(或服务器可以处理的任意数量)。 在运行任何命令之前,我们可能会花点时间考虑哪些虚拟主机将具有前缀子域,有时我们可能有一个像 example.tld 和 www 子域(www.example.tld)这样的域,其他时候我们可能不想使用前缀是因为我们希望我们的电子邮件地址仅位于顶级域(user@example.tld 而不是 user@www.example.tld)。 例如,我们将添加三个虚拟主机,其中两个没有子域: # ~/aetolos/aetolos --verbose --module=virtualhost --add-virtualhost=example1.tld --no-prefix [DEBUG] Verify domain: example1.tld [DEBUG] Add virtual host: example1.tld # ~/aetolos/aetolos --verbose --module=virtualhost --add-virtualhost=example2.tld --no-prefix [DEBUG] Verify domain: example2.tld [DEBUG] Add virtual host: example2.tld # ~/aetolos/aetolos --verbose --module=virtualhost --add-virtualhost=example3.tld [DEBUG] Verify domain: example3.tld [DEBUG] Add virtual host: example3.tld 我们现在可以列出所有虚拟主机: # ~/aetolos/aetolos --module=virtualhost --list-virtualhosts +----+---------------------+--------------+-------------+----------------+--------+--------------+ | ID | Timestamp | Virtual host | System user | MariaDB prefix | Parked | Prefix alias | +----+---------------------+--------------+-------------+----------------+--------+--------------+ | 1 | 2023-01-10 08:51:36 | example1.tld | example1tld | example1 | [ ] | | | 2 | 2023-01-10 08:51:40 | example2.tld | example2tld | example2 | [ ] | | | 3 | 2023-01-10 08:51:45 | example3.tld | example3tld | example3 | [ ] | www | +----+---------------------+--------------+-------------+----------------+--------+--------------+ 每个虚拟主机都将“独立”在自己的主目录中,拥有自己的用户/组所有权,所以让我们看看我们的 /home: # ls -la /home total 0 drwxr-xr-x. 5 root root 63 Jan 20 10:51 . dr-xr-xr-x. 17 root root 224 Dec 21 2021 .. drwx--x--x. 8 example1tld example1tld 159 Jan 10 08:51 example1tld drwx--x--x. 8 example2tld example2tld 159 Jan 10 08:51 example2tld drwx--x--x. 8 example3tld example3tld 159 Jan 10 08:51 example3tld 设置 使用 Aetolos 命令进行修改后,我们需要为所有相关服务(Apache、PHP-FPM、Dehydrated 等)生成新的配置文件。因此,我们最后一次执行 Aetolos 设置。 # ~/aetolos/aetolos --setup TLS 证书 此时所有服务都将使用相同的自签名证书,因此现在是手动运行脱水以收集所有虚拟主机证书的好时机。由于 Aetolos 使用 ACME 协议并通过 HTTP 端口 80 进行域验证,因此我们首先启动 Apache 服务器来为这些请求提供服务。 # systemctl start httpd # ~/dehydrated/dehydrated --cron --config /root/dehydrated/config --keep-going # systemctl stop httpd # ~/aetolos/aetolos --setup 服务 我们现在可以启用并启动所有服务。 # systemctl --now enable httpd httpd@example1.tld httpd@example2.tld httpd@example3.tld mariadb php-fpm Created symlink /etc/systemd/system/multi-user.target.wants/httpd@example1.tld.service → /usr/lib/systemd/system/httpd@.service. Created symlink /etc/systemd/system/multi-user.target.wants/httpd@example2.tld.service → /usr/lib/systemd/system/httpd@.service. Created symlink /etc/systemd/system/multi-user.target.wants/httpd@example3.tld.service → /usr/lib/systemd/system/httpd@.service. 您会注意到每个虚拟主机都有自己的 Apache 服务,这是 systemd 提供的一项功能,允许我们将虚拟主机定义为单独的 systemd 服务。 参数将启动所有服务。 --now 架构 应该特别提到用于托管网站的架构。出于安全原因,每个虚拟主机都隔离在存储在它们自己的 目录下的 unix 帐户中。公共 Web 内容应存储在 目录中。 /home public_html /home/example1tld -> virtual host home /home/example1tld/etc -> email accounts /home/example1tld/mail -> email storage /home/example1tld/public_html -> web content /home/example1tld/www -> symbolic link to public_html /home/example1tld/tmp -> temp directory 埃托洛斯 此时 Aetolos 已经配置了服务器,没有任何自定义修改,所有更改都是在默认配置文件上完成的。我们可能会从服务器中删除 Aetolos 并继续手动修改,或者我们可能会保留 Aetolos 以备将来添加更多虚拟主机时使用。 学分 来自 pexels 的 Manuel Geissinger 的文章照片