Apache 101 on OSX

By | November 8, 2020

Did you know that you have the Apache web server already installed on your Mac? To test, run this command:

seans-MacBook-Pro:~ sean$ apachectl -v
Server version: Apache/2.4.33 (Unix)
Server built:   Jan 18 2020 09:52:26
seans-MacBook-Pro:~ sean$

Great. So you have proved that you have Apache. Now what? Let’s practise starting and stopping it. To start it, the command is apachectl start, but if you are not a root user, you’ll get the error down below.

seans-MacBook-Pro:Documents sean$ apachectl start
This operation requires root.

As root:

seans-MacBook-Pro:Documents sean$ sudo apachectl start
Password:
seans-MacBook-Pro:Documents sean$

This is great, but how do you actually know the web server is running? There are several ways. The first option is that you can visit “localhost” in your web browser. If you have an index.html file here, it will be displayed. Then you can stop the server and an error should appear. But there is a better way.

Running the command ps aux | grep httpd will show the following:

seans-MacBook-Pro:Documents sean$ ps aux | grep httpd
sean              2609   0.0  0.0  4267792    852 s001  S+    2:48pm   0:00.00 grep httpd
_www              2607   0.0  0.0  4318236   1176   ??  S     2:48pm   0:00.00 /usr/sbin/httpd -D FOREGROUND
root              2606   0.0  0.0  4309020   6392   ??  Ss    2:48pm   0:00.14 /usr/sbin/httpd -D FOREGROUND

Or you could open Activity Monitor and filter on httpd.

Where is the web server directory? It can be located at: /Library/WebServer/Documents. What this means is that if you place your index.html or php file, it will show up when you visit localhost.

Category: OSX

Leave a Reply

Your email address will not be published. Required fields are marked *