Configuring Apache Server in CentOS 7

Configuring Apache Server in CentOS 7

Share this Post to earn Money ( Upto ₹100 per 1000 Views )


Configuring Apache Server in CentOS 7

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.

bash
sudo yum update -y

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.

  1. To install Apache, run:

    bash
    sudo yum install httpd -y
  2. Once the installation is complete, verify the Apache version:

    bash
    httpd -v

    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.

  1. Start the Apache service:

    bash
    sudo systemctl start httpd
  2. Verify that Apache is running:

    bash
    sudo systemctl status httpd

    The output should indicate that the service is active and running.

  3. Enable Apache to start on boot:

    bash
    sudo systemctl enable httpd

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.

  1. Check the current firewall state:

    bash
    sudo firewall-cmd --state

    If it’s running, proceed to configure it.

  2. Allow HTTP service:

    bash
    sudo firewall-cmd --permanent --add-service=http
  3. Allow HTTPS service (if SSL is required):

    bash
    sudo firewall-cmd --permanent --add-service=https
  4. Reload the firewall to apply changes:

    bash
    sudo firewall-cmd --reload

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:

arduino
http://your-server-ip

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.

  1. Create a Directory for Your Website Let’s assume your domain is example.com. Create a directory to store your website’s files:

    bash
    sudo mkdir -p /var/www/example.com
  2. Set Permissions Assign ownership of the directory to the Apache user:

    bash
    sudo chown -R apache:apache /var/www/example.com

    Set the correct permissions:

    bash
    sudo chmod -R 755 /var/www/example.com
  3. Create a Virtual Host Configuration File Create a configuration file for your site:

    bash
    sudo nano /etc/httpd/conf.d/example.com.conf

    Add the following content to the file:

    apache
    <VirtualHost *:80> ServerAdmin admin@example.com DocumentRoot /var/www/example.com ServerName example.com ServerAlias www.example.com <Directory /var/www/example.com> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined </VirtualHost>
  4. Test Apache Configuration Verify that the configuration file is correct:

    bash
    sudo apachectl configtest

    If the syntax is correct, you’ll see Syntax OK.

  5. Restart Apache Apply the changes by restarting Apache:

    bash
    sudo systemctl restart httpd

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.

  1. Verify SELinux status:

    bash
    sestatus
  2. If SELinux is enforcing, update the file context:

    bash
    sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?" sudo restorecon -R /var/www/example.com

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.

  1. Edit your virtual host file:

    bash
    sudo nano /etc/httpd/conf.d/example.com.conf
  2. Confirm the AllowOverride All directive is set:

    apache
    <Directory /var/www/example.com> AllowOverride All </Directory>
  3. Restart Apache:

    bash
    sudo systemctl restart httpd

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.

  1. Install Certbot:

    bash
    sudo yum install epel-release -y sudo yum install certbot python-certbot-apache -y
  2. Obtain an SSL certificate:

    bash
    sudo certbot --apache -d example.com -d www.example.com
  3. Follow the prompts to configure SSL automatically.

  4. Verify SSL configuration by visiting:

    arduino
    https://example.com
  5. Set up automatic certificate renewal:

    bash
    sudo crontab -e

    Add the following line to renew certificates automatically:

    bash
    0 0 * * * certbot renew --quiet

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:

bash
sudo systemctl status httpd

Restart/Stop/Start

  • Restart:
    bash
    sudo systemctl restart httpd
  • Stop:
    bash
    sudo systemctl stop httpd
  • Start:
    bash
    sudo systemctl start httpd

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.