{"id":1593,"date":"2020-10-22T22:50:35","date_gmt":"2020-10-22T19:50:35","guid":{"rendered":"https:\/\/helia.ee\/koolitus\/?page_id=1593"},"modified":"2020-10-27T12:27:15","modified_gmt":"2020-10-27T10:27:15","slug":"debian-how-to-setup-virtual-hosts-with-apache-web-server-on-linux-virtuaal-veebi-serveri-seadistamine","status":"publish","type":"page","link":"https:\/\/helia.ee\/koolitus\/?page_id=1593","title":{"rendered":"Debian &#8211; How to Setup Virtual Hosts with Apache Web Server on Linux (Virtuaal veebi serveri seadistamine)"},"content":{"rendered":"<h1 class=\"qa_title\">How to Setup Virtual Hosts with Apache Web Server on Linux<\/h1>\n<div class=\"qa_category\"><\/div>\n<hr \/>\n<p>In this article, you will learn about how to set up Apache virtual hosts on an Ubuntu Linux. During this process, you will learn how to serve different sites to different users depending on the domain\/site they are requesting.<\/p>\n<h2>Apache Web Server<\/h2>\n<p>The Apache web server is the most popular and powerful way of serving web sites on the internet. It is used for more than half of all the active websites on the internet and is extremely powerful and flexible.<\/p>\n<p>Apache gives its functionality and the components to individual units which can be customized and configured independently. The basic unit that describes a site or a domain is called a virtual host.<\/p>\n<p>This allows the administrator to use one server to host multiple domains or sites with a single interface or IP address by using a mechanism.<\/p>\n<h3>Apache Installation<\/h3>\n<pre class=\"result notranslate\">$ sudo apt-get update\r\n$ sudo apt-get install apache2<\/pre>\n<p>After these steps are complete, we can get started.<\/p>\n<p>For Demo purpose, we shall configure <strong>apache<\/strong> to make a virtual host for webserver1.com and another for webserver2.com. This will be like a reference guide, but you should substitute your own site\/domains.<\/p>\n<h3>Create the Directory Structure<\/h3>\n<p>The first step here would be that, a directory structure shall hold the site information and data which will be served to visitors. By default, the top-level directory for apache is \/var\/www and we will be setting them to the individual directories under the \/var\/www directory for each site\/domain.<\/p>\n<p>For each of our site, we are going to make site directories \u2013<\/p>\n<p>$ sudo mkdir -p \/var\/www\/html\/webserver1.com<br \/>\n$ sudo mkdir -p \/var\/www\/html\/webserver2.com<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"result notranslate\">$ sudo mkdir -p \/var\/www\/html\/webserver1.com\/public_html\r\n$ sudo mkdir -p \/var\/www\/html\/webserver2.com\/public_html<\/pre>\n<h3>Grant Permissions<\/h3>\n<p>Now, we have the directory structure of our site\/domain, but they are created and owned by our root user. If we want our normal user to be able to modify files in our web directories, then we have to change the ownership to others normal users.<\/p>\n<pre class=\"result notranslate\">$ sudo chown -R $USER:$USER \/var\/www\/html\/webserver1.com\/public_html\r\n$ sudo chown -R $USER:$USER \/var\/www\/html\/webserver2.com\/public_html<\/pre>\n<p>We should also modify our permissions a little bit to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:<\/p>\n<pre class=\"result notranslate\">$ sudo chmod -R 755 \/var\/www<\/pre>\n<p>Your web server should now have the permissions it needs to serve the site, and your user should be able to create content within the necessary folders.<\/p>\n<h3>Create Demo Pages for Each Virtual Host<\/h3>\n<p>First, we shall start with webserver1.com. Then, we can create an index.html file in our editor by typing:<\/p>\n<pre class=\"result notranslate\">$ touch \/var\/www\/html\/webserver1.com\/\/public_html\/index.html<\/pre>\n<p>Edit the index.html file with a simple HTML script which indicates the site it is connected to. &lt;html&gt;<\/p>\n<pre class=\"result notranslate\">$ nano \/var\/www\/html\/webserver1.com\/public_html\/index.html\r\n&lt;html&gt;\r\n\u00a0 \u00a0&lt;head&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;title&gt;Welcome to webserver1.com!&lt;\/title&gt;\r\n\u00a0 \u00a0&lt;\/head&gt;\r\n\u00a0 \u00a0&lt;body&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;h1&gt;Success! The webserver1.com virtual host is working!&lt;\/h1&gt;\r\n\u00a0 \u00a0&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<pre>class=\"result notranslate\"$ cp \/var\/www\/html\/webserver1.com\/public_html\/index.html \/var\/www\/html\/webserver2.com\/public_html\/index.html<\/pre>\n<p>We can then edit the file and modify the relevant pieces of information:<\/p>\n<pre class=\"result notranslate\">$ nano \/var\/www\/html\/webserver2.com\/public_html\/index.html\r\n&lt;html&gt;\r\n\u00a0 \u00a0&lt;head&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;title&gt;Welcome to webserver2.com!&lt;\/title&gt;\r\n\u00a0 \u00a0&lt;\/head&gt;\r\n\u00a0 \u00a0&lt;body&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;h1&gt;Success! The webserver2.com virtual host is working!&lt;\/h1&gt;\r\n\u00a0 \u00a0&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<h3>Create New Virtual Host Files<\/h3>\n<p>Virtual host files are the configuration files that specify the virtual host and instructs the Apache to respond to various domain requests.<\/p>\n<pre class=\"result notranslate\">$ sudo cp \/etc\/apache2\/sites-available\/000-default.conf \/etc\/apache2\/sites-available\/webserver1.com.conf\r\n$ sudo nano \/etc\/apache2\/sites-available\/webserver1.com.conf\r\n&lt;VirtualHost *:80&gt;\r\n\u00a0 \u00a0ServerAdmin admin@webserver1.com\r\n\u00a0 \u00a0ServerName webserver1.com\r\n\u00a0 \u00a0ServerAlias www.webserver1.com\r\n\u00a0 \u00a0DocumentRoot \/var\/www\/html\/webserver1.com\/public_html\r\n\u00a0 \u00a0ErrorLog ${APACHE_LOG_DIR}\/error.log\r\n\u00a0 \u00a0CustomLog ${APACHE_LOG_DIR}\/access.log combined\r\n&lt;\/VirtualHost&gt;<\/pre>\n<p>Copy First Virtual Host file and customize for Second Domain webserver2.com<\/p>\n<pre class=\"result notranslate\">$ sudo cp \/etc\/apache2\/sites-available\/webserver1.com.conf \/etc\/apache2\/sites-available\/webserver2.com.conf\r\n$ sudo nano \/etc\/apache2\/sites-available\/webserver2.com.conf<\/pre>\n<p>You now need to modify all of the pieces of information to reference your second domain. When you are finished, it may look something like this:<\/p>\n<pre class=\"result notranslate\">&lt;VirtualHost *:80&gt;\r\n\u00a0 \u00a0ServerAdmin admin@webserver2.com\r\n\u00a0 \u00a0ServerName webserver2.com\r\n\u00a0 \u00a0ServerAlias www.webserver2.com\r\n\u00a0 \u00a0DocumentRoot \/var\/www\/html\/webserver2.com\/public_html\r\n\u00a0 \u00a0ErrorLog ${APACHE_LOG_DIR}\/error.log\r\n\u00a0 \u00a0CustomLog ${APACHE_LOG_DIR}\/access.log combined\r\n&lt;\/VirtualHost&gt;<\/pre>\n<h3>Enable the New Virtual Host Files<\/h3>\n<p>Now that we have already created our virtual host files, we must enable these sites. Apache has its own tools that allow us to do this.<\/p>\n<pre class=\"result notranslate\">$ sudo a2ensite webserver1.com.conf\r\n$ sudo a2ensite webserver2.com.conf<\/pre>\n<p>We needed to restart Apache to make these changes take effect:<\/p>\n<pre class=\"result notranslate\">$ sudo service apache2 restart\r\n* Restarting web server apache2\r\nAH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1.\r\nSet the 'ServerName' directive globally to suppress this message.\r\nThis is a harmless message that does not affect our site.<\/pre>\n<h3>Testing the Websites<\/h3>\n<p>As these site\/domains are not the public so no entry will be there to easy the steps we needed to edit the \/etc\/hosts file so that if we browse with domain names webserver1.com and webserver2.com we can see the web pages<\/p>\n<pre class=\"result notranslate\">$ sudo \/etc\/hosts\r\n127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4\r\n::1 localhost localhost.localdomain localhost6 localhost6.localdomain6\r\n192.168.1.84 website1.com\r\n192.168.1.84 website1.com<\/pre>\n<p>Now that you have two hosts configured, you can test your setup easily by opening the favorite browser and open the domains.<\/p>\n<p><strong>http:\/\/webserver1.com<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"fr-fic fr-dib\" src=\"https:\/\/www.tutorialspoint.com\/assets\/questions\/media\/34747\/index1.png\" width=\"600\" height=\"267\" \/><\/p>\n<p><strong>http:\/\/webserver2.com<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"fr-fic fr-dib\" src=\"https:\/\/www.tutorialspoint.com\/assets\/questions\/media\/34747\/index2.png\" width=\"600\" height=\"380\" \/><\/strong><br \/>\nAfter the configuration you should now have a single server handling two separate domain\/site names. You can expand this process by following the steps we outlined above to make additional virtual hosts. There is no software limit for configuring the domain names Apache can handle any no of sites, we can make as many as sites\/domains till your servers are capable of handling them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Setup Virtual Hosts with Apache Web Server on Linux In this article, you will learn about how to set up Apache virtual hosts on an Ubuntu Linux. During this process, you will learn how to serve different sites to different users depending on the domain\/site they are requesting. Apache Web Server The Apache [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":621,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"class_list":["post-1593","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/1593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1593"}],"version-history":[{"count":8,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/1593\/revisions"}],"predecessor-version":[{"id":1601,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/1593\/revisions\/1601"}],"up":[{"embeddable":true,"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=\/wp\/v2\/pages\/621"}],"wp:attachment":[{"href":"https:\/\/helia.ee\/koolitus\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}