Apache HTTP Server is one of the most popular web servers due to its robustness, excellent performance, and a variety of features. Although macOS comes with a built-in version of Apache, many users prefer to remove this version and install the latest one through Homebrew, a package manager for macOS.
This article will guide you through the process of removing the built-in Apache server and installing a new one using Homebrew on macOS.
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: Remove Built-in Apache Server
macOS comes with a pre-installed Apache server. If you wish to install the latest version of Apache, you need to stop and disable this built-in version first.
- Stop the Apache Server: Open your terminal and type the following command to stop the Apache server:
sudo apachectl stop
- Disable the Apache Server: To disable the server from starting up automatically, use the following command:
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
Step 2: Install Apache HTTP Server on macOS
- Update Homebrew: Always update Homebrew before installing a new package to ensure that you’re getting the latest version. To update Homebrew, run:
- Install Apache: After updating Homebrew, you can install Apache by running:
brew install httpd
- Start Apache: Once the installation is complete, you can start Apache by typing:
sudo brew services start httpd
You have successfully installed the Apache webserver via Homebrew, and configured it to auto-start with a privileged account.
- Verify Installation: To verify that Apache was installed correctly, open your web browser and navigate to http://localhost:8080. You should see a message saying “It works!”
Step 3: Configuring the Apache HTTP 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 /use/local/etc/httpd/httpd.conf is generated by the installer which you need to edit in a text editor and make following changes.
- 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 withListen 8080 and change it toListen 80
- 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:
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>
- 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
- 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 4: Create Test HTML Page on Apache
Let’s create a Sites folder in your home directory and create a sample index.html file for testing.
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 5: Managing Apache HTTP Server
Managing your Apache HTTP Server involves starting, stopping, and restarting the service, as well as checking its status. Here’s how you can do it:
- Start Apache Service: If you have stopped the Apache service and want to start it again, use this command:
brew services start httpd
- Stop Apache Service: If you need to stop the Apache service, you can do it by typing:
brew services stop httpd
- Restart Apache Service: In case you have made changes to the configuration file and need to restart the Apache service for changes to take effect, use this command:
brew services restart httpd
- Check Apache Service Status: To check the status of the Apache service, you can use this command:
brew services list
This command will list all services managed by Homebrew along with their status. Look for httpd in this list to check its status.
Remember, every time you make changes to the Apache configuration file, you need to restart the service for changes to take effect.
Step 6: Uninstalling Apache HTTP Server
If you ever need to uninstall Apache HTTP Server installed via Homebrew, you can easily do so by following these steps:
- Stop Apache Service: Before uninstalling, you need to stop the Apache service if it’s currently running. You can do this by typing:
brew services stop httpd
- Uninstall Apache: After stopping the service, you can uninstall Apache by running:
brew uninstall httpd
- Verify Uninstallation: To verify that Apache has been uninstalled, you can try to navigate to http://localhost (or http://localhost:8080 if you didn’t change the default port) in your web browser. You should no longer see the “It works!” message.
Please note that uninstalling Apache via Homebrew won’t automatically delete the configuration files located in /usr/local/etc/httpd/. If you want to completely remove all traces of Apache, you’ll need to manually delete this directory.
Installing Apache HTTP Server on macOS is a straightforward process when using Homebrew. This package manager simplifies the installation and configuration process, making it easy for anyone to set up a local web server.
It’s important to note that while this guide covers a basic installation, Apache is a powerful and complex piece of software that can be configured in a variety of ways to suit your specific needs. Always refer to the official Apache documentation for more detailed information and advanced configuration options.