Configuring Apache Server in CentOS 7
Configuring Apache Server in CentOS 7
Share this Post to earn Money ( Upto ₹100 per 1000 Views )
Apache HTTP Server, commonly referred to as Apache, is one of the most popular web servers in the world. It is open-source, robust, and highly customizable, making it a preferred choice for hosting websites and web applications. In this guide, we will explore the step-by-step process to configure Apache on CentOS 7. By the end of this article, you will have a fully functional Apache server running on your CentOS 7 system.
Prerequisites
Before we start, ensure you have the following:
- A CentOS 7 server with root or sudo access.
- A basic understanding of command-line operations.
- An active internet connection to download packages.
Step 1: Update Your System
It is always a good practice to update your system before installing any software. This ensures you have the latest security patches and bug fixes.
This command will synchronize your system with the latest repositories.
Step 2: Install Apache HTTP Server
Apache is available in CentOS’s default YUM repositories, which makes installation straightforward.
-
To install Apache, run:
-
Once the installation is complete, verify the Apache version:
You should see output indicating the version of Apache installed, e.g.,
Apache/2.4.6
.
Step 3: Start and Enable Apache Service
After installation, you need to start the Apache service and enable it to start automatically on boot.
-
Start the Apache service:
-
Verify that Apache is running:
The output should indicate that the service is active and running.
-
Enable Apache to start on boot:
Step 4: Configure Firewall to Allow HTTP and HTTPS Traffic
By default, CentOS 7 uses firewalld to manage firewall rules. You need to allow HTTP and HTTPS traffic to access your web server.
-
Check the current firewall state:
If it’s running, proceed to configure it.
-
Allow HTTP service:
-
Allow HTTPS service (if SSL is required):
-
Reload the firewall to apply changes:
Step 5: Test Apache Installation
To confirm Apache is working, you can open a web browser and visit your server's IP address. For example:
If Apache is installed and running, you should see the default CentOS 7 Apache test page.
Step 6: Configure Virtual Hosts
Virtual hosts allow you to host multiple websites on a single server. This is useful for shared hosting environments.
-
Create a Directory for Your Website Let’s assume your domain is
example.com
. Create a directory to store your website’s files: -
Set Permissions Assign ownership of the directory to the Apache user:
Set the correct permissions:
-
Create a Virtual Host Configuration File Create a configuration file for your site:
Add the following content to the file:
-
Test Apache Configuration Verify that the configuration file is correct:
If the syntax is correct, you’ll see
Syntax OK
. -
Restart Apache Apply the changes by restarting Apache:
Step 7: Configure SELinux (If Enabled)
Security-Enhanced Linux (SELinux) might block Apache from accessing your website’s directory. To avoid this, configure the appropriate SELinux rules.
-
Verify SELinux status:
-
If SELinux is enforcing, update the file context:
Step 8: Enable .htaccess (Optional)
The .htaccess
file is used for per-directory configuration, such as enabling URL rewriting. To use .htaccess
, ensure AllowOverride All
is set in your virtual host configuration.
-
Edit your virtual host file:
-
Confirm the
AllowOverride All
directive is set: -
Restart Apache:
Step 9: Configure SSL for Secure Connections (Optional)
For HTTPS support, you need an SSL certificate. Let’s configure a free SSL certificate from Let’s Encrypt using Certbot.
-
Install Certbot:
-
Obtain an SSL certificate:
-
Follow the prompts to configure SSL automatically.
-
Verify SSL configuration by visiting:
-
Set up automatic certificate renewal:
Add the following line to renew certificates automatically:
Step 10: Monitor and Manage Apache
Logs
Monitor Apache logs for errors and access information:
- Error log:
/var/log/httpd/error_log
- Access log:
/var/log/httpd/access_log
Status
Check Apache status:
Restart/Stop/Start
- Restart:
- Stop:
- Start:
Conclusion
You have successfully configured an Apache web server on CentOS 7. This guide covered the installation, basic configuration, virtual hosts, SSL setup, and firewall adjustments. With this setup, you are ready to host websites or web applications. For advanced configurations, refer to the official Apache documentation and experiment with modules and performance tuning.