Homebrew Apache



Homebrew Apache

Set Apache Port The Homebrew’s version of httpd uses port 8080. You have to manually change the listen port from the default of 8080 to standard port 80. Find the line with Listen 8080 and change it to Listen 80. Eventhough Apache ships with macOS, a better maintained version would be coming from Homebrew, also Apple is making noises about not supporting legacy software like PHP so in the future this may be more of a compulsory option. First thing to do would be to remove the builtin Apache version of which in macOS Big Sur is Apache/2.4.46. Using Homebrew installer on OSX. If you use OSX as your platform, you can use Homebrew package manager to easily install Apache ActiveMQ. After installing Homebrew package manager successfully, just run. Homebrew is a package manager designed for Mac that is useful to install. You will find that you can utilize Homebrew for data science as it makes it a lot easier to install additional technologies on Mac such as Apache Spark and the software Graphviz. With that, let’s get started. Install Command Line Tools.

Homebrew apache spark

Apache and PHP-FPM via Homebrew on macOS

Homebrew Apache Bench

I like to keep my development environment lean, running Apache and PHP-FPM on my MacBook Pro without MAMP or similar. Even tools like Vagrant, while great, for many feel more like an hassle.
I try to mimic as much as possible the staging or production environments I use, that’s why I avoid the simple way of using the PHP module for Apache. Instead, the better way to go is to setup PHP-FPM.

Since I mentioned that I’m using macOS, the best way to install system software is Homebrew. Over the years the ecosystem has routinely changed though, especially with macOS updates. Until some time ago, I was able to tap homebrew-apache and among other things install fastcgi_module, and configure PHP via a “FastCgiExternalServer” directive, but that became an unviable solution.

I’m writing down here what I believe it’s a stable solution going forward, both for personal reference and for anyone that wants to use Apache (on port 80 instead of 8080) running with PHP-FPM on macOS Sierra or higher (probably also El Capitan, I haven’t tested yet).

Instructions

1. Install Homebrew, then run $ brew tap homebrew/homebrew-php

2. $ brew install httpd
Install Apache. The caveats will tell you that DocumentRoot is /usr/local/var/www and that the default ports are 8080 and 8443, but it’s easy to use ~/Sites and port 80 and 443 if you’d prefer. Read more for details.

3. brew install php70 –with-homebrew-curl
Install PHP 7.0. Of course you can choose another version of PHP, for example PHP 7.1 or later, or PHP 5.6 or earlier if necessary. Here I advise to build from source by including curl, instead of using the pre-built bottle. curl is a requirement for mostly everybody, and unless that’s not the case for you, you can skip the parameter “–with-homebrew-curl”.

4. $ brew install php70-imagick php70-intl php70-mcrypt php70-opcache php70-xdebug # (optional step)
Just like curl above, these are some PHP modules that are generally useful but not always necessary. Feel free to skip unless you also need them.

5. Download this httpd vhost configuration file — I linked to a specific commit to avoid link breakage, check the master branch for possible updates.

In this file, right at the beginning, there are some indications about the changes to Apache configuration files and to FPM pool configuration for proper setup.
In short you need to edit the Apache configuration file /usr/local/etc/httpd/httpd.conf by adding a line with the path of configuration file linked above. Then, to keep PHP running as your user (to avoid permissions and ownership headaches) you need to change the PHP configuration file /usr/local/etc/php/7.x/php-fpm.d/www.conf (make sure you replace the x with your actual PHP version) by replacing “_www” with your username and the “staff” group name.

6. Lastly, use brew services Digital postbox. to start Apache and PHP-FPM. This makes it super-easy to get up and running and to enable the webserver at system startup, since brew services will take care of placing the correct plist files in the right folders for launchd. NB: for httpd (Apache) perform brew services operations with sudo. This will allow you to use port 80 and 443 with your web server.
Example: $ sudo brew services restart httpd && brew services restart php70

Conclusions

The “secret” to make PHP-FPM work with Apache 2.4 is to use the following directive:

<FilesMatch .php$>
SetHandler proxy:fcgi://localhost:9000
</FilesMatch>

Apache

Homebrew Apache Ant

Of course you need to make sure that PHP-FPM is running on that port, which is configured in the file /usr/local/etc/php/7.x/php-fpm.d/www.conf at the listen directive. Port 9000 is the default.

Homebrew Apache

One bonus of using the Apache configuration file I prepared is that you get “Automatic .dev Virtual Hosts”. If you setup your ~/Sites/ folder as the web server root directory, any directory that you create in it will be treated as a website ending in the .dev TLD. To make it work you need to add lines to your /etc/hosts file, for example: 127.0.0.1 wordpress.dev. You could make it completely dynamic and automatic with the use of dnsmasq (which is also available on Homebrew) but that’s a bit too much for this post.

Homebrew Apache Family

That’s it! Now you can rock a lean setup with close-to-stock Apache and PHP-FPM with minimal modification to core configuration files, and with the advantage of using FPM instead of the PHP Apache module.