Configuring Apache2 on macOS Big Sur 11.1 (2021) to serve a local website.

The goal of this tutorial is to render a static website from the folder ‘<your_user>/sites’

  1. Open the spotlight search (shortcut: Command + Space Bar) or press the magnifying glass icon on the top right bar of your screen and write: ‘terminal’
  2. Once the terminal loads, execute the command for initialise apache:

Type your admin password and press enter. At this point, if you open your any browser on your machine and point it to localhost (or 127.0.0.1) it should say: ‘it works’

Now, we need a place to add the files that will be served locally by apache in your mac.

3. Create a ‘sites’ folder under your user name:

  • Open the finder and point it to Macintosh HD /<your_user >/ and create a folder named ‘Sites’ . The goal is to make apache serve the files placed in this new folder when the browser is pointed to localhost (or 127.0.0.1).

4. Go back to the terminal and execute:

5. Obtain your user name by executing in the terminal:

It will return your username, which it is referred as <your_user > in this tutorial.

6. Execute:

This will open the “nano” editor in your terminal, which is a command line text editor that comes with your mac. Paste the following configuration: (remember to change <your_user> with the result of the ‘whoami’ command)

Save the file (control + o) and exit (control + x)

7. At this point, your terminal should be pointing to the ‘/etc/apache2/users’ folder, go back one level (type ‘cd ..’), then you should be on the folder ‘/etc/apache2'

8. While on ‘/etc/apache2’ execute:

This will open the apache main configuration file. Every directive added, deleted, or modified there will be followed when apache restarts. The directives that start with ‘#’ are comments.

You should be looking at ‘httpd.conf’ file on the nano editor, and it should look like this

There are some modules inside the configuration that are commented, therefore are not loaded by apache, let’s uncomment them.

Press (control + w) and search each of the following and remove the initial ‘#’.

Save the file (control + o), do not exit nano, and Search (control + w) for:

And replace with, where your username is what ‘whoami” returns

Save the file (control + o), do not exit nano, and Search (control + w) for ‘AllowOverride None’ and substitute for ‘AllowOverride All

Save the file (control + o), do not exit nano, and Search (control + w) for:

And uncomment it (remove the ‘#’ at the beginning of the line)

Save the file (control + o) and exit nano (control + x)

9. While on the folder ‘/etc/apache2’ go to the “extra” folder (cd extra), you should be on ‘/etc/apache2/extra’

Execute:

Search (control + w) for:

Uncomment it (remove the ‘#’ at the beginning of the line), Save the file (control + o) and exit nano (control + x).

10. Go to the “Sites” directory you previously created (Macintosh HD /<your_user >/Sites) and create a new PHP file by executing:

While on nano, you will see an empty file, write the following unique line on it:

Save the file (control + o) and exit nano (control + x).

11. Restart Apache by executing:

12. Open a browser and type in the address bar: ‘localhost’ or ‘127.0.0.1’, now, instead of “It works” you should see the following

That means that everything was done correctly, now, delete the file phpinfo.php, and add the sites containing your site. All the files placed on “Sites” directory will be served from localhost or (127.0.0.1)

Have fun and create something awesome! :)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store