Installation instructions for PheMail.
By YazzY: yazzy at yazzy dot org.

Introduction.

Phemail is an ISP suite.
It consists of a few applications which make it easy and convinient to host and administrate your users.
As opposite to most other similar suites which use unreliable scripting to change and store the user info, PheMail uses database storage for every configuration options.
This makes it not only extremely flexible and reliable but also very fast and portable.
Thanks to the technology used in the PhemMal one can frankly use any major database system.
As for now PheMail supports two of the most popular and widely used databases in the world: MySQL and PostgreSQL.
Hooks against other database systems like MSSQL are also possible but not well tested. 
Phemail runs on *NIX systems including various Linux distributions like Red Hat, SuSe, Slackware, Debian, Gentoo, Mandrake and all the BSD's - FreeBSD, NetBSD, OpenBSD.

The reccomended databse for PheMail is PostgresSQL and this HowTo will use it in the configurartion examples.
Besides the difference in the choice of database system , the rest of the software is the same for any other databases.
The configuration differs though.

This HowTo is written for FreeBSD and was tested on FreeBSD 5.2.


Installing the software.

We assume you have updated your ports tree.

Apache:
It's wise to have your default login site secured with SSL. That's why we will install apache with support for it.
Note: Do not install Apache 2. Use the stable 1.3 version!

# cd /usr/ports/www/apache13-modssl ; make APACHE_WITH_MODDEFLATE=yes install clean

mod_deflate compresses files using gzip before sending them to web browsers, resulting in as much as 90% reduction of bandwidth usage and download time.

Mod_Perl:
# cd /usr/ports/www/mod_perl ; make install clean

Mod_Security:
# cd /usr/ports/www/mod_security ; make install clean

Mod_Frontpage:
# cd /usr/ports/www/mod_frontpage ; make install clean

PheMail-General-0.01:
# cd /usr/ports/mail/p5-PheMail-General ; make install clean

PheMail-Vhost-0.14:
# cd /usr/ports/mail/p5-PheMail-Vhost ; make install clean

Perl DBD-Pg
# cd /usr/ports/databases/p5-DBD-Pg ; make install clean

PHP:
# cd /usr/ports/lang/php4 ; make install clean.

You will see a ncurses menu with several options. Be sure to chose PostgreSQL 7.4, PEAR, IMAP, Socket, GD, mbstring (multibyte string) and gettext support.
IMAP and mbstring is required for webmail only, not essential for PheMail to work.
The PHP port will install all the dependiencies, including the PostgreSQL database.

Pear-DB:
# /usr/ports/databases/pear-DB ; make install clean

Postfix:
# cd /usr/ports/mail/postfix ; make install clean
Chose support for PostgreSQL in the ncurses menu just like with the PHP port.

# ln -s /usr/local/sbin/postfix /usr/local/etc/rc.d/postfix.sh

Edit your /etc/rc.conf and add there :
sendmail_enable="NONE"


Amavisd-new:
# cd /usr/ports/security/amavisd-new ; make install clean
# cp /usr/local/etc/amavisd.conf-dist /usr/local/etc/amavisd.conf


PowerDNS:
edit your /etc/make.conf and for future portupgrades add there 
POWERDNS_OPTIONS="PostgreSQL" 

Run: 
# cd  /usr/ports/dns/powerdns ; make install clean.
# cp /usr/local/etc/rc.d/pdns.sh.sample /usr/local/etc/rc.d/pdns.sh

Note: If you do not want to touch your make.conf file you can also run following to enable PostgreSQL support:
# cd  /usr/ports/dns/powerdns ; make WITH_POSTGRESQL_DRIVER=yes install clean


SpamAssassin:
# cd /usr/ports/mail/p5-Mail-SpamAssassin/ ; make install clean
Edit your /etc/rc.conf and add there :
spamd_enable="YES"


Clamav:
# cd /usr/ports/security/clamav ; make install clean
Edit your /etc/rc.conf and add there:
clamav_clamd_enable="YES"
freshclam_enable="YES"


Yadda:
# cd /usr/ports/mail/yadda ; make install clean


Courier-IMAP:
# cd /usr/ports/mail/courier-imap/ ; make WITH_POSTGRESQL=yes install clean



Now, lets go further to where all the fun begins. Configuration of you installed software.



Configuring your software:

PHP:
Edit /usr/local/etc/php.conf and change PHP_PEAR=no to PHP_PEAR=yes


Courier-IMAP:
# cd /usr/local/etc/courier-imap
Copy all the files with .dist extension to the ones without it, i.e 
# cp authpgsqlrc.dist authpgsqlrc
and so on.

Use your favourite text editor and edit the authdaemonrc file.
Replace 
authmodulelist="authmysql authpam"
with
authmodulelist="authpgsql authpam"

Edit authpgsqlrc

Replace following lines with your own options:
PGSQL_HOST              pgsql.example.com
PGSQL_PORT              5400
PGSQL_USERNAME          admin
PGSQL_PASSWORD          admin

I used 
PGSQL_HOST              localhost
PGSQL_PORT              5400
PGSQL_USERNAME          postfix
PGSQL_PASSWORD          mypassword


Replace 
PGSQL_DATABASE          template1
with 
PGSQL_DATABASE          phemail

