Setting Up an Apache Web Server on Red Hat Linux
This comprehensive guide will walk you through the process of setting up and running an Apache web server on Red Hat Linux. We will cover the installation, configuration of common features like virtual hosts, and discuss important security settings.
Step 1: Installing Apache HTTP Server
First, you need to install the Apache HTTP Server package. You can do this using the following command:
sudo yum install httpd
Step 2: Starting and Enabling Apache
Once the installation is complete, you can start the Apache service and enable it to run at startup:
sudo systemctl start httpd
sudo systemctl enable httpd
Step 3: Configuring Apache
The main configuration file for Apache is located at /etc/httpd/conf/httpd.conf. You can open this file using your favorite text editor to modify the configuration settings:
sudo vi /etc/httpd/conf/httpd.conf
Virtual Hosts Configuration
Virtual hosts allow you to serve multiple websites using a single Apache server. To set up virtual hosts, follow these steps:
- Create a directory for each website's document root. For example:
- Set the proper permissions for each directory:
- Create and edit the virtual host configuration file for each website:
- Add the following virtual host configuration in each file, adjusting the values accordingly:
sudo mkdir -p /var/www/site1.example.com/public_html
sudo mkdir -p /var/www/site2.example.com/public_html
sudo chown -R apache:apache /var/www/site1.example.com/public_html
sudo chown -R apache:apache /var/www/site2.example.com/public_html
sudo vi /etc/httpd/conf.d/site1.example.com.conf
sudo vi /etc/httpd/conf.d/site2.example.com.conf
<VirtualHost *:80>
ServerAdmin admin@site1.example.com
DocumentRoot /var/www/site1.example.com/public_html
ServerName site1.example.com
ServerAlias www.site1.example.com
ErrorLog /var/log/httpd/site1.example.com-error.log
CustomLog /var/log/httpd/site1.example.com-access.log combined
</VirtualHost>
After creating the virtual host files, restart Apache for the changes to take effect:
sudo systemctl restart httpd
Step 4: Configuring Security Settings
Firewall Configuration
Allow incoming HTTP and HTTPS connections through the firewall:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
Secure Sockets Layer (SSL) Configuration
For SSL configuration, you need to install the mod_ssl module and obtain an SSL certificate:
sudo yum install mod_ssl
Once the module is installed, you can configure SSL settings in the /etc/httpd/conf.d/ssl.conf file.
Securing the Server Configuration
Some security best practices include disabling directory indexes, disabling server signature, and hiding server tokens. Add or modify these lines in the /etc/httpd/conf/httpd.conf file:
Options -Indexes
ServerSignature Off
ServerTokens Prod
Restart Apache for the changes to take effect:
sudo systemctl restart httpd