开源 Ubuntu nginx shell apache Firefox 微软 centos 程序员 linux mysql HTML5 Python linux命令 Windows java wordpress php Android google

LNMP环境下SendMail+OpenWebMail的详细配置

随着网络的发展和普及,邮件服务器正在成为人们日常生活中不可缺少的部分。现在,许多企业采用 Lotus Note, Exchange 作为公司内部的邮件服务器。本文主要介绍一种基于Linux系统的邮件服务器软件(sendmail)提供邮件服务。 

注意:之前已经配置好了LNMP web环境。

配置过程/步骤:

1. 安装Sendmail
yum安装或者rpm包安装:
yum -y install sendmail sendmail-devel sendmail-cf sendmail-do m4

2. 设置在系统3和5级别启动
chkconfig --level 35 sendmail on
chkconfig --list sendmail
sendmail        0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

3. 添加邮件服务器的邮件服务域名
vim /etc/mail/local-host-names
将:“linux.com”添加到 local-host-names 文件中

4. 开启sendmail服务器的网络接口
cp sendmail.mc sendmail.mc.bak   为了防止出错,备份配置文件
vim /etc/mail/sendmail.mc
查 找:DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
修改为:DAEMON_OPTIONS(`Port=smtp,Addr=0.0.0.0, Name=MTA')dnl

5. 设置SMTP的用户认证
vim /etc/mail/sendmail.mc
查找:

dnl 52 TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 login PLAIN')dnl
dnl 53 define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl

去掉前面注释:“dnl”

在sendmail服务器中使用了saslauthd服务程序提供用户认证功能,所以要开启它
chkconfig --level 35 saslauthd on
service saslauthd start

6. 生成sendmail.cf文件
cd /etc/mail
m4 sendmail.mc > sendmail.cf
service sendmail restart

7. 用户管理
groupadd mailuser   /* 建立邮件用户组*/
useradd -g mailuser -s /sbin/nologin mike   /*建立不能登录系统的邮件用户*/
vim /etc/aliases   /*sendmail使用aliases机制实现邮件别名和群发功能*/
admin:  mike   /*admin是mike的别名*/
testgroup:  mike,john,tom,jack   /*邮件群发,向testgroup邮箱发送就是向4个人发送*/
newaliases   /*对aliases.db数据库更新*/

8. 安装POP3,IMAP功能
yum install dovecot
vim /etc/dovecot.conf
查 找:protocols = imap imaps
修改为:protocols = imap imaps pop3 pop3s
chkconfig --level 35 dovecot on
service dovecot restart

到这里sendmail已经配置完成,开启服务器后,可以用outlook邮件客户端软件进行收发邮件。下面在介绍openwebmail的安装:

9. 安装openwebmail
下载所需软件包:www.openwebmail.org   www.rpmfind.net
yum install perl-CGI-SpeedyCGI perl-suidperl perl-compress-Zlib perl-Text-Iconv
rpm –ivh --nodeps openwebmail-data-2.53-3.i386.rpm
rpm –ivh --nodeps openwebmail-2.53-3.i386.rpm

10. 初始化openwebmail系统
/var/www/cgi-bin/openwebmail/openwebmail-tool.pl --init

11. 修改配置文件
vim /var/www/cgi-bin/openwebmail/etc/openwebmail.conf
domainnames     linux.com
default_language    zh_CN.GB2312
default_iconset                 Cool3D.Chinese.Simplified

12. 安装Perl的FastCGI模块
下载FCGI: http://cpan.wenzk.com/authors/id/F/FL/FLORA/FCGI-0.71.tar.gz
tar zxvf FCGI-0.71.tar.gz
cd FCGI-0.71
perl Makefile.PL
make && make install
rpm -ivh perl-FCGI-ProcManager-0.18-1.el4.rf.noarch.rpm
或者:yum install perl-FCGI-ProcManager

13. 配置Perl的FastCGI脚本(perl-fast)

vim /etc/init.d/perl-fast

#!/usr/bin/perl -w
use FCGI;
use Socket;
use FCGI::ProcManager;
sub shutdown { FCGI::CloseSocket($socket); exit; }
sub restart { FCGI::CloseSocket($socket); &main; }
use sigtrap 'handler', \&shutdown, 'normal-signals';
use sigtrap 'handler', \&restart, 'HUP';
require 'syscall.ph';
use POSIX qw(setsid);
#export FCGI_SOCKET_PATH="/tmp/perl-fastcgi.sock"
#export FCGI_NPROCESSES=4
#&daemonize; we don't daemonize when running under runsv
#this keeps the program alive or something after exec'ing perl scripts
END() { }
BEGIN() { }
{
no warnings;
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };
};
eval q{exit};
if ($@) {
exit unless $@ =~ /^fakeexit/;
}
&main;

sub daemonize() {
chdir '/' or die "Can't chdir to /: $!";
defined( my $pid = fork ) or die "Can't fork: $!";
exit if $pid;
setsid() or die "Can't start a new session: $!";
umask 0;
}

sub main {
#.... ip sockets
#$socket = FCGI::OpenSocket( "127.0.0.1:8999", 10 );
#.... UNIX sockets
#$socket = FCGI::OpenSocket( "/temp/perl-fastcgi.sock", 10 );
#foreach $item (keys %env) { delete $ENV{$item}; }
#..fastcgi........
my $n_processes = $ENV{FCGI_NPROCESSES} || 4;
$proc_manager = FCGI::ProcManager->new( {n_processes => $n_processes} );
#..unix socket
$socket = FCGI::OpenSocket( "$ENV{FCGI_SOCKET_PATH}", 10 );
#..Socket..
chmod 0777, $ENV{FCGI_SOCKET_PATH};

; #use UNIX sockets - user running this script must have w access to the 'Nginx' folder!!
$request =
FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,
&FCGI::FAIL_ACCEPT_ON_INTR );
$proc_manager->pm_manage();
if ($request) { request_loop() }
FCGI::CloseSocket($socket);
}

sub request_loop {
while ( $request->accept() >= 0 ) {
$proc_manager->pm_pre_dispatch();

#processing any STDIN input from WebServer (for CGI-POST actions)
$stdin_passthrough = '';
{ no warnings; $req_len = 0 + $req_params{'CONTENT_LENGTH'}; };
if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) )
{
my $bytes_read = 0;
while ( $bytes_read < $req_len ) {
my $data = '';
my $bytes = read( STDIN, $data, ( $req_len - $bytes_read ) );
last if ( $bytes == 0 || !defined($bytes) );
$stdin_passthrough .= $data;
$bytes_read += $bytes;
}
}

#running the cgi app
if (
( -x $req_params{SCRIPT_FILENAME} ) && #can I execute this?
( -s $req_params{SCRIPT_FILENAME} ) && #Is this file empty?
( -r $req_params{SCRIPT_FILENAME} ) #can I read this file?
)
{
pipe( CHILD_RD, PARENT_WR );
pipe( PARENT_ERR, CHILD_ERR );
my $pid = open( CHILD_O, "-|" );
unless ( defined($pid) ) {
print("Content-type: text/plain\r\n\r\n");
print
"Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
next;
}
$oldfh = select(PARENT_ERR);
$| = 1;
select(CHILD_O);
$| = 1;
select($oldfh);
if ( $pid > 0 ) {
close(CHILD_RD);
close(CHILD_ERR);
print PARENT_WR $stdin_passthrough;
close(PARENT_WR);
$rin = $rout = $ein = $eout = '';
vec( $rin, fileno(CHILD_O), 1 ) = 1;
vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
$ein = $rin;
$nfound = 0;

while ( $nfound =
select( $rout = $rin, undef, $ein = $eout, 10 ) )
{
die "$!" unless $nfound != -1;
$r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
$r2 = vec( $rout, fileno(CHILD_O), 1 ) == 1;
$e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
$e2 = vec( $eout, fileno(CHILD_O), 1 ) == 1;

if ($r1) {
while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
print STDERR $errbytes;
}

if ($!) {
$err = $!;
die $!;
vec( $rin, fileno(PARENT_ERR), 1 ) = 0
unless ( $err == EINTR or $err == EAGAIN );
}
}
if ($r2) {
while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
print $s;
}
if ( !defined($bytes) ) {
$err = $!;
die $!;
vec( $rin, fileno(CHILD_O), 1 ) = 0
unless ( $err == EINTR or $err == EAGAIN );
}
}
last if ( $e1 || $e2 );
}
close CHILD_RD;
close PARENT_ERR;
waitpid( $pid, 0 );
} else {
foreach $key ( keys %req_params ) {
$ENV{$key} = $req_params{$key};
}

# cd to the script's local directory
if ( $req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/]+$/ ) {
chdir $1;
}
close(PARENT_WR);

#close(PARENT_ERR);
close(STDIN);
close(STDERR);

#fcntl(CHILD_RD, F_DUPFD, 0);
syscall( &SYS_dup2, fileno(CHILD_RD), 0 );
syscall( &SYS_dup2, fileno(CHILD_ERR), 2 );

#open(STDIN, "<&CHILD_RD");
exec( $req_params{SCRIPT_FILENAME} );
die("exec failed");
}
} else {
print("Content-type: text/plain\r\n\r\n");
print
"Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
}
}
}

chmod +x  /etc/init.d/perl-fast
vim /etc/profile

添加以下内容:

export FCGI_SOCKET_PATH="/tmp/perl-fastcgi.sock"
export FCGI_NPROCESSES=4

执行perl-fast脚本,在后台运行。
source /etc/profile
/etc/init.d/perl-fast &

14. 配置enable_perl.conf文件,文件放在 nginx/conf  目录下。内容如下:

#enable openwebmail

fastcgi_pass unix:/tmp/perl-fastcgi.sock;
fastcgi_index openwebmail.pl;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

fastcgi_read_timeout 60;

然后为Nginx添加FastCGI的Perl支持

server {
       listen 80;
       server_name mail.linuxde.net;
       charset gb2312;
       access_log /wslogs/linuxde/mail_access.log combined;
       location / {
                root /var/www;
                index index.html index.htm index.PHP;
       }
       location ~* .*\.pl$ {
                root /var/www;
                include enable_perl.conf;
        }
}

15. 在web服务器中发布openwebmail
为了浏览器中不用输入很长的地址,做一个引导页
vim /var/www/index.php
<script language=javascript>
location.href="cgi-bin/openwebmail/openwebmail.pl";
</script>

16. 文件权限的设置
#chown root.root /var/www/cgi-bin/openwebmail/
#chown root.mail /var/www/cgi-bin/openwebmail/*
#chmod 775 /var/www/cgi-bin/openwebmail/openwebmail*.pl
#chown root.mail /var/www/cgi-bin/openwebmail/openwebmail*.pl
#chmod 4555 /usr/bin/suidperl
#chmod 775 /var/www/cgi-bin/openwebmail/etc/sessions

配置完毕,测试和如何使用我想应该不用多韶了!

延伸阅读

  • 抱歉,暂无相关内容!

评论