Manage Apache 2 VirtualHost with Macro on Ubuntu 18.04 LTS

Chưa phân loại
Apache Macro is like a function in shell script or a programming language. You define a macro and then you expand it where needed to simply configuration task of the Apache web server.

An Apache macro has a name, some variables, and lines of Apache configuration codes. When you want to use the macro, you call it by the macro name, pass the values for each variable, the variables are replaced with the value in the macro and then the macro expands. It makes configuration super easy and modular.

In this article, I am going to show you how to use macro to manage Apache 2 VirtualHost. So, let’s get started.

VirtualHost Setup:

In this article, I am going to implement the VirtualHost configuration as shown in fig 1 with macro. I will have 3 users bob, alice and linda. bob will host his website on the Apache server with the IP address The same way alice and linda will host their websites and respectively on the same Apache server.

Fig 1: Name based VirtualHost in Apache 2

This article is an extension of the article I’ve written on configuring Apache VirtualHost on Ubuntu 18.04 LTS which you can read at How to Configure Apache VirtualHost on Ubuntu 18.04 LTS (link that article here).


If you’re new to Apache 2 configuration and you want to learn everything step by step, then please read my article How to Configure Apache VirtualHost on Ubuntu 18.04 LTS (link that article here) first. From this article, you need to follow the sections as given below:

  • Installing Apache 2 Web Server
  • Configuring DNS
  • Directory Structures
  • Creating Users

Once you follow these sections from the reference article, you can continue with the next sections in this article.

Enabling Apache 2 mod_macro Module:

By default, mod_macro module is not enabled on Apache 2 web server on Ubuntu 18.04 LTS. But, the module is there and you can easily enable it with the a2enmod command.

To enable mod_macro module, run the following command:

$ sudo a2enmod macro

mod_macro should be enabled.

Creating a Macro for VirtualHost:

The default directory where you should put your macro files on Ubuntu 18.04 LTS is /etc/apache2/conf-available/.

First, navigate to the directory /etc/apache2/conf-available/ as follows:

$ cd /etc/apache2/conf-available/

Now, create a new macro file vhost-macro.conf with the following command:

$ sudo nano vhost-macro.conf

A text editor should be opened.

Now, type in the following lines:

<Macro VHost $domain>
<VirtualHost *:80>
ServerName $domain
ServerAlias www.$domain
DocumentRoot "/www/$domain/www/public_html"
<Directory "/www/$domain/www/public_html">
Options -FollowSymLinks +MultiViews +Indexes
AllowOverride all
Require all granted
ErrorLog "/www/$domain/www/logs/error.log"
CustomLog "/www/$domain/www/logs/access.log" combined

Finally, the macro looks as follows. Now, save the file by pressing <Ctrl> + x followed by y and <Enter>.

Using Macro for Each VirtualHost:

In the earlier article, I created VirtualHost configuration files for bob (, alice ( and linda (

Now, it’s time to replace it with macro.

First, navigate to the default Apache 2 sites directory /etc/apache2/sites-available as follows:

$ cd /etc/apache2/sites-available

So, open the VirtualHost configuration file for bob ( as follows:

$ sudo nano

That’s a lot of lines. Well, delete all of these lines. We don’t need those anymore.

Just put in the following line and save the file by pressing <Ctrl> + x followed by y and <Enter>.

Use VHost

Now, do the same thing for alice (

$ sudo nano

Remove all the lines, type in the following line and save the file.

Use VHost

Again, do the same thing for linda (

$ sudo nano

Remove all the lines, type in the following line and save the file.

Use VHost

Enabling Macro:

Now, you have to enable the macro vhost-macro.conf that you just created.

To do that, run the following command:

$ sudo a2enconf vhost-macro

The macro vhost-macro is enabled.

Now, restart the Apache 2 service as follows:

$ sudo systemctl restart apache2

Testing Macro Based VirtualHosts:

Now, try to access the websites,,

As you can see, all of these websites works as expected.

Did you see how macros make a difference? Earlier we had to put lot of lines of codes in the VirtualHost configuration file. But now, only one line of code is enough to configure a VirtualHost. It makes configuring VirtualHost super easy.

So, that’s how you use Apache 2 macros to simply VirtualHost configuration on Ubuntu 18.04 LTS. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

What is DNS and how does it work

This tutorial aims to explain, in the simplest way, what DNS (the Domain Name System) is and how it works. This article...

How to Use MySQL JSON Data Type

MySQL is a relational database system developed to store data in medium to large computer systems. It uses schema to define...

Installing Dash to Dock on Ubuntu 20.10 – GNOME Shell Extension for Enhanced Dock

Personalization of setting in Linux distributions is one of the highlighted features. There are tons of themes, icons, cursors,...
Bài Viết

Bài Viết Mới Cập Nhật

Huớng dẫn dùng proxy cho ios, iphone 2023

Cách gắn set proxy cho điện thoại android, oppo, giả lập android, Ldplayer Bằng Proxydroid

Mua Proxy Socks5 VN Chơi Game Gia Lập Tăng Cường Trải Nghiệm Chơi Game

Mua Proxy Mỹ, Us Nuôi Tài Khoản Etsy, eBay Tìm Hiểu Về Mua Proxy Mỹ tại

Mua Proxy Game – Giải pháp tuyệt vời cho việc chơi game trên mạng mà không bị giới hạn về vị trí địa lý