Installing Apache, MySQL Postfix and Dovecot on CentOS 6.x

CentOS Webserver 6.x

First off folks, this is entry level tutorial for users who are new to CentOS or are Migrating from Ubuntu.

Installing a webserver on CentOS is very easy and require very little experience.

The article assumes you have CentOS installed and that your system is connected to the internet.
It also assumes you have root access through SSH.

Each install comprises Three Steps
1. Installation
2. Activation (so it is on if you reboot your server)
3. and Starting, Stoping or Restarting the Service

This tutorial uses the Yum Archive Manager

Installing Apache

Installing Apache

Apache is the #1 Webserver software on the internet today, it is fully customizable and comes roaring out or the box, ready to use.

To install Apache, log on to your server using root.

First, update Yum

yum update

Then check that yum has finished all the installation procedures for previous installs

yum -y update

Apache Server

Apache HTTP Server is an open-source HTTP server for modern operating systems including UNIX and Windows NT. For more information on Apache HTTP Server, you can visit their website at

This documentation was written for Apache HTTP Server 2.2.3 but may be usefull with other versions.
How to install Apache Server on CentOS, RedHat, Linux

Install Apache HTTP Server
yum install httpd
Note: This is typically installed with CentOS by default

How to configure Apache Server on CentOS, RedHat, Linux

Set the apache service to start on boot
chkconfig --levels 235 httpd on
Enable name-based virtual hosting on port 80
Open the httpd configuration file located at /etc/httpd/conf/httpd.conf
Un-comment the line containing the text NameVirtualHost *:80
Save the file
Restart the Apache HTTP Server daemon
service httpd restart
Note: Ignore the "NameVirtualHost *:80 has no VirtualHosts" warning for now.

How to test Apache Server on CentOS, RedHat, Linux

Visit localhost in your web browser. You should see an Apache Test Page.

Installing Mysql


MySQL is a Relational Database Management System (RDBMS) that runs as a server providing multi-user access to a number of databases. For more information on MySQL, you can visit their website at

This documentation was written for MySQL 5.0.45 but may be usefull with other versions.
How to install MySQL

Install MySQL
yum install mysql-server mysql php-mysql

How to configure MySQL

Set the MySQL service to start on boot
chkconfig --levels 235 mysqld on
Start the MySQL service
service mysqld start
Log into MySQL
mysql -u root
Set the root user password for all local domains
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new-password');
SET PASSWORD FOR 'root'@'localhost.localdomain' = PASSWORD('new-password');
SET PASSWORD FOR 'root'@'' = PASSWORD('new-password');
Drop the Any user
DROP USER ''@'localhost';
DROP USER ''@'localhost.localdomain';
Exit MySQL

Install PHP

How to install PHP

Run the yum install command
yum install php

How to configure PHP

Increase PHP script memory limit
In the /etc/php.ini file replace memory_limit = 16M with memory_limit = 128M
Increase PHP script max execution time
In the /etc/php.ini file replace max_execution_time = 30 with max_execution_time = 120
Increase PHP script max upload size
In the /etc/php.ini file replace max_upload_size = 2M with max_upload_size = 50M
In the /etc/php.ini file replace post_max_size = 8M with post_max_size = 50M
Create the /usr/share/phpinfo folder
mkdir /usr/share/phpinfo
Create the /usr/share/phpinfo/index.php file with the following text:
Change permissions on the index.php file
chmod 0755 /usr/share/phpinfo/index.php
Create the /etc/httpd/conf.d/phpinfo.conf file with the following text:
# phpinfo - PHP utility function for displaying php configuration
# Allows only localhost by default

Alias /phpinfo /usr/share/phpinfo

order deny,allow
deny from all
allow from

Restart Apache Server
service httpd restart


Visit localhost/phpinfo in your web browser. You should see the infamous phpinfo output. If the address bar changes to somthing other than http://localhost/phpinfo/, than somthing went wrong while following the instructions and your browser has interpreted this as a search. Try following the instruction again. If it still doesn't work, send me an email via my contact page and let me know somthing isn't working right. If it did work, you have successfully installed PHP.

Install Postfix

PostFix/Dovecot - This need more configuration

yum install postfix dovecot system-switch-mail system-switch-mail-gnome

Yum should automatically resolve any dependencies. Dovecot is dependent on mysql and perl, so these will likely be installed too if they are not already installed on the system.

Alternatively, we can skip 'system-switch-mail' and 'system-switch-mail-gnome' installation and remove the default MTA 'sendmail', this will make 'postfix' the default MTA in our system.

yum install postfix dovecot
yum remove sendmail

Please note that the default MTA in CentOS 5 is sendmail. If you do not make postfix the default MTA, updating postfix may cause the default MTA to revert to sendmail.

3. Configuration

Next we need to configure the various parts of our email server.

3.1. Postfix

Postfix configuration files are stored in /etc/postfix. The two main postfix configuration files are and, although we will only be dealing with here. First we are going to make some additions or changes to the configuration file. The following lines should be added, edited or uncommented:

myhostname =
mydomain =
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks =,
relay_domains =
home_mailbox = Maildir/

