公司的企业邮是用阿里云的,早几天人事跟我说加不了账号了,我一看免费用户数50,已经满了,问了下续费要上万块钱一年,还不如迁回本地服务器自己搭建邮件系统使用呢。 问了下同事说他们之前给用户用过zimbra,所以就先用这个系统做测试了。 先测试,测试可以的话后续再做迁移。然后zimbra我搭了就很麻烦,安装的时候卡着,可能是这个太大了,所以找个轻量一点的,所以就试试mailcow。

准备工作

虚拟机和操作系统

在exsi上开了台虚拟机做测试,由于使用的人数很多,配置给的还挺高的,16核32G,500G存储。

看了下官方的系统要求,可以用ubunt24.04,这里就用ubuntu24.04了。

装系统的时候分区分了400G给/opt,其余的给/根目录,配置好IP。再通过ssh进去进行下一步。

域名和公网ip

域名需要提前买好,本地化部署需要网络有公网的静态ip

DNS设置

修改域名解析到cloudflare,虽然域名在阿里云买的,但是cf对邮件的解析支持更好。

然后还有其他一些dns设置,先按照官方文档去配着。

操作系统优化

更换更新源

nano /etc/apt/sources.list

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse

更换完之后更新一下 apt-get update && apt-get dist-upgrade && apt-get autoremove && apt-get autoclean -y

sudo不需要密码

nano /etc/sudoers 添加一行

user ALL=(ALL) NOPASSWD: NOPASSWD: ALL

修改时区

sudo timedatectl set-timezone Asia/Shanghai

安装部署

docker安装

apt install curl vim wget gnupg dpkg apt-transport-https lsb-release ca-certificates

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/aliyun-docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/aliyun-docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

apt update

apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin

docker镜像加速

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": [
    "https://docker.1ms.run",
    "https://docker.mybacc.com",
    "https://dytt.online",
    "https://lispy.org",
    "https://docker.xiaogenban1993.com",
    "https://docker.yomansunter.com",
    "https://aicarbon.xyz",
    "https://666860.xyz",
    "https://docker.zhai.cm",
    "https://a.ussh.net",
    "https://hub.littlediary.cn",
    "https://hub.rat.dev",
    "https://docker.m.daocloud.io"
  ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

邮件系统安装部署

按照官方的教程进行安装部署

$ su
# umask
0022 # <- Verify it is 0022
# cd /opt
# git clone https://github.com/mailcow/mailcow-dockerized
# cd mailcow-dockerized
./generate_config.sh

输入mail servers hostname,时区和版本。

docker compose pull
docker compose up -d

这样就完成安装部署了。

反向代理设置

由于服务器在本地,公司的静态ip没有开通80/443端口,本来要找电信开通的,结果要我们重新备案,我弄crm的时候都已经在阿里云备案过了的,好烦,不想再弄了,还是用其他端口吧。

用的npm做反向代理,但是如果按照平常那样设置的话,sogo的跳转会有问题,sogo的跳转不会带端口,这样sogo的访问就成问题了。 找了下教程,首先把generate_config.sh的https端口改了,改成了反向代理的端口,然后在npm的高级里增加里以下字段:

location / {
   proxy_pass http://192.168.1.208:80/; 
   proxy_set_header Host $http_host;
   proxy_redirect  http:// https://; 
   proxy_set_header X-Forwarded-Host  $http_host;  
   proxy_set_header X-Forwarded-Port  $server_port;  
   proxy_set_header X-Forwarded-Proto $scheme;
   proxy_set_header X-Forwarded-Scheme $scheme;
   proxy_set_header X-Real-IP         $remote_addr;
   proxy_set_header X-Forwarded-For   $remote_addr;
   proxy_set_header Upgrade    $http_upgrade;
   proxy_set_header Connection $http_connection;
   proxy_http_version 1.1;
  }

这样就解决了sogo的跳转的端口问题了。

邮件优化设置

RDNS设置

rdns需要在本地路由上设置,公司用的路由器是ikuai,可以在路由器上设置rdns

使用rdns,要把邮件服务器的dns设置成路由器地址。

还可以在cf上设置PTR,dkim,srv,_dmarc,spf。

设置好之后就可以登录管理后台,创建域名和邮箱账号了。

备份

crontab -e 添加一个备份的脚本

5 3 * * * cd /opt/mailcow-dockerized/; MAILCOW_BACKUP_LOCATION=/opt/backup /opt/mailcow-dockerized/helper-scripts/backup_and_restore.sh backup all

邮件得分

虽然测试是满分,但是发给国内的一些企业邮还是被垃圾邮箱了,这个也没办法了,也没什么能优化的方向了。 就这样吧