Postfix Virtual Hosting
- Note: Webmin can be used for virtual hosting most easily with the Virtualmin hosting control panel. It automates all of the following tasks, as well as many others commonly needed in a virtual hosting environment, such as setting up email, name service, and databases. Virtualmin is available for free download in Webmin module form from Webmin's home page, as well as an easy to install script from the Virtualmin download page at Virtualmin.com.
Virtual Hosting email with Postfix
At this point, if you've performed the configuration in the previous tutorial, you'll be able to accept mail for any number of domains. However, this is not the same as providing independent virtual hosting support with Postfix, because you can only have one user of a given name and mail sent to that user name at any of the domains for which you accept mail will be delivered to that user. So, for example, if you hosted swelltech.com, penguinfeet.org, and nostarch.com on the same server, and mail was sent to user joe at each of those domains, all three mails would end up in the same mailbox. Therefore, you have to introduce another layer to solve this problem.
Postfix Mail Server has two commonly used methods for solving this problem. The first is the native Postfix method, using a virtual table to direct mail to the correct destination. The second method is modeled after the way Sendmail handles the problem, and is therefore a lot more complex. Because simplicity is better than complexity, you'll learn the native Postfix mechanism exclusively. The Postfix virtual man page covers both methods in moderate detail. If you have an older Sendmail Mail Server installation that is being converted to Postfix you may wish to use the second method and maintain your current virtual mail configuration. If you will be running an extremely large number of virtual domains, it is likely preferable to use the second method, as well.
The first step for setting up virtual domain delivery is for you to create a virtual map table using the Virtual Domains page (Figure 10.7, "The Virtual Domains Table". Enter the map type (hash, dbm, etc.), followed by the file name of the flat file that will contain the table information. For example, you could use /etc/postfix/virtual for this purpose. This is a pretty common location for this file.
Figure 10.7. The Virtual Domains Table Adding a virtual domains table
Save and apply the change, and return to the Virtual Domains page. Now, you can click the New mapping button. You first have to create a generic map for the new domain. So, for the Name field, enter your virtual domain name. In the Maps to... field, you can technically enter anything you like (as long as we enter something). The custom seems to be to enter virtual in this field, as that is its purpose. Click Save mapping to add it to the virtual table.
Next, you'll want to add a postmaster alias, as all mail servers must have a functioning postmaster address to be compliant with the relevant RFC. So, click New mapping again. This time enter firstname.lastname@example.org into the Name field, where virtual.domain is the name of your domain. Then enter postmaster into the Maps to... field so that mail to this address will be mapper to the local postmaster address for normal delivery.
Finally, you're ready to start adding your virtual domain users to the table. Once again, create a new mapping. Fill in your new virtual domain mail address in the Name field. For example, you might fill in email@example.com. In the Maps to... section, enter the name of a local user that you would like to receive mail for this address. In this case, you would use virtual-joe or perhaps virtual.domain.joe. This new local user must exist for mail to be delivered, therefore you'll need to add the new user to the system.
Now, Save and Apply your changes, and test it out! The virtual maps can be handled by various database types, or exported to an LDAP Server database. There is no reasonable limit to the number of virtual users and domains you can have.