(ve):Setting up LAMP and email on Ubuntu 9.1

  • This page was last modified on March 8, 2011, at 11:46.
The (mt) Community Wiki is a collaborative project. Any (mt) Media Temple customer or employee may contribute. Not all articles and/or content have been tested for accuracy by (mt) Media Temple.

For officially moderated and tested articles, be sure to visit our KnowledgeBase.

From (mt) Community Wiki

Setting up your new (ve) Server with Ubuntu 9.1 Karmic:

Before starting you'll want to make sure that everything is up to date. We'll need to make sure that aptitude is up to date so run the following:

sudo apt-get update

This will update the sources to see what is new

Next you will want to upgrade aptitude, which will download and install new packages

sudo apt-get upgrade

Once this is complete, should only take a few seconds, you are ready to start configuring your server.

MySQL:

aptitude install mysql-server mysql-client

You will be pompted with "Do you want to continue? [Y/n/?]"

hit "y" to continue

Next you will be prompted to enter a root password for your MySQL server. This is optional but HIGHLY recommended, as leaving this blank leaves it with no password. Once you have entered a password you will need to confirm the new password.

Next we will install Apache, a popular HTTP server.

aptitude install apache2

Apache paths:

document root: /var/www

configuration file: /etc/apache2/apache2.conf

Apache Modules: /etc/apache2/mods-enabled

Virtual hosts: /etc/apache2/sites-enabled (and) /etc/apache2/conf.d

PHP5

Installing PHP5 is similar to the previous steps:

aptitude install php5 libapache2-mod-php5

After you install PHP5 you will need to restart Apache:

/etc/init.d/apache2 restart

Now that PHP5 and Apache are running it is time to test the installation. A good way to do this is to create a phpinfo file in the document root and pull it up in a browser.

vi /var/www/phpinfo.php

This will create and open the file allowing you to insert data to the file. Press "i" to insert data and input the following code:

<?php
phpinfo();
?>

Press "ESC" to get out of insert mode and press ":" and type "wq" to save your changes.

Now visit your phpinfo file via a browser by pulling up your IP address /phpinfo.php

You should see something like this:

File:File phpinfo.png

This will show you that PHP5 is working correctly and as you can see on the Server API line Apache 2 is functioning correctly as well. On this page you can also find all the modules that are currently enabled in PHP5.

You may have noticed that MySQL is not listed here, which brings us to our next step:

Enable MySQL Support in PHP5:

aptitude install php5-mysql

This will install MySQL support for PHP5. At this point it may be a good idea to install any other modules that you may need for PHP5, to see a list of modules run:

aptitude search php5

You can install any modules you like using the following format:

aptitude install php5-[module]

Once you have installed all the modules that you need you will have to restart Apache for them to take effect:

/etc/init.d/apache2 restart

You can no re-visit your phpinfo.php page to confirm that all your modules are enabled.

phpMyAdmin

phpMyAdmin provide a GUI for you to manage your MySQL databases. This can make database management easier and it is a good idea to install it.

aptitude install phpmyadmin

You'll receive a prompt, select Yes

You'll be prompted to enter passwords, please enter and confirm them as prompted. Once completed you'll see another prompt. Select "apache2"

Once the install is complete you will need to symlink the default phpMyAdmin directoy to the default Apache directory.

cd /var/www
ln -s /usr/share/phpmyadmin

Now to set up email:

aptitude install dovecot-postfix

You will receive a prompt.

Chose "Internet SIte"

On the next prompt input your system mail name: yourdomain.com (this is an example domain, please replace with your actual domain.)

The install should complete. This process automatically installs and configures the server accordingly.

By default dovecot-postfix is set up to manage email addresses based on system users. The following steps will help you change this to MySQL so that you do not have to create a new system user ever time you add an email address

aptitude install postfix-mysql

This will install the Postfix MySQL compatibility. Next we will need to load up MySQL

mysql -u root -p

once logged in run the following (remember that ";" is the end of the command)

CREATE DATABASE postfix;
CREATE USER 'postfix'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON postfix.* to 'postfix'@'localhost';

exit

It is important to note that you need to replace 'password' with your own password, otherwise 'password' will be your password.

Postfix Admin:

Postfix admin is a web based control panel for Postfix. It will allow you to create and managed email addresses as well as set auto-responders etc.

cd /var/www

Now download the postfixadmin files:

sudo wget http://downloads.sourceforge.net/sourceforge/postfixadmin/postfixadmin_2.3rc7.tar.gz

Now extract the contents:

sudo tar -zxvf postfixadmin_2.3rc7.tar.gz

You may want to rename the directory something more manageable and it is advisable that you remove the tarball:

sudo mv postfixadmin-2.3rc7 postfixadmin
sudo rm postfixadmin_2.3rc7.tar.gz

Now you will need to update the postfixadmin configuration file with your settings:

cd posftixadmin
vi config.inc.php

Edit these variables as follows pres "I" to enter insert mode:

