Let me show you how to set up Apache virtual hosts on Alpine Linux! This lets you run multiple websites on one server. Pretty cool for hosting different sites or testing environments.
๐ค What are Virtual Hosts?
Virtual hosts let Apache serve different websites based on the domain name. Itโs like having multiple apartments in one building - same server, different websites.
Benefits include:
- Host multiple sites on one server
- Separate development and production
- Easy testing environments
- Better organization
- Cost savings
๐ฏ What You Need
Before starting, make sure you have:
- Alpine Linux running
- Root or sudo access
- Domain names (or use fake ones)
- About 15 minutes
๐ Step 1: Install Apache
First, letโs install Apache web server:
# Update packages
apk update
# Install Apache
apk add apache2
# Start Apache
rc-service apache2 start
# Enable at boot
rc-update add apache2
Test if it works:
curl http://localhost
# Should show Apache default page
๐ Step 2: Create Website Directories
Letโs create folders for two websites:
# Create document roots
mkdir -p /var/www/site1.local/public_html
mkdir -p /var/www/site2.local/public_html
# Create log directories
mkdir -p /var/www/site1.local/logs
mkdir -p /var/www/site2.local/logs
# Set permissions
chown -R apache:apache /var/www
๐ Step 3: Create Test Pages
Add content to each site:
# Site 1 index page
cat > /var/www/site1.local/public_html/index.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
<title>Site 1</title>
</head>
<body>
<h1>Welcome to Site 1!</h1>
<p>This is the first virtual host.</p>
</body>
</html>
EOF
# Site 2 index page
cat > /var/www/site2.local/public_html/index.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
<title>Site 2</title>
</head>
<body>
<h1>Welcome to Site 2!</h1>
<p>This is the second virtual host.</p>
</body>
</html>
EOF
๐ Step 4: Configure Virtual Hosts
Create virtual host config files:
# Site 1 configuration
cat > /etc/apache2/conf.d/site1.conf << 'EOF'
<VirtualHost *:80>
ServerName site1.local
ServerAlias www.site1.local
DocumentRoot /var/www/site1.local/public_html
ErrorLog /var/www/site1.local/logs/error.log
CustomLog /var/www/site1.local/logs/access.log combined
<Directory /var/www/site1.local/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
# Site 2 configuration
cat > /etc/apache2/conf.d/site2.conf << 'EOF'
<VirtualHost *:80>
ServerName site2.local
ServerAlias www.site2.local
DocumentRoot /var/www/site2.local/public_html
ErrorLog /var/www/site2.local/logs/error.log
CustomLog /var/www/site2.local/logs/access.log combined
<Directory /var/www/site2.local/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
EOF
๐ Step 5: Test Configuration
Check Apache config and restart:
# Test configuration
apachectl configtest
# If OK, restart Apache
rc-service apache2 restart
# Check if sites loaded
apachectl -S
๐ Step 6: Set Up Local Testing
For testing, add entries to hosts file:
# Edit hosts file
echo "127.0.0.1 site1.local www.site1.local" >> /etc/hosts
echo "127.0.0.1 site2.local www.site2.local" >> /etc/hosts
# Test sites
curl http://site1.local
curl http://site2.local
๐ฎ Practice Exercise
Letโs create a more complex setup:
- Add a third site
- Configure SSL
- Set up redirects
# Create site3
mkdir -p /var/www/site3.local/{public_html,logs}
# Create landing page
cat > /var/www/site3.local/public_html/index.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
<title>Site 3 - Practice</title>
<style>
body { font-family: Arial; text-align: center; padding: 50px; }
.box { background: #f0f0f0; padding: 20px; border-radius: 10px; }
</style>
</head>
<body>
<div class="box">
<h1>Practice Site 3</h1>
<p>You successfully created a third virtual host!</p>
</div>
</body>
</html>
EOF
# Configure it
cat > /etc/apache2/conf.d/site3.conf << 'EOF'
<VirtualHost *:80>
ServerName site3.local
DocumentRoot /var/www/site3.local/public_html
# Redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</VirtualHost>
EOF
# Apply changes
rc-service apache2 reload
๐จ Troubleshooting Common Issues
Sites Not Loading
If virtual hosts donโt work:
# Check Apache error log
tail -f /var/log/apache2/error.log
# Verify listening ports
netstat -tlpn | grep :80
# Check enabled modules
apachectl -M
Permission Denied
Getting 403 errors?
# Fix permissions
chown -R apache:apache /var/www
chmod -R 755 /var/www
# Check directory config
grep -r "Require all" /etc/apache2/
Wrong Site Loading
Default site loading instead?
# Disable default site
mv /etc/apache2/conf.d/default.conf /etc/apache2/conf.d/default.conf.disabled
# Check server names
apachectl -S | grep port
๐ก Pro Tips
Tip 1: Use Include for Common Settings
Create reusable configs:
# Common security settings
cat > /etc/apache2/conf.d/common-security.conf << 'EOF'
# Hide Apache version
ServerTokens Prod
ServerSignature Off
# Security headers
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "SAMEORIGIN"
EOF
Tip 2: Set Up SSL
Add HTTPS support:
# Install SSL module
apk add apache2-ssl
# Generate self-signed cert
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/ssl/private/site1.key \
-out /etc/ssl/certs/site1.crt
Tip 3: Enable Useful Modules
Load helpful Apache modules:
# Enable modules
sed -i 's/#LoadModule rewrite_module/LoadModule rewrite_module/' /etc/apache2/httpd.conf
sed -i 's/#LoadModule headers_module/LoadModule headers_module/' /etc/apache2/httpd.conf
โ Verification Steps
Letโs verify everything works:
# List all virtual hosts
apachectl -S
# Test each site
for site in site1 site2 site3; do
echo "Testing $site.local:"
curl -s http://$site.local | grep -o "<title>.*</title>"
done
# Check logs
ls -la /var/www/*/logs/
๐ What You Learned
Excellent work! You can now:
- โ Install Apache on Alpine
- โ Create virtual hosts
- โ Configure multiple sites
- โ Set up local testing
- โ Troubleshoot issues
Youโre ready to host multiple websites!
๐ฏ Whatโs Next?
Now that you understand virtual hosts, try:
- Setting up SSL certificates
- Configuring .htaccess files
- Adding PHP support
- Setting up reverse proxy
Remember, virtual hosts make web hosting so flexible. I run dozens of sites on single servers this way! Start simple and build up.
Happy hosting! ๐