How to setup a name based VirtualHost on linux mint 18.3
To set up a name based VirtualHost on Linux Mint 18.3
- Virtual hosting is a method for hosting multiple domain names on a single server. In this tutorial, the method to set up a name based Virtual host on Linux Mint 18.3 is covered.
- The are two types of virtual hosting: “ Name-based virtual hosting” and “ IP-based virtual hosting.” With the name-based virtual hosting, you can host multiple websites on a single machine with a single IP address.
- Here, we are going to host two websites &ndash namely www.virtualhost1.com and www.virtualhost2.com &ndash on a single IP on the Apache web server & the concept is applicable to all the Linux Distributions.
Setting Up
First, you need to update your system repository and then install Apache web server.
linuxhelp ~ # apt-get update
Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://archive.canonical.com/ubuntu xenial InRelease [11.5 kB]
Ign:3 http://packages.linuxmint.com sylvia InRelease
Get:4 http://archive.canonical.com/ubuntu xenial/partner amd64 Packages [3,128 B]
Get:5 http://archive.canonical.com/ubuntu xenial/partner i386 Packages [3,116 B]
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Get:7 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
.
.
t:30 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [7,084 B]
Get:31 http://archive.ubuntu.com/ubuntu xenial-backports/universe i386 Packages [7,060 B]
Get:32 http://security.ubuntu.com/ubuntu xenial-security/multiverse i386 Packages [3,376 B]
Get:33 http://archive.ubuntu.com/ubuntu xenial-backports/universe Translation-en [3,812 B]
Fetched 5,509 kB in 10s (549 kB/s)
Reading package lists... Done
Let' s install apache web server now.
linuxhelp ~ # apt-get install apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.1-0
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.1-0
0 upgraded, 9 newly installed, 0 to remove and 282 not upgraded.
Need to get 1,540 kB of archives.
After this operation, 6,369 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
.
.
Enabling conf other-vhosts-access-log.
Enabling conf security.
Enabling conf serve-cgi-bin.
Enabling site 000-default.
Processing triggers for libc-bin (2.23-0ubuntu9) ...
Processing triggers for systemd (229-4ubuntu21) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...
Verify by checking the status of apache service.
linuxhelp ~ # systemctl status apache2.service
● apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2 bad vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Mon 2018-03-26 08:01:26 IST 59s ago
Docs: man:systemd-sysv-generator(8)
CGroup: /system.slice/apache2.service
├─31119 /usr/sbin/apache2 -k start
├─31122 /usr/sbin/apache2 -k start
└─31123 /usr/sbin/apache2 -k start
Mar 26 08:01:25 linuxhelp systemd[1]: Starting LSB: Apache2 web server...
Mar 26 08:01:25 linuxhelp apache2[31097]: * Starting Apache httpd web server apache2
Mar 26 08:01:25 linuxhelp apache2[31097]: AH00558: apache2: Could not reliably determine the server' s fully qualified domain name, using 127.0.1.
Mar 26 08:01:26 linuxhelp apache2[31097]: *
Mar 26 08:01:26 linuxhelp systemd[1]: Started LSB: Apache2 web server.
To verify whether the Web server is working or not, open the web browser and navigate to the URL http://your-ip-address.
Create two directories for the websites www.virtualhost1.com and www.virtualhost2.com.
linuxhelp ~ # mkdir -p /var/www/html/www.virtualhost1.com
linuxhelp ~ # mkdir -p /var/www/html/www.virtualhost2.com
Now you need to create an “ index.html” file for both websites. This will be served by the Apache web server. Create an index.html file for “ www.virtualhost1.com” virtual host:
linuxhelp ~ # vim /var/www/html/www.virtualhost1.com/index.html
Add the following content:
< html>
< head>
< title> www.virtualhost1.com< /title>
< /head>
< body>
< h1> Welcome To www.virtualhost1.com website< /h1>
< /body>
< /html>
~
Similarly, create an index.html file for “ www.virtualhost2.com” virtual host
linuxhelp ~ # vim /var/www/html/www.virtualhost2.com/index.html
add the following lines
< html>
< head>
< title> www.virtualhost2.com< /title>
< /head>
< body>
< h1> Welcome To www.virtualhost2.com website< /h1>
< /body>
< /html>
Save and close the file.
You must change the ownership of these two virtual directories to “ www-data,” so that Apache can read and write data.
linuxhelp ~ # chown -R www-data:www-data /var/www/html/www.virtualhost1.com/
linuxhelp ~ # chown -R www-data:www-data /var/www/html/www.virtualhost2.com/
Also, you need to make the Apache web root (/var/www/html) directory world readable so that everyone can read files from that directory.
linuxhelp ~ # chmod -R 755 /var/www/html/
By default, Apache comes with a default virtual host file called “ 000-default.conf.” You need to disable this virtual host file first.
linuxhelp ~ # a2dissite 000-default.conf
Site 000-default disabled.
To activate the new configuration, you need to run:
service apache2 reload
Now, create a virtual host file “ www.virtualhost1.com.conf” for the virtual host www.virtualhost1.com.
Add the following lines.
linuxhelp ~ # vim /etc/apache2/sites-available/www.virtualhost1.com.conf
< VirtualHost *:80>
ServerAdmin admin@virtualhost1.com
ServerName www.virtualhost1.com
DocumentRoot /var/www/html/www.virtualhost1.com
ErrorLog ${APACHE_LOG_DIR}/www.virtualhost1.com_error.log
CustomLog ${APACHE_LOG_DIR}/www.virtualhost1.com_access.log combined
< /VirtualHost>
~
Similarly, create a virtual host file “ www.virtualhost2.com.conf” for the virtual host www.virtualhost2.com.
vim /etc/apache2/sites-available/www.virtualhost2.com.conf
< VirtualHost *:80>
ServerAdmin admin@virtualhost2.com
ServerName www.virtualhost2.com
DocumentRoot /var/www/html/www.virtualhost2.com
ErrorLog ${APACHE_LOG_DIR}/www.virtualhost1.com_error.log
CustomLog ${APACHE_LOG_DIR}/www.virtualhost1.com_access.log combined
< /VirtualHost>
Save and close the file.
After creating the virtual host files, you need to enable a new virtual host.
linuxhelp ~ # a2ensite www.virtualhost1.com.conf Enabling site www.virtualhost1.com. To activate the new configuration, you need to run: service apache2 reload linuxhelp ~ # a2ensite www.virtualhost2.com.conf Enabling site www.virtualhost2.com. To activate the new configuration, you need to run: service apache2 reload
Finally, restart the Apache service.
linuxhelp ~ # systemctl restart apache2.service
You need to add a host entry on each and every remote or local system to resolve the website by name. You can do this by editing the “ /etc/hosts” file.
linuxhelp ~ # vim /etc/hosts
192.168.7.234 www.virtualhost1.com
192.168.7.234 www.virtualhost2.com
Save and close the file.
Open your web browser and navigate to the URLs “ http://www.virtualhost1.com” and “ http://www.virtualhost2.com.” You should see the sample demo pages which we created earlier.
With this, the method to setup a name based Virtual host on Linux Mint 18.3 comes to an end.
1. open a "/etc/hosts" file -> "vim /etc/hosts" (or) "nano /etc/hosts"
2. Add the line into the file as follow -> "0.0.0.0 www.example.com" (or) "0.0.0.0 www.test.com"
When a request arrives, the server will find the best (most specific) matching
If you omit the ServerName directive from any name-based virtual host, the server will default to a fully qualified domain name (FQDN) derived from the system hostname. This implicitly set server name can lead to counter-intuitive virtual host matching and is discouraged.
1. A server running CentOS v. 7 with Apache installed
2. A desktop machine running Linux
3. A static IP address.
"
ServerName www.domain.tld
ServerPath /domain
DocumentRoot /web/domain