How would I restart Apache using the Terminal in Mac OS X?

This is what I’ve done but I’m really new to terminal:

Jeremys-MacBook-Pro-2:~ jeremyoconnor$ apachectl restart

This operation requires root.

Solution 1:

Since Apache runs on a reserved port (80) that is considered a sensitive system level port you would have to run apachectl restart via sudo like this:

sudo apachectl -k restart

After typing that in it will as you for your password. Assuming you have admin privileges, just type in the password and Apache should restart. Other commands include start and stop that can be run like this to start Apache:

sudo apachectl -k start

And like this to stop Apache:

sudo apachectl -k stop

Also, if you just type in apachectl without anything you will receive a list of options and directives you can use with the apachectl command; this is my output from Mac OS X 10.9.5:

Usage: /usr/sbin/httpd [-D name] [-d directory] [-f file]
                       [-C "directive"] [-c "directive"]
                       [-k start|restart|graceful|graceful-stop|stop]
                       [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S]
Options:
  -D name            : define a name for use in <IfDefine name> directives
  -d directory       : specify an alternate initial ServerRoot
  -f file            : specify an alternate ServerConfigFile
  -C "directive"     : process directive before reading config files
  -c "directive"     : process directive after reading config files
  -e level           : show startup errors of level (see LogLevel)
  -E file            : log startup errors to file
  -v                 : show version number
  -V                 : show compile settings
  -h                 : list available command line options (this page)
  -l                 : list compiled in modules
  -L                 : list available configuration directives
  -t -D DUMP_VHOSTS  : show parsed settings (currently only vhost settings)
  -S                 : a synonym for -t -D DUMP_VHOSTS
  -t -D DUMP_MODULES : show all loaded modules 
  -M                 : a synonym for -t -D DUMP_MODULES
  -t                 : run syntax check for config files
  -T                 : start without DocumentRoot(s) check