<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title>Luke Armstrong</title>
	<link href="https://lukearmstrong.co.uk/atom.xml" rel="self" />
	<link href="https://lukearmstrong.co.uk" />
	<updated>2020-04-11T17:47:47+01:00</updated>
	<id>https://lukearmstrong.co.uk</id>
	<author>
		<name>Luke Armstrong</name>
	</author>

	
		<entry>
			<title>Use Nginx and Apache at the same time</title>
			<link href="https://lukearmstrong.co.uk/2016/12/use-nginx-apache-at-the-same-time/"/>
			<updated>2016-12-29T23:32:00+00:00</updated>
			<id>https://lukearmstrong.co.uk/2016/12/use-nginx-apache-at-the-same-time</id>
			<content type="html">&lt;p&gt;Whilst the majority of the projects I work on are using PHP and running on Apache in production, I often play with other languages and frameworks that tend to prefer using &lt;a href=&quot;https://nginx.org/en/&quot;&gt;Nginx&lt;/a&gt;. So I’ve decided to install Nginx alongside Apache so that I can use both.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is a follow-up from my previous tutorial, &lt;a href=&quot;/2016/12/setup-apache-mysql-php-homebrew-macos-sierra/&quot;&gt;Setup Apache, MySQL and PHP using Homebrew on macOS Sierra&lt;/a&gt;, this tutorial isn’t intended to be used on production servers, but to make life easier when working with your local development environment.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Nginx will be the primary web server listening on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;80&lt;/code&gt;, Apache will be listening on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8080&lt;/code&gt;, so I will proxy all unhandled requests to port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8080&lt;/code&gt; for any projects that I need to run under Apache.&lt;/p&gt;

&lt;h2 id=&quot;setup-apache-to-listen-on-port-8080&quot;&gt;Setup Apache to listen on port 8080&lt;/h2&gt;

&lt;p&gt;Edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/apache2/2.4/http.conf&lt;/code&gt; and find the line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Listen 80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Change it to:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Listen 8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now restart apache.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo apachectl restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In your browser if you go to &lt;a href=&quot;http://localhost/&quot;&gt;http://localhost/&lt;/a&gt; it should show an error, but if you go to &lt;a href=&quot;http://localhost:8080/&quot;&gt;http://localhost:8080/&lt;/a&gt; you should see that Apache is running and showing whatever your default virtual host is.&lt;/p&gt;

