{"id":184,"date":"2012-06-25T10:00:12","date_gmt":"2012-06-25T15:00:12","guid":{"rendered":"https:\/\/www.foell.org\/justin\/?p=184"},"modified":"2019-10-21T11:52:57","modified_gmt":"2019-10-21T16:52:57","slug":"pimpin-your-dev-env-with-dnsmasq","status":"publish","type":"post","link":"https:\/\/www.foell.org\/justin\/pimpin-your-dev-env-with-dnsmasq\/","title":{"rendered":"Pimpin&#8217; your dev env with Dnsmasq"},"content":{"rendered":"<p>The <a title=\"DNS\" href=\"http:\/\/en.wikipedia.org\/wiki\/Domain_Name_System\">Domain Name System (DNS)<\/a> is arguably the best (and most used) service that the internet has spawned.\u00a0 Why leave all the fun <a title=\"TLD\" href=\"http:\/\/en.wikipedia.org\/wiki\/TLD\">Top Level Domain (TLD)<\/a> stuff to <a title=\"ICANN\" href=\"http:\/\/en.wikipedia.org\/wiki\/Internet_Corporation_for_Assigned_Names_and_Numbers\">ICANN<\/a> &amp; your registrar?\u00a0 You can have your own TLD using <a title=\"Dnsmasq\" href=\"http:\/\/www.thekelleys.org.uk\/dnsmasq\/doc.html\">Dnsmasq<\/a>.<\/p>\n<p>This is part one of a three part series covering Dnsmasq&#8217;s uses regarding:<\/p>\n<ol>\n<li>Local Development DNS<\/li>\n<li><a title=\"Pimp your LAN with OpenWRT &amp; Dnsmasq\" href=\"https:\/\/www.foell.org\/justin\/pimp-your-lan-with-openwrt-and-dnsmasq\/\">Local Area Network (LAN) DNS<\/a><\/li>\n<li><a title=\"(Re)routing VPN traffic with Dnsmasq\" href=\"https:\/\/www.foell.org\/justin\/rerouting-vpn-traffic-with-dnsmasq\/\">Virtual Private Network (VPN) routing<\/a><\/li>\n<\/ol>\n<p><!--more--><\/p>\n<p>The only prerequisites for running Dnsmasq is a Unix-based operating system (including Mac OSX).\u00a0 The only prerequisite for reading this article in the series is interest in doing any sort of web development locally. \u00a0I&#8217;m going to cover a simple example using <a title=\"WordPress\" href=\"http:\/\/wordpress.org\">WordPress<\/a>, but the principle is the same for any web site or service software written in any language. \u00a0Basically, if your normal local development addresses look something like http:\/\/localhost\/~me\/bobscountrybunker\/ then Dnsmasq is for you.<\/p>\n<h2>Installing &amp; Starting Dnsmasq<\/h2>\n<p>I&#8217;m going to cover installation &amp; configuration for Ubuntu 12.04, but over time I haven&#8217;t seen a lot of changes in the way Dnsmasq is configured. \u00a0So configuration should apply to OSX and other Linux distros.<\/p>\n<p>To install on a\u00a0Debian-based system:<\/p>\n<pre>$ sudo apt-get install dnsmasq<\/pre>\n<p>Dnsmasq will probably start automatically the next time you boot, but to start it now execute:<\/p>\n<pre>$ sudo service dnsmasq start<\/pre>\n<p>Systems that don&#8217;t use <a title=\"Upstart FAQ\" href=\"http:\/\/upstart.ubuntu.com\/faq.html\">upstart<\/a> will probably start it like this<\/p>\n<pre>$ sudo \/etc\/init.d\/dnsmasq start<\/pre>\n<p>Even with no configuration changes, your computer still should work as normal, but if you inspect your <code>\/etc\/resolv.conf<\/code> it probably now has a line like this:<\/p>\n<pre>nameserver 127.0.0.1<\/pre>\n<p>Dnsmasq is already up and running, taking in DNS requests from your computer and at this point just forwarding most of them to your upstream DNS provider. Nothing groundbreaking, but now we can control DNS ourselves and have a little fun.<\/p>\n<h2>Configuring Dnsmasq<\/h2>\n<p>The configuration files will be in <code>\/etc\/dnsmasq.conf<\/code> and\/or <code>\/etc\/dnsmasq.d<\/code>. The addition of the <code>\/etc\/dnsmasq.d<\/code>\u00a0directory is new to Ubuntu 12.04 (not sure which Dnsmasq version it corresponds to), and it&#8217;s a welcome addition because you can put your customized files in there without worrying about <code>\/etc\/dnsmasq.conf<\/code> getting overwritten during the next release upgrade.<\/p>\n<p>The files in <code>\/etc\/dnsmasq.d<\/code> are executed by the order of their filename, so it would be wise to number your most important configuration with a `01` prefix, such as\u00a0<code>\/etc\/dnsmasq.d\/01_localhost<\/code><\/p>\n<h3>Top Level Domains &#8211; not just for the big-boys (anymore)<\/h3>\n<p>You can invent a new Top Level Domain for use on your local system. \u00a0On some distributions the convention is to use &#8216;.lan&#8217; but you could name it what ever you&#8217;d like. I don&#8217;t recommend anything too long or the &#8216;.local&#8217; extension which is used by <a title=\"Multicast DNS\" href=\"http:\/\/en.wikipedia.org\/wiki\/Multicast_DNS\">multicast dns<\/a>. \u00a0Here&#8217;s a simple <code>\/etc\/dnsmasq.d\/01_localhost<\/code>\u00a0configuration file:<\/p>\n<pre>address=\/lan\/127.0.0.1<\/pre>\n<p>Now every address that ends in &#8216;.lan&#8217; will belong to your local IP. \u00a0After (re)starting Dnsmasq, try it out:<\/p>\n<pre>$ ping wordpress.lan\r\nPING wordpress.lan (127.0.0.1) 56(84) bytes of data.\r\n64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.021 ms<\/pre>\n<h2>Pairing Dnsmasq &amp; Apache Web Server<\/h2>\n<p>Now let&#8217;s combine this with some Apache configuration for a real-world development example. I don&#8217;t want to get into the specifics of Apache configurations amongst different OSes and distributions, but on Ubuntu I put a config file in <code>\/etc\/apache2\/sites-available<\/code> and symlink it to <code>\/etc\/apache2\/sites-enabled<\/code>, however it can really go anywhere that Apache will read it:<\/p>\n<pre>&lt;VirtualHost *:80&gt;\r\n    ServerName wordpress.lan\r\n    ServerAlias *.wordpress.lan\r\n\r\n    DocumentRoot \/home\/justin\/wordpress\r\n    &lt;Directory \/home\/justin\/wordpress\/&gt;\r\n        Options Indexes FollowSymLinks MultiViews\r\n        AllowOverride All\r\n        Order allow,deny\r\n        allow from all\r\n    &lt;\/Directory&gt;\r\n&lt;\/VirtualHost&gt;<\/pre>\n<p>Once I&#8217;ve restarted\/reloaded Apache, I can visit http:\/\/wordpress.lan in my browser on my local system to bring up an instance of WordPress. You may have noticed the <code>ServerAlias *.wordpress.lan<\/code> line in the configuration. With mod_rewrite enabled in Apache, I can take the set up a step further and turn this single WordPress installation into a <a title=\"WordPress Network\" href=\"http:\/\/codex.wordpress.org\/Create_A_Network\">subdomain network installation<\/a>. This is a great set up if you are working on several sites but want to maintain a single main codebase for your content management software.<\/p>\n<div class='kindleWidget kindleLight' ><img decoding=\"async\" src=\"https:\/\/www.foell.org\/justin\/wp-content\/plugins\/send-to-kindle\/media\/white-15.png\" \/><span>Send to Kindle<\/span><\/div>","protected":false},"excerpt":{"rendered":"<p>The Domain Name System (DNS) is arguably the best (and most used) service that the internet has spawned.\u00a0 Why leave all the fun Top Level Domain (TLD) stuff to ICANN &amp; your registrar?\u00a0 You can have your own TLD using Dnsmasq. This is part one of a three part series covering Dnsmasq&#8217;s uses regarding: Local&hellip; <a href=\"https:\/\/www.foell.org\/justin\/pimpin-your-dev-env-with-dnsmasq\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1,17,8,22],"tags":[23,166],"class_list":["post-184","post","type-post","status-publish","format-standard","hentry","category-business","category-dns","category-ubuntu","category-wordpress","tag-dnsmasq","tag-linux"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.foell.org\/justin\/wp-json\/wp\/v2\/posts\/184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.foell.org\/justin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.foell.org\/justin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.foell.org\/justin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.foell.org\/justin\/wp-json\/wp\/v2\/comments?post=184"}],"version-history":[{"count":9,"href":"https:\/\/www.foell.org\/justin\/wp-json\/wp\/v2\/posts\/184\/revisions"}],"predecessor-version":[{"id":3586,"href":"https:\/\/www.foell.org\/justin\/wp-json\/wp\/v2\/posts\/184\/revisions\/3586"}],"wp:attachment":[{"href":"https:\/\/www.foell.org\/justin\/wp-json\/wp\/v2\/media?parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.foell.org\/justin\/wp-json\/wp\/v2\/categories?post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.foell.org\/justin\/wp-json\/wp\/v2\/tags?post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}