One thing that can be a hassle is handling differing development and production database configurations. On your local box you have set of database credentials, on the live system you have another. Frequently, you may end up commenting out one set or another, and that can easily lead to accidentally putting code on the live server that will not work. There are a number of ways to handle this, but one simple way is outlined below.
The first step is to decide what subdomain to use. I frequently use local, as it makes logical sense. To get that to resolve to your system at 127.0.0.1, the simplest way is to modify your local hosts file. In Windows, it is typically located at C:\Windows\System32\drivers\etc\hosts. To actually modify it, you will want to run Notepad as an admin. Now add your new host in and then save it:
127.0.0.1 local.ericbrandel.com
Your computer will now properly resolve the local subdomain to itself.
The second step is to configure PHP to utilize the proper database credentials depending on where the code is sitting. The simplest way to do this is to base it on the $_SERVER[‘HTTP_HOST’] value.
switch($_SERVER['HTTP_HOST']) { // Local Dev Site case 'local.ericbrandel.com': define("DB_HOST", "localhost"); define("DB_USER", "dev_user"); define("DB_PASS", "dev_pass"); define("DB_DATABASE", "dev_database"); break; // Live Site case 'ericbrandel.com': define("DB_HOST", "live_host"); define("DB_USER", "live_user"); define("DB_PASS", "live_password"); define("DB_DATABASE", "live_database"); break; }
When the code is on my system, the host will be local.ericbrandel.com. When the code is on the live server, the host will be ericbrandel.com. If your site does not redirect visitors to the www subdomain like mine does, you may want to add another case statement to the switch:
// Live Site case 'ericbrandel.com': case 'www.ericbrandel.com': define("DB_HOST", "live_host"); define("DB_USER", "live_user"); define("DB_PASS", "live_password"); define("DB_DATABASE", "live_database"); break;
Finally, to get WAMP to recognize the new local subdomain, click on the WAMP tray icon and go to Apache->httpd.conf. At the bottom will likely be an existing Virtual Host for localhost:
NameVirtualHost localhost:80 <VirtualHost localhost:80> ServerName localhost ServerAlias localhost DocumentRoot C:/dev/tools/wamp/www ErrorLog "C:/dev/tools/wamp/error.log" CustomLog C:/dev/tools/wamp/access.log common <Directory "C:/dev/tools/wamp/"> AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
Below that, add another entry, but modify it for our new subdomain:
NameVirtualHost local.ericbrandel.com:80 <VirtualHost local.ericbrandel.com:80> ServerName local.ericbrandel.com ServerAlias local.ericbrandel.com DocumentRoot c:/dev/projects/ericbrandel.com ErrorLog "c:/dev/projects/ericbrandel.com/error.log" CustomLog c:/dev/projects/ericbrandel.com/access.log common <Directory "c:/dev/projects/ericbrandel.com"> AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
Now restart Apache in WAMP and you will be able to pull up local.ericbrandel.com in your browser and WAMP will serve up whatever content is located in the DocumentRoot directory.
Even without database configs to worry about, this setup also works well for just handling multiple projects on your local system. Having a VirtualHost for each project, as well as an entry in the hosts file, allows you to have multiple projects running without them stepping on each other.