MacOS Catalina – set up localhost

The most recent update to OS Catalina swiped away localhost access in my computer. If you don?t mind setting it up directly in the machine, here is how I did it. The original article was found on https://discussions.apple.com/docs/DOC-11238. It applies to High Sierra, but has worked for me for future OS updates as well. This applies if you are the administrator of your mac and if you own your pc.

  1. Open up the Terminal. Set up the Sites directory in the user root folder. On a new Mac with no localhost set up yet we must create a Sites folder in the root user folder. This is to host the server from the user root directory. The system root is left untouched.

mkdir ~/Sitesecho “<html><body><h2> My site works! </h2></body></html>” > ~/Sites/index.html.en

3. Make a back up of the config file ?httpd.conf?. This is where all the necessary changes need to be made to allow localhost to be served on Mac. Using the sudo command allows super user access in mac. You may be able to do it only if you own your pc or if you are the administrator.

sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.bak

4. Check if the file is copied by,

ls /etc/apache2

5. Edit the apache config file as root user. Vi is the built-in text editor in Mac.

sudo vi /etc/apache2/httpd.conf

Once in the editor, enable line number by typing :set nu on the keyboard. The cursor placement is not important. Next, scroll down to line 186.

6. Make the following changes. They enable PHP, personal website, user home directories. Line numbers as they appeared on my computer are listed. They may be different in other computers. Uncommenting is done by pressing the letter x on the keyboard (small letter; no caps)

Line 186#LoadModule php5_module libexec/apache2/libphp5.so becomesLoadModule php5_module libexec/apache2/libphp5.soLine 183LoadModule userdir_module libexec/apache2/mod_userdir.soLine 520Include /private/etc/apache2/extra/httpd-userdir.conf

7. Another small change to be made is on permissions. Since this set up does not use the system root, for safety purposes, access to it is denied. Thus, scroll down to lines after 255. We see a section on Document Root: <Directory ?/Library/WebServer/Documents?>?.. </Directory> Here we comment out Require all granted and add a new line, Require all Denied. (Note: this note does not include making changes to allow .htaccess. That is beyond the scope of this article). I do not recommend changing anything else here.

<Directory “/Library/WebServer/Documents”>….#Require all grantedRequire all Denied</Directory>

Save and exit by pressing ZZ on the keyboard (caps using shift key)

ZZ

8. Since we are using a user document root, we need to make changes to the user directory configuration file. But first, make a back up. Then, open it in Vi.

sudo cp /etc/apache2/extra/httpd-userdir.conf /etc/apache2/extra/httpd-userdir.conf.baksudo vi /etc/apache2/extra/httpd-userdir.conf

9. Uncomment the following at line 16 (enable line numbering as before by typing :set nu). Save and exit.

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

10. We have seen the directory for system root above. But we are interested in our Sites directory that we created above to serve as our server.

The next step is then to make a user config file. It is here that we give our ?Sites? directory all the rights to be used as our root. But first let us see if one exist. Its quite likely that initially there is no config file for users. Mac does not automatically create one. Typically the first time you will see only guest.conf. Type the following to list all files in the ?users? directory.

ls /etc/apache2/users

11. If <username>.conf file does not exist, here is how to create one. (Note: If you do not know what your user name you can look it up easily on the internet. For instance, here is one link: https://support.apple.com/hr-hr/HT201548)

And, remember to put your username in place of <username>.

sudo vi /etc/apache2/users/<username>.conf

12. The sudo vi command will open a new note for us. Type the following in the text space.

<Directory “/Users/<yourusername>/Sites/”> AddLanguage en .en Options Indexes MultiViews FollowSymLinks AllowOverride All Require local</Directory>

Note: If you need .htaccess, then other set ups are necessary.

Save and exit.

[We see the power that this allows us. The Sites directory is only one folder which we have given rights. We can optionally have multiple users and multiple directories to serve our websites. This is how we do it. But I am only 1 user and I have only 1 folder, my Sites folder. All other websites are created under this folder.]

13. Finally, turn on and launch apache on the system. If it is turned on already, it will inform us.

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

You may chose to restart apache. Here are a few apache commands:

sudo apachectl startsudo apachectl stopsudo apachectl restarthttpd -v (find your apache version; a fun thing to do 🙂 )

That?s it! We can test our handywork now. In any browser, type the following address in the address bar on top: http://localhost/. It should say,

It works!

Now let us try the user home directory that we created on our own. Type,

http://localhost/~<yourusername> (Note: don?t forget the ?~? tag)

It should say,

My site works!

[a comment: if you can’t get the local host site to load, try the following: You should find a file at /Library/WebServer/Documents/index.html.en . This contains the text “It works!” referred to in the post. What I did was duplicate that file in the same folder and changed the duplicate’s name to ‘index.html’, leaving the original in situ. Both local and user sites then loaded. After which, I was able to delete the duplicated file and everything now works without issue. Just to be clear, leave the original file index.html.en where it is, untouched and unharmed throughout this step.]

Now, let?s try PHP.

echo “<?php echo phpinfo(); ?>” > ~/Sites/info.php

Now, test it in the browser. http://localhost/~<yourusername>/info.php. We can see our PHP configuration information

Hope it helps!

This is a basic set up for converting your Mac to a server served on local host. Setting up virtual hosts, serving .htaccess and pretty urls is not included in this note. Thus, if we want to run a wordpress site, this is not enough. This set up is sufficient to develop our own web designs from ground up.

1

No Responses

Write a response