$CONF['configured'] = true;
$CONF['postfix_admin_url'] = $_SERVER['HTTP_HOST'].'/postfixadmin';
$CONF['database_password'] = 'yourdbpasswdhere';
$CONF['admin_email']

Hit ESCAPE and press ":" then type wq to save your changes

Now load your server IP /postfixadmin/setup.php in a browser

You will see a warning about MagicQuotes, ignore this and scroll to the bottom. Here you will need to enter your setup password. You will need to copy the hashed password it provides you and replace "changme" in the following line with your config.inc.php with the hashed password:

$CONF['setup_password'] = 'changeme'

Now you will need to visit the setup.php page once more (your server IP/postfixadmin/setup.php) and create your super admin user. Scroll to the bottom and fill out the required information. This user will be used to log into Postfixadmin and manage your email service.


Now we will need to create 4 files containing SQL queries so that Postfix will have the information it needs to deliver to active addresses:

cd /etc/postfix
vi my_alias_maps.cf

user = postfix
password = yourdbpasswd
hosts = localhost
dbname = postfix
query = SELECT goto FROM alias WHERE address = '%s' AND active = 1

Save and close (ESCAPE, ":" and wq)

vi my_domains_maps.cf

user = postfix
password = yourdbpasswd
hosts = localhost
dbname = postfix
query = SELECT domain FROM domain WHERE domain = '%s' AND backupmx = 0 AND active = 1

Save and Close (ESCAPE, ":" and wq)

vi my_mailbox_limits.cf

user = postfix
password = yourdbpasswd
hosts = localhost
dbname = postfix
query = SELECT quota FROM mailbox WHERE username = '%s' AND active = 1

Save and Close (ESCAPE, ":" and wq)

vi my_mailbox_maps.cf

user = postfix
password = yourdbpasswd
hosts = localhost
dbname = postfix
query = SELECT CONCAT(domain,'/',maildir) FROM mailbox WHERE username = '%s' AND active = 1

Save and Close (ESCAPE, ":" and wq)

Next we need to update the main.cf to add the paths to the new files and add a few other tweaks.

vi main.cf

Add the following the end of your file:

virtual_minimum_uid = 150
virtual_uid_maps = static:150
virtual_gid_maps = static:8
virtual_mailbox_base = /var/vmail
virtual_transport = dovecot
dovecot_destination_recipient_limit = 1 virtual_alias_maps = proxy:mysql:/etc/postfix/my_alias_maps.cf
virtual_mailbox_limit = proxy:mysql:/etc/postfix/my_mailbox_limits.cf
virtual_mailbox_domains = proxy:mysql:/etc/postfix/my_domains_maps.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/my_mailbox_maps.cf

Comment out (or remove) the following set of options:


#home_mailbox = Maildir/

#mailbox_command = /usr/lib/dovecot/deliver -c /etc/dovecot/dovecot-postfix.conf -n -m "${EXTENSION}"

Remove yourcompany.com from mydestination. (It can't be in mydestination and virtual_mailbox_domains)

Save and Close (ESCAPE, ":" and wq)

vi master.cf

dovecot unix - n n - - pipe flags=DRhu user=vmail:mail argv=/usr/lib/dovecot/deliver -c /etc/dovecot/dovecot-postfix.conf -f ${sender} -d $(recipient)

Save and Close (ESCAPE, ":" and wq)

Now create the directory for the mail and the user who will handle it:

sudo useradd -r -u 150 -g mail -d /var/vmail -s /sbin/nologin vmail
sudo mkdir /var/vmail
sudo chmod 770 /var/vmail
sudo chown vmail:mail /var/vmail/

Next change the dovecot configuration to accept mail and deliver it:

cd /etc/dovecot
vi dovecot-sql.conf

driver = mysql
connect = host=localhost dbname=postfix user=postfix password=yourdbpassword
default_pass_scheme = MD5-CRYPT

user_query = SELECT '/var/vmail/%d/%n' as home, 'maildir:/var/vmail/%d/%n' as mail, 150 AS uid, 8 AS gid, concat('dirsize:storage=', quota) AS quota FROM mailbox WHERE username = '%u' AND active = 1

password_query = SELECT username as user, password, '/var/vmail/%d/%n' as userdb_home, 'maildir:/var/vmail/%d/%n' as userdb_mail, 150 as userdb_uid, 8 as userdb_gid FROM mailbox WHERE username = '%u' AND active = 1

Save and Close (ESCAPE, ":" and wq)

vi dovecot-postfix.conf

Change (or uncomment) a few options
mail_location = maildir:/var/vmail/%d/%n
first_valid_uid = 150
last_valid_uid = 150
passdb sql {
args = /etc/dovecot/dovecot-sql.conf
}
userdb sql {
args = /etc/dovecot/dovecot-sql.conf
}
master {
path = /var/run/dovecot/auth-master
mode = 0660
user = vmail
group = mail
}

Save and Close (ESCAPE, ":" and wq)

Now restart both services for the changes to take effect

/etc/init.d/postfix restart

/etc/init.d/dovecot restart

All Done!