This guide covers Debian 13/Ubuntu and RHEL 10 setups.
To set up an Apache2 web server on a Linux system, follow these steps:
Make sure your package lists are up-to-date.
sudo apt update
sudo dnf update
You can install Apache2 using the following command:
sudo apt install apache2 -y
sudo dnf install httpd -y
sudo ufw allow 'Apache'
Verify the status of the firewall:
sudo ufw status
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
You can start, stop, or restart the Apache2 service with the following commands:
Start Apache2:
sudo systemctl start apache2
Stop Apache2:
sudo systemctl stop apache2
Restart Apache2:
sudo systemctl restart apache2
Enable Apache2 to start at boot:
sudo systemctl enable apache2
Check the status of the service:
sudo systemctl status apache2
Start httpd:
sudo systemctl start httpd
Stop httpd:
sudo systemctl stop httpd
Restart httpd:
sudo systemctl restart httpd
Enable httpd to start at boot:
sudo systemctl enable httpd
Check the status of the service:
sudo systemctl status httpd
Once Apache2 is running, you can test if it is working by visiting your server’s IP address in a browser. For example:
http://your-server-ip
You should see the Apache2 default welcome page.
To configure Apache, you can modify files in the /etc/apache2/ directory.
The main configuration file is:
/etc/apache2/apache2.conf
On RHEL:
/etc/httpd/conf/httpd.conf
Virtual hosts are stored in:
/etc/apache2/sites-available/
To edit or create a new virtual host, copy the default configuration:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/your-site.conf
Then, edit your-site.conf with a text editor:
sudo nano /etc/apache2/sites-available/your-site.conf
<VirtualHost *:80>
ServerAdmin webmaster@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/your-site
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the virtual host:
sudo a2ensite your-site.conf
Disable the default site if needed:
sudo a2dissite 000-default.conf
Reload Apache to apply changes:
sudo systemctl reload apache2
Ensure the correct permissions for your web root directory:
sudo chown -R www-data:www-data /var/www/your-site
sudo chmod -R 755 /var/www/your-site
You may need to enable additional modules for Apache. For example, to enable mod_rewrite:
sudo a2enmod rewrite
sudo systemctl restart apache2
See Apache2 Webserver Security for a focused security checklist.
See Apache Hardening.
See Apache Configuration.
Do you need help or support? Feel free to contact us!
Prefer automation? See Apache HTTP Server Ansible Setup for an example playbook.
Prefer containers? See Apache HTTP Server Docker Setup.
Setting up web servers can be complex. We offer consulting services for:
Contact us at office@linux-server-admin.com or visit our contact page.