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’
- 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’
- Once the terminal loads, execute the command for initialise apache:
sudo apachectl start
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:
cd /etc/apache2/users
5. Obtain your user name by executing in the terminal:
whoami
It will return your username, which it is referred as <your_user > in this tutorial.
6. Execute:
sudo nano <your_user>.conf
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)
<Directory "/Users/<your_user>/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>
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:
sudo nano httpd.conf
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 ‘#’.
LoadModule authn_core_module libexec/apache2/mod_authn_core.so
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule include_module libexec/apache2/mod_include.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Include /private/etc/apache2/extra/httpd-userdir.conf
Save the file (control + o), do not exit nano, and Search (control + w) for:
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
And replace with, where your username is what ‘whoami” returns
DocumentRoot "/Users/<your_user>/Sites/"
<Directory "/Users/<your_user>/Sites/">
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:
LoadModule php7_module libexec/apache2/libphp7.so
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:
sudo nano httpd-userdir.conf
Search (control + w) for:
Include /private/etc/apache2/users/*.conf
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:
sudo nano phpinfo.php
While on nano, you will see an empty file, write the following unique line on it:
<?php phpinfo(); ?>
Save the file (control + o) and exit nano (control + x).
11. Restart Apache by executing:
sudo apachectl restart
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! :)