🖥️ Apache HTTP Server (httpd) Documentation
🌟 Overview
Apache HTTP Server, commonly referred to as Apache or httpd, is an open-source web server software that is highly customizable and widely used. It can serve static content, dynamic web pages, and integrate with various modules to extend its capabilities.
📦 Installation
Ubuntu/Debian
-
Update the package list:
sudo apt update
-
Install Apache:
sudo apt install apache2 -y
-
Start and enable Apache service:
sudo systemctl start apache2 sudo systemctl enable apache2
CentOS/RHEL
-
Update the package list:
sudo yum update -y
-
Install Apache:
sudo yum install httpd -y
-
Start and enable Apache service:
sudo systemctl start httpd sudo systemctl enable httpd
🔧 Basic Configuration
Main Configuration File
- Location:
/etc/apache2/apache2.conf
(Ubuntu/Debian),/etc/httpd/conf/httpd.conf
(CentOS/RHEL)
Virtual Hosts
Virtual hosts allow you to run multiple websites on a single server.
Sample Configuration
-
Create a configuration file:
-
Ubuntu/Debian:
sudo nano /etc/apache2/sites-available/example.com.conf
-
CentOS/RHEL:
sudo nano /etc/httpd/conf.d/example.com.conf
-
-
Add the following content:
<VirtualHost *:80> ServerAdmin [email protected] ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
-
Enable the virtual host configuration:
-
Ubuntu/Debian:
sudo a2ensite example.com.conf sudo systemctl reload apache2
-
CentOS/RHEL:
sudo systemctl restart httpd
-
-
Create the Document Root:
sudo mkdir -p /var/www/example.com sudo chown -R $USER:$USER /var/www/example.com
-
Add a sample index file:
echo "<html><body><h1>Welcome to example.com!</h1></body></html>" | sudo tee /var/www/example.com/index.html
🛠️ Common Commands
Managing Apache Service
-
Start Apache:
sudo systemctl start apache2 # Ubuntu/Debian sudo systemctl start httpd # CentOS/RHEL
-
Stop Apache:
sudo systemctl stop apache2 # Ubuntu/Debian sudo systemctl stop httpd # CentOS/RHEL
-
Restart Apache:
sudo systemctl restart apache2 # Ubuntu/Debian sudo systemctl restart httpd # CentOS/RHEL
-
Reload Apache (for configuration changes):
sudo systemctl reload apache2 # Ubuntu/Debian sudo systemctl reload httpd # CentOS/RHEL
-
Check Apache status:
sudo systemctl status apache2 # Ubuntu/Debian sudo systemctl status httpd # CentOS/RHEL
Enable/Disable Modules (Ubuntu/Debian)
-
Enable a module:
sudo a2enmod module_name sudo systemctl restart apache2
-
Disable a module:
sudo a2dismod module_name sudo systemctl restart apache2
Logs
- Access log:
/var/log/apache2/access.log
(Ubuntu/Debian),/var/log/httpd/access_log
(CentOS/RHEL) - Error log:
/var/log/apache2/error.log
(Ubuntu/Debian),/var/log/httpd/error_log
(CentOS/RHEL)
🔍 Additional Configuration
Enabling SSL (HTTPS)
-
Install OpenSSL:
-
Ubuntu/Debian:
sudo apt install openssl
-
CentOS/RHEL:
sudo yum install mod_ssl
-
-
Generate a self-signed certificate:
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
-
Configure SSL in the virtual host file:
<VirtualHost *:443> ServerAdmin [email protected] ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> </VirtualHost>
-
Enable SSL module and site:
-
Ubuntu/Debian:
sudo a2enmod ssl sudo a2ensite default-ssl sudo systemctl reload apache2
-
CentOS/RHEL:
sudo systemctl restart httpd
-
🏁 Conclusion
Apache HTTP Server is a versatile and powerful tool for serving web content. By following this guide, you can set up and configure Apache on your system, create virtual hosts, and enable SSL for secure communication. Use the commands provided to manage your Apache server effectively.