How to set up Apache Virtual Host on CentOS 7

Apr 03, 2024

To set up Apache Virtual Host on CentOS 7, follow the steps below.

Step 1 : Ensure your system is up to date by running:

sudo yum update

Step 2 : If Apache is not already installed, install it using the following command:

sudo yum install httpd

Step 3 : Create a directory to store your website files:

sudo mkdir /var/www/example.com

Step 4 : Create a simple index.html file for testing purposes:

sudo echo "Welcome to your Virtual Host" | sudo tee /var/www/example.com/index.html

Step 5 : Set appropriate permissions for the web directory:

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

Step 6 : Create a new virtual host configuration file for your domain:

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

Add the following content to the configuration file:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    DocumentRoot /var/www/example.com
    ErrorLog /var/log/httpd/example_error.log
    CustomLog /var/log/httpd/example_access.log combined

Step 7 : Check the configuration for syntax errors and restart Apache:

sudo apachectl configtest
sudo systemctl restart httpd

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

Congratulations! You have successfully set up Apache Virtual Host on CentOS 7.