Instructions for creating virtual hosts with Apache in Ubuntu

For system administrators, Apache is indeed one of the most effective support tools today, with high flexibility and stability, easy to set up, configure, and support multiple management. domain even though the web server system has only one IP address. Besides, we can also use to store many different sites ...

TipsMake.com - For system administrators, Apache is indeed one of the most effective support tools today, with flexibility and high stability, easy to set up, configure, support. managing multiple domains even if the web server system has only one IP address. Besides, we can also use it to host many different sites, or simply set up a sandbox system for application development. In the following article, we will cover some basic operations to create virtual host - virtual host using Apache.

In essence, the process of setting up a host is a 'challenge', but it is not so simple. Users can completely set up a virtual host system with just a few simple steps such as custom changes in Apache. Specifically in this test, we use the Ubuntu server operating system and the corresponding Apache version.

Create directory structure:

Before embarking on the configuration process, you must complete the directory structure step for virtual sites. Here we will work with Apache installed on the Ubuntu server system, so the root document section of Apache will appear at / var / www. In essence, the directory structure for the new website can be created anywhere, possibly in the directory (~ /) , or / usr / local / apache . To simplify the maximum of this test, We will set up virtual host in the Apache root document section (in Ubuntu will be the directory / var / www ), with this step we will not need to change the ownership level of the directories created later. This or the root directory contains the virtual host.

Specifically, in this test we will set up virtual host test_site, and type the following command to create the directory containing virtual host:

sudo mkdir / var / www / test_site
sudo chmod -R 755 / var / www / test_site

Then, the root directory of the virtual host has been successfully created, and this is also the time to set up Apache to begin the process of creating a new website. After completing this Apache configuration step, the entire website will be built inside / var / www / test_site.

Configure Apache:

The first step in setting up Apache is to make sure that Apache 'knows' that the virtual host is enabled. Specifically, in the configuration section of Ubuntu, go to the following paragraph in file / var / www / test_site:

Include sites-enabled /

Make sure this line of code is still active (does not start with # ). The sites-enabled command structure will be pointing to / etc / apache / sites-enabled . When searching in that directory, we will see that there is a 000-default file - which contains information about all the necessary directories for the virtual site to be created later. And to create a new virtual host, we need to create a new file inside the available directory, which is / etc / apache / sites-available / test_site . Specifically, the content of that file will look similar to the following:


ServerAdmin webmaster @ localhost
#We'd like to be able to access web site using www.test.domain.com or test.domain.com
ServerAlias ​​www.test.domain.com
DocumentRoot / var / www / test_site
#log file for this server
CustomLog /var/log/apache2/www.test.domain.com-access.log combined

The above code is used in case of assuming that domain.com will be the domain used (replacing this value with the domain name you use). And besides, we need to do 2 more simple steps below before restarting Apache. The first is to initialize the connection path in / etc / apache / sites-enabled to the newly created file:

- Access the directory / etc / apache / sites-enabled with the cd command / etc / apache / sites-enabled

- Create connection path using sudo ln -s / etc / apache / sites-available test_site command

Besides, we can also create links automatically by using sudo a2ensite test_site , and then temporarily disable virtual site operation, remove the connection with the sudo a2dissite test_site command .

And the last step to make sure that the server 'knows' about the existence of virtual site is not over the Internet. Specifically, we need to do the following:

- Open the / etc / hosts file with any text editor with Administrator rights

- Add the line of code 127.0.0.1 localhost.localdomain localhost test.domain.com www.test.domain.com

- Save and close the file.

Restart Apache with the sudo /etc/init.d/apache2 command and restart the virtual host by typing the address into the browser.

For non-debian server systems:

If the server system hosting virtual site is not a Debian-based distributor, some setup steps will be slightly different. As follows:

- Create a directory containing virtual site in the file /etc/httpd/conf/httpd.conf - similar to the Debian system.

-Make sure that the Apache configuration file is 'aware' of the virtual host's existence by maintaining the functionality of the Include conf.d / *. Conf code .

- Create a new virtual host file (often called vhosts.conf ) in the /etc/httpd/conf.d/ directory

- Add virtual site to the / etc / hosts file

- Restart Apache with the command /etc/rc.d/init.d/httpd restart

Our storage directory (for non-Debian systems) will look similar to the following:


ServerName test.domain.com
DocumentRoot / var / www / html / test_site

The virtual host file mentioned above will look like this:


DocumentRoot / var / http / www / test_site
ServerName www.test.domain.com


DocumentRoot / var / http / www / test_site
ServerName test.domain.com

This is also the time when the virtual host has been set up and working properly, and the next is to build the website within the directory / var / www / test_site . We can do this with multiple virtual host systems at will, or with tools like Drupal, Xoops or Joomla. Good luck!

5 ★ | 1 Vote