background image

Content tagged with: development

Eric's picture

There are some great development modules for Drupal (Devel, Coder, Reroute_Email, Demo, etc), but you probably don't want to have them enabled in a production environment. Deployments to production can be simplified by adding a hook_update_N function in your module's .install file. In this function you can take care of administrative functions such as importing views and CCK node type definitions (essentially, anything exportable). In this quick code snippet, I'll show how you can create a module update function to disable your development modules on update.

<?php
// NOTE: see the documentation on hook_update_N for version naming conventions
function MYMODULE_update_6100() {
 
 
// check for production environment hostname
 
if ($_SERVER['HTTP_HOST'] == 'your-production-hostname') {
   
   
// rebuild the module cache
   
module_rebuild_cache();
   
   
// define a list of development modules to disable
   
$modules_disable = array(
     
'reroute_email',
     
'coder',
     
'demo',
     
'performance',
     
'devel_node_access',
     
'devel_generate',
     
'devel_themer',
     
'devel',
    );
   
   
// disable modules
   
module_disable($modules_disable);

  }

}
?>

Now that your module update function is created, you can deploy your file updates to production (preferably using subversion) and run the update.php script, apply your module update, and disable your development modules.

Eric's picture

I recently got a new MacBook Pro laptop (awesome!) and went through my usual rigmarole of setting up a new [VirtualBox] virtual machine for LAMP development. In previous situations I used bridged network connections which allow my virtual machine to have its own network connection, and acquire an IP address via DHCP. Now that I plan on being more mobile, I was concerned about having a static IP address for my virtual machine (for samba connections, scripting, and any other processes that relies on a static IP address). I decided to explore alternative virtual network connections and changed my virtual NIC to use NAT (the default network connection for VirtualBox). This configuration establishes a virtual NAT for your virtual machines which is great, but the downside is I know have to setup port forwarding to connect to my virtual machine. In bridged network configurations, I could simply SSH or use Samba to connect to my virtual machine by IP address. With NAT I have to setup port forwarding for the services I need to connect to. Initially I decided to setup port forwarding for SSH (port 22) and HTTP (port 80). While my virtual machine was powered down, I executed the following commands. NOTE: you'll have to replace "Centos" with the name of your virtual machine.

$ VBoxManage setextradata "Centos" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol" TCP
$ VBoxManage setextradata "Centos" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort" 22
$ VBoxManage setextradata "Centos" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort" 2222
$ VBoxManage setextradata "Centos" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/GuestPort" 80
$ VBoxManage setextradata "Centos" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/HostPort" 8080

Now, I can SSH to my virtual machine..

$ ssh -p 2222 Eric@localhost

Or, connect to Apache by browsing to http://localhost:8080

The benefit of this network configuration allows me to travel anywhere with my laptop, use any type of network connection, and not have to worry about changing the way I connect to my virtual machine for development.

Eric's picture

It's important to treat content management systems like any other application and segment development, test, and production. Before adding content and attaching files to nodes, it's very important to set the file system path correctly and establish a deployment procedure.

One way to make the transition from development to production a smooth process is to use the same directory for your files. If you are using one Drupal installation for each project sites/default/files will work fine. If you set your file system path specific to your development hostname (for example: site/dev.HOSTNAME.com/files), you'll be in trouble when you transition to a new hostname.

Here's an example process to show how you could transition a site from development to test (or any other environment).

1. Migrate your MySQL database to the new server. Each environment should use its own database.

# connect to mysql as a privileged user to create a new database and user
$ mysql -u USER -pPASSWORD -h NEWSERVERHOST
> create database NEWDATABASE;
> GRANT ALL PRIVILEGES ON NEWDATABASE.* TO 'NEWUSER'@'localhost' IDENTIFIED BY 'NEWPASSWORD';
> exit;

# backup database
$ mysqldump -u USER -pPASSWORD DATABASE > DATABASE.sql

# import database
$ mysql -u NEWUSER -pNEWPASSWORD -h NEWSERVERHOST NEWDATABASE < DATABASE.sql

2. Copy your Drupal installation to the new server. Hopefully, you're using subversion (or some other version control software) to deploy these files.

3. Create a new folder in your sites directory for the new hostname. Copy your existing settings.php into the new folder. Edit this file and update the MySQL connection string to match your new database, user, and password.

4. Create a new Apache virtual host for your new hostname and direct DNS accordingly.

Now, if DNS has been setup for your new hostname, the site should be up and running on the new server with a new hostname.

Additional thoughts: It's very important to avoid hard coding hostnames and absolute paths in your links, code, and themes. I promise, it will make your deployment process a nightmare.

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

Here's an (incomplete) list of the software and tools I prefer to use:

Internet Browsing
I use Firefox3 as my primary browser. Here are some of the extensions I have installed:
- Colorzilla - allows you to pick a color from a website
- Delicious Bookmarks - allows me to access, create, and search my bookmarks wherever I go
- DownloadHelper - allows you to download embedded flash. movies, objects, etc
- Firebug - the BEST tool for web development. GET IT NOW
- FireFTP - a FTP client that fits nicely into a tab in FireFox
- MeasureIt - allows you to measure distances on a website
- Web Developer - another essential tool for web development
- YSlow - a Yahoo! plugin used to evaluate and analyze page load times
I also use the following browsers for testing web apps: Safari, Flock, Camino, Opera, & ies4osx. IES4OSX allows you to install Internet Explorer on a MAC using Wine for OSX, sweet!

Development
I run OSX as my primary operating system. An operating system is useless to me if it doesn't have a fully functional shell. I use Parallels to install a Centos virtual machine (with no GUI). Centos is used to run LAMP and any other development and application services. I then connect to my linux filesystem using Samba and SSH. I use Eclipse as my primary IDE (with the PDT plugin for PHP). For database management I use Navicat and MySQL Tools. For a Subversion client I use Versions.

Other
- Quicksilver - file/application indexing to increase productivity
- Adobe Photoshop CS3 - image creation and manipulation
- Aquamacs Emacs - text editor
- FFMPEGX - video conversion
- Fugu - secure file copy
- Google Notifier - email and calendar event reminders
- IPSecuritas - VPN Connections
- Skype - Office chats, video conferencing, IMs
- iChat - IMs & video chats
- iTunes - I listen to music all day long off my iPod
- NeoOffice - OpenOffice for OSX (FREE equivalent of M$ Office)