Configure virtual hosts on CentOS

About to host websites on a Linux server? Using Apache? Excellent. This article will show you how to do just that using Apache virtual hosts.

In Apache, you can use virtual hosts to direct HTTP traffic from a given domain name to a specific directory (ie, the domain’s website root directory in the request). This feature is commonly used to host multiple websites, but we recommend using it for all websites on your server, including the first one.

Throughout this article, we’ll use an example domain – example.com – but you should replace it with the domain name or subdomain you want to host on your server.

Install the Apache web server

To get Apache on your server, you can either install it as part of a LAMP stack, or you can install Apache by itself:

  • Update your packages using yum
$ sudo yum update
  • Install Apache
$ sudo yum install httpd
  • Enable Apache Service
$ sudo systemctl enable httpd.service
  • Start up Apache
$ sudo service httpd start

Set up the virtual hosts

  • Create the virtual directories for your domain
$ sudo mkdir -p /var/www/example.com/public_html
  • Change the ownership to the Apache group
$ sudo chown -R apache:apache /var/www/example.com/public_html
# This lets Apache modify files in your web directories
  • Change the directory’s permissions so they can be read from the internet
$ sudo chmod -R 755 /var/www/

Create content for the website

  • Create the index file
$ sudo nano /var/www/example.com/public_html/index.html
  • Add some content to the file
<html>
<head>
    <title>Welcome to Example.com!</title>
</head>
<body>
    <h1>Success! The example.com virtual host is working!</h1>
</body>
</html>
  • Save and close the file
Ctrl + o (Save File)
Ctrl + x (Exit)

Configure your virtual host directories

We’re going to copy a configuration usually used in Ubuntu/Debian and create two directories: one to store the virtual host files (sites-available) and another to hold symbolic links to virtual hosts that will be published (sites-enabled).

Create sites-available and sites-enabled directories

  • Create the directories
$ sudo mkdir /etc/httpd/sites-available
$ sudo mkdir /etc/httpd/sites-enabled

Edit your Apache configuration file

Edit the main configuration file (httpd.conf) so that Apache will look for virtual hosts in the sites-enabled directory.

  • Open your config file
sudo nano /etc/httpd/conf/httpd.conf
  • Add this line at the very end of the file
IncludeOptional sites-enabled/*.conf
# This way, we’re telling Apache to look for additional config files in the sites-enabled directory
  • Save and close the file
Ctrl + o (Save File)
Ctrl + x (Exit)

Create virtual host file

We’re going to build it from a new file in your sites-available directory.

  • Create a new config file
$ sudo nano /etc/httpd/sites-available/example.com.conf
  • Paste this code in, replacing your own domain for example.com.conf. Here’s what the whole file could look like after your changes.
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com    
    ServerName www.example.com
    ServerAlias example.com 
    DocumentRoot /var/www/example.com/public_html 
    ErrorLog /var/www/example.com/error.log 
    CustomLog /var/www/example.com/requests.log combined 
</VirtualHost>

The lines ErrorLog and CustomLog are not required to set up your virtual host, but we’ve included them, in case you do want to tell Apache where to keep error and request logs for your site.

  • Save and close the file
Ctrl + o (Save File)
Ctrl + x (Exit)
  • Enable your virtual host file with a sym link to the sites-enabled directory
$ sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
  • Restart Apache
$ sudo service httpd restart
(Visited 8 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *