How to start or stop apache web server in macOS using command line?

In Mac OS the apache web server is installed by default. Many of us don’t know how to start and stop apache web server from the command line in Apple OS X (macOS) UNIX operating systems:

To control the apache web server in macOS apachectl is the main command. When we edit the Apache config file /etc/apache2/httpd.conf, we need to restart the apache web server.

To start or stop the apache web server open the terminal application and type the following commands:

Stop Apache web server

$ sudo apachectl stop
   OR
$ sudo apachectl -k stop

Start Apache web server

$ sudo apachectl start
   OR
$ sudo apachectl -k start

Restart Apache web server

$ sudo apachectl restart
   OR
$ sudo apachectl -k restart

Configuration file syntax test

$ sudo apachectl configtest