Apache is an open source web server developed for modern operating systems including UNIX and Windows. It is a secure, efficient and extensible server that provides HTTP services synchronized with the current HTTP standards.
Many people use it to host their websites and apps. In fact, according to the latest statistics, Apache is the most used web server in the world, with Windows IIS following in second and in third the Russian machinery that is Nginx.
Apache’s functionality is great and one can find lots of good features for his website or application. In this tutorial, we will provide you with info about Apache Virtual Host through configuration examples on a CentOS 7 VPS.
A virtual host is, in fact, a unit that describes an individual domain in the grand scheme of the Apache web server. It enables you to host multiple websites using one server which is very useful for people that have more than one website.
Every domain that is configured with a virtual host directive inside Apache’s config will direct the visitor to a specific directory where that very website is hosted on the server. For example, you might have your WordPress site installed in /var/www/wordpress/ so when creating the virtual host directive, the document root for the site will be set to /var/www/wordpress/ which will enable the web server to pull the website data from the respective configured directory.
So let’s focus on our task at hand now.
We will be using our SSD 1 Linux VPS hosting plan for this tutorial.
Log in to your server via SSH:
# ssh root@server_ip
Before starting, enter the command below to check whether you have the proper version of CentOS installed on your machine:
# cat /etc/redhat-release
which should give you the underneath output:
CentOS Linux release 7.2.1511 (Core)
Update the system
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 that with a fairly simple command:
# yum install httpd
Engage Apache and enable it to start on boot:
# systemctl start httpd.service # systemctl enable httpd.service
Most webmasters today use WordPress as a platform to host their website. A WordPress site runs with a database so if you plan to use this article to create virtual hosts for a WordPress website/s (or any platform that needs a database for that matter), you will need to install MySQL and PHP on your server which complete a so-called LAMP (Linux Apache, MariaDB & PHP) stack. We have an excellent article on how to install LAMP on CentOS 7 so you might want to check it out here.
Apache Configuration Settings
Apache has a global configuration file where all the default settings are stored and applied to the server. The virtual host directive can be stored in that same default httpd.conf file or another one which will be respective to the configured website/domain.
That global Apache configuration file in CentOS 7 is /etc/httpd/conf/httpd.conf. You may use a text editor of your choice and check the content of that file so you get some insight on how Apache is configured.
The config file has commented lines before every setting that explain their use. So for example, the ServerRoot setting is the top of the directory tree under which lie the Apache configuration files.
The Listen directive binds Apache to a specific IP address and port. Apache’s default listening port is 80.
The user/group values are names of the user/group that HTTPD (apache) runs as. In CentOS the user and group are apache/apache and in Ubuntu/Debian the values are www-data/www-data.
Another important setting is DocumentRoot which is the directory out of which the data will be served to the visitor. Usually, in most distros the default document root for Apache is set to /var/www/html/ so if you put data in /var/www/html/ you will be able to access that same data via a web browser using your server IP address.
We’ve covered just a glimpse 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 config file, however, for a cleaner and more organized setup we will create a new separate file in which the configuration lines will be stored. There is a setting (IncludeOptional) in Apache’s global config whose included directory stores separate config files. For example, in CentOS 7 and Apache 2.4 (the latest Apache version as of writing this tutorial) the IncludeOptional setting is set to the /etc/httpd/conf.d/ which means that every *.conf file created in /etc/httpd/conf.d/ will be used by the web server to handle the data.
Therefore, let’s create a your_domain.conf file where we will configure a 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 the below content into the file:
<VirtualHost *:80> ServerAdmin webmaster@your_domain.com DocumentRoot "/var/www/html/your_domain/" ServerName your_domain.com ServerAlias www.your_domain.com ErrorLog "/var/log/httpd/your_domain.com-error_log" CustomLog "/var/log/httpd/your_domain.com-access_log" combined <Directory "/var/www/html/your_domain/"> DirectoryIndex index.html index.php Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>
Let’s explain the settings one by one.
- <VirtualHost *:80> – this means that this virtual host directive binds to any IP address on the server and on port 80.
- ServerAdmin – this is the email address where problems with the web server will be emailed to.
- DocumentRoot – the directory where the website data will be stored which in the above case will be /var/www/html/your_domain/. Of course, you can replace the your_domain value with anything you find it suitable.
- ServerName – this value gives the name that the server uses to identify the website. Here you can enter your actual domain.
- ServerAlias – The ServerAlias directive sets the alternate names for a host, for example: www.your_domain.com. It can also include wildcards if needed.
- <Directory> and </Directory> and are used to enclose a group of directives that will apply only to the named directory, sub-directories of that directory, and the files within the respective directories. Any directive that is allowed in a directory context may be used. Directory-path is either the full path to a directory or a wild-card string using Unix shell-style matching. In our case we are enabling the FollowSymLinks option which will follow symbolic links in the /var/www/html/your_domain/ directory. We also set the AllowOverride directive to All which means that any directive which has the .htaccess context is allowed in .htaccess files. The Require directive selects which authenticated users can access a resource.
This kind of config can be applied to most of the platforms out there. However, sometimes a specific WordPress site config such as the one above may not be enough for other kinds of apps. For example, a Magento site will need more features enabled or an Odoo site will need a reverse proxy configured.
One thing that is worth mentioning (even though most users know it) is that all the above configuration settings need to be properly setup before or after the Apache config file creation. For example, the DocumentRoot needs to be created and data hosted in it.
To create a directory use:
# mkdir /var/www/html/your_domain/
Then upload the data you want to host inside that directory.
Permissions and file ownership is another important task for webmasters. In most cases in servers without a control panel, the file and directory ownership should belong to the user and group under which Apache operates. In CentOS 7 Apache runs under apache as user and apache as group. So you will need to recursively assign that values to the files and directories in the document root. The below command does exactly that:
# chown apache:apache -R /var/www/html/your_domain/
This command will assign apache:apache to every file and directory/subdirectory inside /var/www/html/your_domain/.
With this article, we’ve just peeked into Apache and virtual host configurations. For more information, you should visit Apache’s official documentation.
Of course, if you use one of our Apache VPS Hosting services you can always contact and ask our expert Linux admins about everything you want to know about Apache. They are available 24×7 and you can contact them via chat or email at email@example.com
PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.