background image

Content tagged with: configuration

Eric's picture

Back in September 2008, I wrote an article on how to configure your virtual machine to deliver email locally (using postfix, cyrus, imap, and sasl). I've had to revisit this article recently to test some bulk emailing functionality. I wanted to change my email server configuration to deliver all email locally to ensure clients and coworkers do not receive test emails. After reading a bunch of web articles, I decided to use Postfix's transport functionality (located /etc/postfix/transport). This configuration file allows you to map email addresses and hostnames to message delivery transports.

I edited this file (/etc/postfix/transport) and added the following to the end of the file:

* discard:

I then edited my postfix configuration file (/etc/postfix/main.cf) and added the following:

transport_maps = hash:/etc/postfix/transport
always_bcc = eric

Reload the transport and restart postfix using the following commands:

postmap /etc/postfix/transport
/etc/init.d/postfix restart

The first configuration change discards all outgoing email, and the second automatically BCC's my user. Although this is a drastic configuration change, it does exactly what I want: it ensures that email will never be delivered to real world addresses, and any email sent from my development server will end up in my local inbox.

You may have to tweak these settings to find a configuration that works with your development situation. For instance, if you wanted to continue delivery to a certain domain, you could add the following transport:

your.domain :

Eric's picture

If you're running a multi-site Drupal configuration, it's helpful to know which settings.php is being loaded. This information may be useful to your module or theme, or for troubleshooting purposes. For instance, you might be sharing the same theme across a few sites, but you'd like to modify the theme for one of your sites. Sharing themes (and modules) is capable in a multi-site configuration by placing your themes and modules in the [sites/all] folder, while defining a directory and settings.php file for each site installation in [sites]. To be able to tell which settings.php is being loaded, you could add the following code to each settings.php file you define:

<?php
$exploded
= explode("/", __FILE__);
$conf['multi_site_hostname'] = $exploded[count($exploded)-2];
?>

For example, you could pass this information to your page.tpl theme file:

<?php
// defining a preprocess_page function, placed in my template.php file:
function MYTHEME_preprocess_page(&$variables) {
 
// example: pass the configuration directly to the page.tpl.php:
 
$variables['multi_site_hostname'] = variable_get('multi_site_hostname',NULL);

 
// another example:
 
switch (variable_get('multi_site_hostname',NULL)) {
    case
'thedrupalblog.erl.dev':
    case
'thedrupalblog.com':
     
$variables['siteHostName'] = 'thedrupalblog.com';
      break;
    default:
     
$variables['siteHostName'] = 'default';
      break;
  }

}
?>

Eric's picture

After losing my development virtual machine yesterday, I thought I'd document my process for creating a new one. The first thing to do is download your favorite Linux distribution. I prefer Centos (RHEL without the support contract) but what's most important is creating one that's as close to your production environment as possible (to avoid package version differences, differences in documentation, and deployment issues). The second step is creating a new virtual machine and defining it's properties. For a basic development system, I give it 512MB of RAM and a large enough hard drive for your projects. After defining it's properties, point the CDrom drive at your downloaded Linux distribution ISO and install the operating system. Here's the fun part, getting everything to work...

Update your packages

$ yum update

Install your favorite text editor

$ yum install emacs

Configure sudo so you don't have to use root (in: /etc/sudoers)

%wheel ALL=(ALL) NOPASSWD: ALL

Create a new user for yourself, and set your new password

$ adduser Eric
$ passwd Eric
# NOTE: add your user to the wheel group in /etc/group so you can setup sudo
# NOTE: add your user to the apache group and vice versa
# NOTE: from now on, log in as your user and use sudo to execute commands that require root privileges

Install subversion

$ sudo yum install subversion

Install mysql

$ sudo yum install mysql-server

Install PHP

$ sudo yum install php php-cli php-common php-devel php-gd php-ldap php-mbstring php-mssql php-mysql php-odbc php-pear php-soap php-xml php-xmlrpc
# NOTE: The previous command will automatically install Apache (httpd) as a dependency

(OPTIONAL) upgrade PEAR packages

$ sudo pear upgrade-all

(OPTIONAL) install additional PEAR packages

$ sudo pear install DB HTML_QuickForm Mail Mail_Mime

(OPTIONAL) install openssl for HTTPS traffic

$ sudo yum install mod_ssl openssl

Set run levels for mysql and apache to ensure the services start automatically

$ sudo /sbin/chkconfig --level 2345 httpd on
$ sudo /sbin/chkconfig --level 2345 mysqld on

Set MySQL passwords

# root password:
$ /usr/bin/mysqladmin -u root password 'YOUR-NEW-PASSWORD'

# your user:
$ mysql -u root -p
mysql> grant all privileges on *.* to 'Eric'@'localhost' identified by 'YOUR-NEW-PASSWORD' with grant option;

# (OPTIONAL) you can add privileges for your user to connect from other computers:
mysql> grant all privileges on *.* to 'Eric'@'%' identified by 'YOUR-NEW-PASSWORD' with grant option;

Configure PHP (edit /etc/php.ini)

$ sudo emacs /etc/php.ini
display_errors = [Off per prod | On per dev/test]
error_reporting = E_ALL & ~E_NOTICE
memory_limit = 100M
upload_max_filesize = 100M
post_max_size = 100M

Configure Apache

# create a directory for all your vhosts, and set permissions
sudo mkdir /var/www/vhosts
sudo chown -R Eric.Eric /var/www/vhosts
sudo chmod -R 2770 /var/www/vhosts

