• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

How to Set Up Apache Virtual Hosts on CentOS 7

Written by Admin, Updated On May 9, 2020
apache, centos, httpd
How to Set Up Apache Virtual Hosts on CentOS 7

When using Apache web server, you can use virtual hosts to host more than one domain in a single server. Apache will break its functionality and components into individual units so you can customize independently. The basic unit that describes an individual site or domain is called a virtual hosts on CentOS server.

Create Apache Virtual Hosts On CentOS 7#

In this tutorial, we will provide a step by step instructions about how to set up Apache Virtual Hosts on a CentOS 7 server.

Prerequisites#

  • A CentOS 7 server with a non-root user with sudo privileges.
  • Apache should installed and configured, as shown in How to Install Apache on CentOS 7 Server.
  • A domain name should pointing to your server IP address.

Creating the Directory Structure#

At first, we will create a directory where website files for a domain will store and serve response to visitors. Generally, it called DocumentRoot. You can set the document root to any location that you want but it’s best practice to set in directory structure. Generally in all /var/www.

/var/www/
├── example1.com
│   └── public_html
├── example2.com
│   └── public_html

Here, we need to create separate directory inside /var/www directory for each domain which we want to host on our server.

sudo mkdir -p /var/www/example1.com/public_html

For testing purpose we will create a index.html file inside the domain document root directory. This page will be show by default when visitors will visit your site.

Creating a new index.html file using your favorite text editor by typing :

sudo vi /var/www/example1.com/public_html/index.html

Add the below lines into it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Welcome!!</title>
  </head>
  <body>
    <h1>Success! example1.com set up completed!</h1>
  </body>
</html>

Since all commands are executed as sudo user, so newly created files and directories are owned by the root user. We will change document root directories ownership to avoid permission issue for our regular user. Thus, regular user can modify files in our web directories without any issues.

sudo chown -R apache: /var/www/example1.com

Creating Virtual Host Files#

There are multiple ways to set up a virtual host. You can make separate file for each Virtual Host Directive or you can add all Virtual Host Directives in a single file. It’s recommended to make separate file for each domain because it’s maintainability.

On CentOS, Apache will load all .conf files from /etc/httpd/conf.d/ directory because of it’s default configuration. So now we will create a separate virtual host.

Create a new file using your choice text editor by typing :

sudo vi /etc/httpd/conf.d/example1.com.conf
<VirtualHost *:80>
    ServerName example1.com
    ServerAlias www.example1.com
    DocumentRoot /var/www/example1.com/public_html

    <Directory /var/www/example1.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog /var/log/httpd/example1.com-error.log
    CustomLog /var/log/httpd/example1.com-access.log combined
</VirtualHost>
  • ServerName: This should be your domain name and match with virtual host configuration.
  • ServerAlias: All other domains or subdomains that should match for this virtual host as well, usually the www subdomain.
  • DocumentRoot: Path of virtual host directory that from which Apache will serve the domain files.
  • Options: This directive controls which server features are available in a specific directory.
  • -Indexes: It will prevent directory listings.
  • FollowSymLinks: Apache will follow the symbolic links if this option is enabled.
  • AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.
  • ErrorLog, CustomLog: Specifies the location for log files.

You can give any names to your configuration file but it’s recommended to give file name same as domain name.

Now check the syntax by type :

sudo apachectl configtest

It will show below output if there are no errors:

Output
Syntax OK

You must restart apache2 service to make active newly created virtual hosts :

sudo systemctl restart httpd

You can verify by opening your http://example1.com to your web browser and it will show you as following :

Apache Virtual Hosts

Conclusion#

Finally, in this tutorial you learned how to create and set up Apache virtual hosts. You repeat same procedure for multiple domain.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install PHP 7.3, 7.2, 7.1 on CentOS 7
Next Article   How to Install MySQL on CentOS 7 Server

Related Posts

  • How to Install Apache, MySQL, PHP (LAMP) on Ubuntu 22.04

    How to Install LAMP on Ubuntu 22.04

    March 20, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04

    How to Set Up Apache Virtual Hosts on Ubuntu 22.04

    March 2, 2023
  • How to Install GCC Compiler on Debian 11

    How to Install GCC Compiler on Debian 11

    December 11, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook