Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Install Apache on CentOS: Step-by-Step

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 sudo privileges.
  • 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

  1. Update your system
    Run sudo dnf update (or sudo yum update on CentOS 7) to refresh the package index and install any pending updates. This helps avoid dependency conflicts.
  2. Install Apache
    Execute sudo dnf install httpd (or sudo yum install httpd). Confirm the installation when prompted. On CentOS 7, the package name is httpd as well.
  3. Start and enable Apache
    Run sudo systemctl start httpd to start the service now, and sudo systemctl enable httpd to make it start automatically at boot. You can check status with sudo systemctl status httpd.
  4. 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.
  5. Test the server
    Open a web browser and visit http://your_server_ip (or http://localhost if on the server). You should see the Apache test page, confirming the installation.
  6. Configure the document root (optional but recommended)
    The default document root is /var/www/html. To create a custom site, create a directory like sudo mkdir -p /var/www/example.com and set ownership: sudo chown -R $USER:$USER /var/www/example.com. Then create a simple index.html inside it.
  7. 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 with sudo apachectl configtest and reload with sudo systemctl reload httpd.
  8. 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, use semanage fcontext for a persistent rule.
  9. 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 yum on CentOS 8+ – While yum is aliased to dnf, it's better to use dnf for consistency.

Tips

  • Use sudo systemctl restart httpd after 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 httpd to 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 httpd to see the error. Common causes: syntax error in config (fix and run apachectl configtest), port 80 already in use (check with sudo netstat -tulpn | grep :80), or missing dependencies (reinstall with sudo dnf reinstall httpd).
  • 403 Forbidden – This often means SELinux is blocking access to the document root. Check with ls -Z and apply the correct context as shown in Step 8. Also ensure the directory has at least 755 permissions.
  • 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 DocumentRoot contains an index.html. Also check that the virtual host file has a .conf extension 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.