| « HOWTO: Install Ubuntu Server as a VirtualBox Guest | HOWTO: Convert from single disk to RAID 1 without reinstalling » |
I wrote this HOWTO after following many other HOWTOs for Postfix and Exim and finding that parts did not work or did not explain how things worked. In short I spent many hours installing and reinstalling, playing around with settings and generally having fun. (I need to get a life) I wrote this HOWTO as record of what I did to get my own mail server up and running. I can now and in the future follow these steps again and rebuild my own mail server. I hope it is of use to others too.
Follow up:
We will run through the setup of this one step at a time, hopefully not making any huge steps. You will be able to test the parts installed as we go, no huge big bangs. All this as we move gently towards our goal of a professional quality mail server running on Ubuntu Karmic Koala 9.10.
I am not a Linux expert or security guru so there are probably some holes in the security of this server. If you believe that, please, let me know what and why.
Feedback is most certainly welcome for this HOWTO. Send your comments, criticisms and suggestions for additions or changes using the comment options below or via email using the link. I will try to reply as needed.
The mail server this HOWTO will be building is called mailserver and will be running Ubuntu Karmic Koala 9.10 server edition. mailserver will run as a Vmware guest which allows for simple backups if nothing else. mailserver will be part of the example.com domain with a sister domain example.net.
mailserver.example.com local IP 192.168.0.11
internet IP 1.2.3.4
The server called mailserver will end up running the following servers and services.
We are starting from a new install of Ubuntu Karmic Koala Server with no other software installed. During the installation process there will be a menu asking what other servers should be installed. Do not select any at this time.
See my HOWTO: Install Ubuntu Server as a VirtualBox Guest For step by step instructions on how to set this up.
After the install is done make sure it is all up to date with the latest patches and security updates.
Install and setup ssh. You can then ssh to mailserver. Copy and paste work better and you can make the terminals bigger. The bigger terminals make editing much easier.
install nfs autofs. These make transfering files simpler. I used these mappings a lot while writing this HOWTO as I installed the complete system a number of times. If at first you don't succeed, go and have another beer.
network interface. Get this sorted out so mailserver has a static IP address.
Make sure that the full qualified domain name is setup and displayed correctly.
The command above should be displaying the following for the example server.
It is does not the problem can be solved by editing the hosts file as fiollows.
Note that the FQDN is first in the list and the short name without the domain part is not.
This would be a good backup point! We are going to create a bunch of map files that tell Postfix how to get the information out of the MySQL database. They are fairly simple and I am sure you will work out how they do their mapping without explanation. In each file change
Installing MySQL we need the server and front end tools these will all be installed with the following command.
When the installation process starts you will be asked for a root password. This is the password for the root user of MySQL this is not the same as the root user on the Linux system
We will edit the system MySQL my.cnf file to check on access and get some logging to happen.
We want to bind to 127.0.0.1 not localhost. This a means there can be no connections from remote machines, which means that we have a security advantage in that only local users can access the MySQL database. It should already be set as it is the default.
I find it is helpful to see what the mysql DB is doing especially in the early stages. Use this option to start the logging of any calls that are made to MySQL. It is probably commented out.
Then in a few weeks comment those out again when everything is working, as the logging slows mysql down. Restart the MySQL daemon
To create a MySQL database is very simple from the command line. We will be calling our database 'postfix' as it is
for Postfix and I like to keep things simple.
We need to login as then user root and tell the command to ask for a password. We do that with the following command:
When you run the command above you will be asked for a password. It is the MySQL password for root, it is the one you entered when you installed MySQL server. There is no feedback in the way of text output to say the database was created. If you want to check you can login to the database using the mysql command and issue the show databases command.If all went well you will see a line with postfix.
The other way to monitor what is going on behind the scenes with MySQL is to watch the mysql log file. Open a new terminal and use the following command to follow the end of the log file as it is written.
We will be needing a user with restricted permissions to access the tables for lookups. This user will only need to SELECT data never INSERTing or UPDATEing if you are not intending to allow users to change their own passwords and aliases with SquirrelMail. If you are going to allow them to change their own password then read on a little further. From the command line we will open up a session as we did above.
and enter this SQL command when you see the mysql> prompt:
This will create a new user called mail who just has SELECT access to our database. To get these new permissions noticed by Mysql you need to flush them. So before you exit the mysql prompt use the following command.
If you are intending on using Squirrelmail as web front end to allow users to create, amend and delete their aliases or change their passwords. Then we need to give mail some extra privileges. We will do it now while we are here. Use the line below from the mysql> prompt and then flush the privileges.
We will be using three tables to interface between Postfix and MySQL. The way I create them it to add the SQL commands to a text file and then load them in via the mysql command. This saves a lot of typing. It also means that when you are testing it is easy to delete all the data and start again.
Create an blank file and add the text above to a file called create_postfix_mysql_tables.sql and save it. You can then load that file and issue the commands it contains with the following:
If you want to see that the tables have now been created use the following.
This would be a good backup point! Now we get to install the star of this HOWTO, postfix. we need the MySQL add-ons which we get from postfix-mysql. That will in turn install postfix.
While these packages are installing you will be asked some questions:
General type of mail configuration: Internet Site
System mail name: mailserver.example.com
The Postfix map files are how we tell Postfix to talk to the MySQL database. Each file contains data telling Postfix how it can connect and send a query to the database and therefore retrieve the data it requires. Once these map files are created we will add them to the Postfix configuration file /etc/postfix/main.cf using the command line tool postconf. The resulting change will automatically be written to the file and the postfix configuration will be reloaded.
How to find valid domains within our system. This table holds one row for each domain that our mailserver will be accepting mail for and that it will deliver to the end user. With an editor create the domain lookup file with the following details. You will need to change the password from
Add the line to /etc/postfix/main.cf from the command line.
As we are intending to use our mailserver to deliver emails to users including but not limited to those that also have Linux accounts. We need to use virtual mappings. This is so Postfix will know where to deliver the email to. That is the directory on the file system. We certainly do not want to create a user account and home directory for all our email users. We will define a top level directory /var/vmail where the email will be stored. The data held in the mappings in the virtual tables will be used to construct the bottom of the directory tree. The actual location of the Maildir subdirectory. For each user we will have:
Following the Postfix specification, we put a '/' at the end to tell Postfix it is a Maildir and not a mbox file.
This maps aliases to real email addresses. This means that one destination can have many email addresses. In a company all the emails for support@example.com and info@example.com will be delivered to fred@example.com. Harry is the IT administrator and therefore gets mail that is not addressed to anyone inparticular. Harry will get mail that cannot be delivered anywhere else. This is a catch all email alias. This can also be used as an effective anti-spam system. Whenever you deal with a new company simply create a new email alias from_
Assuming that all worked as before we can move on to that last mapping.
This mapping table is used as part of the virtual_alias_maps mapping and saves you having to map every user back to themselves. Why would you need to do that? You ask. See below it is to do with the way catch all addresses work.
As these lookup tables all contain passwords in open text they should not be world readable. We can do that by restricting the group and removing all world permissions.Aliases are the mechanism that allows mail to be delivered in a very flexible way with a very simple setup. That is the users get their mail and you don't have too much extra work. ![]()
To test those MySQL tables and the interface between Postfix and MySQL we will add some test data to the database. We can them test that this is working by adding some test data and using the postmap command to read it back.
I also put the SQL required to build up some test data into a file so it can be loaded again when necessary while testing is on going.
You can then rebuild your data base with test data with the following command.
We can now do some very simple tests to check the Postfix-MySQL interface it working. The fist test will lookup example.com to see if it is valid domain for our system. If it is it will return example.com. Also try this with a domain name that is not valid.
We can lookup a user to see if they are a real use. This test should return fred@example.com as he is valid.
This final test checks that aliases are working bert@example.com is an alias for fred@example.com.
This would be a good backup point! To get POP3 and IMAP features from your mail server we will need Dovecot, it is installed with the command below.
As the recipient, for the mail that will have a final destination here, does not require a Linux account some users will not have any home directory to store mails. It is therefore wise and less work to create a virtual user for all incoming mail.
To tell Postfix that Dovecot will be the mail delivery service we add it to the Postfix configuration file /etc/postfix/master.cf.
The second line must be indented or Postfix will think it is the first line for the next service.
(Note: the second line has to start with white space or it will not be taken as an extension of the line above)
At this point I start monitoring /var/log/mail.log in a separate terminal. It will help you when mistakes
are made
Run the following in a terminal window and leave it running. It will display the end of the log file and then follow it as it grows longer.
You should see the output from postfix as it reloads its config files in the window where you are tailing the file.
Also make Postfix use the Dovecot service for virtual delivery by adding these lines to your /etc/postfix/main.cf
Setting up Dovecot is performed by editing its two configuration files. We will not be editing dovecot.cont as Ubuntu has added a new file dovecot-postfix.conf. If the new file exists then it is used and dovecot.conf is not read in. We will not be using that as there are problems with it not creating the Maildir directory structure if it does not already exist. Since we are not usibng the new Ubuntu file we need to move it out of the way and use the main file /etc/dovecot/dovecot.conf.
Make a backup of the original file as this is a file where it is easy to make mistakes. I know I did, all those nested comments.Search for the protocols line and this is where we say which protocols you want to offer to our users. The default line what we will be using with Dovecot. It is already setup for using POP3 and IMAP as well as the secure alternatives over a SSL connection. The default file also has managesieve which can be left in if you are going to use that feature.
There appears to be a problem with some Microsoft mail clients and they need disable_plaintext_auth set to no.
It is less secure if this is done. Another joy of using that software :-( If none of your users are unfortunate to be limited to using that email client you can leave it commented.
While we do the initial testing we will require to enable plain text auth, so set the variable to no.
Next stop is the option mail_location. If you see the comments above this line the variables are explained. We are setting the MailDir to be under /var/vmail and then using the domain name and then the username as further subdirectories to store this users email. The directories are automatically generated as required hich is rather nice ![]()
The next setting we need to check is to change is in the section "auth default". You will see that the majority of it is commented out. The first option is to the authentication mechanisms we want to use. Change the line to read as below as necessary.
As we are using an SQL db to store the users data and their passwords we need to tell dovecot. This is done in the section that is call passwd sql, what a surprise.
Uncomment the section. This is where the second Dovecot configuration file it effectively turned on. Also uncomment the section userdb sql.
Remember when we created the vmail user above we forced the user ID and the group ID to be 5010 this is where we tell dovecot what they are and to point out where the mail boxes will be stored on disk.
We can also comment out the section called "passdb pam and the one for passdb passwd as these deal with system users. We are only going to be looking for users and passwords in the sql DB so this is not needed. If you leave them live you will see two lines for each in the log file one for the lookup and the other to say the user is unknown.
Searching a little further down you will find the section socket listen. This section defines how Dovecot will talk to Postfix for the authentication of the users.
The Master socket provides access to userdb information. It's typically used to give Dovecot's local delivery agent access to userdb so it can find mailbox locations. The client socket is generally safe to export to everyone. Typical use is to export it to your SMTP server so it can do SMTP AUTH lookups using it.
You should change the postmaster email address to a valid one. The address should probably be your email address or an alias to yours, as you are the Administrator. The auth_socket_path is the same path as the one you entered in "socket listen" in the "master" section. While we are testing if you add the log_path value, you can monitor the delivery of emails it will help find errors if they occur.
All done with that configuration file. Save and exit. We can now move on to setting up the SQL connection to Dovecot. These settings are in the file we specified above the section "passdb sql"
We only need to change a few settings from the generic values that are given in the original file. Set the drive to mysql as that is the database we are using. The default_pass_scheme list is a list of parameter to allow Dovecot to log into your SQL database. We will be encrypting the passwords that are held on the SQL database so we need to tell Dovecot here. The final value is so Dovecot can check the password against the user as they login.
We need to fix file system permissions for the /etc/dovecot/dovecot-sql.conf file as it contains the password for the mysql database. Since this file is only read by the app Dovecot while it is being run by root we can restrict these files like we did for the postfix map files. The dovecot.conf file on the other hand needs to be made readable by root and the user vmail. We do that by making the file owned by the group vmail and allowing group read permissions.
We can now restart the Dovecot daemon.
We can check everything went well and is working by looking at the mail.log file with the tail command.
Now it's time to send our first email
Are you sitting down this is a momentous occasion after all! We can use telnet to emulate an email client. We can connect and send our email as below. I have indented the text that is output from the mailserver to make this part a little clearer.
If all went well the email has been sent and you can see the log entries to prove it.
We sent the mail to Fred at example.com so there should now be an email sitting in the directory
/var/vmail/example.com/fred/Maildir. Go and have a look. Remember that the directory structure
is owned by the user vmail. I just open a root shell and use ls ![]()
This would be a good backup point! We can send mails now we need to allow reads to fetch their emails and then read them. We can test this with telnet again. We will test first with IMAP and then with POP3.
With an IMAP login after the initial connection (telnet in our case) you need to add a character or string at the beginning of each command this text will be returned in the status output from the IMAP server. The text can be anything. By convention it is usually a counter, but we will use test with an incrementing number at the end.
There you go you have sent and delivered your first email via your very own mail server. Time for a coffee to celebrate. Lets do the same thing again this time accessing the mail with POP3. It is a good way to test it is working as you can see each stage and monitor the logs at the same time.
We should do the same test again from another IP address that is another host to test we can login from machines other than localhost. You should see a similar set of output and log entries. The difference being you use "telnet mailserver imap" as the initial login string.
Again you need to repeat the process above but from a host other than mailserver to ensure that your users will be able to log in.
Although we can now send and read emails, I don't think your users would be too impressed if telnet was the only way to access their mail.
Just think of all the help calls from end users trying to access their email and getting the commands in the wrong order.
At this point we can set up Thunderbird or Evolution or any other email client. Using Evolution it is easy to create a new account that points at our new server. I'm sure you can work this out but here is a very quick and dirty description.
You will be asked for your password, Click on the Folder for "Fred POP3 no encryption" you may need to refresh it. Your first email will appear in the client. Set up a POP3 account also with no encryption as we have not turned that on yet. POP3 will actually download the email from the mailserver and store it with your client.
If All is working. Now would be a good time to make a full backup of your new server
We can send and receive emails now we need to make it a little bit more secure by using TLS and SSL. First of all we need to generate the self-signed certificates that will make this all possible.
The Common Name must be the FQDN for your mailserver. The days option sets the number of days that this certificate will be valid. I have used 1825 days or 5 years, give or take. I doubt very much I'll still be using the same hardware and Ubuntu, Postfix etc. would have moved on anyway. You will need to set the permissions on the private certificate so only root and the members of the group 'root' can read it. You should also restart Dovecot
We need to change the certificates that are being used by Dovecot as we now have our own. Re-edit the dovecot.conf file.
To generate the authentication string needed for the next test use the following Perl command line to Base64-encode your password.
You will need to change the fred@example.com text to the domain name of your test user. Also change
"yourpassword" will have to be what ever you chose as your password.
To use smtps you need to uncomment some lines in master.cf, Only uncomment the first two extension lines as shown below:
We can use the same certificates here as we did for Dovecot as long as we are on the same server.
Also add the ssl certificates to the postfix configuration file main.cf
It is the users choice to use SSL/TSL or not with the settings so far. Once that is all in place you can test it out with a real world test site. This will test your mail is configured correctly and is not an open-relay that spammers can use. The site connects to your mail server repeatedly with different parameters and takes about a minute to finish.
You should see a message
This would be a good backup point! We can now enable dovecot so it only accepts authenticated connections. We will no longer accept remote plain text authorizations unless TLS/SSL is being used. Edit the dovecot.conf file and and comment out the disable_plain-text_auth line.
When you try to connect to mailserver with either POP3 or IMAP you should now see an error message saying,
In the account settings for your mail client change the encryption to SSL for POP and IMAP connections. You should now be able to connect to mailserver and see your test mails again.
We can now send and receive emails over encrypted connections (SSL/TLS). The SMTP connection will only allow authorized users to use our server to relay messages.
Pretty damn good, don't you think.![]()
This would be a good backup point! It is a necessary evil that we should scan all incoming and outgoing email for viruses and all incoming email for possible spam content. We do that with a service called Amavis this plugs into Postfix and accepts mail before it is delivered.
Add some extra tools for extracting files from archives and compressed files.
The extra compression modules are not normally installed by default as they are supplied on a less then free GPL license. The choice is yours whether you install them or not. If you do add them then you will need to turn them on by editing the file 01-debian.
Find the unrar and lha lines and uncomment them. When you restart Amavis look in the mail log to see if there are any other missing modules.
if the output you get from running the following command is the fully qualified domain name for your server. You can skip the edit in 05-node.
As you are reading this part you need to update the $myhostname line to be the FQDN for your server this is used for ESMTP EHLO, and loop detection. You can also comment out the line that starts "chomp" as it is no longer needed as you have hard coded the value it finds.
Spamassassin is already configured for us we just need to turn in on by uncomment the bypass_spam line in 15-content_filter_mode.
There are some other changes we need to make these should go into the file 50-user. Any changes you make there will override those of the earlier files. (with a smaller number at the start of the file name).
I like to see that the spam detector is working so I get it to update the subject line for any emails it the scanner believes are spam. This is purely cosmetic, it simply changes the string that is added to the subject line emails it believes are spam. Since I already have a rule in my email client looking for the string "[**SPAM**] that is what I change it to.
The variable $final_spam_destiny is used to determine the outcome of finding an email that is believed to be spam. Since one man's spam is another man's valuable message. e shall let the user decide, It will be marked as suspected spam but allow it to be delivered.
The last variable for lookup_sql_dsn, is required because AMaViS tries to find out whether an email is incoming (sent from the internet to your domains) or outgoing (sent from your system to the internet) by looking at the @acl_local_domains setting. You need to tell AMaVis where to check if a certain domain is one of your destination domains. The reason is that you usually don't want to scan your outgoing emails. Imagine that an email is accidentally deemed to be spam and your customer gets warned of your emails.
While debugging you can also set the following variable so that the spam header flags are always added to the delivered mail.
And the now usual change of ownership as this file now contains our Mysql password in open text.
Restart amavis, take a look at the log file to check for any missuing compression modules.
Now setup the interface configuration for Postfix to talk to Amavis
And the follow long set of lines need to go into the master.cf file.
Do not forget to reload the postfix files now you have updated them.
To turn on Spamassassin you need to edit the spamassassin file in /etc/default.
Now change the two lines for ENABLE and CRON to a number larger than zero (0). Save the changes.
we also need to change the line for CRON to a number larger than zero (0). Save the changes and restart the Spamassassin service.
The application that updates the rules for Spamassassin is called "sa-update" and is normally run via a daily crontab job when the value in the file above is set. To update any existing rules now we can run it now. As it will not restart "spamd" or otherwise cause a scanner to reload the now-updated ruleset automatically. We need to run the update and then restart the server.
************************************************************
************************************************************
************************************************************
FIXME:run sa_update with thw -d option to see a list or possibly required modules.
************************************************************
************************************************************
************************************************************
You can test that Spamassassin is working by sending an email containing a know spam signature. One such signature was installed already in the following file.
Copy/Paste the contents to an email and send it to yourself. If should be flagged as spam and you should also see some messages in the mail.log.
Clamav is already configured in the file "15-av_scanners, but to call any scanner you need to uncommented the bypass_spam line in 15-content_filter_mode.
So that the user that is going to be running clamav can "talk" to the amavis service we need to add it to the amavis group.
Make sure the clanav databases are up to date.
As your server will be connected 24/7 to the internet use the daemon option for updating to clamav. If you use the daemon update method select
sudo dpkg-reconfigure clamav-base
There are a number of questions to answer here, The options below are the ones that I changed or needed a mention. The rest I used the defaults
Now that keeps the virus-DB upto date but not the engine. To keep the engine up to date is some what harder. I used the following to get a more up to date version of clamav. This worked for a while and then I received a broken package and had to go digging into the use of aptitude. https://help.ubuntu.com/community/ClamAV
Testing that Calmav is working is simple once you get a virus you can email around. Don't panic the test virus will not do anything, harmful in fact it won't do anything at all. Go to the following link and have a read
http://www.eicar.org/anti_virus_test_file.htm
Copy the string 68 characters and save them to a file. Now attach the file to an email and send it. You SHOULD see some lines in the mail.log that indicate
that the file was infected. Also try out the same text file but packed in an archive, tar, zip or compressed file .gzip, tgz zip.
Download the latest binaries from AVG http://free.avg.com/us-en/download
On 64-bit systems you will need to install the 32 bit compatibility libs and to use the '--force-architecture' switch.
The above installs the software into /opt/avg/avg8
AVG Free is already configured in the file "15-av_scanners, but to call any scanner you need to uncommented the bypass_spam line in 15-content_filter_mode.
In the above file you can squeeze a few extra milliseconds of speed by commenting out all the scanners you will not be using.
The people that use my mailserver are, actually sensible. There machines are also not on the same network as mine. 7| Therefore I do not quarantine mails that are marked as infected with a virus. They have their subject's updated to clearly show the mail is infected. This maens I also PASS infected mail on to their mail box for them to deal with.
Set the following parameters for communication via INET socket:
Make the script executable by root so the daily cron can run it.
We can do the first update now so we can start with an upto date AV system.
Testing that AVG Free is working is simple once you get a virus you can email around. Don't panic the test virus will not do anything harmful in fact it won't do anything at all. Go to the following link and have a read
http://www.eicar.org/anti_virus_test_file.htm
Copy the string 68 characters and save them to a file. Now attach the file to an email and send it. You SHOULD see some lines in the mail.log that indicate
that the file was infected. Also try out the same text file but packed in an archive or compressed file such as a tar or tar gzip file.
There are a number of ways to stop spam from reaching your inbox, we have already setup Amavis and Spamassassin to do some spam filtering. Grey-listing is not designed to replace this but to work with it. By providing a first hurdle for spam to get past before hitting your inbox. It is yet another tool in your arsenal against the endless barrage of pointless emails we all receive. Grey-listing is very simple and requires very little CPU or processing time. It simply looks at the senders name and if they are not known already, it politely says to them "Please try again later". For any well setup mail server this is not a problem and the mail will be redelivered later as requested. But for spammers that want to get as many emails out there as possible, they never bother to "try again later". ![]()
To install Postgrey is just a apt-get away with:
We need to tell postfix how to talk to postgrey. Add the following lines to your /etc/postfix/main.cf Postgey defaults to port 10023.
You can adjust some other settings. These are available in /etc/default/postgrey. There are also some white lists you can add to if you feel lucky.
This would be a good backup point! You should at this point be able to see a web page displayed that says "It works!" if you use the URL http://www.example.com Give it a try, it you do not see the test page from the Apache install then you'll need to sort that out before continuing.
When you stop and start your Apache server you may see an error message like the one below. Lets start by getting that sorted out.
Apache cannot work out the domain name of your server machnice or perhaps you simply do not have a domain name setup. Not to worry it is simple to fix. Edit /etc/apache2/apache2.conf add the ServerName option. Use the output from the Linux command hostname as the value. I add the line after ServerRoot. Then restart the Apache server and you are done. We should see the following forthe example server.
Add these lines to /etc/apache2/apache2.conf
Test that all is still well with the Apache configuration files with the following command. This is usful as if there are errors the server is left up and running in the pre-edit state. We can then use reload it take the changes into use without shutting down the server and bringing back up again. On a live server this is less likely to be noticed that the restart.
You now have Apache serving pages locally on your LAN, but you also want to be able to access the same Web site via the Internet. With Apache it is simple, once you know how. We will setup Apache to serve web pages for a number of Domains by using the virtual hosts feature.
We will be using two different ports to access our web pages 80 the standard http port and the https port 443. We need to tell Apache these will be setup so add the lines below to /etc/apache2/apache2.conf anywhere before line towards the bottom that includes the files in sites-available. I tend to place then just after the ServerRoot and ServerName lines.
By using the'*' wild card we do not differentiate between different IP addresses we will only use the URL to guide us to the right virtual host.
We will create the web site and index.html page that will be the fall back when there are no rules to use any other site. We will place it in /var/www/default for want of a better place. Start by changing the default web site config file.
Create the index page for the default site, Nothing to fancy at this point ![]()
We need to comment out one line in the following file it will remove the warning about unconfigured virtual host. Place a # at the start of the line for NameVirtualHost *:80.
Check the files are ok. Also from now
on I will only mention to reload the Apache files and miss off the part to check in for errors. I assume you will do that bit yourself. It's been a lot of typing so far.
You should now be able you use the URL for mailserver, mailserver.example.com and example.com and even example.net and they all go to the default web page from above.
Let us create the top level virtual host for example.com. This is similar to the default we created above, but hard codes a server name in the virtual host definition.
Create the index page for example.com site. No need to go too mad on this temporary marker page.
We will need to tell the Apache server that this new virtual site exists so we enable it. The enable command makes a symbolic link in sites-enabled back to sites-available.
Now test the syntax and reload the files when there are no errors.
Now you will see that example.com goes to the new virtual host whereas mailserver.example.com goes to the default host. You may have noticed the ServerName we used for this site was www.example.com and the we had aServerAlias of example.com. This means you can use either of those URLs to find this web site.
One more little tweak to the example.com virtual host configuration file. We can get Apache to rewrite the URL so users will always see http://example.com even if the original URL was www.example.com.
We need to add three lines which will see if the URL matches and then rewrite it to something else as necessary. We will use this trick later on too for the HTTPS connection with Squirrelmail.
Before that can work we need to enable the rewrite module.
Do not forget to reload the changes, before tying this new feature out.
If you have more domains linking to this one Apache server you can set those you by repeating the process above that we used for example.com. All that is left is to add the Web site contents in the normal way starting at the temporary index.html.
This would be a good backup point! When I install Squirrelmail I remove the American English dictionaries and replace then with er, um the English dictionaries, "Like what I speak"
. You can do the same but install the dictionaries for your own language are required. These should automatically be configured by ispell. But if you use Firefox you already have access to the great feature of auto-spell checking as you type.
You need make a link from SquirrelMail configuration to the Apache site-available directory.
The default supplied setup does not provide a secure connection between the client and the server. Communication between the web browser and the mail server will be in plain text, which is not good. We can run the connection over SSL (HTTPS) by enabling the ssl module and using the same self-signed certificates we are using for Dovecot. You can always generate another set if you want to. Also we can change the default URL to access the web mail server from one like these
It is much better to provide our users with something that is easier to remember. The Apache setup for this web service is already designed to run as virtual hosts and that allows us to access Squirrelmail with a URL such as
We can also use the Apache mod_rewrite module to rewrite the URL we use to access Squirrelmail so that it will always access the web interface through HTTPS. First we need to enable the Apache modules for ssl and rewrite.
Now to make some changes to the supplied Squirrelmail config file for Apache. There are a lot of changes so here is one that is ready to use. Change the domain name to suite your environment.
As we will be using a virtual host for webmail via HTTPS and the SSL keys we have are not for webmail.example.com the Apache server will warn us that the host name and the Common name (CN) are not the same. If we generate another pair of self-signed certificates with the corret CN. This warning is removed.
You can of course use the same certificates as we used for dovecot and put up with the warnings.
Update the permissions and the owner of the private file as it will be read by the Apache server.
Now to enable your new Apache configuration for Sqiurellmail and then reload the Apache configuration.
With that all in place you should be able to access your mailserver via Squirrelmail with the test user, fred@example.com, and his password. The user will not have to remember to use HTTPS:// as we have told Apache to rewrite the URL to force the secure access. It is one less thing for the users to do so one less thing to get wrong ![]()
Load up the URL https://webmail.example.com/src/configtest.php in your web browser this will run the Squirrelmail test script and show any errors it finds there. You will most likely see an error message like the one below.
To turn these off edit the php.ini from found in /etc/php5/apache2 and search for "magic_quotes_gpc" and set it to off. This was the only offending setting that was turned on.
Make the changes and then restart the Apcahe server, yes you must restart it reloading will not bring the changes into action.
Now to configure Squirrelmail a little bit more. In a terminal start the configuration script for squirrel mail. Do not change anything to start with just have look around to get the idea of what is there. ![]()
Ok, you are back from looking at the Squirrelmail configuration.
Start with the D option for "D. Set pre-defined settings for specific IMAP Servers" and
set it to "dovecot" as that is what we are using.
Return to the main menu and select "2. Server Settings." Then using the 'A' and 'B' options to setup IMAP and SMTP settings. The settings below are the ones I am using.
We are also going to use the SQL DB as a backend to hold the address and preferences data. From the main menu select '9 databases' it is options 1 and 3 you should need to change. When the options are set it should look similar to this
Remember to save the settings before exiting and reload the Apache settings.
The Pear plugins are required to access the MySQL DB with Php. Checking pear is loaded and also that the DB access module and also the newer MDB2 modules are loaded. We do need both if you are going to be adding the plugin below. Start by making sure that the Pear libraries are installed.You get install this extra software with the following the commands below. Use the pear list command to see which packages are installed you need to to have:
Load up the URL https://webmail.example.com/src/configtest.php again in your web browser to check all is working as expected.
From another machine test that you can send and receive emails with Squirrelmail. If the tests you did with Evolution or thunderbird worked this should be a simple case of send your test user an email from within Squirrelmail. Waiting a second and then seeing if it have arrived by clicking the "Check mail" link in Squirrelmail.
This would be a good backup point! There are two plugins I like to use with Squirrelmail.
Download change_sqlpass (not change_mysqlpass) from the Squirrelmail web site. Also get the latest "Compatibility plugin". (This plugin requires the DB module from above). You will need that too!
Unzip the compatibility plugin somewhere safe, like /tmp so you can read the README and INSTALL instructions. When I installed the compatibility plugin, I simply had to
Now for change_sqlpass, remove the software you unzipped into /tmp and unzip the change_sqlpass tar/zip file. Again read the README and INSTALL file. Follow the instructions. This is what I had to do.
There are some small changes that needed to be made at this point. The first change sets the login parameters for the mysql DB. in the case below they are.
Save the changes you made and as this file has a password it should not be world readable so we change the group and permissions to suit.
Now we can run the squirrelmail config program and get the plugin taken into use.
Then navigate you way to the plugin selection screen. '8 Plugins'. Find change_pass on the list of available plugins and enter the number. It should change to the top of the screen under Installed plugins. Do not forget to save the changes before leaving the script. Load up Squirrelmail goto the options link and follow the link to change password.
Remove the software you unzipped into /tmp.
(This plugin requires the MDB2 modules from above). Download the mail_alias tar file from the Squirrelmail web site and unzip into /tmp. Read the README and INSTALL file. Follow the instructions. This is what I did.
The lines I changed in the config.php are these
Save the changes you made and as this file has a password it should not be world readable
so we change the group and permissions to suit.
Now we need to do some major surgery on the file functions.php. This is the script that reads and writes the new aliases. Search for the line that sets $sql_data, we need to remove the last parameter as shown below
Find the function heading add_db_alias_for_user scroll down to the select_search1 part and comment out the parts shown below.
Now do the same thing again in this function.
Lastly replace the function validate_email with this code. This is necessary as the function eregi has been deprieciated.
Save the changes you made and then run the configure script to add the plugin to SquirrelMail.
Then navigate you way to the plugin selection screen. '8 Plugins'. Find mail_alias on the list
of available plugins and enter the number. It should change to the top of the sceen under
Installed plugins. Load up Squirrelmail goto the options link and follow the link to Mail Alias.
Remove the software you unzipped into /tmp.
This would be a good backup point! You have backed up your mailserver haven't you? This is where things can go wrong and panic really sets in.
We will be opening ports to allow access to mailserver and using port numbers. These port numbers come from /etc/services.
It is recommended that by default you deny all access to the server and then add rules to open ports and allow IP addresses through your firewall. If you are accessing your mailserver via an ssh connection, like I do then we need to make sure that connection stays open.
We will therefore start the setup with a rule so we can access mailserver via ssh from one specific IP address and then enable UFW. This will stop all other traffic until we open the ports. I setup mailserver as a virtual machine on an Ubuntu box. The host system for the examples used in this part will have an IP address of say 192.168.0.22. Remember that the local LAN IP address for mailserver was 192.168.0.11.
If you try and connect from a machine other then 192.168.0.22 your connection request will timeout. This is good and shows we have climbed the first step
Try accessing the test mail account from Evolution from a different machine. it will time out.
We need to allow the outside world access to the ports for POP3, IMAP, SMTP and web access and also the secure alternative to each. If we just add a rule to open those ports we are still open to brute force attacks. Where the hacker scum
sends thousands of login attempts hoping one will allow entry. We can add a limiting option to make our firewall more secure. For instance what happens when (yes I did mean when not if) someone tries to hack your server. We can limit the number of login attempts from an individual IP address with one rule. Once enabled anyone trying to access your server six or more times in the last 30 seconds will be banned for one minute. All the connection attempts within that minute will be simply dropped.
The backup software I use is called BackupPC and it uses rsync to connect to the servers and workstations. Rsync sites on port 873. So I open that up to the local LAN.
You may have a few other services that need to connect from a specific machine (IP addreess) or you may want to open all ports from the local LAN. This is a security issue.
If you later see that IP 58.151.230.42 is trying to hack your server with repeated logins. It would be nice to deny all access to that IP address. But on problem with IPTABLES is that once access is granted with a rule it will not be removed again. for example we all ready granted access for SMPTS connections. To add the new deny rule for the scum hacker
at 58.151.230.42. We need to addthe rule at the top of the list. Upto now we have just added them at the bottom. We can see the line number forthe existing rules witht he following command
We can then use the number to add a new rule. We want to add our rule to deny all access to the hacker as our new rule two. We can use the following command.
We then end up with the following output, note the new rule on line two.
| ssh | 22 |
| SMTP | 25 |
| www | 80 |
| POP3 | 110 |
| IMAP | 143 |
| HTTPS | 443 |
| SMTPs | 465 |
| IMAPs | 993 |
| POP3s | 995 |
| MySQL | 3306 |
Turn off the extra logging
Something to think about is that these log files can grow to a large size rather quickly. If you add them to the logrotate configuration, they can be kept under some control.
/etc/mysql/my.cnf
$HOME/create_postfix_mysql_tables.sql
/etc/postfix/mysql-virtual-mailbox-domains.cf
/etc/postfix/mysql-virtual-mailbox-maps.cf
/etc/postfix/mysql-virtual-alias-maps.cf
/etc/postfix/mysql-virtual-alias-maps-self.cf
/etc/postfix/main.cf
/etc/postfix/master.cf
/etc/dovecot/dovecot.conf
/etc/dovecot/dovecot-sql.conf
/etc/amavis/conf.d/01-debian
/etc/amavis/conf.d/05-node_id
/etc/amavis/conf.d/15-av_scanners
/etc/amavis/conf.d/15-content_filter_mode
/etc/amavis/conf.d/50-user
/etc/default/spamassassin
/etc/cron.daily/spamassassin
/etc/cron.daily/avgfree
/etc/postgrey/whitelist_clients
/etc/postgrey/whitelist_recipients
/etc/apache2/apache2.conf
/etc/apache2/ports.conf
/etc/apache2/sites-available/default
/etc/apache2/sites-available/example.com
/etc/apache2/sites-available/squirrelmail
/etc/php5/apache2/php.ini
/usr/share/squirrelmail/plugins/change_sqlpass/config.php
/usr/share/squirrelmail/plugins/mail_alias/config.php
/usr/share/squirrelmail/plugins/mail_alias/functions.php
For debugging the smpt service add a -v ti the smpt line in master.cf
If you get problem with authentication, try setting these in /etc/dovecot/dovecot.conf and then restart Dovecot
enable auth_debug setting. See http://wiki.dovecot.org/WhyDoesItNotWork