How to Install Apache on CentOS: Step-by-Step
In this article
This guide walks you through installing the Apache HTTP Server (httpd) on CentOS, from initial package installation to enabling the service and configuring a basic virtual host. By the end, you'll have a working web server that can serve pages to your local network or the internet, ready for further customization.
What You'll Need
- A CentOS system (7, 8, or 9) with a user account that has
sudoprivileges. - Access to a terminal (local or SSH).
- An internet connection to download packages.
- If you plan to host a public site, a domain name pointed to your server's IP address (optional but recommended).
Step-by-Step
- Update your system
Runsudo dnf update(orsudo yum updateon CentOS 7) to refresh the package index and install any pending updates. This helps avoid dependency conflicts. - Install Apache
Executesudo dnf install httpd(orsudo yum install httpd). Confirm the installation when prompted. On CentOS 7, the package name ishttpdas well. - Start and enable Apache
Runsudo systemctl start httpdto start the service now, andsudo systemctl enable httpdto make it start automatically at boot. You can check status withsudo systemctl status httpd. - Adjust the firewall
If you have firewalld running (default on CentOS), allow HTTP and HTTPS traffic:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
If you use iptables instead, add rules for ports 80 and 443. - Test the server
Open a web browser and visithttp://your_server_ip(orhttp://localhostif on the server). You should see the Apache test page, confirming the installation. - Configure the document root (optional but recommended)
The default document root is/var/www/html. To create a custom site, create a directory likesudo mkdir -p /var/www/example.comand set ownership:sudo chown -R $USER:$USER /var/www/example.com. Then create a simpleindex.htmlinside it. - Create a virtual host (optional)
In/etc/httpd/conf.d/, create a new file, e.g.,example.com.conf, with content like:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com ErrorLog /var/log/httpd/example_error.log CustomLog /var/log/httpd/example_access.log combined </VirtualHost>
Then test the config withsudo apachectl configtestand reload withsudo systemctl reload httpd. - Secure with SELinux (if enabled)
If you used a non-default document root, you may need to set the correct SELinux context:sudo chcon -R -t httpd_sys_content_t /var/www/example.com. Alternatively, usesemanage fcontextfor a persistent rule. - Test your site
Visit your domain or IP again. You should see your custom page instead of the default test page.
Common Mistakes
- Forgot to enable the service – If you don't run
systemctl enable httpd, Apache won't start after a reboot. - Firewall blocking – Not opening ports 80/443 is the most common reason the server appears down from outside.
- SELinux blocking access – Using a custom document root without adjusting the SELinux type can cause 403 Forbidden errors.
- Typo in virtual host config – A missing bracket or wrong path can prevent Apache from starting. Always run
apachectl configtest. - Using
yumon CentOS 8+ – Whileyumis aliased todnf, it's better to usednffor consistency.
Tips
- Use
sudo systemctl restart httpdafter making major config changes to ensure they take effect. - Check logs in
/var/log/httpd/(error_log and access_log) when troubleshooting. - For production, consider setting up HTTPS with Let's Encrypt (certbot) after the base install.
- Keep Apache updated with
sudo dnf update httpdto get security patches. - If you plan to host multiple sites, use separate virtual host files for clarity.
Troubleshooting
- Apache won't start – Run
sudo systemctl status httpdto see the error. Common causes: syntax error in config (fix and runapachectl configtest), port 80 already in use (check withsudo netstat -tulpn| grep :80), or missing dependencies (reinstall withsudo dnf reinstall httpd). - 403 Forbidden – This often means SELinux is blocking access to the document root. Check with
ls -Zand apply the correct context as shown in Step 8. Also ensure the directory has at least755permissions. - Server reachable locally but not externally – Verify firewall rules and that the server has a public IP. Also check your cloud provider's security group settings if applicable.
- Default test page still shows – If you created a virtual host, make sure it's enabled and that the
DocumentRootcontains anindex.html. Also check that the virtual host file has a.confextension and is in/etc/httpd/conf.d/. - Apache is slow – Check memory usage and review the prefork/worker MPM settings in
/etc/httpd/conf/httpd.conf. For high traffic, consider tuning or using a more performant MPM.
Frequently Asked Questions
Q: How do I uninstall Apache?
A: Run sudo dnf remove httpd (or yum remove httpd). This will stop and remove the service, but your document root files remain.
Q: Can I install Apache on CentOS 7 using the same commands?
A: Yes, but use yum instead of dnf. The package name is still httpd.
Q: How do I change the default port from 80 to something else?
A: Edit /etc/httpd/conf/httpd.conf and change the Listen directive to your desired port (e.g., Listen 8080). Then update your firewall and virtual host ports accordingly.
Q: How do I enable HTTPS on Apache?
A: Install mod_ssl with sudo dnf install mod_ssl, then configure a virtual host with port 443 and provide SSL certificate files. For a free certificate, use Let's Encrypt's certbot.
Q: Where are Apache configuration files located?
A: The main config is /etc/httpd/conf/httpd.conf. Additional configs are in /etc/httpd/conf.d/ and module configs in /etc/httpd/conf.modules.d/.
Q: How do I set up multiple websites on one server?
A: Use name-based virtual hosts. Create a separate .conf file in /etc/httpd/conf.d/ for each site, each with its own ServerName and DocumentRoot.