&lt;h2 id=&quot;install-nginx&quot;&gt;Install Nginx&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew install nginx --with-http2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The default settings for the brew installation of nginx are to listen on port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8080&lt;/code&gt;, we need to change this to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;80&lt;/code&gt;. Edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/nginx/nginx.conf&lt;/code&gt; and find this line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Listen 8080
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Change it to:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Listen 80
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Also, we need to change another part of the config to test things are working, so find these lines:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;location / {
    root   html;
    index  index.html index.htm;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And change them to:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;location / {
    root   /usr/local/var/www/vhosts/_localhost/nginx;
    index  index.html index.htm;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now lets create a test page.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mkdir -p /usr/local/var/www/vhosts/_localhost/nginx
$ echo &quot;Hello World from Nginx&quot; &amp;gt; /usr/local/var/www/vhosts/_localhost/nginx/index.html
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now lets see if it works, start the nginx server.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo brew services start nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Going to &lt;a href=&quot;http://localhost/&quot;&gt;http://localhost/&lt;/a&gt; in your browser should now show the message &lt;em&gt;“Hello World from Nginx”&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;configure-nginx-to-run-php&quot;&gt;Configure Nginx to run PHP&lt;/h2&gt;

&lt;p&gt;If you followed &lt;a href=&quot;/2016/12/setup-apache-mysql-php-homebrew-macos-sierra/&quot;&gt;my previous tutorial&lt;/a&gt;, you should have PHP 5.6 and PHP 7.0 installed. Just to give you an example of how to get these working with Nginx, I will show you how to setup a couple of virtual hosts.&lt;/p&gt;

&lt;p&gt;First though, we need to setup Nginx to read config files for our virtual hosts. Edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/nginx/nginx.conf&lt;/code&gt; and replace the entire config with this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;user nobody nobody;
worker_processes 1;

events {
    worker_connections 1024;
}

http {
    include           mime.types;
    default_type      application/octet-stream;
    sendfile          on;
    keepalive_timeout 65;

    # Virtual Hosts Config
    include servers/*;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Make a new config file for your virtual host &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/nginx/servers/_localhost.conf&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mkdir -p /usr/local/etc/nginx/servers
$ vi /usr/local/etc/nginx/servers/_localhost.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Enter the following config, it will setup your original &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localhost&lt;/code&gt; site, and create virtual hosts &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;php70.nginx.localhost&lt;/code&gt; for PHP 7.0 and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;php56.nginx.localhost&lt;/code&gt; for PHP 5.6.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#
# Localhost
#
server {
    listen 80;
    server_name localhost nginx.localhost;

    root /usr/local/var/www/vhosts/_localhost/nginx;
    index index.html index.htm;
}


#
# PHP 7.0
#
server {
    listen 80;
    server_name php70.nginx.localhost;

    root /usr/local/var/www/vhosts/_localhost/php;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php {
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9070;

        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}


#
# PHP 5.6
#
server {
    listen 80;
    server_name php56.nginx.localhost;

    root /usr/local/var/www/vhosts/_localhost/php;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php {
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9056;

        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now that you’ve changed your Nginx config, you’ll need to restart the server.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo brew services restart nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now add these lines to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt; file.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;127.0.0.1  nginx.localhost
127.0.0.1  php56.nginx.localhost
127.0.0.1  php70.nginx.localhost
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now open these URLs in your web browser, if you visit &lt;a href=&quot;http://nginx.localhost&quot;&gt;http://nginx.localhost&lt;/a&gt; you should see your test page, then &lt;a href=&quot;http://php56.nginx.localhost&quot;&gt;http://php56.nginx.localhost&lt;/a&gt; the output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;phpinfo()&lt;/code&gt; from PHP 5.6, then &lt;a href=&quot;http://php70.nginx.localhost&quot;&gt;http://php70.nginx.localhost&lt;/a&gt; the output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;phpinfo()&lt;/code&gt; from PHP 7.0.&lt;/p&gt;

&lt;p&gt;Now you can create any other virtual host files in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/nginx/servers&lt;/code&gt; when you need to setup any sites in the future, you don’t need to edit the file we just created.&lt;/p&gt;

&lt;h2 id=&quot;proxy-unhandled-requests-to-apache&quot;&gt;Proxy unhandled requests to Apache&lt;/h2&gt;

&lt;p&gt;Now we need to get Apache working again using &lt;em&gt;“real”&lt;/em&gt; domain names without needing to add the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:8080&lt;/code&gt; port to the end. We need to setup a new virtual host in Nginx which will catch any request not satisfied by our other Nginx virtual hosts, and will be proxied to Apache to be handled normally.&lt;/p&gt;

&lt;p&gt;Make a new config file for your virtual host &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/nginx/servers/_default.conf&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#
# Any unhandled requests should go to Apache
#
server {
    listen 80 default_server;
    server_name _;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass_request_headers on;
        proxy_pass http://127.0.0.1:8080;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This virtual host is set as the default and simply passes the request to port &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;8080&lt;/code&gt; to be handled by Apache. Now that you’ve changed your Nginx config, you’ll need to restart the server.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo brew services restart nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So now if you go to any Apache virtual host you setup previously, it should still be working. Here are the two URLs we setup for Apache in the previous tutorial;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Apache: PHP 7.0 – &lt;a href=&quot;http://php70.apache.localhost&quot;&gt;http://php70.apache.localhost&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Apache: PHP 5.6 – &lt;a href=&quot;http://php56.apache.localhost&quot;&gt;http://php56.apache.localhost&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Now you should have Nginx and Apache both setup with PHP 5.6 and PHP 7.0, all running at the same time without needing to stop one to start another.&lt;/p&gt;

&lt;p&gt;If you got stuck at any point, and want to reference my configuration files, you can browse them here:
&lt;a href=&quot;https://github.com/lukearmstrong/localhost&quot;&gt;https://github.com/lukearmstrong/localhost&lt;/a&gt;&lt;/p&gt;

</content>
		</entry>
	
		<entry>
			<title>Setup Wildcard Virtual Hosts for Apache</title>
			<link href="https://lukearmstrong.co.uk/2016/12/setup-wildcard-virtual-hosts-apache/"/>
			<updated>2016-12-28T21:00:00+00:00</updated>
			<id>https://lukearmstrong.co.uk/2016/12/setup-wildcard-virtual-hosts-apache</id>
			<content type="html">&lt;p&gt;Fed up with setting up a virtual host each time you work on a new project? Here I explain how to setup wildcard virtual hosts with Apache.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is a follow-up from my previous tutorial, &lt;a href=&quot;/2016/12/setup-apache-mysql-php-homebrew-macos-sierra/&quot;&gt;Setup Apache, MySQL and PHP using Homebrew on macOS Sierra&lt;/a&gt;, this tutorial isn’t intended to be used on production servers, but to make life easier when working with your local development environment.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Ordinarily you would have to create a virtual host config file for each project you work on. But if you work on lots of different projects, and you find yourself just copying the same virtual host configuration, we can make life slightly easier by getting Apache to automatically pick up new sites.&lt;/p&gt;

&lt;h2 id=&quot;enable-apache-module-mod_vhost_alias&quot;&gt;Enable Apache Module: mod_vhost_alias&lt;/h2&gt;

&lt;p&gt;Edit Apache’s main configuration file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/apache2/2.4/httpd.conf&lt;/code&gt;. Find the following line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#LoadModule vhost_alias_module libexec/mod_vhost_alias.so
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Uncomment it by removing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#&lt;/code&gt; so it looks like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;LoadModule vhost_alias_module libexec/mod_vhost_alias.so
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Save the file.&lt;/p&gt;

&lt;h2 id=&quot;create-a-wildcard-virtual-host&quot;&gt;Create a Wildcard Virtual Host&lt;/h2&gt;

&lt;p&gt;Next we need to create a Virtual Host that will catch any unhandled requests, we don’t want to break the ability to create our own virtual hosts manually as that can be useful for certain projects.&lt;/p&gt;

&lt;p&gt;Create this file: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/apache2/2.4/vhosts/_default.conf&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;UseCanonicalName Off

&amp;lt;Directory &quot;/usr/local/var/www/vhosts/_wildcard&quot;&amp;gt;
    Allow From All
    AllowOverride All
    Options Indexes FollowSymlinks MultiViews
    Require all granted
&amp;lt;/Directory&amp;gt;

&amp;lt;VirtualHost _default_:*&amp;gt;
    VirtualDocumentRoot /usr/local/var/www/vhosts/_wildcard/%0

    &amp;lt;FilesMatch &quot;\.php$&quot;&amp;gt;
        SetHandler proxy:fcgi://127.0.0.1:9070
    &amp;lt;/FilesMatch&amp;gt;
&amp;lt;/VirtualHost&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;All wildcard sites will be using PHP 7.0, if you would prefer to use PHP 5.6, change the port number on this line &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SetHandler proxy:fcgi://127.0.0.1:9070&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9056&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Because we have changed Apache’s settings, we need to restart it.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo apachectl restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;so-how-does-this-work&quot;&gt;So how does this work?&lt;/h2&gt;

&lt;p&gt;It will take the domain name of the request, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wildcardtest1.site&lt;/code&gt; and start serving files stored in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/var/www/vhosts/_wildcard/wildcardtest1.site/&lt;/code&gt;. So you could just start adding your project folders to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/var/www/vhosts/_wildcard/&lt;/code&gt; for each site you start working on.&lt;/p&gt;

&lt;h2 id=&quot;but-this-wont-work-for-me&quot;&gt;But, this won’t work for me…&lt;/h2&gt;

&lt;p&gt;The projects I work on often have different folder structures, for example; Some may have their public web root in a sub-folder called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;site/&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public_html/&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public/&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;htdocs/&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;httpdocs/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Because of this, I couldn’t just use the same configuration for all sites.&lt;/p&gt;

&lt;p&gt;Also, if you have any subdomains, even &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;www.wildcardtest1.site&lt;/code&gt;, then it will be serving files from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/var/www/vhosts/_wildcard/www.wildcardtest1.site/&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/var/www/vhosts/_wildcard/wildcardtest1.site/&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;we-can-use-symlinks-instead&quot;&gt;We can use symlinks instead&lt;/h2&gt;

&lt;p&gt;A symbolic link is essentially a shortcut to another location. So what I have decided to do is use symlinks to point to my project’s public web root folder. So my wildcard projects still live in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/var/www/vhosts/&lt;/code&gt; with all my existing projects, but I point to them from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/var/www/vhosts/_wildcard/&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;We can also use symlinks to add multiple domains/sub-domains for the same project folder. As you’ll see, even &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;www.&lt;/code&gt; prefixed to the domain gets routed to a different location.&lt;/p&gt;

&lt;p&gt;In my &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/var/www/vhosts/_wildcard/&lt;/code&gt; folder:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wildcardtest1.site     -&amp;gt; /usr/local/var/www/vhosts/wildcardtest1.site/public/
wildcardtest2.site     -&amp;gt; /usr/local/var/www/vhosts/wildcardtest2.site/htdocs/
wildcardtest3.site     -&amp;gt; /usr/local/var/www/vhosts/wildcardtest3.site/
www.wildcardtest1.site -&amp;gt; /usr/local/var/www/vhosts/wildcardtest1.site/public/
www.wildcardtest2.site -&amp;gt; /usr/local/var/www/vhosts/wildcardtest2.site/htdocs/
www.wildcardtest3.site -&amp;gt; /usr/local/var/www/vhosts/wildcardtest3.site/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;create-a-new-symlink&quot;&gt;Create a new symlink&lt;/h2&gt;

&lt;p&gt;Assuming you have copied your project folder to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/var/www/vhosts/my-project/&lt;/code&gt; and your public web root is in a sub-folder called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To create a symlink, open a terminal.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cd /usr/local/var/www/vhosts/_wildcard/
$ ln -s ../my-project/public/ my-project.site
$ ln -s ../my-project/public/ www.my-project.site
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here we have created two symlinks for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my-project.site&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;www.my-project.site&lt;/code&gt;, and they will point to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/var/www/vhosts/my-project/public/&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;dont-forget-to-update-your-etchosts-file&quot;&gt;Don’t forget to update your /etc/hosts file&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo vi /etc/hosts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Add this line to the bottom of the file.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;127.0.0.1  my-project.site  www.my-project.site
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So now when you go to &lt;a href=&quot;http://my-project.site/&quot;&gt;http://my-project.site/&lt;/a&gt; in your browser you should see your site.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;So now setting up new sites should just involve creating a symlink, and adding a line to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt; file. You won’t need to create any more virtual host config files, so you won’t need to restart Apache either!&lt;/p&gt;

&lt;p&gt;If you got stuck at any point, and want to reference my configuration files, you can browse them here:
&lt;a href=&quot;https://github.com/lukearmstrong/localhost&quot;&gt;https://github.com/lukearmstrong/localhost&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;p&gt;I didn’t just figure this out on my own, the article below pretty much got me there.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.sitepoint.com/set-automatic-virtual-hosts-nginx-apache/&quot;&gt;Set up Automatic Virtual Hosts with Nginx and Apache&lt;/a&gt; by &lt;a href=&quot;https://twitter.com/bitfalls&quot;&gt;Bruno Skvorc&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I also include a link to the Apache Documentation in case you are interested in how this wildcard config actually works and what else you can do with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mod_vhost_alias&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html&quot;&gt;Apache Documentation: mod_vhost_alias&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
		</entry>
	
		<entry>
			<title>Setup Apache, MySQL and PHP using Homebrew on macOS Sierra</title>
			<link href="https://lukearmstrong.co.uk/2016/12/setup-apache-mysql-php-homebrew-macos-sierra/"/>
			<updated>2016-12-28T20:40:00+00:00</updated>
			<id>https://lukearmstrong.co.uk/2016/12/setup-apache-mysql-php-homebrew-macos-sierra</id>
			<content type="html">&lt;p&gt;Step by step instructions to setup a local development environment running multiple versions of PHP simultaneously.&lt;/p&gt;

&lt;p&gt;Sure, you could use &lt;a href=&quot;https://www.mamp.info/en/&quot;&gt;MAMP&lt;/a&gt; like many other developers out there, and there’s nothing wrong with that, but setting things up this way will give you a better understanding of how all of these things come together and nearly everything you learn here will help you in the future if you need to setup Linux servers; Which in all likelihood you will do as understanding &lt;a href=&quot;https://en.wikipedia.org/wiki/DevOps&quot;&gt;DevOps&lt;/a&gt; is a useful skill to have.&lt;/p&gt;

&lt;p&gt;I took the steps below using a clean install of macOS Sierra, so hopefully I have covered everything that you need to do. macOS Sierra / Xcode is bundled with Apache and PHP, but we won’t be using these as it is an old version of PHP and messing around trying to update it could potentially break something. A disclaimer is necessary here, if you already have a functional development environment, and you decide to follow these steps, you can’t hold me responsible if it all goes wrong and you break your current setup.&lt;/p&gt;

&lt;h2 id=&quot;install-xcode-and-xcode-command-line-tools&quot;&gt;Install Xcode and Xcode Command Line Tools&lt;/h2&gt;

&lt;p&gt;Open up the App Store and search for Xcode to install it.&lt;/p&gt;

&lt;p&gt;Once it has installed, you’ll need to open Xcode at least once as there is a Terms &amp;amp; Conditions prompt to accept before you can use the command line tools. Once you have got past the prompt, if any appears you may have already cleared it previously, open the Terminal and enter the command below to install Xcode Command Line Tools.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ xcode-select --install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;The $ is just to indicate this is a command prompt, do not enter it with the command.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;ensure-the-system-installation-of-apache-is-not-running&quot;&gt;Ensure the system installation of Apache is not running.&lt;/h2&gt;

&lt;p&gt;Enter this command to stop Apache if it is running, the second line below (not prefixed with $) is the error I received from the command, this is to be expected as it shouldn’t be running.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo apachectl stop
/System/Library/LaunchDaemons/org.apache.httpd.plist: Could not find specified service
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This command will stop Apache from starting on boot.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2&amp;gt;/dev/null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;install-homebrew&quot;&gt;Install Homebrew&lt;/h2&gt;

&lt;p&gt;To install Homebrew, check for the latest installation instructions at &lt;a href=&quot;http://brew.sh/&quot;&gt;http://brew.sh/&lt;/a&gt;, but when I installed it I ran the command below.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ /usr/bin/ruby -e &quot;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Homebrew has a utility that will check if everything is setup correctly, run this after installation.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew doctor
Your system is ready to brew.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;add-homebrew-taps&quot;&gt;Add Homebrew taps&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;taps&lt;/em&gt; are extra repositories containing formulae to install software, we will need to add these by entering the commands below.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew tap homebrew/apache
$ brew tap homebrew/completions
$ brew tap homebrew/php
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;enable-auto-completion-for-brew-commands-optional&quot;&gt;Enable auto-completion for brew commands (optional)&lt;/h3&gt;

&lt;p&gt;You can skip this step if you want, but to help you use brew, you can enable auto-completion for commands; So when you type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;brew&lt;/code&gt; and mash the tab button you’ll see what commands you can run.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew install bash-completion
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After installation you should see a message telling you how to enable auto-completion, mine told me to add this to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.bash_profile&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[ -f /usr/local/etc/bash_completion ] &amp;amp;&amp;amp; . /usr/local/etc/bash_completion
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once you have done this, open a new terminal (or tab), and now you should have auto-completion hints when typing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;brew&lt;/code&gt; and then hitting tab several times.&lt;/p&gt;

&lt;h2 id=&quot;install-apache&quot;&gt;Install Apache&lt;/h2&gt;

&lt;p&gt;Now we are ready to install Apache 2.4&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew install httpd24 --with-privileged-ports --with-http2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The next step involves using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apachectl&lt;/code&gt; again, you may remember we used this right at the start to disable the system version of Apache. But now that we have installed Apache using Homebrew, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apachectl&lt;/code&gt; will be controlling our version of Apache, not the system version. So let’s check this has worked by typing:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ which apachectl
/usr/local/bin/apachectl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Homebrew installs everything within &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/&lt;/code&gt; so there is no danger of it conflicting with any system-level software. If you get anything else returned as the file path for apachectl, then something hasn’t been setup properly, likely your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PATH&lt;/code&gt; isn’t setup correctly, run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;brew doctor&lt;/code&gt; again to check.&lt;/p&gt;

&lt;p&gt;Assuming everything is fine, we are now ready to start Apache. You’ll need to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo&lt;/code&gt; with this command because it opens port 80. &lt;em&gt;Don’t use sudo when you don’t need to, it could mess up file ownership and/or permissions.&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo apachectl start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now open your web browser and go to &lt;a href=&quot;http://127.0.0.1&quot;&gt;http://127.0.0.1&lt;/a&gt;, you should see a message saying “It works!”&lt;/p&gt;

&lt;p&gt;You should now set Apache to launch on startup.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo brew services start homebrew/apache/httpd24
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;install-php&quot;&gt;Install PHP&lt;/h2&gt;

&lt;p&gt;I’ll be installing PHP 5.6 and 7.0, as previous projects I have worked on are deployed to servers running PHP 5.x, so it is sensible to try to match your development environment to your production environment.&lt;/p&gt;

&lt;h3 id=&quot;install-php-56&quot;&gt;Install PHP 5.6&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew install php56 --with-fpm
$ brew install php56-mcrypt
$ brew install php56-xdebug
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because we will be installing multiple versions of PHP, we need to change the default PHP-FPM (FastCGI Process Manager) port from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9000&lt;/code&gt; to something else, so I suggest setting it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9056&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/php/5.6/php-fpm.conf&lt;/code&gt; and replace this line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;listen = 127.0.0.1:9000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;listen = 127.0.0.1:9056
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we are ready to start PHP-FPM (this will also set it to launch on startup).&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew services start homebrew/php/php56
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;setup-apache-to-work-with-php-fpm&quot;&gt;Setup Apache to work with PHP-FPM&lt;/h3&gt;

&lt;p&gt;Edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/apache2/2.4/httpd.conf&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We need to enable some Apache modules, they have been commented out, so find these lines and remove the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#&lt;/code&gt; to uncomment them.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#LoadModule proxy_module libexec/mod_proxy.so
#LoadModule proxy_fcgi_module libexec/mod_proxy_fcgi.so
#LoadModule rewrite_module libexec/mod_rewrite.so
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So it should now look like…&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;LoadModule proxy_module libexec/mod_proxy.so
LoadModule proxy_fcgi_module libexec/mod_proxy_fcgi.so
LoadModule rewrite_module libexec/mod_rewrite.so
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We need to configure Apache to attempt to load &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.php&lt;/code&gt; by default, so find these lines.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
&amp;lt;IfModule dir_module&amp;gt;
    DirectoryIndex index.html
&amp;lt;/IfModule&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And change it to this.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
&amp;lt;IfModule dir_module&amp;gt;
    DirectoryIndex index.php index.html index.htm
&amp;lt;/IfModule&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We also need to enable virtual hosts, so find this line and uncomment it.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#Include /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now save the file.&lt;/p&gt;

&lt;p&gt;We now need to edit the virtual hosts configuration file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;By default, all of your virtual hosts would go into this file, but it would soon become a mess once you have setup just a few sites. It’s better to use individual files, so either delete or comment out all the lines in this file, and add this to the end.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Include /usr/local/etc/apache2/2.4/vhosts/*.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will read all the files with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.conf&lt;/code&gt; extension in that folder, so to add/edit/remove a virtual host, we just add/edit/delete the individual file. Now we are ready to setup our first virtual host. Create a directory for the config files to live in.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mkdir -p /usr/local/etc/apache2/2.4/vhosts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now create a file for our virtual host &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/apache2/2.4/vhosts/_localhost.conf&lt;/code&gt; and enter the configuration below.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;Directory &quot;/usr/local/var/www/vhosts/_localhost/php&quot;&amp;gt;
    Allow From All
    AllowOverride All
    Options +Indexes
    Require all granted
&amp;lt;/Directory&amp;gt;

&amp;lt;VirtualHost *:*&amp;gt;
    ServerName php56.apache.localhost
    DocumentRoot &quot;/usr/local/var/www/vhosts/_localhost/php&quot;
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9056/usr/local/var/www/vhosts/_localhost/php/$1
&amp;lt;/VirtualHost&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we need to create the folder for the site files.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mkdir -p /usr/local/var/www/vhosts/_localhost/php
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now create an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;index.php&lt;/code&gt; file with the following code in that folder.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;?php

phpinfo();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because we have changed Apache’s settings, we need to restart it.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo apachectl restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To get our new virtual host to work in the browser, we need to edit our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt; file. You’ll need to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo&lt;/code&gt; to edit this file, so assuming you are comfortable using Vim…&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo vi /etc/hosts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Add this line to the bottom of the file.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;127.0.0.1  php56.apache.localhost
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now open your web browser, and go to &lt;a href=&quot;http://php56.apache.localhost&quot;&gt;http://php56.apache.localhost&lt;/a&gt;, you should see the output from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;phpinfo()&lt;/code&gt; showing PHP 5.6.29 is installed.&lt;/p&gt;

&lt;p&gt;You’ll also see an error saying “It is not safe to rely on the system’s timezone settings. You are &lt;em&gt;required&lt;/em&gt; to use the date.timezone setting or the date_default_timezone_set() function.”&lt;/p&gt;

&lt;p&gt;To resolve this edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/php/5.6/php.ini&lt;/code&gt;, and find this line.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;;date.timezone =
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Remove the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;;&lt;/code&gt; to uncomment the setting and set the timezone to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UTC&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;date.timezone = UTC
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we have changed PHP’s settings, we need to restart PHP-FPM.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew services restart homebrew/php/php56
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you refresh the browser you’ll now see that the error has gone.&lt;/p&gt;

&lt;p&gt;Now we have one version of PHP working with Apache, let’s install PHP 7.0.&lt;/p&gt;

&lt;h3 id=&quot;install-php-70&quot;&gt;Install PHP 7.0&lt;/h3&gt;

&lt;p&gt;First we need to run this, otherwise the installation will fail as there will conflicts.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew unlink php56
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we can install PHP 7.0 with the same extensions as above.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew install php70 --with-fpm
$ brew install php70-mcrypt
$ brew install php70-xdebug
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Again, because we are installing multiple versions of PHP we need to change the default FPM port from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9000&lt;/code&gt; to something else, so I suggest setting it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9070&lt;/code&gt;. The layout of the config files is slightly different in this version of PHP.&lt;/p&gt;

&lt;p&gt;Edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/php/7.0/php-fpm.d/www.conf&lt;/code&gt; and replace this line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;listen = 127.0.0.1:9000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;listen = 127.0.0.1:9070
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To avoid the same error we had before, edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/php/7.0/php.ini&lt;/code&gt; and find:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;;date.timezone =
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Replace the line with:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;date.timezone = UTC
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we are ready to start PHP-FPM (this will also set it to launch on startup).&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew services start homebrew/php/php70
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Edit our existing Apache virtual host config file to test PHP 7.0 is working, edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/apache2/2.4/vhosts/_localhost.conf&lt;/code&gt; and add this block to the end of the file.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;VirtualHost *:*&amp;gt;
    ServerName php70.apache.localhost
    DocumentRoot &quot;/usr/local/var/www/vhosts/_localhost/php&quot;
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9070/usr/local/var/www/vhosts/_localhost/php/$1
&amp;lt;/VirtualHost&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because we have changed Apache’s settings, we need to restart it.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo apachectl restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Again, we need to create an entry in our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt; file.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ sudo vi /etc/hosts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Add this line to the end of the file.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;127.0.0.1  php70.apache.localhost
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now open your web browser, and go to &lt;a href=&quot;http://php70.apache.localhost&quot;&gt;http://php70.apache.localhost&lt;/a&gt;, you should see the output from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;phpinfo()&lt;/code&gt; showing PHP 7.0.14 is installed.&lt;/p&gt;

&lt;h2 id=&quot;install-mysql&quot;&gt;Install MySQL&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew install mysql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now lets start MySQL and get it to run on startup.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew services start mysql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I would recommend you setup a password for root, and only allow access from localhost. There is an easy way to do this.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mysql_secure_installation
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It’s an interactive prompt, here were my responses.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Would you like to setup VALIDATE PASSWORD plugin? n
New password: root
Re-enter new password: root
Remove anonymous users? y
Disallow root login remotely? y
Remove test database and access to it? y
Reload privilege tables now? y
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I’ve noticed that since working on older projects, that MySQL is returning errors to PHP when doing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SELECT DISTINCT&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GROUP BY&lt;/code&gt; queries. Turned out it the default &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sql_mode&lt;/code&gt; in MySQL 5.7 is more restrictive than previous versions. Now, by default it is:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now this is fine to leave as the default, I would recommend you leave the settings alone unless you have problems.&lt;/p&gt;

&lt;p&gt;Initially I tried fixing some of the custom queries, but then started noticing problems from queries generated by WordPress’ &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_posts()&lt;/code&gt; function, and as I work on so many different WordPress sites I figured that I would end up wasting loads of time trying to fix these on other sites.&lt;/p&gt;

&lt;p&gt;You can override this setting by creating a config file for mysql, create a file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/local/etc/my.cnf&lt;/code&gt; with these settings:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[mysqld]
#sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION # Default for MySQL 5.7
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

[client]
user=root
password=root
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now if you restart the MySQL server, you shouldn’t be getting these errors.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ brew services restart mysql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;Now you should have everything you need to get started, each time you need to work on a new project, reference the previous steps to setup a new virtual host for it and restart Apache. In the future, if you need to install a different version of PHP, you can reference the steps here, and it should work.&lt;/p&gt;

&lt;p&gt;If you got stuck at any point, and want to reference my configuration files, you can browse them here:
&lt;a href=&quot;https://github.com/lukearmstrong/localhost&quot;&gt;https://github.com/lukearmstrong/localhost&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;/2016/12/setup-wildcard-virtual-hosts-apache/&quot;&gt;Setup Wildcard Virtual Hosts for Apache&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/2016/12/use-nginx-apache-at-the-same-time/&quot;&gt;Use Nginx and Apache at the same time&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
		</entry>
	
</feed>