DEV Community

David Jonson
David Jonson

Posted on

How to Configure Apache Server in CentOS 7: Step-by-Step Guide

Image description

Setting up an Apache server on CentOS 7 is a fundamental task for anyone working with Linux-based web servers. Apache HTTP Server, often referred to simply as Apache, is one of the most popular and reliable web servers worldwide. In this article, we’ll walk through configuring an Apache server on CentOS 7 in a clear, step-by-step manner.

Prerequisites
Before we dive into the setup, make sure of the following:

You have a CentOS 7 server with root or sudo privileges.
Your server is connected to the internet.
You have basic knowledge of Linux terminal commands.
Step 1: Update Your System
It's always a good practice to update your system packages to ensure compatibility and security.

Open your terminal and log in as a root user or a user with sudo privileges.

Run the following command to update your system:

sudo yum update -y
This will update all installed packages to their latest versions. Once done, proceed to install Apache.

Step 2: Install Apache on CentOS 7
Apache is available in the default CentOS repository, so installation is straightforward.

To install Apache, use the following command:

sudo yum install httpd -y
Once installed, verify the installation by checking the Apache version:

httpd -v
This should display the installed version of Apache, confirming that it’s installed correctly.

Step 3: Start and Enable Apache Service
After installing Apache, the next step is to start the service and ensure it runs automatically on boot.

To start Apache, run:

sudo systemctl start httpd
Enable Apache to start at boot:

sudo systemctl enable httpd
To check the status of the Apache service, use:

sudo systemctl status httpd
If Apache is running, you’ll see an active status message.

Step 4: Configure Firewall to Allow HTTP and HTTPS Traffic
To make your Apache server accessible to users, you need to configure your firewall to allow HTTP and HTTPS traffic.

To open the HTTP (port 80) and HTTPS (port 443) ports, use the following commands:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
Reload the firewall to apply the changes:

sudo firewall-cmd --reload
To verify that the rules are active, you can list the allowed services:

sudo firewall-cmd --list-all
After completing this step, your Apache server should be accessible from any device through the internet.

Step 5: Test Apache Server
To confirm that Apache is running properly, open a web browser and navigate to your server's IP address. The default CentOS 7 Apache welcome page should appear.

To get your server's IP address, use the following command:

ip addr
Enter the IP address in the address bar of a web browser like this:

http://your_server_ip
If Apache is configured correctly, you should see the Apache test page, confirming that the installation is successful.

Step 6: Basic Apache Configuration
Now that Apache is up and running, it’s time to make some basic configurations to personalize your server settings. Apache's main configuration file is located at /etc/httpd/conf/httpd.conf.

Open the configuration file with a text editor like nano:

sudo nano /etc/httpd/conf/httpd.conf
Here, you can modify various settings, such as the server name, document root, and directory permissions.

Setting the Server Name
Setting the ServerName directive is essential to avoid the "Could not reliably determine the server's fully qualified domain name" error.

In the httpd.conf file, add or modify the ServerName directive. Replace your_domain_or_IP with your actual domain or IP address:

ServerName your_domain_or_IP
Save and close the file by pressing CTRL+X, then Y, and Enter to confirm.

Restart Apache to apply the changes:

sudo systemctl restart httpd
Step 7: Setting Up Virtual Hosts (Optional)
Virtual Hosts allow you to host multiple websites on a single server. Each Virtual Host configuration is stored in a separate file under /etc/httpd/conf.d/.

Create a new configuration file for each domain. For example, to create a Virtual Host for example.com, use:

sudo nano /etc/httpd/conf.d/example.com.conf
Add the following configuration, replacing example.com and the paths with your domain and document root:


ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined

Create the document root directory:

sudo mkdir -p /var/www/example.com
Assign proper permissions to the directory:

sudo chown -R apache:apache /var/www/example.com
Restart Apache to apply the new Virtual Host configuration:

sudo systemctl restart httpd
You should now be able to access example.com in your web browser if DNS is configured correctly.

Step 8: Managing Apache Services
Here are some common commands for managing your Apache service:

Stop Apache:

sudo systemctl stop httpd
Restart Apache:

sudo systemctl restart httpd
Reload Apache (to apply configuration changes without stopping):

sudo systemctl reload httpd
Check Apache status:

sudo systemctl status httpd
These commands help you manage Apache efficiently and troubleshoot if any issues arise.

Step 9: Configuring Apache to Use SSL (Optional)
For secure HTTP traffic, it’s crucial to install SSL. The Let's Encrypt SSL certificate is a free and popular option for securing web traffic.

Install mod_ssl:

sudo yum install mod_ssl -y
Obtain and configure an SSL certificate (e.g., with Certbot, Let’s Encrypt’s recommended client) to enable HTTPS. Detailed SSL setup is beyond this guide, but this step is essential if you need encrypted connections.

Step 10: Monitor Apache Logs
Monitoring logs helps you troubleshoot issues and improve server performance.

Access Logs:

/var/log/httpd/access_log
Error Logs:

/var/log/httpd/error_log
You can use the tail command to view logs in real-time:

tail -f /var/log/httpd/access_log

Enjoy !!!

Get in touch
or give some ♥ on mail.

Top comments (0)