DEV Community

David Jonson
David Jonson

Posted on

Configuring Apache Server in CentOS 7

Image description
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
Updating your system before installing any software is always a good practice. This ensures you have the latest security patches and bug fixes.

bash
Copy code
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.

To install Apache, run:

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

bash
Copy code
httpd -v
You should see an 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:

bash
Copy code
sudo systemctl start httpd
Verify that Apache is running:

bash
Copy code
sudo systemctl status httpd
The output should indicate that the service is active and running.

Enable Apache to start on boot:

bash
Copy code
sudo systemctl enable httpd
Step 4: Configure Firewall to Allow HTTP and HTTPS Traffic
By default, CentOS 7 uses Firewalld to manage firewall rules. It would be best if you allowed HTTP and HTTPS traffic to access your web server.

Check the current firewall state:

bash
Copy code
sudo firewall-cmd --state
If it’s running, proceed to configure it.

Allow HTTP service:

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

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

bash
Copy code
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
Copy code
http://your-server-ip
You should see the default CentOS 7 Apache test page if Apache is installed and running.

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:

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

bash
Copy code
sudo chown -R apache: apache /var/www/example.com
Set the correct permissions:

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

bash
Copy code
sudo nano /etc/httpd/conf.d/example.com.conf
Add the following content to the file:

apache
Copy code

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
Enter fullscreen mode Exit fullscreen mode


Test Apache Configuration Verify that the configuration file is correct:

bash
Copy code
sudo apachectl configtest
If the syntax is correct, you’ll see Syntax OK.

Restart Apache Apply the changes by restarting Apache:

bash
Copy code
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.

Verify SELinux status:

bash
Copy code
status
If SELinux is enforcing, update the file context:

bash
Copy code
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.

Edit your virtual host file:

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

apache
Copy code

AllowOverride All

Restart Apache:

bash
Copy code
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.

Install Certbot:

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

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

Verify SSL configuration by visiting:

Arduino
Copy code
https://example.com
Set up automatic certificate renewal:

bash
Copy code
sudo crontab -e
Add the following line to renew certificates automatically:

bash
Copy code
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
Copy code
sudo systemctl status httpd
Restart/Stop/Start
Restart:
bash
Copy code
sudo systemctl restart httpd
Stop:
bash
Copy code
sudo systemctl stop httpd
Start:
bash
Copy code
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.

Reference

Top comments (0)