How to Install Apache on Windows, macOS, and Linux (By OS)
In the previous article, "What is Apache? A Beginner-Friendly Guide to Web Servers and How They Work," we learned about the roles of web servers and Apache. Once you've learned the theory, it's time for practice! In this article, we'll walk you through installing Apache on your own computer so you can experience getting a web server up and running.
We'll provide detailed, step-by-step instructions for each of the major operating systemsโWindows, macOS, and Linuxโto ensure even beginners won't get lost. We'll cover everything from the easy installation method using XAMPP, an integrated web development environment, to the standard installation methods for each OS. We've prepared plenty of commands you can copy and paste, so let's get our hands dirty and launch your very own web server!
[For Beginners] The Quick and Easy Way to Install Apache with XAMPP (Windows & macOS)
If you're thinking, "I just want to get it working without all the complicated stuff!" then XAMPP is definitely the way to go. XAMPP is an incredibly convenient package that installs all the necessary software to run a website in one go.
- X: Cross-platform (works on Windows, macOS, and Linux)
- A: Apache (the web server)
- M: MariaDB (the database)
- P: PHP (the programming language)
- P: Perl (the programming language)
With just this one package, you can do everything from setting up a web server to trying out web application development with a database, all on your local machine. Let's look at a brief overview of how to install XAMPP and start Apache.
- Download from the Official Site: Go to the official Apache Friends website and download the XAMPP installer for your operating system.
- Install: Run the downloaded file and follow the on-screen instructions to proceed with the installation. The default settings should be fine for most cases.
- Launch the XAMPP Control Panel: Once the installation is complete, launch the XAMPP Control Panel.
- Start Apache: From the "Modules" list in the control panel, find "Apache" and click the "Start" button on the right. If the Apache module name turns green, it has started successfully!
And just like that, a web server is now running on your computer. Open your web browser and type `http://localhost` into the address bar. If you see the XAMPP welcome page, the installation was a success.
Any HTML files you want to publish should be placed in the `htdocs` folder inside the XAMPP installation directory. This `htdocs` folder is the root directory for your website.
How to Install Apache Manually on Windows
This method is for those who want to install Apache by itself, without using XAMPP. It gives you an experience closer to setting up a more professional environment. The common way to get Apache binaries (executable files) for Windows is from a site called Apache Lounge.
Note: To run Apache downloaded from Apache Lounge, you may need the Microsoft Visual C++ (VC++) Redistributable package. Follow the instructions on their site to install it beforehand.
Step 1: Install Apache as a Service
Unzip the downloaded file and place it in a location of your choice, like `C:\Apache24`. Next, run Command Prompt as an administrator, navigate to the bin directory with `cd C:\Apache24\bin`, and then use the following command to register Apache as a Windows service.
httpd.exe -k install
Step 2: Start Apache
Once it's registered as a service, use the next command to start Apache. If you see "Apache2.4 service is starting.", it was successful.
httpd.exe -k start
Now, go to `http://localhost` in your browser. If you see a page that says "It works!", then it's running correctly.
Step 3: Stopping and Restarting Apache
To stop Apache, use the following command.
httpd.exe -k stop
If you've changed a configuration file (like `httpd.conf`) and want to restart Apache, this is the command.
httpd.exe -k restart
How to Install Apache on macOS Using Homebrew
On modern versions of macOS, the recommended method is to install Apache using a package manager called Homebrew, rather than using the version that comes pre-installed. Homebrew makes installing and updating software as easy as running a single command.
Step 1: Install Homebrew
If you don't have Homebrew installed yet, open the Terminal and run the following command. (If you already have it, you can skip this step.)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Install Apache with Homebrew
Once Homebrew is ready, use the next command to install Apache (the package is named `httpd`).
brew install httpd
Step 3: Start Apache
After the installation is complete, use the following command to start Apache and configure it to launch automatically when you boot your Mac.
brew services start httpd
By default, Apache installed via Homebrew uses port `8080`. Go to `http://localhost:8080` in your browser. If you see a page that says "It works!", you're all set.
How to Install Apache on Linux Using a Package Manager
Most Linux distributions (flavors) allow you to easily install Apache using the default package manager. Here, we'll cover the two main families: Debian/Ubuntu-based and Red Hat/CentOS-based systems.
For Debian / Ubuntu (using apt)
First, update your package list to the latest version.
sudo apt update
Next, install Apache (the package name is `apache2`).
sudo apt install apache2
Once the installation is complete, Apache will usually start automatically. You can check its status with the following command.
sudo systemctl status apache2
For Red Hat / CentOS / Fedora (using dnf/yum)
Use dnf (or yum on older versions) to install Apache (the package name is `httpd`).
sudo dnf install httpd
After installation, use the following commands to start Apache and enable it to run on boot.
sudo systemctl start httpd
sudo systemctl enable httpd
Firewall Configuration on Linux
On a Linux server, the firewall might be blocking connections. You'll need to open the ports that Apache uses: HTTP (port 80) and HTTPS (port 443).
For Ubuntu (using ufw):
sudo ufw allow 'Apache'
sudo ufw reload
For CentOS (using firewall-cmd):
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Post-Installation Checks and Troubleshooting
Once you've installed it successfully, you should always verify that it's working.
- Check the Document Root: The location where you should place your HTML files (the document root) differs depending on the installation method. For XAMPP, it's `htdocs`; for Linux, it's often `/var/www/html`. Confirm the document root for your environment and place a simple `index.html` file there to test.
- Port Conflicts: If you get a "Port is already in use" error, another application (like Skype) might be using port 80. You'll either need to close that application or change the port number in Apache's configuration file (`httpd.conf`).
- Check the Error Logs: If it won't start, always check the error logs. The log files contain important clues to help you identify the cause of the problem.
Conclusion
Great work! You now have a working web server environment on your computer. Whether you chose the easy method with XAMPP or a standard installation for your OS, you've hopefully experienced the satisfaction of getting it up and running.
Once your web server is running, the next step is to configure it to your liking. In the next article, we'll dive into `httpd.conf`, the heart of Apache's configuration. Let's learn the basics of server administration together, including how to change port numbers and set the document root!