• Digital accessories
  • Server
  • Digital life
  • Privacy policy
  • Contact us
  1. Home
  2. Article
  3. How to set up Apache Virtual Hosts on CentOS 7

How to set up Apache Virtual Hosts on CentOS 7

Rsdaa 04/12/2021 1916

Today we will show you How to setup Apache Virtual Hosts on CentOS 7. 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. To setup Apache Virtual Hosts on CentOS 7 just follow our step by step tutorial below.

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.

1. Requirements

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)

2. 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.

3. 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.

4. Configure a virtual host directive for your domain.

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:

ServerAdmin webmaster@your_domain.comDocumentRoot "/var/www/html/your_domain/"ServerName your_domain.comServerAlias www.your_domain.comErrorLog "/var/log/httpd/your_domain.com-error_log"CustomLog "/var/log/httpd/your_domain.com-access_log" combinedDirectoryIndex index.html index.phpOptions FollowSymLinksAllowOverride AllRequire all granted

5. Settings explanation

Let’s explain the settings one by one.

– 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.andand 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.

[ecko_alert color=”blue”]Stuck somewhere? Get a VPS from us and we’ll do all of this for you, free of charge![/ecko_alert]

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 to to setup Apache Virtual Hosts on CentOS 7, They are available 24×7 and you can contact them via chat or email at support@rosehosting.com

PS. If you liked this post on how to setup Apache Virtual Hosts on CentOS 7 please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.


PREV: The 5 Different Types of Firewalls Explained

NEXT: Azure Functions networking options | Microsoft Docs

Popular Articles

Hot Articles

Navigation Lists

Back to Top