How to Configure Apache Server in CentOS 7: A Step-by-Step Guide
How to Configure Apache Server in CentOS 7: A Step-by-Step Guide
Apache HTTP Server, commonly known as Apache, is one of the most popular and widely used web servers in the world. It is open-source, highly customizable, and compatible with various platforms, including CentOS 7. In this guide, we will go through the step-by-step process of configuring Apache on CentOS 7, from installation to fine-tuning for performance and security.
This tutorial is designed for beginners and intermediate users who want to set up a reliable web server environment using Apache on CentOS 7.
Prerequisites
Before diving into the configuration steps, ensure that you have the following:
- A CentOS 7 server with root or sudo privileges.
- Basic knowledge of the Linux command line.
- Internet access to download Apache and related packages.
Step 1: Update Your System
The first step in configuring an Apache server is to ensure that your system packages are up to date. This helps to avoid compatibility issues and ensures that the server software is secure and stable.
Run the following command to update all packages:
This command will check for any available package updates and install them on your CentOS 7 server.
Step 2: Install Apache on CentOS 7
This command will download and install Apache, along with any required dependencies. Once the installation is complete, you can confirm that Apache is installed by checking its version:
You should see output indicating the installed version of Apache.
Step 3: Start and Enable Apache
After installation, you need to start Apache and enable it to run at system startup.
To start Apache, run:
To enable Apache to start on boot, use:
You can verify that Apache is running by checking its status:
If everything is configured correctly, you should see that the service is “active” and “running.”
Step 4: Configure Firewall to Allow HTTP and HTTPS Traffic
By default, CentOS 7 uses firewalld
as its firewall management tool. You’ll need to configure it to allow traffic on the HTTP (port 80) and HTTPS (port 443) ports so that users can access your Apache server.
To allow HTTP traffic, run:
To allow HTTPS traffic, run:
After making these changes, reload the firewall for them to take effect:
You can check that the ports are open by running:
This will display a list of open services, which should include http
and https
.
Step 5: Test Apache Web Server
Now that Apache is installed and running, you can verify that it is working by accessing the default Apache welcome page.
Open a web browser and enter your server’s IP address:
If Apache is correctly installed, you should see the Apache CentOS 7 default welcome page, which indicates that Apache is running successfully.
Step 6: Configure Virtual Hosts (Optional but Recommended)
Apache’s virtual hosting feature allows you to host multiple websites on a single server. Each website can have its own configuration files, root directories, and domain names.
Creating a Directory for the Website
First, create a directory to hold your website’s files. For this example, let’s create a directory for example.com
:
Then set the correct permissions for the directory:
Creating a Virtual Host Configuration File
Next, create a new configuration file for your virtual host in the Apache configuration directory:
Inside this file, add the following configuration, replacing example.com
with your domain name and the path to your website directory:
Save and close the file.
Create a Test HTML File
To test if the virtual host is working, create an HTML file in your website’s root directory:
Restart Apache
After configuring your virtual host, restart Apache to apply the changes:
Now, if you access http://example.com
(replace with your actual domain or IP), you should see your test HTML page.
Step 7: Enable and Configure SSL (Optional for Secure Connections)
To enable HTTPS on your server, you’ll need to install an SSL certificate. One way to do this is with Let’s Encrypt, a free certificate authority. You can install the Certbot tool to manage SSL certificates.
Install Certbot:
Request a certificate for your domain:
Certbot will ask for your email address and other information. After completing the prompts, Certbot will obtain and install an SSL certificate.
Auto-Renewing the SSL Certificate
Let’s Encrypt certificates expire every 90 days, but you can automate renewal with a cron job:
This job will check daily for any certificates that need renewal.
Step 8: Basic Security and Optimization
Securing and optimizing Apache is crucial for performance and protection against vulnerabilities.
Disable Directory Listing
To prevent users from viewing the contents of directories that don’t have an index.html
file, disable directory listing by editing the Apache configuration file.
Add the following line to the default Apache configuration or the virtual host file:
Hide Apache Version and OS Information
Exposing the Apache version and OS information can make your server more vulnerable to attacks. To hide this information, edit /etc/httpd/conf/httpd.conf
and add these lines: https://blog.oudel.com/how-to-configure-apache-server-in-centos-7-step-by-step-guide/
Restart Apache for the changes to take effect:
Step 9: Troubleshooting Common Apache Issues
- Permission Errors: If you encounter
403 Forbidden
errors, ensure your document root and directories have the proper permissions and ownership. - Syntax Errors: Run
apachectl configtest
to check for syntax errors in your configuration files. - Apache Won’t Start: Check logs in
/var/log/httpd/
for clues.
Conclusion
Setting up an Apache server on CentOS 7 is straightforward and provides a reliable platform for web hosting. With the steps outlined above, you can install, configure, and secure your Apache server. Additionally, virtual hosting allows you to manage multiple sites, while SSL setup helps secure your connections. Regular maintenance, updates, and monitoring will keep your server running smoothly and securely.
Firewall tips
https://www.rootusers.com/how-to-open-a-port-in-centos-7-with-firewalld/
List port - https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
Comments
Post a Comment