For our Blog Visitor only Get Additional 3 Month Free + 10% OFF on TriAnnual Plan YSBLOG10
Grab the Deal

How to Setup Apache on Linux Server in 2026 – Step-by-step Guide

To set up Apache on a Linux server, install the apache2/httpd package, enable and start the service, allow ports 80/443 in your firewall, create a Virtual Host for your domain, test the configuration for syntax errors, and enable HTTPS with Let’s Encrypt. The steps vary slightly by distribution.

In this beginner-friendly guide on how to setup Apache on Linux server, you’ll learn the exact steps to install, configure, secure, and optimize Apache (httpd) on popular distributions like Ubuntu/Debian and CentOS/RHEL/AlmaLinux/Rocky Linux. As a senior hosting professional, I’ll also share real-world tips, security hardening, and performance tuning to run production-grade websites.

What You’ll Need (Prerequisites)

Before you begin, confirm the following:

  • A Linux server (VPS, cloud, or dedicated) with Ubuntu/Debian or CentOS/RHEL-based distro
  • Root or sudo access
  • A registered domain (optional but recommended)
  • Firewall access to open ports 80 (HTTP) and 443 (HTTPS)
  • Package manager ready: apt for Ubuntu/Debian, dnf/yum for RHEL-based

Apache on Linux: Naming Differences That Matter

Across distributions, Apache uses different package and service names:

  • Ubuntu/Debian: package apache2, service apache2, configs in /etc/apache2/
  • RHEL/CentOS/AlmaLinux/Rocky: package httpd, service httpd, configs in /etc/httpd/

Keep these in mind whenever you run commands or edit configuration files.

Step-by-Step: Install Apache

Ubuntu/Debian

sudo apt update
sudo apt install -y apache2
sudo systemctl enable --now apache2
sudo ufw allow "Apache Full"    # enables 80 and 443
sudo systemctl status apache2
curl -I http://<your_server_ip>

RHEL/CentOS/AlmaLinux/Rocky

sudo dnf install -y httpd
sudo systemctl enable --now httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo systemctl status httpd
curl -I http://<your_server_ip>

If you’re on older CentOS with yum, replace dnf with yum. On minimal RHEL setups, ensure your network and DNS resolve correctly before proceeding.

Know Your Key Apache Files and Directories

  • Ubuntu/Debian: /etc/apache2/apache2.conf, /etc/apache2/ports.conf, /etc/apache2/sites-available/, /etc/apache2/sites-enabled/, logs in /var/log/apache2/
  • RHEL-based: /etc/httpd/conf/httpd.conf, /etc/httpd/conf.d/, logs in /var/log/httpd/

Service management (works on both families):

sudo systemctl status apache2|httpd
sudo systemctl reload apache2|httpd
sudo systemctl restart apache2|httpd

Create Your First Virtual Host (Website)

1) Prepare the document root and permissions

sudo mkdir -p /var/www/example.com/public_html
echo "<h1>Hello from Apache</h1>" | sudo tee /var/www/example.com/public_html/index.html

# Ownership differs by distro:
# Ubuntu/Debian user is "www-data", RHEL-based user is "apache"
# Ubuntu/Debian:
sudo chown -R www-data:www-data /var/www/example.com
# RHEL-based:
# sudo chown -R apache:apache /var/www/example.com

2) Add the Virtual Host configuration

Ubuntu/Debian (sites-available + a2ensite):

sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com/public_html

  <Directory /var/www/example.com/public_html>
    AllowOverride All
    Require all granted
    Options -Indexes
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/example_error.log
  CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>
sudo a2ensite example.com.conf
sudo a2enmod rewrite
sudo apache2ctl configtest
sudo systemctl reload apache2

RHEL/CentOS/AlmaLinux/Rocky (conf.d):

sudo nano /etc/httpd/conf.d/example.com.conf
<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com/public_html

  <Directory /var/www/example.com/public_html>
    AllowOverride All
    Require all granted
    Options -Indexes
  </Directory>

  ErrorLog /var/log/httpd/example_error.log
  CustomLog /var/log/httpd/example_access.log combined
</VirtualHost>
sudo apachectl -t
sudo systemctl reload httpd

If SELinux is enforcing and your document root is outside default paths, set proper contexts:

# RHEL-based SELinux example for a custom docroot:
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?"
sudo restorecon -Rv /var/www/example.com

Enable HTTPS with Let’s Encrypt (Free SSL)

Ubuntu/Debian

sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
# Test auto-renewal
sudo certbot renew --dry-run

RHEL/CentOS/AlmaLinux/Rocky

# Enable EPEL if needed (RHEL/CentOS):
sudo dnf install -y epel-release
sudo dnf install -y certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
sudo certbot renew --dry-run

Certbot adds SSL Virtual Hosts, installs the certificates, and configures HTTP-to-HTTPS redirection. Certificates renew automatically via systemd timers.

Essential Security Hardening

Hide server details and disable listing

# Ubuntu/Debian: edit /etc/apache2/conf-available/security.conf (or apache2.conf)
# RHEL-based: edit /etc/httpd/conf.d/security.conf (create if missing)

ServerTokens Prod
ServerSignature Off

# In each <Directory> or global:
Options -Indexes

Firewall, permissions, and SELinux

  • Keep ports 80/443 open only; block others unless needed.
  • Use least-privilege permissions for web roots. Files: 644, directories: 755.
  • On RHEL, for outbound connections (e.g., to app servers), allow: sudo setsebool -P httpd_can_network_connect 1.

Security modules and HTTPS best practices

  • Consider ModSecurity WAF: Ubuntu sudo apt install libapache2-mod-security2, RHEL sudo dnf install mod_security.
  • Use strong TLS ciphers and enable HSTS when you’re fully on HTTPS.
  • Regularly patch: sudo apt upgrade or sudo dnf upgrade.

