Install and configure Apache on MacOS 12 (Monterey)

Author
By Darío Rivera
Posted On in MacOS

In a previous post we saw how to Install multiple versions of PHP on MacOS. You probably need to now install and configure apache to run on your MacOS machine and to be able to run your PHP developed applications. In this post we will see how to make this possible.

The first thing to keep in mind is that in MacOS 12 Monterey Apache 2.4 comes pre-installed. However, it is not through brew. You can verify this if you can see the configuration but not the packages in brew.

~  apachectl -t -D DUMP_INCLUDES

Included configuration files:
  (*) /private/etc/apache2/httpd.conf
    (509) /private/etc/apache2/extra/httpd-mpm.conf
    (515) /private/etc/apache2/extra/httpd-autoindex.conf
    (555) /private/etc/apache2/other/mpm.conf
    (555) /private/etc/apache2/other/php7.conf
☁  ~  brew search apache
==> Formulae
apache-activemq     apache-arrow-glib      apache-drill     apache-forrest    apache-opennlp      apache2
apache-archiva      apache-brooklyn-cli    apache-flink     apache-geode      apache-pulsar       apachetop
apache-arrow        apache-ctakes          apache-fop       apache-httpd      apache-spark

==> Casks
apache-couchdb     apache-directory-studio

The most advisable thing in this case is to dispense with the default version if this is the case, and install everything again from brew.

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Install Apache with Brew

The first thing we must install is the http service.

brew install httpd

Now, to start apache with the system, we must configure it as a service.

brew services start httpd

At this point you should already see the popular "It works!".

it works

Apache Configuration

The configuration of Apache with brew is located at /opt/homebrew/etc/httpd/httpd.conf. The first thing you can do in this case is to change the default port (8080) to 80. To do this, you must change the line

Listen 8080

For

Listen 80

You can also change the directory from which web files are taken with the DocumentRoot directive.

DocumentRoot "/opt/homebrew/var/www"

For something like

DocumentRoot "/Users/my_user/www"

With this you should also change the <Directory> tag from

<Directory "/opt/homebrew/var/www">

To something like

<Directory "/Users/my_user/www">

Finally, if you want to use .htaccess files (most common), you must allow overwriting in the same block with.

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All

You must also activate the apache mod_rewrite module.

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

Optionally, you can change the ServerName of apache. Your sites will continue to work even if you do not do this, however, when you are verifying that everything is ok in the apache config, you may come across this error.

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using P2P-TEC-MED-009.local. Set the 'ServerName' directive globally to suppress this message

To do this, you must configure the directive as localhost.

ServerName localhost

Once you have done this, you can restart the apache service.

brew services restart httpd

Troubleshooting

Here are some very useful commands that will help you detect problems in the configuration.

Verify the apache configuration after a change.

/opt/homebrew/bin/apachectl configtest

Restart the apache httpd service.

/opt/homebrew/bin/httpd -k restart

Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.