What is Apache?
Apache is a wonderful application software. It is the most widely used web server application in the world with over 50% participation in the commercial web server market. Apache is the most widely used web server application on Unix operating systems, but can be used on almost any platform, including Windows, OS X, OS / 2, and so on.
What is a virtual host?
The Apache web server can host multiple websites on a single server. You do not need a separate server and Apache software for each website. Perhaps you already know the virtual host of the VHost concept.
Prerequisites
Before starting this tutorial, it should not be a non-root stand-alone account, that is, users with sudo privileges configured on the server. You can check the weather for the hard drive configured for Ubuntu 16.04.
Step 1. Install the Apache Web Server.
The Apache Web Server is one of the most popular web servers in the world. This is well documented. We can only install Apache with the Ubuntu package manager, apt. The package manager will install it, the Ubuntu software. You can simply choose here.
We can first write the following commands:
1 2 |
sudo apt-get update sudo apt-get install apache2 |
Now, Apache must be installed and able to provide HTML pages from the default document in / var / www / html.
You can do this by contacting http://127.0.0.1 or http: // localhost / on the local computer.
If Apache is installed, connect two Ubuntu machines via SSH or open a terminal session (according to Ubuntu Desktop), then follow these steps to configure a new virtual host.
Step 2. Create a directory
Assuming that scipthere.com is our virtual host website, we create a directory under / var / www, called scipthere.com.
1 |
sudo mkdir /var/www/scripthere.com |
Step 3: Create a test HTML file.
Create an HTML file called index.html in the root directory created in the previous step and add the following HTML code.
1 |
sudo vim /var/www/scripthere.com/index.html |
Add the sample html content to the file and then save it.
1 2 3 4 5 6 7 8 |
<html> <head> <title>scripthere.com</title> </head> <body> scripthere.com </body> </html> |
Step 4. Create a virtual host file.
Apache comes with a default virtual host file named 000-default.conf, which can be used as a template for new virtual hosts. It is a normal virtual host. Att acts as a placeholder, but is not associated with the virtual host.
Copy the default virtual host with the following command:
1 |
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/scripthere.com.conf |
Open the virtual host file:
1 |
sudo vim /etc/apache2/sites-available/scripthere.com.conf |
The file should look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet |
As you can see in the DocumentRoot directive, files are provided from / var / www / html. We were inherited in the directory we thing on our stack. For example:
1 |
DocumentRoot /var/www/scripthere.com |
We also need to add a new ad that assigns the scripthere.com domain to this virtual host. This is done using the ServerName and ServerAlias directives. For example:
1 2 |
ServerName scripthere.com ServerAlias www.scripthere.com |
The final file should look like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. ServerName scripthere.com ServerAlias www.scripthere.com ServerAdmin webmaster@localhost DocumentRoot /var/www/scripthere.com # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet |
Step 5:Enable the New Virtual Host File
1 |
$sudo a2ensite scripthere.com.conf |
When you are finished, you need to restart Apache to make these changes take effect:
1 |
$sudo service apache2 reload |
Step 6: Add domain information to the local host file:
1 2 3 |
$sudo vim /etc/hosts //Add following line to the end of hosts file 127.0.0.1 scripthere.com |
Step 7: Check the site
Open a browser then type your domain name to test whether its working or not.
http://scripthere.com