Setting up VirtualHost in Centos 7

Posted December 11, 2015 by  ‐ 2 min read

This is gonna be a quick and dirty post about how to properly setup VirtualHost in Centos 7. I mainly made this as part of the Let’s encrypt setup. So, yeah let’s get started

First of all, you obviously need to install the LAMP stack in your server. Go HERE

Create the directory structure

We need a specific directory structure so that we can easily manage the VirtualHosts. Here, I’ll be using the structure /var/www/ as the document root for and so on

# Let us create the directory
mkdir -p /var/www/

# Here "" is the domain name I'm gonna be using in this example
# replace it with your desired domain name

# Give the ownership of the directories to Apache
chown -R apache. /var/www/

# Set the permissions properly
chmod -R 755 /var/www

Now let us create a “Hello World” index file. This is just to make sure that our new virtualhost works.

echo 'hello world' > /vaw/www/

Make sure that you replace with your domain name.

Configuring the VirtualHost

# Create a directory to hold the virtualhost configuration files
mkdir /etc/httpd/sites-enabled
mkdir -p /var/log/apache/ 

# Edit the "/etc/httpd/conf/httpd.conf" file and add the following at the end of the file
IncludeOptional sites-enabled/*.conf

# If you do not know how to edit the file, do the following
echo 'IncludeOptional sites-enabled/*.conf' >> /etc/httpd/conf/httpd.conf

Great.  Now let us create the VirtualHost configuration file.

nano /etc/httpd/sites-enabled/

Make sure you replace the domain name with your domain name. Now paste the following content in to the file

	<VirtualHost *:80>
	DocumentRoot /var/www/
	ErrorLog /var/log/apache/
	CustomLog /var/log/apache/ combined

Now press Ctrl+X and then Y and press enter. The configuration now should be saved

# Restart apache
systemctl restart httpd

Aaanddd, done. That’s it. Now, go ahead and open up your domain in your browser and you should see your “Hello world” message.