Performance Tuning for Real Traffic

Select the right MPM

For most modern sites, the event MPM scales better than prefork. If you run PHP, pair event MPM with PHP-FPM (not mod_php).

# Ubuntu/Debian:
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event proxy_fcgi setenvif
sudo a2enconf php*-fpm   # choose your installed PHP-FPM version
sudo systemctl reload apache2

# RHEL-based:
sudo dnf install -y php-fpm
sudo systemctl enable --now php-fpm
# Example (in your vhost) to pass PHP via FPM socket:
# <FilesMatch "\.php$">
#   SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost/"
# </FilesMatch>

Compression and caching

# Enable modules (Ubuntu/Debian):
sudo a2enmod deflate headers expires brotli
sudo systemctl reload apache2

# Example rules (add inside vhost or a conf file):
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
# If Brotli installed:
# AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css application/javascript application/json

# Caching static assets:
ExpiresActive On
ExpiresByType image/webp "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"

Connection limits and KeepAlive

Tune for your RAM/CPU. As a starting point on a 2–4 GB VPS:

# event MPM example (Ubuntu: /etc/apache2/mods-available/mpm_event.conf)
# RHEL: in /etc/httpd/conf.modules.d/ prefixed file or main httpd.conf
<IfModule mpm_event_module>
   StartServers             2
   MinSpareThreads         25
   MaxSpareThreads         75
   ThreadLimit             64
   ThreadsPerChild         25
   MaxRequestWorkers      150
   MaxConnectionsPerChild   0
</IfModule>

KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

Measure with real traffic and adjust. Always reload after changes and monitor memory usage to prevent swapping.

Logs, Monitoring, and Maintenance

  • Access logs: /var/log/apache2/access.log or /var/log/httpd/access_log
  • Error logs: /var/log/apache2/error.log or /var/log/httpd/error_log
  • Service logs: journalctl -u apache2 or journalctl -u httpd
  • Tail live logs: sudo tail -f /var/log/apache2/error.log
  • Log rotation: handled by logrotate; verify configs in /etc/logrotate.d/

For traffic insights, tools like GoAccess provide real-time reports from access logs without adding overhead to Apache.

Troubleshooting Common Issues

  • Port already in use: sudo ss -tulpn | grep :80 to find conflicts. Stop the other service or change Apache’s Listen port in ports.conf (Ubuntu) or httpd.conf (RHEL).
  • 403 Forbidden: Check file permissions/ownership and Apache <Directory> rules. On SELinux, ensure contexts are correct and use restorecon.
  • 500 Internal Server Error: Inspect the error log for .htaccess syntax or PHP errors. Test with apachectl -t.
  • Changes not applied: Use systemctl reload (graceful) after config edits; verify with apachectl -t.
  • Slow site: Enable compression, caching, switch to event MPM, and use PHP-FPM. Profile the app and database, not just Apache.

Optional: Build a Full LAMP Stack

Adding PHP and a database turns your server into a LAMP stack for WordPress and dynamic apps:

# Ubuntu/Debian:
sudo apt install -y php php-fpm php-mysql mariadb-server

# RHEL-based:
sudo dnf install -y php php-fpm php-mysqlnd mariadb-server
sudo systemctl enable --now mariadb

Prefer PHP-FPM with Apache’s event MPM for better concurrency than mod_php.

When to Use YouStable for Apache Hosting

If you’re running production workloads, a reliable platform matters. YouStable’s SSD-powered VPS and dedicated servers deliver consistent I/O, DDoS protection, IPv6 support, and free Let’s Encrypt SSL. Our engineers can pre-harden Apache, configure PHP-FPM, and set up automated backups—so you focus on your website, not server firefighting.

By following the steps above, you now know how to setup Apache on Linux server from installation to SSL, security, and tuning. With a solid foundation and the right hosting, Apache can power fast, secure, and scalable websites.

FAQs: How to Setup Apache on Linux Server

Is Apache or Nginx better for WordPress on Linux?

Both work well. Apache offers flexible .htaccess and deep module support. Nginx excels at handling static assets and high concurrency. A common production pattern is Nginx as a reverse proxy in front of Apache+PHP-FPM, or Apache alone with event MPM and caching. Choose based on your stack and team expertise.

How do I change Apache’s default port from 80?

Ubuntu/Debian: edit /etc/apache2/ports.conf and VirtualHost to match, e.g., Listen 8080 and <VirtualHost *:8080>. RHEL-based: update /etc/httpd/conf/httpd.conf and vhosts. Reload the service and open the new port in your firewall.

How can I host multiple websites on one Apache server?

Use Virtual Hosts. Create separate document roots and vhost files for each domain. On Ubuntu/Debian, place configs in sites-available and enable with a2ensite. On RHEL-based systems, add one .conf per site in /etc/httpd/conf.d/. Point DNS A/AAAA records to your server’s IP.

How do I auto-renew Let’s Encrypt certificates on Apache?

Certbot sets up a systemd timer by default. Verify with systemctl list-timers | grep certbot and run a dry-run: sudo certbot renew --dry-run. Ensure your firewall allows TCP 80/443 and your domain points to the server so HTTP-01/ALPN can validate.

Where are Apache configuration files on Linux?

Ubuntu/Debian stores configs in /etc/apache2/ (sites in sites-available/sites-enabled) with logs in /var/log/apache2/. RHEL/CentOS/AlmaLinux/Rocky use /etc/httpd/ (vhosts in conf.d) with logs in /var/log/httpd/. The main files are apache2.conf or httpd.conf.

Alok Trivedi

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top