How to Configure Apache Virtual Hosts on Ubuntu Using Terraform

 

 

Apache virtual hosts allow you to host multiple websites on a single server by mapping different domain names to different directories. This guide will show you how to configure Apache virtual hosts on Ubuntu using Terraform, enabling you to automate the creation of virtual hosts with Infrastructure as Code. Whether you are deploying on a local server or on a Windows VPS UK, this tutorial will help you streamline your virtual host setup.

Step 1: Install Terraform and Apache

Before configuring virtual hosts, ensure Terraform and Apache are installed on your Ubuntu server.

Install Terraform:

Download and install Terraform from the official website or use the package manager:

sudo apt-get install terraform

Verify the installation:

terraform --version

Install Apache:

Install Apache with the following command:

sudo apt install apache2 -y

Start and enable Apache to run at boot:

sudo systemctl start apache2
sudo systemctl enable apache2

Step 2: Create a Terraform Configuration for Apache Virtual Hosts

In this step, we will create a Terraform configuration file that provisions an Apache server and configures virtual hosts. Begin by creating a new directory for your Terraform project and navigate to it:

mkdir apache-vhosts
cd apache-vhosts

Create a new main.tf file for your Terraform configuration:

nano main.tf

Add the following Terraform code to define the Apache installation and virtual host configuration:

provider "local" {}

resource "null_resource" "configure_apache" {
  provisioner "remote-exec" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get install apache2 -y",
      "sudo a2enmod vhost_alias"
    ]
  }
}

resource "local_file" "vhost1" {
  content = <
    ServerAdmin [email protected]
    ServerName domain1.com
    ServerAlias www.domain1.com
    DocumentRoot /var/www/domain1.com
    ErrorLog \${APACHE_LOG_DIR}/domain1-error.log
    CustomLog \${APACHE_LOG_DIR}/domain1-access.log combined

EOT
  filename = "vhost1.conf"
}

resource "local_file" "vhost2" {
  content = <
    ServerAdmin [email protected]
    ServerName domain2.com
    ServerAlias www.domain2.com
    DocumentRoot /var/www/domain2.com
    ErrorLog \${APACHE_LOG_DIR}/domain2-error.log
    CustomLog \${APACHE_LOG_DIR}/domain2-access.log combined

EOT
  filename = "vhost2.conf"
}

resource "null_resource" "deploy_vhosts" {
  provisioner "remote-exec" {
    inline = [
      "sudo mkdir -p /var/www/domain1.com /var/www/domain2.com",
      "sudo cp vhost1.conf /etc/apache2/sites-available/domain1.conf",
      "sudo cp vhost2.conf /etc/apache2/sites-available/domain2.conf",
      "sudo a2ensite domain1.conf",
      "sudo a2ensite domain2.conf",
      "sudo systemctl reload apache2"
    ]
  }
}

This configuration defines two Apache virtual hosts, one for domain1.com and another for domain2.com, using Terraform to provision the server and copy the virtual host configuration files.

Step 3: Apply the Terraform Configuration

After configuring Terraform, you can now apply the configuration to provision Apache and create the virtual hosts. Start by initializing Terraform in your project directory:

terraform init

Next, apply the configuration:

terraform apply

Terraform will prompt you for confirmation. Type yes to continue. Terraform will install Apache, configure the virtual hosts, and reload the Apache service.

Step 4: Verify the Virtual Host Configuration

Once the Terraform configuration is applied, verify that the virtual hosts are working by visiting the domains in your browser (e.g., http://domain1.com or http://domain2.com).

You can also check the status of the Apache virtual hosts using the following command:

sudo apache2ctl -S

You have successfully configured Apache virtual hosts on Ubuntu using Terraform. This approach allows you to automate the setup of multiple websites on a single server, streamlining deployment processes. For scalable hosting solutions, consider using Windows VPS UK. They offer a range of hosting options, including windows virtual private servers, vps windows hosting, and windows virtual dedicated server hosting. Whether you're looking for windows vps italy or uk vps windows solutions, their hosting services provide the flexibility and performance needed for hosting multiple websites.

  • 0 Utilisateurs l'ont trouvée utile
Cette réponse était-elle pertinente?

Articles connexes

Boost Your Ubuntu System's Performance with a Swap File: A Step-by-Step Guide

What is a Swap File? A swap file in Ubuntu serves as dedicated virtual memory on your hard...

How to Migrate ISPConfig 2, ISPConfig 3.x, Confixx, CPanel or Plesk to ISPConfig 3.2 (single server)

Introduction Migration from other control panels like ISPConfig 2, ISPConfig 3.x, Confixx,...

How to Install and Configure Zabbix Server and Client on Rocky Linux 9

Introduction Zabbix is an open-source monitoring solution that provides real-time...

How to Install CockroachDB Cluster on Debian 12

Introduction CockroachDB is a distributed SQL database built to handle large-scale,...

How to Install Joomla with Apache and Let's Encrypt SSL on AlmaLinux 9

Introduction Joomla is a popular open-source content management system (CMS) used to build...