How to Set up Apache HTTP Server on macOS Sonoma

There is a pre-bundled version of Apache HTTP Server that comes along with macOS Sonoma. As in the few previous versions of macOS, it is easy to start it up.

Bring up the "Spotlight Search"; press + SPACE and search for terminal. Start as root

				
					sudo su -
				
			

1) The Apache HTTP Server

Check the server version

					
						httpd -v
					
				
mac apache version
Apache version

If the version comes proper, start it by running the command

					
						apachectl start
					
				

Now type http://localhost in the URL bar of your browser. It should be displaying the It works! page.

Now this default It works! page is located inside /Library/WebServer/Documents as the index.html.en file.

mac it works location
The "It works!" Page Location

You can place all your web projects inside the /Library/WebServer/Documents directory and work on them. However, it is more convenient to create your own directory and point the server to it.

2) Set Up your Work Directory

Get back to the root directory. Type

					
						cd ~
					
				

Navigate to the home directory (that one named after your user name). In my computer, it is dennisgabil.

					
						cd /Users/dennisgabil
					
				

There, create a directory, say Sites, for your web projects

					
						mkdir Sites
					
				

To have easy access to your project directory, slot this newly created Sites directory in the Favorites section of your Mac's Finder. Click on Finder > Go > Home and drag and drop your Sites directory to the Favorites sidebar.

3) Modifying httpd.conf

Navigate to /etc/apache2

					
						cd /etc/apache2
					
				

The httpd.conf file has to be modified. So create a backup of the file.

					
						cp httpd.conf httpd.conf.bak
					
				

Next, few changes to the httpd.conf file has to be done. To open and edit the file with vi editor, type

					
						vi httpd.conf
					
				

Scroll down and find the line

Listen 80

Change it to

Listen 127.0.0.1:80

And few lines below, find the commented line

#ServerName www.example.com:80

Uncomment it, and change the line to

ServerName localhost

Next, find the below two lines

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

and change them to

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

(/dennisgabil/ is just for my computer, please change the two paths to your /username/ ). For inserting text in vi, press i. Save the changes (press ESC, then : (SHIFT + :), then wq!)

Then, uncomment the below lines

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

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

to

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

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

Lastly, inside the Sites directory, execute

					
						sudo chmod +a "_www allow execute" ~
					
				

7) Restart Apache

Restart your server by typing the command

					
						sudo apachectl -k restart
					
				

Now if you have a web project called someproject inside the Sites directory, you can access it by typing localhost/someproject in Safari's URL bar and it will display your default page index.html. If you have a Git repository for your projects, you can clone them into the Sites directory and work on them.