| 89 | Using virtual hosts, one Apache instance can serve multiple websites. Each domain or individual site that is configured using Apache will direct the visitor to a specific directory holding that site’s information. This is done without indicating to the visitor that the same server is also responsible for other sites. |
| 90 | |
| 91 | Note: Before creating a virtual host for a web site domain, the particular domain or subdomain should be created. For this please contact your domain manager of your institutional IT department or relevant domain company. Then this domain should be assigned a DNS entry (A or AAAA record) to work on a web browser. In our case DNS entry should be pointed to your server IP. |
| 92 | |
| 93 | Presuming you meet the prerequisites below will be the steps to create virtual hosts. |
| 94 | |
| 95 | First you need to create a appropriate directory structure for each website hosted under each domain. By default root web directory in a Ubuntu system will be /var/www/html. For our setup we will use below directory structure for each site. |
| 96 | |
| 97 | web site for domain 1 - /var/www/your_domain_1/public_html |
| 98 | web site for domain 2 - /var/www/your_domain_2/public_html |
| 99 | web site for domain 3 - /var/www/your_domain_3/public_html |
| 100 | |
| 101 | In our example we hope to create three subdomains called web,sales and lms. So the full domain names will be web.your_domain.com, sales.your_domain.com and lms.your_domain.com respectively. |
| 102 | Use these commands, with your own domain names, to create your directories: |
| 103 | |
| 104 | {{{ |
| 105 | sudo mkdir -p /var/www/web.your_domain/public_html |
| 106 | sudo mkdir -p /var/www/sales.your_domain/public_html |
| 107 | sudo mkdir -p /var/www/lms.your_domain/public_html |
| 108 | }}} |
| 109 | |
| 110 | We are going to install moodle under lms subdomain. For other two domains we will create dummy web pages for each. |
| 111 | |
| 112 | {{{ |
| 113 | sudo nano /var/www/web.your_domain/public_html/index.html |
| 114 | }}} |
| 115 | |
| 116 | {{{ |
| 117 | <!DOCTYPE html> |
| 118 | <html lang="en" dir="ltr"> |
| 119 | <head> |
| 120 | <meta charset="utf-8"> |
| 121 | <title>Welcome to WEB</title> |
| 122 | </head> |
| 123 | <body> |
| 124 | <h1>Success! WEB home page!</h1> |
| 125 | </body> |
| 126 | </html> |
| 127 | }}} |
| 128 | |
| 129 | Also create a similar page for the sales sub-domain. |
| 130 | |
| 131 | Once created with a sample web site we have to change ownership of the directories to the web server user. |
| 132 | |
| 133 | {{{ |
| 134 | sudo chown -R www-data: /var/www/* |
| 135 | }}} |
| 136 | |
| 137 | Now we are done with creating web sites and directories. Now we need to do the actual apache server configurations for the virtual hosting. |
| 138 | |
| 139 | On Ubuntu systems, Apache Virtual Hosts configuration files are located in /etc/apache2/sites-available directory. They can be enabled by creating symbolic links to the /etc/apache2/sites-enabled directory, which Apache read during the startup. Here we need to create a seperate configuration file for each domain/sub-domain as below. |
| 140 | |
| 141 | {{{ |
| 142 | sudo nano /etc/apache2/sites-available/web.your_domain.com.conf |
| 143 | }}} |
| 144 | |
| 145 | And add the below configurations, |
| 146 | |
| 147 | {{{ |
| 148 | <VirtualHost *:80> |
| 149 | ServerName web.dhammikalalantha.com |
| 150 | ServerAlias web.dhammikalalantha.com |
| 151 | ServerAdmin webmaster@dhammikalalantha.com |
| 152 | DocumentRoot /var/www/web.dhammikalalantha.com/public_html |
| 153 | |
| 154 | <Directory /var/www/web.dhammikalalantha.com/public_html> |
| 155 | Options -Indexes +FollowSymLinks |
| 156 | AllowOverride All |
| 157 | </Directory> |
| 158 | |
| 159 | ErrorLog ${APACHE_LOG_DIR}/web.dhammikalalantha.com-error.log |
| 160 | CustomLog ${APACHE_LOG_DIR}/web.dhammikalalantha.com-access.log combined |
| 161 | </VirtualHost> |
| 162 | }}} |
| 163 | |
| 164 | Similarly do the necessary configurations for the domain sales. |
| 165 | |
| 166 | {{{ |
| 167 | sudo nano /etc/apache2/sites-available/sales.your_domain.com.conf |
| 168 | }}} |
| 169 | |
| 170 | {{{ |
| 171 | <VirtualHost *:80> |
| 172 | ServerName sales.dhammikalalantha.com |
| 173 | ServerAlias sales.dhammikalalantha.com |
| 174 | ServerAdmin webmaster@sales.dhammikalalantha.com |
| 175 | DocumentRoot /var/www/sales.dhammikalalantha.com/public_html |
| 176 | |
| 177 | <Directory /var/www/sales.dhammikalalantha.com/public_html> |
| 178 | Options -Indexes +FollowSymLinks |
| 179 | AllowOverride All |
| 180 | </Directory> |
| 181 | |
| 182 | ErrorLog ${APACHE_LOG_DIR}/sales.dhammikalalantha.com-error.log |
| 183 | CustomLog ${APACHE_LOG_DIR}/sales.dhammikalalantha.com-access.log combined |
| 184 | </VirtualHost> |
| 185 | }}} |
| 186 | |
| 187 | Once we do the configurations we have to enable the created sites as below, |
| 188 | |
| 189 | {{{ |
| 190 | sudo a2ensite web.dhammikalalantha.com |
| 191 | sudo a2ensite sales.dhammikalalantha.com |
| 192 | }}} |
| 193 | |
| 194 | Once done, test the configuration for any syntax errors with. |
| 195 | {{{ |
| 196 | sudo apachectl configtest |
| 197 | }}} |
| 198 | |
| 199 | Restart the Apache service for the changes to take effect. |
| 200 | {{{ |
| 201 | sudo systemctl restart apache2 |
| 202 | }}} |
| 203 | |
| 204 | Now that you have your virtual hosts configured, you can test your setup by going to the domains that you configured in your web browser. Below will be the output for the sites we created. |
| 205 | |
| 206 | [[Image(https://ws.learn.ac.lk/raw-attachment/wiki/Csle2022/Agenda/databaseandweb/web4.png)]] |
| 207 | |
| 208 | Also check the other site created. |