How To Configure Apache Server In CentOS 7

How To Configure Apache Server In CentOS 7? Step-by-Step Guide

Configuring Apache servers in CentOS 7 involves a few straightforward steps. Apache, or HTTPD as it’s sometimes called on CentOS, is a widely-used open-source web server that allows you to host and serve websites. This guide will walk you through configure Apache server in centOS 7, setting up a virtual host, testing configurations, and implementing basic security practices.

How To Configure Apache Server In CentOS 7 Step-by-Step Guide

1. Update Your System

To ensure you’re starting with the latest software, update your system packages:

bash Copy code
sudo yum update -y

This will download and install any available updates for CentOS 7, ensuring system stability and security.

2. Install Apache HTTPD

CentOS has the Apache HTTP server available in its default YUM repositories. Install it with the following command:

bash Copy code
sudo yum install httpd -y

After installation, you can verify the Apache version to ensure it’s installed:

bash Copy code
httpd -v

3. Start and Enable Apache

To have Apache start automatically on boot, enable it as a system service:

bash Copy code
sudo systemctl start httpd
sudo systemctl enable httpd

To verify that Apache is running, use:

bash Copy code
sudo systemctl status httpd

This command will show the status, confirming that Apache is active.

4. Configure the Firewall

By default, CentOS uses the firewalld firewall. To allow web traffic on ports 80 (HTTP) and 443 (HTTPS), add the following rules:

bash Copy code
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

This opens the necessary ports and reloads the firewall settings to apply the changes.

5. Verify Apache Installation

To verify that Apache is working, open a web browser and navigate to your server’s IP address. If you see the default CentOS Apache welcome page, the server is correctly installed. For example, if your server IP is 192.168.1.100, enter:

arduino Copy code
http://192.168.1.100

This page confirms that Apache is installed and running.

6. Configure Apache to Serve Websites

Apache uses a concept called “virtual hosts” to allow multiple websites to be hosted on a single server. Each virtual host has its configuration, allowing unique domains to share server resources.

Create a Directory for Your Website
First, create a directory for your website files. In this example, we’ll use /var/www/html/yourwebsite:

bash Copy code
sudo mkdir -p /var/www/html/yourwebsite

Then, set permissions and ownership:

bash Copy code
sudo chown -R apache:apache /var/www/html/yourwebsite
sudo chmod -R 755 /var/www/html

Create a Virtual Host Configuration

Apache’s virtual host files are typically stored in /etc/httpd/conf.d/. Create a new configuration file for your site (e.g., yourwebsite.conf):

bash Copy code
sudo nano /etc/httpd/conf.d/yourwebsite.conf

In this file, add the following configuration, adjusting the ServerName, ServerAlias, and document root as necessary:

apache Copy code
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName yourwebsite.com
ServerAlias www.yourwebsite.com
DocumentRoot /var/www/html/yourwebsite
ErrorLog /var/log/httpd/yourwebsite-error.log
CustomLog /var/log/httpd/yourwebsite-access.log combined
</VirtualHost>

This configuration tells Apache to listen for requests to yourwebsite.com and serve files from /var/www/html/yourwebsite.

Create a Test Page

To verify that your virtual host is working, create an easy test HTML file:

bash   Copy code
echo "<h1>Welcome to YourWebsite.com!</h1>" | sudo tee /var/www/html/yourwebsite/index.html

7. Restart Apache

For any changes to take effect, you’ll need to restart Apache:

bash Copy code
sudo systemctl restart httpd

8. Test Your Virtual Host

Open a web browser and navigate to http://yourwebsite.com (or your IP address if the domain not yet configured). You should see the message you added to index.html, which indicates your virtual host is configured correctly.

9. Secure Apache with Basic Settings

Securing your Apache server helps protect your website from unauthorized access and improves performance.

Hide Apache Version and OS Identity
Edit the Apache configuration file:

bash Copy code
sudo nano /etc/httpd/conf/httpd.conf

Add or modify the following lines:

apache   Copy code
ServerTokens Prod
ServerSignature Off

These settings prevent Apache from displaying version information on error pages, which can help mitigate information leaks.

Disable Directory Listings

By default, Apache may screens a directory listing if there’s no index file. To prevent this, add the Options -Indexes directive in your configuration:

Set Up a Basic Firewall Rule

You’ve already enabled HTTP and HTTPS in firewalld, but it’s also wise to limit access to sensitive parts of your server, like SSH (port 22) if you haven’t already configured it.

apache Copy code
<Directory "/var/www/html">
Options -Indexes
</Directory>

10. Enable SSL for Secure Connections (Optional but Recommended)

To secure your site with HTTPS, you’ll need to install an SSL certificate. You can get a free Secure Sockets Layer certificate from Let’s Encrypt. Install the Certbot client to manage this method:

bash Copy code
sudo yum install certbot python-certbot-apache -y

Then, obtain and install an SSL certificate:

bash Copy code
sudo certbot --apache -d yourwebsite.com -d www.yourwebsite.com

This command automatically configures Apache for SSL and reloads the configuration.

11. Regular Maintenance and Updates

Keeping Apache updated is essential for security. To update Apache on CentOS 7, periodically run:

bash Copy code
sudo yum update httpd

After an update, always restart the Apache service:

bash Copy code
sudo systemctl restart httpd

Conclusion

Configuring an Apache server on CentOS 7 involves installation, setting up virtual hosts, enabling the firewall, and basic security practices. With these steps, your server is ready to serve websites and customized further for performance and security. Following a regular maintenance schedule and applying security best practices can help keep your server running smoothly and securely.

Scroll to Top