OS X | Web development | Posted on December 8, 2015

nginx virtual domainsThere is plenty of articles like, install MAMP or MEMP stack for Drupal or WordPress development. But none of them ( which I tried to Google) is for these two content management systems.I prefer Nginx so I will set up MEMP stack. As everyday normal developers, we will also set up local dns server so we can use some fancy domains like .app .dev .idrinkbeer.I will show you way how to set up .dev and. web domains.Of course, you can add additional domain too like .joomla or anything else.

Install Nginx + php-fpm + mysql + phpmyadmin

First of all I won’t tell you “How to install nginx, mysql & php-fpm with a homebrew” . These steps you can find here This guy also gives some nice config for phpmyadmin .He also give you some bash aliases , so you don’t need to cd to nginx log directory you can just type nginx.logs.error and see nginx error logs.

Install dnsmasq

This is straight forward with homebrew

brew install dnsmasq

Copy deamon files into right folders

sudo cp $(brew list dnsmasq | grep /homebrew.mxcl.dnsmasq.plist$) /Library/LaunchDaemons/

Enable Dnsmasq to run at startup

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

Now open dnsmasq configuration file ( /usr/local/etc/dnsmasq.conf ) and add your local domains. Mine looks like this https://gist.github.com/emilslv/91f158261fe6a3da7f47 Then restart Dnsmasq

sudo launchctl stop homebrew.mxcl.dnsmasq
sudo launchctl start homebrew.mxcl.dnsmasq

Now it’s time to check if everything works as expected.for this we will use dig command in terminal.

dig myweb.dev

If everything is ok your output should look like this:

;; ANSWER SECTION: myweb.dev. 3599 IN A 127.0.53.53

Now we need to setup Mac os x to use our local DNS server for these domains. Let’s create folder for our local domain entries

sudo mkdir -p /etc/resolver

Now in this directory, we can create our resolver files. In our case, these are dev and web files.Here is a copy of minesWe don’t need to restart OS X , because our local domains will work after we create these files.Let’s test if it works.First, try to ping Google and check if you can do it and then your local domain. If it works then local domains like myweb.dev and mywebsite.web will ping to 127.0.0.1

Nginx virtual domains for each CMS.

Maybe as you know Drupal and WordPress have a different config for Nginx. Different rewrite urls etc. So we will set up that everything that goes to .dev will use a Drupal config file, but everything that resolves to .web domain uses WordPress config. Here is my default Nginx vhosts file.As you can see I have 2 statement parts in my config. First one is .web for WordPress and below it second server statement for Drupal.The ”magic” that Nginx checks for http_host part in your request and if your request contains domain .dev it checks if folder with your requested address .dev is located in /var/www/. It if is your website will be opened and will use config for .dev domain. Otherwise, will display 404 error.For Drupal, I have default Nginx config from Drupal.org.I put all config in one file for better readability but if you like you can store each config in a separate file and just include it the right place.

The ”magic” that Nginx checks for http_host part in your request and if your request contains domain .dev it checks if folder with your requested address .dev is located in /var/www/. It if is your website will be opened and will use config for .dev domain. Otherwise, will display 404 error. So fat that it, now you can run Drupal or WordPress apps on separate domains in a local environment. If any questions leave them in comments section