Installing Apache HTTP Server on Alpine Linux: Complete Web Server Setup Guide
I’ll show you how to install and configure Apache HTTP Server on Alpine Linux. This creates a reliable web server that can host websites, web applications, and serve static content.
Introduction
Apache is one of the most popular web servers in the world, and it runs great on Alpine Linux. I’ve been using this setup for years to host everything from simple websites to complex web applications.
The main reason I choose Apache is its flexibility and extensive module system. You can configure it for almost any web hosting scenario, from basic static sites to complex dynamic applications with PHP, Python, or other languages.
Why You Need This
- Host websites and web applications reliably
- Serve static content with excellent performance
- Support multiple domains with virtual hosts
- Enable SSL/HTTPS for secure connections
Prerequisites
You’ll need these things first:
- Alpine Linux system with root access
- Basic knowledge of Linux command line
- Domain name or IP address for testing
- Internet connection for package downloads
- Understanding of basic web server concepts
Step 1: Install Apache HTTP Server
Installing Apache Package
I’ll start by installing Apache and essential modules from Alpine’s package repository.
What we’re doing: Installing Apache web server and commonly used modules.
# Update package index
sudo apk update
# Install Apache HTTP Server
sudo apk add apache2
# Install common Apache modules
sudo apk add apache2-ssl apache2-utils apache2-dev
Code explanation:
apache2
: Main Apache web server packageapache2-ssl
: SSL/TLS support for HTTPS connectionsapache2-utils
: Additional utilities for Apache managementapache2-dev
: Development headers for compiling modules
Expected Output:
(1/4) Installing apache2 (2.4.57-r0)
(2/4) Installing apache2-ssl (2.4.57-r0)
(3/4) Installing apache2-utils (2.4.57-r0)
(4/4) Installing apache2-dev (2.4.57-r0)
OK: 125 MiB in 87 packages
Verifying Installation
What we’re doing: Checking that Apache installed correctly and understanding the file structure.
# Check Apache version
httpd -v
# List Apache configuration files
ls -la /etc/apache2/
# Check Apache binary location
which httpd
Code explanation:
httpd -v
: Shows Apache version and compile-time settings/etc/apache2/
: Main Apache configuration directorywhich httpd
: Shows where Apache binary is installed
Expected Output:
Server version: Apache/2.4.57 (Unix)
Server built: May 1 2023 10:30:15
Step 2: Configure Basic Apache Settings
Main Configuration File
What we’re doing: Setting up basic Apache configuration for Alpine Linux environment.
# Backup original configuration
sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.backup
# Edit main configuration
sudo nano /etc/apache2/httpd.conf
Key Configuration Changes:
# Server root directory
ServerRoot /etc/apache2
# Listen on port 80 for HTTP
Listen 80
# Set server name (replace with your domain)
ServerName localhost:80
# Document root where web files are stored
DocumentRoot "/var/www/localhost/htdocs"
# Directory permissions
<Directory "/var/www/localhost/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
# Load essential modules
LoadModule dir_module modules/mod_dir.so
LoadModule mime_module modules/mod_mime.so
LoadModule rewrite_module modules/mod_rewrite.so
Configuration explanation:
ServerRoot
: Base directory for Apache configurationListen 80
: Makes Apache listen on HTTP port 80ServerName
: Sets the server’s hostnameDocumentRoot
: Directory where website files are storedDirectory
block: Sets permissions for document root
Setting Up Web Directory
What we’re doing: Creating the web directory structure and setting proper permissions.
# Create web directory
sudo mkdir -p /var/www/localhost/htdocs
# Set ownership to Apache user
sudo chown -R apache:apache /var/www/localhost/
# Set proper permissions
sudo chmod -R 755 /var/www/localhost/htdocs/
# Create a test page
echo "<h1>Welcome to Alpine Linux Apache Server</h1>" | sudo tee /var/www/localhost/htdocs/index.html
Code explanation:
mkdir -p
: Creates directory structure if it doesn’t existchown -R apache:apache
: Sets Apache user as owner recursivelychmod -R 755
: Sets read/write for owner, read for otherstee /var/www/localhost/htdocs/index.html
: Creates test webpage
Step 3: Start and Enable Apache Service
Starting Apache Service
What we’re doing: Starting Apache and configuring it to start automatically at boot.
# Start Apache service
sudo service apache2 start
# Check Apache status
sudo service apache2 status
# Enable Apache to start at boot
sudo rc-update add apache2 default
Code explanation:
service apache2 start
: Starts Apache web serverservice apache2 status
: Shows current service statusrc-update add apache2 default
: Adds Apache to default runlevel for auto-start
Expected Output:
* Starting apache2...
* apache2: started
* service started
Testing Apache Installation
What we’re doing: Verifying that Apache is running and serving web pages correctly.
# Test locally
curl http://localhost
# Check if Apache is listening on port 80
netstat -tlnp | grep :80
# Test from another machine (replace with your IP)
curl http://192.168.1.100
Code explanation:
curl http://localhost
: Tests web server locallynetstat -tlnp | grep :80
: Verifies Apache is listening on port 80- External IP test confirms remote access works
Tip: If you can’t access from other machines, check your firewall settings.
Step 4: Configure Virtual Hosts
Setting Up Virtual Hosts Directory
What we’re doing: Creating structure for hosting multiple websites on one Apache server.
# Create virtual hosts configuration directory
sudo mkdir -p /etc/apache2/sites-available
sudo mkdir -p /etc/apache2/sites-enabled
# Create virtual host for example.com
sudo nano /etc/apache2/sites-available/example.com.conf
Virtual Host Configuration:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/apache2/example.com_error.log
CustomLog /var/log/apache2/example.com_access.log combined
</VirtualHost>
Virtual host explanation:
ServerName
: Primary domain name for this siteServerAlias
: Alternative domain names (like www.example.com)DocumentRoot
: Directory containing website filesAllowOverride All
: Enables .htaccess filesErrorLog
/CustomLog
: Separate log files for this site
Creating Website Directory
What we’re doing: Setting up directory structure and content for the virtual host.
# Create website directory
sudo mkdir -p /var/www/example.com/public_html
# Set proper ownership
sudo chown -R apache:apache /var/www/example.com/
# Create sample website
cat > /tmp/index.html <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Example.com</title>
</head>
<body>
<h1>Example.com Website</h1>
<p>This site is hosted on Alpine Linux with Apache.</p>
</body>
</html>
EOF
sudo mv /tmp/index.html /var/www/example.com/public_html/
Code explanation:
- Creates complete directory structure for website
- Sets Apache as owner for security
- Creates proper HTML page for testing
Enabling Virtual Host
What we’re doing: Activating the virtual host and updating Apache configuration.
# Enable virtual host
sudo ln -s /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-enabled/
# Add virtual host loading to main config
echo "IncludeOptional sites-enabled/*.conf" | sudo tee -a /etc/apache2/httpd.conf
# Test configuration
sudo httpd -t
# Reload Apache
sudo service apache2 reload
Code explanation:
ln -s
: Creates symbolic link to enable siteIncludeOptional
: Loads all enabled site configurationshttpd -t
: Tests configuration for syntax errorsservice apache2 reload
: Applies configuration changes
Step 5: Enable SSL/HTTPS Support
SSL Module Configuration
What we’re doing: Enabling SSL support for secure HTTPS connections.
# Load SSL module
echo "LoadModule ssl_module modules/mod_ssl.so" | sudo tee -a /etc/apache2/httpd.conf
# Add SSL configuration
echo "Include /etc/apache2/extra/httpd-ssl.conf" | sudo tee -a /etc/apache2/httpd.conf
# Create SSL certificate directory
sudo mkdir -p /etc/apache2/ssl
Code explanation:
mod_ssl.so
: Apache module for SSL/TLS supporthttpd-ssl.conf
: Contains SSL-specific configuration/etc/apache2/ssl
: Directory for SSL certificates
Creating Self-Signed Certificate
What we’re doing: Generating SSL certificate for testing HTTPS functionality.
# Generate private key
sudo openssl genrsa -out /etc/apache2/ssl/apache.key 2048
# Create certificate signing request
sudo openssl req -new -key /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.csr
# Generate self-signed certificate
sudo openssl x509 -req -days 365 -in /etc/apache2/ssl/apache.csr -signkey /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
# Set proper permissions
sudo chmod 600 /etc/apache2/ssl/apache.key
sudo chmod 644 /etc/apache2/ssl/apache.crt
Code explanation:
genrsa -out
: Creates 2048-bit RSA private keyreq -new
: Creates certificate signing requestx509 -req
: Generates self-signed certificate valid for 365 days- Permission settings secure private key and allow certificate reading
SSL Virtual Host
What we’re doing: Creating HTTPS version of virtual host with SSL configuration.
# Create SSL virtual host
sudo nano /etc/apache2/sites-available/example.com-ssl.conf
SSL Virtual Host Configuration:
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
<Directory /var/www/example.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/apache2/example.com_ssl_error.log
CustomLog /var/log/apache2/example.com_ssl_access.log combined
</VirtualHost>
Practical Examples
Example 1: PHP Support
What we’re doing: Adding PHP support for dynamic web applications.
# Install PHP and Apache PHP module
sudo apk add php81 php81-apache2
# Add PHP module to Apache
echo "LoadModule php_module modules/mod_php.so" | sudo tee -a /etc/apache2/httpd.conf
# Configure PHP file handling
cat >> /tmp/php.conf <<EOF
<IfModule mod_php.c>
DirectoryIndex index.php index.html
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
</IfModule>
EOF
sudo mv /tmp/php.conf /etc/apache2/conf.d/php.conf
# Create PHP test file
echo "<?php phpinfo(); ?>" | sudo tee /var/www/localhost/htdocs/info.php
# Restart Apache
sudo service apache2 restart
Code explanation:
- Installs PHP 8.1 and Apache integration
- Configures Apache to process .php files
- Creates PHP info page for testing
Example 2: URL Rewriting
What we’re doing: Enabling clean URLs and URL rewriting functionality.
# Enable rewrite module (if not already enabled)
echo "LoadModule rewrite_module modules/mod_rewrite.so" | sudo tee -a /etc/apache2/httpd.conf
# Create .htaccess example
cat > /tmp/.htaccess <<EOF
RewriteEngine On
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Clean URLs - remove .html extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
EOF
sudo mv /tmp/.htaccess /var/www/example.com/public_html/
Code explanation:
- Enables mod_rewrite for URL manipulation
- Forces HTTPS redirects for security
- Removes .html extensions from URLs
Troubleshooting
Apache Won’t Start
Problem: Apache service fails to start Solution: Check configuration and log files
# Test configuration syntax
sudo httpd -t
# Check Apache error logs
sudo tail -f /var/log/apache2/error.log
# Check if port 80 is already in use
sudo netstat -tlnp | grep :80
Permission Denied Errors
Problem: 403 Forbidden errors when accessing websites Solution: Fix file permissions and ownership
# Set correct ownership
sudo chown -R apache:apache /var/www/
# Set proper permissions
sudo find /var/www/ -type d -exec chmod 755 {} \;
sudo find /var/www/ -type f -exec chmod 644 {} \;
# Check SELinux if enabled
sudo setsebool -P httpd_can_network_connect 1
SSL Certificate Issues
Problem: SSL/HTTPS not working properly Solution: Verify certificate configuration
# Test SSL configuration
sudo openssl s_client -connect localhost:443
# Verify certificate details
sudo openssl x509 -in /etc/apache2/ssl/apache.crt -text -noout
# Check SSL module loading
httpd -M | grep ssl
Best Practices
-
Security Hardening:
# Hide Apache version echo "ServerTokens Prod" | sudo tee -a /etc/apache2/httpd.conf echo "ServerSignature Off" | sudo tee -a /etc/apache2/httpd.conf
-
Performance Optimization:
- Enable compression with mod_deflate
- Configure caching headers
- Optimize worker processes for your hardware
-
Regular Maintenance:
- Monitor log files regularly
- Keep Apache and modules updated
- Backup configurations before changes
Verification
To verify Apache installation and configuration:
# Check Apache is running
sudo service apache2 status
# Test HTTP access
curl -I http://localhost
# Test HTTPS access (if SSL configured)
curl -I -k https://localhost
# Check loaded modules
httpd -M | head -10
Expected Output:
HTTP/1.1 200 OK
Date: Mon, 26 May 2025 12:00:00 GMT
Server: Apache
Content-Type: text/html
Wrapping Up
You just learned how to:
- Install Apache HTTP Server on Alpine Linux
- Configure virtual hosts for multiple websites
- Enable SSL/HTTPS for secure connections
- Set up PHP support for dynamic content
That’s it! You now have a fully functional Apache web server on Alpine Linux. This setup handles everything from simple static websites to complex web applications. I use this configuration for production servers all the time.