Creating and configuring a virtual machine for LAMP development

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)


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


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

# 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

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


Configure MySQL (edit /etc/my.cnf)

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!