How to Set Up Local Webserver in OSX BigSur

I need to experiment with my Unifi API, and given that all my machine is occupied, I decided to use my new Hackintosh BigSur as a test bed. So here is the steps that I used to configure my machine.

Step 1. Turn on the build in Apache server

Open the mac Terminal and type in the following command

sudo apachectl start

It will ask you for your password, once you entered your password, the Apache webserver should start, and you can test this using your browser by pointing to localhost or 127.0.0.1. You should see the following message.

Step 2. Create Sites Directory where you page will be hosted

The next step is to create the Sites directory as your homepage. You will create this under your username, and this will allow multiple users to have their own site. So you you will need to create a Sites folder under your user folder. E.g.: if your user name is Bob, you will need to create a folder Sites under Bob.

/Users/bob/Sites

Step 3. Create a .conf file inside the /etc/apache2/users folder

Using the example above you will need to create a Bob.conf file inside the /etc/apache2/users/bob.conf, you can do that using the following command in terminal

sudo vi /etc/apache2/Users/bob.conf

then you need to copy and paste the following configuration

<Directory "/Users/developer/Sites/"> 
AllowOverride All 
Options Indexes MultiViews FollowSymLinks 
Require all granted 
</Directory>

Hit ESC button and then type

:wq

This will save the config file. If you are lost at this point you might want to refer to vi manual.

Step 4. Configure the httpd.conf file to enable features on your webserver

Type the following command in your terminal

sudo vi /etc/apache2/httpd.conf

If you are not familiar with vi editor you might want to use your other favourite editor such as nano

Search using / command and uncommented the following lines by removing the # infront of the line.

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

Then search and uncomment the following line

Include /private/etc/apache2/extra/httpd-userdir.conf

LoadModule php7_module libexec/apache2/libphp7.so

Then find the following lines and replace it with the Sites folder we have created earlier

DocumentRoot "/Library/WebServer/Documents" 
<Directory "/Library/WebServer/Documents">

So it will read

DocumentRoot "/Users/bob/Sites" 
<Directory "/Users/bob/Sites">

Then search for AllowOverride None in a few lines underneath the above <Directory “/Users/bob/Sites”> and change it to AllowOverride All, so it reads like the following

DocumentRoot "/Users/bob/Sites"  
<Directory "/Users/bob/Sites">    
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options FollowSymLinks Multiviews
    MultiviewsMatch Any

    #
    # 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

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

Save the file by hitting ESC and :wq if you are using vi editor

Step 5. Configure the httpd-userdir.conf file

In the terminal window type the following command

cd /etc/apache2/extra
sudo cp httpd-userdir.conf httpd-userdir.conf.bak

This will make a backup copy of the httpd-userdir.conf before we modifying it. Now open the file using the following command or your favourite editor.

sudo vi httpd-userdir.conf

and uncommented the following line

Include /private/etc/apache2/users/*.conf

Save the file by pressing ESC and :wq and restart the Apache server using the following command.

sudo apachectl restart

Step 6. Test the Webserver using the PHP file

Now you can create the php test file to test that everything is working as expected. Type the following command in terminal

cd /Users/bob/Sites

Then create a php file called phpinfo.php with the following command

vi phpinfo.php

Type the following in the file

<?php phpinfo(); ?>

Save the file by pressing ESC and :wq. Then point your browser to the site using the following

http://localhost/~bob/phpinfo.php

If all goes well you should see the following php info being displayed.

Phew, now I can get on to test my Unifi API php script.

Congratulations if you made it so far. Let me know whether this is useful and drop me a line if you have any questions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.