# create a new configuration file to keep your changes separate from httpd.conf
$ sudo emacs /etc/httpd/conf.d/Eric.conf

# FILE CONTENTS - START

# set directory indexes to ensure php files are not read as text
DirectoryIndex index.php index.html index.html.var index.htm

# set a default character set
AddDefaultCharset ISO-8859-1

# enable name based virtual hosts, so you can host multiple hostnames on one server
NameVirtualHost *:80

# I create a separate directory for all my virtual hosts. This allows .htaccess files to work properly
<Directory /var/www/vhosts>
AllowOverride All
</Directory>

# add your first virtual host entry
<VirtualHost *:80>
ServerName SITEHOSTNAME
DocumentRoot /var/www/vhosts/SITEHOSTNAME/httpdocs
ErrorLog logs/SITEHOSTNAME-error_log
CustomLog logs/SITEHOSTNAME-access_log common
</VirtualHost>

# FILE CONTENTS - END

Configure MySQL (edit /etc/my.cnf)

[mysqld]
set-variable = max_allowed_packet=32M

Start Apache & MySQL

$ /etc/init.d/mysqld start
$ /etc/init.d/httpd start

(OPTIONAL) Configure Samba so you can edit your virtual machine filesystem from your host operating system
[documentation here]

Now, from your host operating system, edit your /etc/hosts file and point your development hostname to the new IP address of your virtual machine. If you go to that web address you should be able to reach your Apache virtual host on your new virtual machine!

Eric's picture

I do all of my development on a Parallels virtual machine 1) to keep my host operating system stable at all times; and 2) so I can run the same operating system as our production servers (to reduce the differences in configuration & chance of a deployment issue). I'm a fan of RPM based operating systems (vs APT) so I prefer Centos. To improve performance I do not run a GUI (KDE, Gnome, etc) and I install the bare minimum set of packages. My linux server philosophy has always been: if you don't use it or don't know what it does, don't install it; and if you don't know it does, Google it. My preferred IDE is Eclipse, so I install that on my host operating system (OSX). So how do I edit my files on my virtual machine? Samba. Here's how I setup a basic samba configuration:

Install Samba (as necessary)

sudo yum install samba

Create a backup of the original samba configuration file, just in case

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.orig

Here's a basic smb.conf file I use with a description for each config setting:

[global]
    # workgroup should match your network that your host operating system is on
    workgroup = MYWORKGROUP
    # server string is description of samba server
    server string = MY CENTOS VM
    # default security level
    security = user
    # performance tuning
    socket options - TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192
[homes]
    # share comment for user's home directories
    comment = Home Directories
    # hides shares to users without permission
    browseable = no
    # allows privileged users to modify files
    writable = yes
[Vhosts]
    # I share my entire vhosts folder
    comment = Vhosts
    # to be more secure, I only allow my primary user from my host operating system access
    valid users = eric
    # allows privileged users to modify files
    writeable = yes
    # this is the path to my Apache vhosts
    path = /var/www/vhosts
    # setting a umask will ensure permissions will be set correctly on files created across a samba share
    directory mask = 775
    create mask = 664

Create a samba user that matches your host operating system

sudo smbpasswd -a eric

Restart Samba

sudo /etc/init.d/smb restart

Now, from your host operating you can connect to your vhosts share and edit your files. From OSX, I use the "connect to server" command in the finder menu and specify the IP address of my virtual machine. The samba share will now be mounted in /Volumes/IPOFYOURVM. When I create a new project in Eclipse, I uncheck the default location and choose to create the project on my virtual machine. tah-dah

Eric's picture

I do almost all of my development on Centos virtual machines. This allows me to keep my filesystem, databases, and server configurations contained in a separate environment without harming my host operating system (OSX). This morning, I encountered a problem with my email not being delivered properly. I'm assuming this happened because my virtual machine is not a full qualified email server. I figured the easiest solution would be to setup my virtual machine as an IMAP server instead of trying to fight with DNS, static IPs, spam filters, etc. When it comes down to it, all I need is for my emails sent from my web applications to end up in my email client inbox. Here's what I did to get this to work...

1. Ditch sendmail and install postfix. Postfix is much easier to configure; Sendmail is jibberish.

yum remove sendmail
yum install postfix
chkconfig --level 2345 postfix on

2. Install cyrus, my preferred IMAP service. NOTE: SASL will be the authentication method service

yum install cyrus-imapd cyrus-sasl
chkconfig --level 2345 cyrus-imapd on
chkconfig --level 2345 saslauthd on

3. Configure Postfix service

# edit /etc/postfix/main.cf and change this line:
mailbox_transport = cyrus

# most importantly, I added a few domains (work, personal, etc) to the mydestination variable to ensure that mail sent from my server will be delivered locally, instead of leaving my virtual machine. Make sure you add all the corresponding aliases in /etc/aliases. For instance: If you want to receive email sent to wee@blah.com, you'll have to add blah.com to the list of domains in mydestination and add an alias for wee to redirect to your email account name.

4. Configure Cyrus

# set sasl passwords
saslpasswd2 cyrus
saslpasswd2 eric

# Use Cyrus admin tool to configure mailbox
cyradm --user cyrus --server localhost
> createmailbox user.eric
> setaclmailbox user.eric eric all
> quit

5. Add a new IMAP account in your favorite email client. For my setup, I used the IP address of my virtual machine as the incoming mail server address. Now, when mail is delivered from my virtual machines, it is delivered locally and my email client is used to fetch the new messages!

6. To ensure your setup is working properly, you can send a quick email from the command line (on your virtual machine)

date | mail eric@somedomain.com