{"id":2305,"date":"2022-08-12T05:57:36","date_gmt":"2022-01-18T01:15:10","guid":{"rendered":""},"modified":"2025-02-02T00:46:18","modified_gmt":"2025-02-01T23:46:18","slug":"how-to-configure-apache-virtual-hosts-on-ubuntu-using-terraform","status":"publish","type":"post","link":"https:\/\/netcloud24.com\/knowledgebase\/how-to-configure-apache-virtual-hosts-on-ubuntu-using-terraform\/","title":{"rendered":"How to Configure Apache Virtual Hosts on Ubuntu Using Terraform"},"content":{"rendered":"<p>\u00a0<\/p>\n<\/p>\n<header>\n<h1>\u00a0<\/h1>\n<\/header>\n<article>\n<p>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 , this tutorial will help you streamline your virtual host setup.<\/p>\n<section>\n<h2>Step 1: Install Terraform and Apache<\/h2>\n<p>Before configuring virtual hosts, ensure Terraform and Apache are installed on your Ubuntu server.<\/p>\n<h3>Install Terraform:<\/h3>\n<p>Download and install Terraform from the official website or use the package manager:<\/p>\n<pre><code>sudo apt-get install terraform<\/code><\/pre>\n<p>Verify the installation:<\/p>\n<pre><code>terraform --version<\/code><\/pre>\n<h3>Install Apache:<\/h3>\n<p>Install Apache with the following command:<\/p>\n<pre><code>sudo apt install apache2 -y<\/code><\/pre>\n<p>Start and enable Apache to run at boot:<\/p>\n<pre><code>sudo systemctl start apache2\r\nsudo systemctl enable apache2<\/code><\/pre>\n<\/section>\n<section>\n<h2>Step 2: Create a Terraform Configuration for Apache Virtual Hosts<\/h2>\n<p>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:<\/p>\n<pre><code>mkdir apache-vhosts\r\ncd apache-vhosts<\/code><\/pre>\n<p>Create a new <code>main.tf<\/code> file for your Terraform configuration:<\/p>\n<pre><code>nano main.tf<\/code><\/pre>\n<p>Add the following Terraform code to define the Apache installation and virtual host configuration:<\/p>\n<pre><code>provider \"local\" {}\r\n\r\nresource \"null_resource\" \"configure_apache\" {\r\n  provisioner \"remote-exec\" {\r\n    inline = [\r\n      \"sudo apt-get update\",\r\n      \"sudo apt-get install apache2 -y\",\r\n      \"sudo a2enmod vhost_alias\"\r\n    ]\r\n  }\r\n}\r\n\r\nresource \"local_file\" \"vhost1\" {\r\n  content = &lt;\r\n    ServerAdmin webmaster@domain1.com\r\n    ServerName domain1.com\r\n    ServerAlias www.domain1.com\r\n    DocumentRoot \/var\/www\/domain1.com\r\n    ErrorLog \\${APACHE_LOG_DIR}\/domain1-error.log\r\n    CustomLog \\${APACHE_LOG_DIR}\/domain1-access.log combined\r\n\r\nEOT\r\n  filename = \"vhost1.conf\"\r\n}\r\n\r\nresource \"local_file\" \"vhost2\" {\r\n  content = &lt;\r\n    ServerAdmin webmaster@domain2.com\r\n    ServerName domain2.com\r\n    ServerAlias www.domain2.com\r\n    DocumentRoot \/var\/www\/domain2.com\r\n    ErrorLog \\${APACHE_LOG_DIR}\/domain2-error.log\r\n    CustomLog \\${APACHE_LOG_DIR}\/domain2-access.log combined\r\n\r\nEOT\r\n  filename = \"vhost2.conf\"\r\n}\r\n\r\nresource \"null_resource\" \"deploy_vhosts\" {\r\n  provisioner \"remote-exec\" {\r\n    inline = [\r\n      \"sudo mkdir -p \/var\/www\/domain1.com \/var\/www\/domain2.com\",\r\n      \"sudo cp vhost1.conf \/etc\/apache2\/sites-available\/domain1.conf\",\r\n      \"sudo cp vhost2.conf \/etc\/apache2\/sites-available\/domain2.conf\",\r\n      \"sudo a2ensite domain1.conf\",\r\n      \"sudo a2ensite domain2.conf\",\r\n      \"sudo systemctl reload apache2\"\r\n    ]\r\n  }\r\n}<\/code><\/pre>\n<p>This configuration defines two Apache virtual hosts, one for <code>domain1.com<\/code> and another for <code>domain2.com<\/code>, using Terraform to provision the server and copy the virtual host configuration files.<\/p>\n<\/section>\n<section>\n<h2>Step 3: Apply the Terraform Configuration<\/h2>\n<p>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:<\/p>\n<pre><code>terraform init<\/code><\/pre>\n<p>Next, apply the configuration:<\/p>\n<pre><code>terraform apply<\/code><\/pre>\n<p>Terraform will prompt you for confirmation. Type <code>yes<\/code> to continue. Terraform will install Apache, configure the virtual hosts, and reload the Apache service.<\/p>\n<\/section>\n<section>\n<h2>Step 4: Verify the Virtual Host Configuration<\/h2>\n<p>Once the Terraform configuration is applied, verify that the virtual hosts are working by visiting the domains in your browser (e.g., <code>http:\/\/domain1.com<\/code> or <code>http:\/\/domain2.com<\/code>).<\/p>\n<p>You can also check the status of the Apache virtual hosts using the following command:<\/p>\n<pre><code>sudo apache2ctl -S<\/code><\/pre>\n<\/section>\n<footer>\n<p>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 . They offer a range of hosting options, including <strong>windows virtual private servers<\/strong>, <strong>vps windows hosting<\/strong>, and <strong>windows virtual dedicated server hosting<\/strong>. Whether you&#8217;re looking for <strong>windows vps italy<\/strong> or <strong>uk vps windows<\/strong> solutions, their hosting services provide the flexibility and performance needed for hosting multiple websites.<\/p>\n<\/footer>\n<\/article>\n<div class=\"post-author-box\" style=\"border-top:1px solid #ddd;margin-top:20px;padding-top:15px;\">\n<p><strong>Author:<\/strong> \u0141ukasz Bodziony<\/p>\n<p><strong>Website:<\/strong> <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a><\/p>\n<p><em>\u0141ukasz Bodziony is the CEO and founder of <a href=\"https:\/\/netcloud24.com\" target=\"_blank\" rel=\"dofollow\">NETCLOUD24<\/a>, a global VPS hosting brand proudly originating from Poland. With extensive experience in cloud computing, virtualization, and server management, he delivers high-performance <strong>Windows VPS<\/strong> and <strong>Remote Desktop Services (RDS)<\/strong> solutions to clients across Europe, North America, and beyond.<\/em><\/p>\n<p><em>His expertise covers a wide range of technologies, including <strong>Microsoft Azure<\/strong>, <strong>Proxmox VE<\/strong>, <strong>Amazon Web Services (AWS)<\/strong>, and numerous other virtualization and cloud platforms.<\/em><\/p>\n<p><em>Beyond running his hosting business, \u0141ukasz also provides <strong>professional paid server configuration and optimization services<\/strong> for companies and individuals. Outside of work, he is dedicated to caring for his children and building a secure future for them.<\/em><\/p>\n<p><em>If you are interested in working with him or need expert assistance with your hosting, cloud environment, or server setup, feel free to reach out via <a href=\"https:\/\/ca.netcloud24.com\" target=\"_blank\" rel=\"dofollow\">Windows VPS<\/a>.<\/em><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 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\u2026<\/p>\n","protected":false},"author":1,"featured_media":3421,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[],"tags":[14,12,11,23,20,21,22,17,7,8,6,10,18,19,15,24,16,5,13,9],"class_list":["post-2305","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","tag-cheapvps","tag-cloudvps","tag-hostingvps","tag-rds","tag-rdscal","tag-remotedesktop","tag-remotedesktopvps","tag-servervps","tag-ukvps","tag-virtualserver","tag-vpshosting","tag-vpsserver","tag-vpssolutions","tag-vpswindows","tag-vpswithwindows","tag-windowsrds","tag-windowsserver","tag-windowsvps","tag-windowsvpshosting","tag-windowsvpsuk"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/2305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=2305"}],"version-history":[{"count":0,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/2305\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media\/3421"}],"wp:attachment":[{"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=2305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=2305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/netcloud24.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=2305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}