Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»MacOS»How to Install Apache Web Server on MacOS

    How to Install Apache Web Server on MacOS

    RahulBy RahulFebruary 11, 20184 Mins ReadUpdated:May 21, 2022

    The latest versions of macOS come with pre-installed Apache 2.4. But it’s hard to manage it via homebrew due to Apple has removed some of the required scrips from the latest macOS releases. This tutorial will help you to remove the default installed Apache from the system and install the Homebrew version of Apache on your system.

    Prerequisites

    Before starting the installation of the Apache (httpd) server using this tutorial you must have the following prerequisites.

    • Terminal: You must have Mac Terminal access and little knowledge about working with the terminal application. Ao login to your Mac system and open terminal
    • Homebrew: Homebrew is a popular package manager for the Mac operating systems. It is useful for installing most open source software like Node. Homebrew installation tutorial

    Step 1 – Installing Apache on macOS

    Remove built-in Apache server (if any) from your system. Open a terminal and execute commands to stop running Apache server and remove it.

    sudo apachectl stop 
    sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 
    

    Now install the new version Apache server provided by Homebrew:

    brew install httpd 
    

    After completing the installation process, configure the httpd service to be auto-started on system boot.

    sudo brew services start httpd 
    

    You have successfully installed the Apache webserver via Homebrew, and configured it to auto-start with a privileged account.

    You can try to reach your server in a browser by pointing it at http://localhost:8080, you should see a simple header that says “It works!”

    Installing Apache on MacOS
    Apache default page on macOS

    Step 2 – Configuring the Apache Server

    The Apache web server is running now on your macOS system. Now you will want to make some configuration changes according to your local development environment. A configuration file /usr/local/etc/httpd/httpd.conf is generated by the installer which you need to edit in a text editor and make following changes.

    1. Set Apache Port:

      The Homebrew’s version of httpd uses port 8080. You have to manually change the listen port from the default of 8080 to the standard port 80.

      Find the line with  
      Listen 8080
      
      and change it to  
      Listen 80
      
    2. Change Document Root:

      Now, configure the document root for Apache. The default document root is set to “/usr/local/var/www”. You can keep the document root unchanged and put your website files to this document root. But I assume you want to change the document root to set it to your home directory.

      Change this to point to your user directory where your_user is the name of your user account:

       DocumentRoot /Users/rahul/Sites
      

      You also need to change the tag configured just right below the DocumentRoot line. This should also be changed to point to your new document root as well:

      In that same block you will find an AllowOverride option, Set this to all to enable the uses of the .htaccess file in Apache.

      <Directory /Users/rahul/Sites>
         [...]
         #
         # AllowOverride controls what directives may be placed in .htaccess files.
         # It can be "All", "None", or any combination of the keywords:
         #   AllowOverride FileInfo AuthConfig Limit
         #
         AllowOverride all 
      </Directory>
      
    3. Enabling Rewrite Module:

      Also, you should enable the mod_rewrite module by removing the leading # symbol from the following line. Search the line and update it. this will enable URL rewrite on Apache.

       LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
      
    4. Setup User and Group for Apache:

      As you have configured the Apache document root to your home directory. You will face issues with the permissions because, by default, Apache runs as the user daemon and group daemon (Maybe username and group are to _www). For the personal systems, You can change these to match your user account (replace user_name with your real username), with a group of staff.

        User user_name
        Group staff
      

    Step 3 – Deploy Sample Application on Apache

    Let’s create a Sites folder in your home directory and create a sample index.html file for the testing.

    mkdir ~/Sites 
    echo "Welcome" > ~/Sites/index.html 
    

    Restart apache to ensure your configuration changes have taken effect:

    sudo apachectl -k restart 
    

    Now, Point your browser to http://localhost, This should display your newly created index.html. All done.

    Step 4 – Manage Apache Service

    At this point, you have successfully installed the Apache server on macOS. The below-mentioned commands will help you to stop, start, and restart the Apache service at any time.

    • Stop the Apache Server:
      sudo apachectl stop 
      
    • Start the Apache Server:
      sudo apachectl start 
      
    • Restart the Apache Server:
      sudo apachectl restart 
      

    Conclusion

    This tutorial helped you to install and configure the Apache webserver on the macOS system.

    Apache Apache 2.4 macOS
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Latest Node.js on MacOS
    Next Article How to Install VirtualBox Extension Pack

    Related Posts

    How To Install Apache Solr 9.0 on Fedora 36/35

    Updated:May 26, 20223 Mins Read

    How to Install Apache ActiveMQ on Ubuntu 22.04

    3 Mins Read

    How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

    Updated:June 28, 20225 Mins Read

    How To Disable HTTP Methods in Apache

    Updated:December 31, 20212 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    Common Apache Commands on Ubuntu & Debian

    4 Mins Read

    23 Comments

    1. Tony on March 10, 2022 11:43 am

      The only one that worked for me and I’ve trawled the net for two days.
      But can you explain/address the comments about continuing to use apachectl after killing the native apache httpd process? Though it works I don’t understand what is linked to what now. I have brew versions [email protected] but before I ran the ‘sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist’ command the localhost kept reporting v7.8.3 in spite of linking the brew version. Can’t find any tutorials that explain the process properly.

      Reply
    2. Saed Yousef on January 21, 2022 10:56 pm

      Man you’re messing up two services. Just stick with brew, use ‘brew services start/stop/restart httpd`

      Reply
    3. Jorge on January 20, 2022 5:03 pm

      Thank you! that worked perfectly to me.

      Reply
    4. Dav Iru on June 28, 2021 1:45 pm

      Why do you say to run `sudo apachectl -k restart` after starting brew services httpd?! Those are two different services. Trying to start apachectl will try to start a separate app that you’ve already stopped and removed from plist.

      You’re not supposed to run both if all you need is the brew service.

      Reply
    5. Dav Iru on June 28, 2021 1:42 pm

      Why do you run `sudo brew services start httpd`? That changes ownership on /usr files:

      “`
      % sudo brew services start httpd
      Password:
      Sorry, try again.
      Password:
      Warning: Taking root:admin ownership of some httpd paths:
      /usr/local/Cellar/httpd/2.4.48/bin
      /usr/local/Cellar/httpd/2.4.48/bin/httpd
      /usr/local/opt/httpd
      /usr/local/opt/httpd/bin
      /usr/local/var/homebrew/linked/httpd
      This will require manual removal of these paths using `sudo rm` on
      brew upgrade/reinstall/uninstall.
      Warning: httpd must be run as non-root to start at user login!
      ==> Successfully started `httpd` (label: homebrew.mxcl.httpd)
      “`

      You say that running the command will “configure httpd service to be auto-started on system boot”, but brew says it’ll do the exact opposite.

      “You have successfully installed Apache web server via Homebrew, and configured it to auto-start with a privileged account.”

      What’s the point of starting it with a privileged account? I’m just using it for testing local repo changes. I don’t need https or anything like that.

      Since I noticed the folders’ owners were changed to `root` (unlike all other folders in the directory), I stopped the service and changed ownership back to my user.

      “`
      % sudo chown -R sydcon /usr/local/Cellar/httpd/2.4.48/bin\
      /usr/local/Cellar/httpd/2.4.48/bin/httpd\
      /usr/local/opt/httpd\
      /usr/local/opt/httpd/bin\
      /usr/local/var/homebrew/linked/httpd\
      “`

      Other tutorials I’ve seen show the command without sudo. I don’t know why sudo is needed or recommended here. You don’t mention what Apache needs privileged access for.

      Reply
    6. Virendra Kesarwani on June 25, 2021 3:04 pm

      Thanks Rahul, Its Very Helpful for me,

      Reply
    7. Mete Demirtas on May 18, 2021 2:06 pm

      if ‘sudo apachectl -k restart’ command give an error, run this command : ‘sudo /usr/sbin/apachectl -k restart’

      Reply
      • Rahul on June 30, 2021 4:46 am

        Thanks Mete, for the useful details.

        Reply
    8. simona on April 10, 2021 8:50 am

      thank you

      Reply
    9. Hildo on February 16, 2020 3:09 am

      Thanks a lot ! This help me so much !

      Reply
    10. Alessandro Trebbi on September 19, 2019 3:50 pm

      I have configured apache but master process starts as user _www as well (should start as root), so that it can’t reads certificates in /etc/letsencrypt/live/ that are private for root ; is there a way to start correctly master process ? I’m running Mac OS server too and and that’s correctly configured

      apache brew:
      _www 68719 0,0 0,0 4488604 1808 ?? S Lun11pm 0:00.12 /usr/local/opt/httpd/bin/httpd -k start
      _www 68642 0,0 0,0 4488604 1788 ?? S Lun11pm 0:00.08 /usr/local/opt/httpd/bin/httpd -k start
      _www 68591 0,0 0,0 4488604 1700 ?? S Lun11pm 0:00.06 /usr/local/opt/httpd/bin/httpd -k start
      _www 68042 0,0 0,0 4488604 1796 ?? S Lun11pm 0:00.10 /usr/local/opt/httpd/bin/httpd -k start
      _www 67725 0,0 0,0 4488604 1792 ?? S Lun11pm 0:00.11 /usr/local/opt/httpd/bin/httpd -k start
      _www 53225 0,0 0,0 4488604 1784 ?? S Dom03pm 0:00.13 /usr/local/opt/httpd/bin/httpd -k start
      _www 49737 0,0 0,0 4488604 1788 ?? S Dom07am 0:00.14 /usr/local/opt/httpd/bin/httpd -k start
      _www 42715 0,0 0,0 4488676 1768 ?? S Sab05pm 0:00.13 /usr/local/opt/httpd/bin/httpd -k start
      _www 37396 0,0 0,0 4488676 1792 ?? S Sab04am 0:00.15 /usr/local/opt/httpd/bin/httpd -k start
      _www 24048 0,0 0,0 4488676 1788 ?? S Gio11pm 0:00.14 /usr/local/opt/httpd/bin/httpd -k start

      Mac OS server (note process with lower pid as root):
      _www 17919 0,0 0,0 4299468 1492 ?? S Gio10pm 0:00.09 /usr/sbin/httpd …
      _www 17918 0,0 0,0 4299468 1488 ?? S Gio10pm 0:00.74 /usr/sbin/httpd …
      _www 17915 0,0 0,0 4299468 1356 ?? S Gio10pm 0:00.30 /usr/sbin/httpd …
      _www 4585 0,0 0,0 4299468 1356 ?? S 11Set19 0:00.10 /usr/sbin/httpd …
      _www 4584 0,0 0,0 4299468 1356 ?? S 11Set19 0:00.09 /usr/sbin/httpd …
      _www 4583 0,0 0,0 4299468 1356 ?? S 11Set19 0:00.28 /usr/sbin/httpd …
      _www 4582 0,0 0,0 4299468 1516 ?? S 11Set19 0:00.51 /usr/sbin/httpd …
      _www 4581 0,0 0,0 4299468 1340 ?? S 11Set19 0:00.64 /usr/sbin/httpd …
      root 4555 0,0 0,0 4299492 5844 ?? Ss 11Set19 0:26.49 /usr/sbin/httpd …

      Reply
    11. Damian Lluch on September 15, 2019 12:12 pm

      Thanks bro!

      Reply
    12. Alexey on August 15, 2019 1:09 am

      How to install the mpm_itk module for latest version of Apache on Mac?

      Reply
    13. Dominique DEBERT on August 1, 2019 4:11 pm

      You saved my life, thx !

      Reply
    14. Mario on July 5, 2019 12:00 pm

      Thank you and good job!

      Reply
    15. rei on May 2, 2019 8:44 am

      awesome! It Works!

      Reply
    16. Noor on March 31, 2019 2:52 am

      Thanks Rahul.

      Reply
    17. Anthony on March 26, 2019 2:46 am

      Rahul you are the man!!! Thank you!!!

      Reply
    18. Mario Silva on February 11, 2019 3:36 am

      worked perfect many thanks

      Reply
    19. JP on February 10, 2019 6:19 am

      Worked up until the last part. Do NOT use ‘/’ , use ‘\’. It worked for me. Also just typing in localhost worked as well. Other than that, well done

      Reply
    20. Candy on February 6, 2019 6:32 pm

      Thank you! This worked perfectly.

      Reply
    21. David on February 1, 2019 11:13 pm

      Very clear and it works!
      Thank you Rahul

      Reply
    22. xin jie on January 19, 2019 3:31 pm

      thanks very much

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.