Logo

How to Install and Configure Apache on CentOS 7

Apr 03, 2024

To install and configure Apache on CentOS 7, follow the steps below.

Step 1 : First, ensure your system is up to date by running:

sudo yum update

Step 2 : Install Apache using the following command:

sudo yum install httpd

Step 3 : If you're using the firewall, allow Apache traffic by running:

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent

Step 4 : Reload the firewall to apply the changes:

sudo firewall-cmd --reload

Step 5 : Start Apache and enable it to start on boot:

sudo systemctl start httpd
sudo systemctl enable httpd

Step 6 : Check Status: Ensure Apache is running without any errors:

sudo systemctl status httpd

Step 7 : Create a directory for your website and an index file:

sudo mkdir /var/www/html/example.com
sudo echo "Welcome to example.com" | sudo tee /var/www/html/example.com/index.html

Step 8 : Create a new virtual host configuration file for example.com:

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

Add the following content to the configuration file:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html/example.com
    ErrorLog /var/log/httpd/example.com_error.log
    CustomLog /var/log/httpd/example.com_access.log combined
</VirtualHost>

Step 9 : Check the configuration for syntax errors:

sudo apachectl configtest

Step 10 : If the test is successful, restart Apache to apply the changes:

sudo systemctl restart httpd

Step 11 : Now, visit your domain (example.com) in a web browser to test your website.

Congratulations! You have successfully installed and configured Apache on CentOS 7.