Apache on Localhost
Apache HTTP Server is the world's most popular web server, powering millions of websites. It's included in XAMPP, MAMP, and WAMP, making it the default choice for PHP development on localhost.
Apache default page:
http://localhost
Apache in Local Dev Stacks
| Stack | Platform | Apache Location |
|---|---|---|
| XAMPP | Windows/Mac/Linux | C:\xampp\apache\ |
| MAMP | Mac/Windows | /Applications/MAMP/conf/apache/ |
| WAMP | Windows | C:\wamp64\bin\apache\ |
| Laragon | Windows | C:\laragon\bin\apache\ |
Install Apache (Standalone)
Ubuntu/Debian
sudo apt update
sudo apt install apache2
sudo systemctl start apache2
sudo systemctl enable apache2
# Check status
sudo systemctl status apache2
macOS (Homebrew)
brew install httpd
brew services start httpd
Key Configuration Files
| File | Purpose |
|---|---|
| httpd.conf | Main configuration |
| apache2.conf | Main config (Ubuntu) |
| .htaccess | Per-directory overrides |
| sites-available/ | Virtual host configs |
| sites-enabled/ | Active virtual hosts |
Enable mod_rewrite
Required for WordPress, Laravel, and most PHP frameworks:
# Ubuntu/Debian
sudo a2enmod rewrite
sudo systemctl restart apache2
# XAMPP (httpd.conf)
# Uncomment this line:
LoadModule rewrite_module modules/mod_rewrite.so
# Also change AllowOverride None to:
AllowOverride All
Virtual Hosts
# /etc/apache2/sites-available/mysite.conf
<VirtualHost *:80>
ServerName mysite.local
DocumentRoot /var/www/mysite
<Directory /var/www/mysite>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
</VirtualHost>
# Enable site
sudo a2ensite mysite.conf
sudo systemctl reload apache2
# Add to /etc/hosts
127.0.0.1 mysite.local
.htaccess Examples
# WordPress/Laravel pretty URLs
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Custom error pages
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
Common Commands
# Start/stop/restart
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2
# XAMPP (Windows)
xampp-control.exe # Use GUI
# Test configuration
sudo apachectl configtest
sudo apache2ctl -t
# View error logs
sudo tail -f /var/log/apache2/error.log
Troubleshooting
| Problem | Solution |
|---|---|
| Port 80 in use | Stop Nginx/IIS/Skype or change port |
| 403 Forbidden | Check Require all granted in config |
| .htaccess not working | Enable mod_rewrite, set AllowOverride All |
| 500 Internal Server Error | Check error.log for details |