Here was my issue. I was moving dozens of domains from their position on a cluster server, and moving over to another machine that was to take over as the cluster 'master'. Since the server they were on was a live server, I couldn't just move the IP addresses without blowing up all of those sites, and DNS would tell me that the old server was working fine; how am I going to test the sites on the new server before I flip the switch?
Well, edit the hosts file on my local machine of course. Hrm. There might be 100 sites that I'd need to change the IP for in my hosts file; that's going to suck.
Not really, because my friends sed and awk can make quick work of this.
Assuming:
A) I have 2 servers that have multiple vhost entries in a file called client-vhost.conf
B) the server IP addresses bound to apache are x.x.x.x (server1) and y.y.y.y (server2)
C) the public document root for the websites have subfolders that represent the domain name:
(ie. drwxrwxr-x 7 www www 4096 Dec 1 2011 example.com)
D) we've already synced the data and config files so they are identical on both machines.E) 5.X CentOS install with configs in /etc/httpd/conf/ and data folder of /var/www/
Since server1 has been up and running serving web sites for a period of time, all of our work is going to be on server2. Edit the client-vhost.conf file, you're going to need to do the first part by hand. Change the IP apache is listening on. We don't want to pull out the old entries, because server2 is going to take the IPs from server1. Comment out the x.x.x.x lines and insert the y.y.y.y lines:
#Listen x.x.x.x:80Then save.
#NameVirtualHost x.x.x.x
Listen y.y.y.y:80
NameVirtualHost y.y.y.y
Now we're going to edit all of the vhost entries, there's a lot- so sed will do all the lifting for us. From the command-line type:
sed -i 's/x.x.x.x/x.x.x.x\ y.y.y.y/g' /etc/httpd/conf/client-vhost.confThat will replace the search string 's/----- with the global replace string /--------/g'. We put a space in between the server1 IP address and the server2 IP address, so we need to escape the space with a '\'.
Now we need to get the new IP into my hosts file with all the domain names. How can I get the columns displayed easily with the new IP and a space followed by the domain name? Like this:
ll /var/www/ | grep com | awk '{print "y.y.y.y " $9}'What I did there- 'll' is kinda like 'ls -la' and displays 9 columns; I'm greping 'com' for the *.com reference because there are potentially folders in there that aren't published domains, and I don't care about those; awk takes the piped information and prints "y.y.y.y " (<- with a space before the last ' " ') then $9 or the ninth column, which is the directory name. This gave me a nice two-column list I can copy/paste into my c:\windows\system32\drivers\etc\hosts file (right-click notepad and Open as Administrator if you're using Windows post-XP) that looks like this:
y.y.y.y example1.comNow I can just paste the domains into my browser and make sure they work on the new box before making any potentially destructive changes. Which was a good call, because the real-life example this post was based off of had a few things missing in the server configuration.
y.y.y.y example2.com
y.y.y.y example3.com
No comments:
Post a Comment