How to set up Apache virtual host on CentOS 7

Apache is an open source web server designed for modern operating systems including UNIX and Windows. It is a secure, efficient and scalable server providing HTTP services synchronized with current HTTP standards.

Many people use it to host their websites and applications. In fact, according to the latest statistics, Apache is the most widely used web server in the world, with Windows, IIS next in second and in third place is Russian development, which is Nginx.

The functionality of Apache is great and you can find many good features for a website or application. In this tutorial, we will provide you with information about Apache virtual host through configuration examples on CentOS 7.

A virtual host is actually a block that describes a single domain in the grand scheme of the Apache web server. This allows multiple websites to be hosted with a single server, which is very useful for people who have more than one site.

Each domain that is executed with a virtual host directive inside the Apache configuration will direct the visitor to a specific directory where that very website is hosted on the server. For example, your WordPress site is set to / var / www / wordpress / so when creating a virtual host directive, the document root for the site will be set to / var / www / wordpress/ which will allow the web server to pull data from the web site of the corresponding configured directory.

Log in to the server using SSH:

ssh [email protected]_ip

Before starting, enter the following command to check if you have the correct version of CentOS installed on your computer:

cat /etc/redhat-release

the output should be something like this:

CentOS Linux release 7.2.1511 (Core)

System update

Make sure your server is fully up to date:

yum update

On our CentOS 7 servers, Apache is installed by default. However, if you don’t have Apache installed on your server, you can do it with a fairly simple command:

yum install httpd

Enable Apache and add it to startup at boot:

systemctl start httpd.service

systemctl enable httpd.service

Most webmasters today use WordPress as a platform to host them on their site. A WordPress site is powered by a MySQL database, so if you plan on using this article to create virtual hosts with WordPress sites (or any other platform that needs a database), you will need to install MySQL and PHP on your server, which will complete the so called LAMP stack (Linux Apache, MariaDB and PHP). We have a great article on how to install LAMP on CentOS 7 so you can check it out here.

Apache configuration options

Apache has a global configuration file where all default settings are stored and applied to the server. The virtual host directive can be saved in the same file by default httpd.conf or another that will match the configured website / domain.

This global Apache config file on CentOS 7 is /etc/httpd/conf/httpd.conf… You can use a text editor of your choice and look at the contents of this file, so you will get some idea of ​​how Apache is configured.

The config file is commented before each installation that explains their use. For example, ServerRoot setting the top of the directory tree where the Apache configuration files are located.

Listen the directive binds Apache to a specific IP address and port. The default Apache listening port is 80.

The user / group values ​​are user / group names, works like HTTPD (Apache). On CentOS, user and group are Apache / Apache, in Ubuntu / Debian are www-data / www-data

Another important parameter is DocumentRoot, which is the directory from which the data will be served to the visitor. Generally, in most distributions, the default root document for Apache is set to / var / www / html /so if you look at the data in / var / www / html / you will be able to access this same data through a web browser using the server’s IP address.

We’ve covered only a fraction of what Apache offers to its user, so now let’s create our first virtual host directive in Apache. You can create a virtual host inside the main Apache configuration file, however, for a cleaner and more organized setup, we will create a new separate file that will store the configuration lines. There is a setting ( IncludeOptional ) in the global Apache server config, where the include directory stores the individual config files. For example on CentOS 7 and Apache 2.4 (latest Apache version as of this writing) IncludeOptional parameter is set to /etc/httpd/conf.d/which means that each file * .confcreated in /etc/httpd/conf.d/ will be used by the web server for data processing.

Therefore, let’s create a file your_domain.confin which we will configure the virtual host directive for your domain. Once you are logged into your server, enter the following command:

nano /etc/httpd/conf.d/your_domain.conf

Paste below content into file:

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/var/www/html/your_domain/"
ErrorLog "/var/log/httpd/"
CustomLog "/var/log/httpd/" combined

<Directory "/var/www/html/your_domain/">
DirectoryIndex index.html index.php
Options FollowSymLinks
AllowOverride All
Require all granted

Let’s explain the settings one by one.

  • – this means that this virtual host directive binds from any IP address on the server, on port 80.
  • ServerAdmin Is the email address where notifications of problems with the web server will be sent.
  • DocumentRoot Is the directory where the website data will be saved, in the above case it will be / var / www / html / your_domain /. You can of course replace your_domain with another value you find suitable.
  • ServerName – This value gives the name that the server uses to identify the website. You can enter your actual domain here.
  • ServerAlias – the ServerAlias ​​directive sets alternative host names, for example: It can also include wildcards if needed.
  • and and are used to attach a group of directives that apply only to a named directory, subdirectories of that directory, as well as files in the corresponding directories. Any directive that is allowed in a directive context can be used. Directory path or full directory path or wild-card string using Unix. In our case, we added FollowSymLinkswhich will apply symbolic links in the directory / var / www / html / your_domain /… We also set the directive AllowOverride all this means that any directive that has .htaccess allowed in files .htaccess… Directive Require specifies which users can access the resource.

This kind of configuration can be applied to most platforms. However, sometimes a specific WordPress site configuration such as the one above may not be sufficient for other kinds of applications. For example, a Magento site will need to enable more features, or an Odoo site will need a reverse proxy – the server is configured.

One detail worth noting (even though most users know this) is that all of the above configuration options must be set correctly before or after the Apache configuration file is created. For example, DocumentRoot, you need to create and the data placed in it.

Create user directory:

mkdir /var/www/html/your_domain/

Now load the data that you want to place inside this directory.

File permissions and ownership is another important task for webmasters. In most cases, on servers without a control panel, file and directory ownership should be owned by the user and group in which Apache is running. On CentOS 7 Apache runs Apache as a user and Apache as a group. Thus, you will need to recursively assign, values ​​for files and directories in the document root. The following command does exactly that:

chown apache:apache -R /var/www/html/your_domain/

This command will assign Apache: Apache for each file and subdirectory / directory inside / var / www / html / your_domain /

In this article, we have only scratched the surface of Apache and virtual host configuration. For more information, you should visit the official Apache web server to review documentation