#TIL : Setup wildcard domains .test for development in MacOS

Too tired of setting your local domain each time you create new virtual development domain, etc helloworld.test, unit.test point to 127.0.0.1

There is a better way to achieve that by using dnsmasq, then set up a wildcard domains for development. In this case I use .test because .dev has been owned by Google and they strictly use HTTPS in mainly browsers.

Install dnsmasq

1
$ brew install dnsmasq

Adding .test wildcard to config file

1
$ echo 'address=/.test/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf

Setup dnsmasq as a startup service

1
$ sudo brew services start dnsmasq

Then add 127.0.0.1 (dnsmasq IP) as first DNS resolver

1
System Preferences > Network > Wi-Fi > Advanced... > DNS > add 127.0.0.1 > move it to top of the list.

Checking everything is worked by listing all resolvers

1
$ scutil --dns

Try it out

1
2
$ nslookup -type=a something.test
$ ping helloworld.test

Set up PHP 7 development environment

Setup requirements

Install VirtualBox

Install Vagrant

Verify all requirements

Open the terminal and try this command to verify Virtualbox is ready.

1
$ Virtualbox -h

Open the terminal and try this command to verify Vagrant is ready.

1
$ vagrant --version

Pull PHP 7 box source code

Clone this repository from Github :

1
$ git clone https://github.com/khanhicetea/phpbox

Provision the box

1
2
$ cd phpbox
$ vagrant up

Note : First provision will take a long time to build the box (about 30-45 minutes). But next time you run the box, it only takes 2 mins.

Development guideline

SSH to the box

Run this command in phpbox folder

1
$ vagrant ssh

Point your virtualhosts to Box IP

Your PHPBox IP address is 192.168.67.101, you can change in Vagrantfile

Open /etc/hosts file in your PC to map virtualhosts IP to PHPBox

1
$ sudo vi /etc/hosts

Each line in this file will have format (IP HOSTNAME). Eg:

1
2
192.168.67.101 test.dev
192.168.67.101 php7iscool.dev

Folder structure

PHPBox Structure

  • conf : Configuration
    • modules.ini : PHP module configuration
    • nginx_default : Nginx default host
    • php-fpm.conf : PHP7-FPM configuration
    • vhosts_apache.conf : Apache virtualhosts (if you use Apache)
    • vhosts_nginx : Nginx virtualhosts
    • www.conf : PHP7-FPM worker configuration
  • scripts : provision scripts
  • www : Web root
    • default : document root of default host 192.168.67.101
    • test : document root of test host test.dev
  • .gitignore : gitignore file
  • Vagrantfile : Vagrantfile of PHPBox

Create a new virtualhost

  • Duplicate a server section in vhosts_nginx or VirtualHost in vhosts_apache.conf
  • Edit the server name and document root
  • Pointing servername to PHPBox IP via hosts file
  • Restart web server sudo service nginx restart or sudo service apache2 restart

Utils

Composer, Git, Docker is ready

This box included helpful tools like composer, git, docker, redis-server, htop, vim, screen and zsh.

PHPMyAdmin or Adminer

  • Adminer (default) : http://192.168.67.101/adminer
  • PHPMyAdmin : http://192.168.67.101/phpmyadmin

Services

  • MySQL user : root / passwd
  • Redis : localhost:6379

Installation config

You can open the scripts/provision/setup.sh to modify some variables to modify some packages.

Ref