Thumbnail article
How to install apache and php in monterey intel processor

Apache and PHP on a mac can be installed without using XAMPP or MAMP. In this article we will tell you how to install apache and php in monterey intel processor using homebrew. Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple’s operating system.

XCode Command Line Tools Installation

Homebrew requires XCode Command Line Tools, if XCode is not installed, you can install it by running the command below in the terminal.

xcode-select --install
Homebrew Installation

Homebrew can be installed very easily. Using the brew command you can easily add a program to your mac. Run the command below in the terminal to do the installation of homebrew.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

If this is a fresh install and you don’t have your path setup properly, you can follow the installation “next steps” which are already customized for you, or you can manually add the following paths to your .bashrc or .zshrc:

eval "$(/usr/local/bin/brew shellenv)"

to ensure everything is configured correctly you can run the command below.

brew doctor

The command above will tell you does brew successfully installed or there is something need to be fixed. We also need to install openssl to avoid warning a few libraries is missing. You can install it by using the command below.

brew install openssl
Apache and PHP Installation

Monterey comes with apache. You can check the status of the apache by running the command below.

sudo apachectl status

Apple has removed some required apache scripts so in this article we will install the apache via homebrew instead using the default apache. If the built-in Apache running, you need to shutdown first, and unload the auto-loading scripts. Run the command below to do this.

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

Now we need to install the new version provided using homebrew

brew install httpd

To see the status of apache httpd is running or not, you can run the command below.

brew services list

Apache is controlled via the brew services command so here some useful commands to use.

brew services run httpd
brew services start httpd
brew services stop httpd
brew services restart httpd
  • Run : Run the apache without registering to launch at login (or boot).
  • Start : Start the apache and register it to launch at login (or boot).
  • Stop : Stop the apache and unregister it from launching at login (or boot).
  • Restart : Stop (if necessary) and start the apache and register it to launch at login (or boot).

If you get a message that the browser can’t connect to the server, first check to ensure the server is up.

ps -aef | grep httpd

You should see a few httpd processes if Apache is up and running. Try to restart Apache with:

brew services restart httpd

You can watch the Apache error log in a new Terminal tab/window during a restart to see if anything is invalid or causing a problem:

tail -f /usr/local/var/log/httpd/error_log

Now we need to configuring the apache after successfully install the apache. First we will change the port from 8080 to 80 by editing the apache’s configuration file /usr/local/etc/httpd/httpd.conf. You don’t need to access the url with port if we change it to port 80. Run the command below to open the file.

open -a TextEdit /usr/local/etc/httpd/httpd.conf

It will open httpd.conf file using the TextEdit application. Find the line that says

Listen 8080

and change it to 80

Listen 80

The document root is configured as /usr/local/var/www by default. We will change the document root according to our desire. You need to search word DocumentRoot.

DocumentRoot "/usr/local/var/www"

Change the document root to directory that you want. For example we will point it to Sites folder in home directory. So we will create Sites folder in home directory using the command below

mkdir ~/Sites

and change the DocumentRoot like code below.

DocumentRoot "/Users/your_user/Sites"

We can’t use ~ when pointing to the home directory so we need to write /Users/your_user. You also need to change the <Directory> tag reference right below the DocumentRoot line.

<Directory "/Users/your_user/Sites">

In that same <Directory> block you need to change the AllowOverride to All like code below.

AllowOverride All

We also need to enable mod_rewrite which is commented out by default. Just search for mod_rewrite and removing the # in the front of the word like code below.

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

Apache runs as the user daemon and group daemon by default. This will cause permission problems when trying to access files in our home directory. We can change the user to your username and the grup to staff. Replace it like the code below.

User your_user
Group staff

We usually use localhost as server name, in apache the server name is disable by default so we need to activate it. You need to search this code.

#ServerName www.example.com:8080

and replace it with:

ServerName localhost

We will trying to copy the html file from the old apache to the current folder. Run the command below to do it.

cp /usr/local/var/www/index.html /Users/your_user/Sites/index.html

Now restart the apache to load the latest configuration using the command below.

brew services restart httpd

If you receive an error upon restarting Apache, try removing the quotes around the DocumentRoot and Directory designations we set up earlier. Now you can access it by using http://localhost/index.html. You also need to press Shift + Reload to clear the browser cache. You can install the php using homebrew. By default it will install php version 8.0.12. You can install it by using the command below

brew install php

You can check the version of php by using the command below

php -v

The php.ini files are located in the following directories.

/usr/local/etc/php/8.0/php.ini

Close your terminal ( press cmd+q ) and reopen the terminal. To switch to the installed PHP you can run the command below

brew unlink php && brew link --overwrite --force [email protected]

You have successfully installed your PHP versions, but we need to tell Apache to use them. You will again need to edit the /usr/local/etc/httpd/httpd.conf file scroll to the bottom of the LoadModule entries. If you have been following this guide correctly, the last entry should be your mod_rewrite module:

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

Below this add the following libphp modules:

LoadModule php_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp.so

Also you must set the Directory Indexes for PHP explicitly, so search for this block:

<IfModule dir_module>
 DirectoryIndex index.html
</IfModule>

and replace it with this:

<IfModule dir_module> 
 DirectoryIndex index.php index.html 
</IfModule> 
<FilesMatch \.php$> 
 SetHandler application/x-httpd-php 
</FilesMatch>

Save the file and restart Apache then start again, now that we have installed PHP:

brew services restart httpd

We can test the php is running as expected by using phpinfo(). Using the command below we will create a version.php inside the Sites folder.

echo "<?php phpinfo();" > ~/Sites/version.php

Now try to access the http://localhost/version.php in the browser. You will see a PHP information page like this. PHP info

Similar Posts

One Comment

  1. Thank you so much. I’ve never seen so well written and clear document.

Leave a Reply

Your email address will not be published. Required fields are marked *