Note: Each line should start at the beginning of a new line and should not be preceded by white space or tabs. White space or tabs at the beginning of a line are treated as a continuation of the previous line, and if the previous line is a comment (#) line then the subsequent line is also treated as such. Further, inline comments should be avoided.

Now lets take a look at each setting it turn to understand what we've just done:

myhostname: is the host name of the system (i.e, the system is called mail or

mydomain: is the domain name for the email server (it can be a real or fake domain name).

myorigin: is the domain name that locally-posted email appears to have come from and is delivered to.

inet_interfaces: sets the network interfaces that Postfix can receive mail on. These need to include at least localhost and the local domain.

mydestination: is the list of domains that will be delivered to (i.e, this server is the final destination for email addressed to these domains).

mynetworks: is a list of trusted IP addresses that may send or relay mail through the server. Users attempting to send email through the server originating from IP addresses not listed here will be rejected.

relay_domains: is a list of destination domains this system will relay mail to. By setting it to be blank we ensure that our mail server isn't acting as an open relay for untrusted networks. The reader is advised to test that their system isn't acting as an open relay here:

home_mailbox: sets the path of the mailbox relative to the users home directory and also specifies the style of mailbox to be used. Postfix supports both Maildir and mbox formats and readers are encouraged to read up on the merits of each for themselves. However, in this article we have chosen to use Maildir format (a trailing slash indicates Maildir format. To specify mbox format, the reader would use home_mailbox = Mailbox).

3.2. Dovecot

The dovecot configuration file is located at /etc/dovecot.conf. The following lines should be added, edited or uncommented:

protocols = imap imaps pop3 pop3s
mail_location = maildir:~/Maildir
pop3_uidl_format = %08Xu%08Xv
# Required on x86_64 kernels
login_process_size = 64

Again, looking at each option:

protocols: specifies the protocols available to users to access their email. Dovecot supports imap(s) and pop3(s), and any or all may be used.

mail_location: specifies the format and location of each users mailbox. Here we see we are using maildir format and each user has their mailbox located at ~/Maildir. Examples for mbox format are provided in the configuration file.

pop3_uidl_format: is required to fix a problem with Outlook 2003 accessing mailboxes via pop3 so it makes sense to set this (see the notes in the configuration file for more details).

login_process_size: The release notes for CentOS 5.1 state that "the Dovecot package on x86_64 kernels requires the parameter "login_process_size = 64" to be added to /etc/dovecot.conf after an upgrade to CentOS 5.1". 32-Bit installations are unaffected and do not require this setting.

Note: If you have any issues connecting with either imap or pop3 to dovecot, check the IMAP specific settings and POP3 specific settings sections of the dovecot.conf configuration file for workarounds. The available options mostly affect older mail clients and workarounds for Microsoft Outlook and Outlook Express.

Note on dovecot and C6: With CentOS 6 the configuration moved to /etc/dovecot/dovecot.conf. Dovecot can be started without any further changes to the configuration file and will automatically listen for connections on the ports for pop3(s) and imap(s). Changes to suite your environment will probably be required.
3.3. Create users mailboxes

Next we need to create a mailbox for each user in their home directory and set the appropriate permission, so using our example user john:

mkdir /home/john/Maildir
chown john:john /home/john/Maildir
chmod -R 700 /home/john/Maildir

Note: If creating the user mailboxes as root then we must set ownership of the directory to the user.

3.4. Aliases

We are nearly finished. We have an email account set up for our user John Smith who logs in as john. His email address would be . However, John may like to receive email as (or any other alias). We can achieve this by setting an alias for John using the system alias file (by default postfix uses /etc/aliases). We can also add aliases for other users, so for example we could also redirect root's email to John by adding the following to /etc/aliases:

# Person who should get root's mail
root: john
# User aliases
jsmith: john
j.smith: john

If you edit the aliases file to set up new aliases for users once postfix is running, you must rebuild the aliases database by running the newaliases command.

4. Starting the server

We are now ready to fire up our new email server. First we need to tell our system to use postfix as the MTA rather than the default sendmail. To do this, run the system-switch-mail command and select postfix as the MTA. This will install the postfix service and set it to start automatically at runlevels 3, 4, and 5. Next we need to set the dovecot service to also automatically start at runlevels 3, 4, and 5, and start both services:

chkconfig --level 345 dovecot on
/etc/init.d/dovecot start
/etc/init.d/postfix start

at which point you should be up and running. Your email server should have no trouble sending and receiving email internally and sending external email. To receive external email on your domain, you will also need to configure MX records in DNS for your domain (ideally a PTR rDNS entry should also be configured through your ISP mapping your IP address to your domain). Don't forget to open any required ports on your Linux firewall depending what services you are running (SMTP 25; POP3 110; IMAP 143; IMAPS 993; POP3S 995) and enable port forwarding for those ports on any routers.

If you make any changes to the postfix configuration file, you can either restart the postfix service or run the postfix reload command to update the changes.

5. Summary

Postfix is an extremely powerful and versatile mail transport agent. In this article we have seen how to implement a basic email server using postfix and dovecot for a single domain based on system user accounts. We have barely scratched the surface of the true capabilities of a postfix-based system, but hopefully have provided a solid working foundation on which new users can build.

Readers are now encouraged to read the complimentary postfix restrictions guide.