This tutorial shows how to setup Apache Virtual Hosts in CentOS 7. This is useful if you want to host more than one website on a single CentOS web server. For instructions on how to setup Apache, PHP, and SQL database on CentOS 7, check this article.
Setup folder structure for your websites
We are going to create 2 folders for each website. The first will hold HTML and other content, second - log files.
Log files will be stored in var/log/httpd/... subfolders, which is the default place to store log files in Linux. People often store Apache log files in /var/www/ subfolders, but in CentOS with SELinux enabled this can cause access denied errors. This can be fixed with chcon command, but I prefer to store all my log files in /var/logs...
You can also place index.html files with some sample text in each html directory which we'll use later for testing.
Create folder structure for virtual host files
sites-available will hold virtual host config files for websites that are configured, but not necessary enabled. These files are actually ignored when Apache starts, but this system allows to easily disable and enable websites without having to delete or create new virtual host files every time we want to take a site offline and put it back online.
sites-enabled will hold symbolic links to virtual host files inside sites-available that are enabled. They will be loaded when Apache starts.
Add sites-enabled to Apache config
Open file /etc/httpd/conf/httpd.conf and at the very end of the file add following text:
This will tell Apache to look for virtual host files with extension .conf in sites-enabled directory.
Create Virtual Host files
In directory /etc/httpd/sites-available create config files for each website (domain1.conf, domain2.conf, etc.) with following content:
CustomLog /var/log/httpd/domain1/access.log combined
Change domain1 to match your domain names and directories you created previously.
If Apache can't match the requested domain to any of the virtual hosts, the first (alphabetically) virtual host site will be loaded. If you want to have more control over this, you can create a dedicated virtual host (i.e. 00_default) that will be loaded only when no matching virtual host exist. ServerName and ServerAlias should not match any of your domains (i.e. example.com) The name starts with 00 so that it is always the first virtual hosts alphabetically. The site linked to this default virtual host could show an error message, redirect to another domain, etc.
Edit the main Apache configuration file /etc/httpd/conf/httpd.conf according to your requirements.
In my particular case, I made following changes:
Set webmaster email address:
Set main document root:
Configure main document root:
AllowOverride All - is required if you intend to use .htaccess files for directory level configuration. By default, this is set to None, in which case .htaccess overrides would be ignored.
Options -Indexes - prevents directory listing.
Enable Virtual Hosts
To enable websites, we need to create symbolic links in /etc/httpd/sites-enabled pointing to appropriate config files in sites-available. To do this, run:
ln -s /etc/httpd/sites-available/domain1.conf /etc/httpd/sites-enabled/domain1.conf
For changes to take effect we need to restart Apache:
systemctl restart httpd.service
To disable a particular website, simply delete relevant symbolic link (or change the extension) and restart Apache.
Before going live, you can test if everything is working locally. To do this, edit the hosts file on your client machine to point domains configured inside virtual hosts to the CentOS web server's IP address. If everything was setup properly, pointing your web browser to one of the domains should load index.html file from an appropriate /var/www/sites/... directory.