<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scriptNode &#187; apache</title>
	<atom:link href="http://scriptnode.com/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://scriptnode.com</link>
	<description>Tips and tricks for web developers.</description>
	<lastBuildDate>Tue, 01 Sep 2009 18:46:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Quickly Update DocumentRoot in Apache2</title>
		<link>http://scriptnode.com/article/quickly-update-documentroot-in-apache2/</link>
		<comments>http://scriptnode.com/article/quickly-update-documentroot-in-apache2/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 19:33:50 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[code-style]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script-sunday]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=63</guid>
		<description><![CDATA[
For today&#8217;s Script Sunday I thought I&#8217;d take a go back in time a little and provide an old utility that I still to this day find useful.
It&#8217;s a script I wrote called ChangeLocalHost which alters the DocumentRoot in apache2. An example of its use would be:


./clh.php local-site.com/public_html/


Note: you must be root to run it


Given [...]]]></description>
			<content:encoded><![CDATA[<p>
For today&#8217;s <a href="/tag/script-sunday/">Script Sunday</a> I thought I&#8217;d take a go back in time a little and provide an old utility that I still to this day find useful.<br />
It&#8217;s a script I wrote called <code>ChangeLocalHost</code> which alters the <code>DocumentRoot</code> in <code>apache2</code>. An example of its use would be:
</p>
<p>
<code>./clh.php local-site.com/public_html/</code>
</p>
<p>
<em>Note: you must be <code>root</code> to run it</em>
</p>
<p>
Given this command, the script will change <code>DocumentRoot</code> to <strong>local-site.com/public_html/</strong> and restart <code>apache2</code>.<br />
I work on <a href="http://dirtybeta.com/">a lot</a> of <a href="http://valadria.com/">websites</a> (developing most of them on my laptop running <code>apache2</code>)<br />
so I use this script constantly.
</p>
<p>
Here&#8217;s the code:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
#!/usr/bin/php<br />
&lt;?php</p>
<p>/*<br />
ChangeLocalHost v0.1 &#8211; last updated Mar 07 2007<br />
Must be root to execute. Alters the DocumentRoot in Apache2&#8217;s conf file, then restarts Apache2.<br />
*/</p>
<p>$obj_clh = new ChangeLocalHost(&#8217;/etc/apache2/sites-available/&#8217;, &#8216;default&#8217;, &#8216;/home/www/&#8217;);</p>
<p>$obj_clh-&gt;setArgs($_SERVER['argv']);<br />
$obj_clh-&gt;run();</p>
<p>/**<br />
 * CLH class constructor<br />
 * @constructor<br />
 */<br />
class ChangeLocalHost {</p>
<p>	private $arr_args,<br />
			$bit_msgs,<br />
			$str_dir,<br />
			$str_file,<br />
			$str_pre;</p>
<p>	protected $str_version;</p>
<p>	/**<br />
	 * Constructor method<br />
	 * @param string str_dir The location of the Apache conf file to mod<br />
	 * @param string str_file The name of the Apache conf file to mod<br />
	 * @param string str_pre The needle to search for in the conf file<br />
	 */<br />
	function __construct($str_dir, $str_file, $str_pre) {</p>
<p>		$this-&gt;str_version = &#8216;ChangeLocalHost v0.1&#8242;;</p>
<p>		$this-&gt;arr_args = Array();<br />
		$this-&gt;bit_msgs = true;<br />
		$this-&gt;str_dir  = $str_dir;<br />
		$this-&gt;str_file = $str_file;<br />
		$this-&gt;str_pre  = $str_pre;</p>
<p>	}</p>
<p>	/**<br />
	 * Modify Apache&#8217;s DocumentRoot<br />
	 * @param string str_file The (conf) file to mod<br />
	 * @param string str_new_dir The new directory to set as DocumentRoot<br />
	 * @private<br />
	 */<br />
	private function alterLocalHost($str_file, $str_new_dir) {</p>
<p>		$str_contents = file_get_contents($str_file);</p>
<p>		if (!$str_contents) {<br />
			$this-&gt;showError(&#8217;Could not read &#8216; . $str_file);<br />
		}</p>
<p>		$int_begin    = strpos($str_contents, $this-&gt;str_pre);<br />
		$int_end      = strpos(substr($str_contents, $int_begin), &#8220;\n&#8221;);<br />
		$str_search   = substr($str_contents, $int_begin, $int_end);<br />
		$str_contents = str_replace($str_search, $str_new_dir, $str_contents);<br />
		$bit_written  = file_put_contents($str_file, $str_contents);</p>
<p>		if ($bit_written) {<br />
			$this-&gt;showMessage(&#8217;Successfully set DocumentRoot to &#8216; . $str_new_dir);<br />
		} else {<br />
			$this-&gt;showError(&#8217;Could not write &#8216; . $str_file);<br />
		}</p>
<p>	}</p>
<p>	/**<br />
	 * Validates the new directory to set<br />
	 * @param string str_new_dir The potential new directory<br />
	 * @private<br />
	 */<br />
	private function checkLocalHost($str_new_dir) {</p>
<p>		$str_set_to = $this-&gt;str_pre . $str_new_dir;</p>
<p>		if (is_dir($str_set_to)) {</p>
<p>			$this-&gt;setLocalHost($str_set_to);<br />
			$this-&gt;restartApache2();</p>
<p>		} else {<br />
			$this-&gt;showError($str_set_to . &#8216; is not a valid directory.&#8217;);<br />
		}</p>
<p>	}</p>
<p>	/**<br />
	 * Ensures this app is being executed as root<br />
	 * @private<br />
	 */<br />
	private function checkRoot() {</p>
<p>		// Make sure user is root<br />
		$str_command = &#8216;whoami&#8217;;</p>
<p>		exec($str_command, $arr_output, $int_return);</p>
<p>		if ($arr_output[0] != &#8216;root&#8217;) {<br />
			$this-&gt;showError(&#8217;You must be root to run this program.&#8217;);<br />
		}</p>
<p>	}</p>
<p>	/**<br />
	 * Makes a backup of the Apache conf file<br />
	 * @param string str_source The original to copy<br />
	 * @param string str_new The location of the backup<br />
	 * @return bool Whether the copy was successful or not<br />
	 * @private<br />
	 */<br />
	private function makeCopy($str_source, $str_new) {</p>
<p>		$bit_copy = copy($str_source, $str_new);</p>
<p>		if (!$bit_copy) {<br />
			$this-&gt;showError(&#8217;Could not make copy of &#8216; . $str_source);<br />
		}</p>
<p>		return true;</p>
<p>	}</p>
<p>	/**<br />
	 * Restarts Apache2<br />
	 * @private<br />
	 */<br />
	private function restartApache2() {</p>
<p>		$str_command = &#8216;apache2ctl restart&#8217;;</p>
<p>		exec($str_command, $arr_output, $int_return);</p>
<p>		if ($int_return &gt; 0) {<br />
			$this-&gt;showError(&#8217;Could not restart Apache2.&#8217; . $str_source);<br />
		}</p>
<p>		$this-&gt;showMessage(&#8217;Successfully restarted Apache2.&#8217;);</p>
<p>	}</p>
<p>	/**<br />
	 * Executes the script<br />
	 */<br />
	function run() {</p>
<p>		$this-&gt;checkRoot();</p>
<p>		$str_new_dir = $this-&gt;arr_args[1];</p>
<p>		switch ($this-&gt;arr_args[1]) {</p>
<p>			case &#8216;-c&#8217; :<br />
			case &#8216;&#8211;current&#8217; :<br />
				$this-&gt;showCurrent();<br />
				break;</p>
<p>			case &#8221; :<br />
			case &#8216;-h&#8217; :<br />
			case &#8216;&#8211;help&#8217; :<br />
				$this-&gt;showHelp();<br />
				break;</p>
<p>			case &#8216;-q&#8217; :<br />
			case &#8216;&#8211;quiet&#8217; :<br />
				$str_new_dir    = $this-&gt;arr_args[2];<br />
				$this-&gt;bit_msgs = false;<br />
				break;</p>
<p>			case &#8216;-v&#8217; :<br />
			case &#8216;&#8211;version&#8217; :<br />
				$this-&gt;showVersion();<br />
				break;</p>
<p>			default :<br />
				break;</p>
<p>		}</p>
<p>		$this-&gt;checkLocalHost($str_new_dir);<br />
		exit(0);</p>
<p>	}</p>
<p>	/**<br />
	 * Create a copy of the arguments global<br />
	 * @param array array_args The arguments to set<br />
	 */<br />
	function setArgs($arr_args) {<br />
		$this-&gt;arr_args = &amp;$arr_args;<br />
	}</p>
<p>	/**<br />
	 * Call the other methods to initiate the altering of the DocumentRoot<br />
	 * @param string str_new_dir The new value of DocumentRoot<br />
	 * @private<br />
	 */<br />
	private function setLocalHost($str_new_dir) {</p>
<p>		$str_ext    = &#8216;.bak&#8217;;<br />
		$str_file   = ($this-&gt;str_dir . $this-&gt;str_file);<br />
		$str_backup = $str_file . $str_ext;</p>
<p>		if (is_file($str_file)) {</p>
<p>			if ($this-&gt;makeCopy($str_file, $str_backup)) {<br />
				$this-&gt;showMessage(&#8217;Copied backup of &#8216; . $this-&gt;str_file . &#8216; to &#8216; . $this-&gt;str_file . $str_ext);<br />
			}</p>
<p>			$this-&gt;alterLocalHost($str_file, $str_new_dir);</p>
<p>		} else {<br />
			$this-&gt;showError($str_file . &#8216; cannot be found.&#8217;);<br />
		}</p>
<p>	}</p>
<p>	/**<br />
	 * Show what DocumentRoot is currently set to<br />
	 * @private<br />
	 */<br />
	private function showCurrent() {</p>
<p>		$str_file = ($this-&gt;str_dir . $this-&gt;str_file);</p>
<p>		if (is_file($str_file)) {</p>
<p>			$str_contents = file_get_contents($str_file);</p>
<p>			if (!$str_contents) {<br />
				$this-&gt;showError(&#8217;Could not read &#8216; . $str_file);<br />
			}</p>
<p>			$int_begin  = strpos($str_contents, $this-&gt;str_pre);<br />
			$int_end    = strpos(substr($str_contents, $int_begin), &#8220;\n&#8221;);<br />
			$str_search = substr($str_contents, $int_begin, $int_end);</p>
<p>			$this-&gt;showMessage(&#8217;The current DocumentRoot is: &#8216; . $str_search);<br />
			exit;</p>
<p>		} else {<br />
			$this-&gt;showError($str_file . &#8216; cannot be found.&#8217;);<br />
		}</p>
<p>	}</p>
<p>	/**<br />
	 * Display a message and exit<br />
	 * @param string str_error The message to display<br />
	 * @private<br />
	 */<br />
	private function showError($str_error) {</p>
<p>		echo &#8216;Error: &#8216; . $str_error . &#8220;\n&#8221;;</p>
<p>		exit(1);</p>
<p>	}</p>
<p>	/**<br />
	 * Show the help text and exit<br />
	 * @private<br />
	 */<br />
	private function showHelp() {</p>
<p>		$str_help .= &#8216;Usage: clh [DIRECTORY]&#8230; [OPTIONS]&#8216; . &#8220;\n&#8221;;<br />
		$str_help .= &#8216;Alter the DocumentRoot in Apache2.&#8217; . &#8220;\n&#8221;;<br />
		$str_help .= &#8220;\n&#8221;;<br />
		$str_help .= &#8216;  -c, &#8211;current  display current DocumentRoot and exit&#8217; . &#8220;\n&#8221;;<br />
		$str_help .= &#8216;  -h, &#8211;help     display this help and exit&#8217; . &#8220;\n&#8221;;<br />
		$str_help .= &#8216;  -q, &#8211;quiet    suppress most messages&#8217; . &#8220;\n&#8221;;<br />
		$str_help .= &#8216;  -v, &#8211;version  output version information and exit&#8217; . &#8220;\n&#8221;;<br />
		$str_help .= &#8220;\n&#8221;;<br />
		$str_help .= &#8216;Report bugs to &lt;scriptnode@gmail.com&gt;.&#8217; . &#8220;\n&#8221;; // Hahahaha</p>
<p>		echo $str_help;</p>
<p>		exit(0);</p>
<p>	}</p>
<p>	/**<br />
	 * Display a message to the user<br />
	 * @param string str_message The message to show<br />
	 * @private<br />
	 */<br />
	private function showMessage($str_message) {</p>
<p>		if ($this-&gt;bit_msgs) {<br />
			echo $str_message . &#8220;\n&#8221;;<br />
		}</p>
<p>	}</p>
<p>	/**<br />
	 * Attempt to be more like a real application by displaying the version<br />
	 * @private<br />
	 */<br />
	private function showVersion() {</p>
<p>		echo $this-&gt;str_version . &#8220;\n&#8221;;</p>
<p>		exit(0);</p>
<p>	}</p>
<p>}<br />
</textarea></p>
<p>
You&#8217;ll notice that the script is at least a year and a half old.<br />
Usually if code of mine is more than a year old, it&#8217;s completely unusuable because my <a href="/article/my-new-coding-habits/">habits change pretty often</a>.<br />
But I think I&#8217;ve finally gotten to that sweet spot where it&#8217;s not total garbage, it just needs tweaking.
</p>
<p>
I was surprised that the code isn&#8217;t too bad really. It&#8217;s easily readable and well organized. There are many things that I wouldn&#8217;t have done were I to write this again;<br />
for example, I stopped using type prefixes long ago (such as an array of users as <code>$arr_users</code>). I understand why I used to do this, but it&#8217;s not necessary<br />
and only clutters up the code.
</p>
<p>
Anyway, I find this script useful and I thought someone else might too. Enjoy!
</p>
<ul>
<li><a href="/assets/php/clh/clh.php.txt">[Download clh.php.txt]</a> (6,614 bytes)</li>
<li><a href="/assets/php/clh/clh.zip">[Download clh.zip]</a> (1,975 bytes)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/quickly-update-documentroot-in-apache2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