Replace 
PGSQL_USER_TABLE        passwd
with
PGSQL_USER_TABLE        virtual8

Add folowing in the PGSQL_SELECT_CLAUSE:0 part:

PGSQL_SELECT_CLAUSE     SELECT ("username" || '@' || "domain"), passwd, '', '65534', '65534', \
                        ('/home/customers/' || "domain" || '/mail/' || "username" || '/'), '.',quota, \
                        realname FROM virtual8 WHERE "username"='$(local_part)' AND \
                        "domain"='$(domain)' and enabled=1 and forward=0


Edit imapd.cnf and pop3d.cnf putting there your own values for your contry, town etc.
Edit imapd and replace IMAPDSTART=NO with IMAPDSTART=YES
Edit imapd-ssl and replace IMAPDSSLSTART=NO with IMAPDSSLSTART=YES
Edit pop3d and replace POP3DSTART=NO with POP3DSTART=YES
Edit pop3d-ssl and replace POP3DSSLSTART=NO with POP3DSSLSTART=YES


Amavisd:
Again use your favourite text editor and edit the  /usr/local/etc/amavisd.conf file.

Replace $mydomain = 'example.com'; with your own domain.
Uncomment the $inet_socket_bind = '127.0.0.1' option.

The most importand section in our case is Section V with the @lookup_sql_dsn part. 
That's where we have our SQL settings. The rest you can adjust to match your taste.
Uncomment the @lookup_sql_dsn and put there something like this:
@lookup_sql_dsn =
        ( ['DBI:Pg:host=localhost;dbname=amavis', 'dbaseusername', 'dbpassword'] );

Where dbaseusername is username connecting to the amavis database and dbpassword is this user's password.



Clamav:

Edit /usr/local/etc/clamav.conf 
Comment the Example line putting # infront of it.
Replace the lines saying: 
LogFile #/tmp/clamd.log with LogFile /var/log/clamav/clamd.log
#PidFile /var/run/clamd.pid with PidFile /var/run/clamav/clamd.pid
#DataDirectory /var/lib/clamav with DatabaseDirectory /usr/local/share/clamav
#User clamav with User vscan

Adjust (or not) resource usage.


Edit /usr/local/etc/freshclam.conf
Replace DatabaseOwner clamav with DatabaseOwner vscan

Finally run:
# chown -R vscan:vscan /var/log/clamav/ /usr/local/share/clamav/ /var/run/clamav/


PowerDNS:

Edit /usr/local/etc/pdns.conf 
Replace daemon=no with daemon=yes
Replace setgid= with setgid=bind
Replace setuid= with setuid=bind

Put following in the launch section:
launch=gpgsql
# gpgsql config
gpgsql-host=localhost
gpgsql-user=dbaseusername
gpgsql-password=dbpassword
gpgsql-dbname=mail


Pure-FTP:
Edit /usr/local/etc/pure-ftpd.conf

Set the NoAnonymous option to yes
Replace:
# PGSQLConfigFile               /etc/pureftpd-pgsql.conf
with 
PGSQLConfigFile               /usr/local/etc/pureftpd-pgsql.conf
Uncomment the LogPID option (remove the # sign infront of it).
Replace
# AltLog                     stats:/var/log/pureftpd.log
with 
AltLog                     stats:/var/log/pureftpd-stats.log
Uncomment the CreateHomeDir  option (remove the # sign infront of it).

Adjust the resto of the values as it suits you.


Postfix:

# cd /usr/local/etc/postfix
# mkdir postgres

Create destination.cf and put there following lines:
user = dbaseusername
password = dbpassword
dbname = mail
table = virtual8
select_field = domain
where_field = domain
hosts = localhost


Create relay file and put there following lines:
192.168.1.0/24 
127.0.0.1

Those are the hosts you relay for. 192.168.1.0/24 would be your computers on yout local LAN.

Now lets configure the postix's hooks against PostgreSQL
# cd postgres

Create 






Apache:

# cd /usr/local/etc/apache
Edit the httpd.conf file.



PostgreSQL:

As root:
# su - pgsql
$ /usr/local/bin/createuser yourusername

As user
# /usr/local/bin/createdb phemail
# psql phemail
phemail=# create user postfix with password 'mypassword';

To remove a user:
phemail=# drop user tester;

You can then start PostgreSQL by running:
# /usr/local/etc/rc.d/010.pgsql.sh start

Edit ~pgsql/data/postgresql.conf and set tcpip_socket = true 
and uncomment the port = 5432 part.
Run:
# /usr/local/etc/rc.d/010.pgsql.sh restart

Read http://www.postgresql.org/docs/faqs/FAQ.html#3.6 for more details.

Create initial phemail database:
# cat phemail.db | psql phemail

To allow many simultaneous connections to your PostgreSQL server, you should raise the SystemV shared memory limits in your kernel. 
Here are example values for allowing up to 180 clients (tinkering in postgresql.conf also needed, of course):
  options         SYSVSHM
  options         SYSVSEM
  options         SYSVMSG
  options         SHMMAXPGS=65536
  options         SEMMNI=40
  options         SEMMNS=240
  options         SEMUME=40
  options         SEMMNU=120


You should vacuum and backup your database regularly. There is a periodic script, /usr/local/share/postgresql/502.pgsql, that you may find useful.