A virtual host is often used for Web services that provide multiple domain names on a single IP address. This is useful if someone wants to run multiple Web sites on a single VPS with a single IP address. In this tutorial, let me show you how to set up a virtual host on Ubuntu 14.04 lts Apache Web server. Please note that this tutorial is for the 32-bit version of Ubuntu14.04 only.
I do not guarantee that it can also work in other lower Ubuntu versions or Ubuntu derivative versions (although the process may be similar).
Scheme
In this tutorial, I will use Ubuntu 14.04 32-bit LTS and build 2 test sites named "Unixmen1.local" and "unixmen2.local" respectively. My test machine is 192.168.1.250/ 24 and Server.unixmen.local. You can change the virtual domain name according to your needs.
Install Apache Web server
Before installing the Apache server, let's update our Ubuntu server:
- sudo apt-get update
Then, use the following command to install the Apache network server:
- sudo apt-get install apache2
After installing the Apache server, let us test whether the Web server is working properly through this URL http//Your server's IP address/
As you can see, the Apache server is already working.
Set up a virtual host
1. Create a virtual directory
Now, let's continue installing the virtual host. As I mentioned earlier, I'm going to create a new 2 virtual host named "Unixmen1.local" and "unixmen2.local", respectively.
Create a public folder to hold the data for both virtual hosts.
First, let's create a directory for the unixmen1.local site:
- sudo mkdir-p/var/www/unixmen1.local/public_html
Next, create a directory for the for unixmen2.local site:
- sudo mkdir-p/var/www/unixmen2.local/public_html
2. Setting up owners and permissions
The above directory now has only root permissions. We need to modify the ownership of these 2 directories to the average user, not just the root user.
- sudo chown-r $USER: $USER/var/www/unixmen1.local/public_html/
- sudo chown-r $USER: $USER/var/www/unixmen2.local/public_html/
The "$USER" variable points to the current logged-on user.
Set read and Write permissions to the Apache Web page root directory (/VAR/WWW) and its subdirectories so that everyone can read files from the directory.
- sudo chmod-r 755/var/www/
In this way, we create some folders to hold the network-related data and assign the necessary permissions and the owning user.
3. Create a sample page for a virtual host
Now, let's add a sample page to the site. In the first step, let's create a sample page for the virtual host unixmen1.local.
Create a sample page for the unixmen1.local virtual host,
- sudo vi/var/www/unixmen1.local/public_html/index.html
Add the following content:
-
-
- <title>www.unixmen1.local</title>
-
- <body>
-
- </body>
-
Save and close the file.
Similarly, add the sample page to the second virtual host.
- sudo vi/var/www/unixmen2.local/public_html/index.html
Add the following content:
-
-
- <title>www.unixmen2.local</title>
-
- <body>
-
- </body>
-
Save and close the file.
4. Create a virtual host configuration file
By default, Apache has a default virtual host file called 000-default.conf. We will copy the contents of the 000-default.conf file into our new virtual host configuration file.
- sudo cp/etc/apache2/sites-available/000-default.conf/etc/apache2/sites-available/unixmen1.local.conf
- sudo cp/etc/apache2/sites-available/000-default.conf/etc/apache2/sites-available/unixmen2.local.conf
Ensure that the end of the virtual host configuration file contains the. conf extension.
Now, modify the unximen1.local.conf file to meet your needs.
- sudo vi/etc/apache2/sites-available/unixmen1.local.conf
Make related changes directly in the UNIXMEN1 site (note: Comment lines that begin with "#" can be ignored.) )。
- <virtualhost *:80>
- # The SERVERNAME directive sets the request scheme, hostname and port that
- # The server uses to identify itself. This was used when creating
- # redirection URLs. In the context of the virtual hosts, the ServerName
- # specifies hostname must appear in the request ' s Host:header to
- # match this virtual host. For the default virtual host (this file) this
- # value was not decisive as it was used as a last resort host regardless.
- # However, must set it for any further virtual host explicitly.
- #ServerName www.example.com
- ServerAdmin [email protected]
- ServerName unixmen1.local
- Serveralias www.unixmen1.local
- Documentroot/var/www/unixmen1.local/public_html
- # Available Loglevels:trace8, ..., Trace1, debug, info, notice, warn,
- # error, Crit, alert, Emerg.
- # It's 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 more configuration files from conf-available/, which is
- # enabled or disabled at a global level, it's 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 had been globally disabled with "a2disconf".
- #Include conf-available/serve-cgi-bin.conf
- </VirtualHost>
Similarly, modify the second host file.
- sudo vi/etc/apache2/sites-available/unixmen2.local.conf
Make the related changes present at the Unixmen2 site.
- <virtualhost *:80>
- # The SERVERNAME directive sets the request scheme, hostname and port that
- # The server uses to identify itself. This was used when creating
- # redirection URLs. In the context of the virtual hosts, the ServerName
- # specifies hostname must appear in the request ' s Host:header to
- # match this virtual host. For the default virtual host (this file) this
- # value was not decisive as it was used as a last resort host regardless.
- # However, must set it for any further virtual host explicitly.
- #ServerName www.example.com
- ServerAdmin [email protected]
- ServerName unixmen2.local
- Serveralias www.unixmen2.local
- Documentroot/var/www/unixmen2.local/public_html
- # Available Loglevels:trace8, ..., Trace1, debug, info, notice, warn,
- # error, Crit, alert, Emerg.
- # It's 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 more configuration files from conf-available/, which is
- # enabled or disabled at a global level, it's 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 had been globally disabled with "a2disconf".
- #Include conf-available/serve-cgi-bin.conf
- </VirtualHost>
After you modify the virtual host file, disable the default virtual host configuration (000.default.conf), and then enable the new virtual host configuration as shown below.
- sudo a2dissite 000-default.conf
- sudo a2ensite unixmen1.local.conf
- sudo a2ensite unixmen2.local.conf
Finally, restart the Apache server.
- sudo service apache2 restart
That's it. Now we have successfully configured the Apach virtual host on our Ubuntu server
Testing a virtual Host
Edit/etc/hosts file,
- sudo vi/etc/hosts
Add the virtual domain name as shown below at the end of the file.
- 192.168.1.250 unixmen1.local
- 192.168.1.250 unixmen2.local
Save and close the file.
Open your browser and access http://unixmen1.local or http://unixmen2.local. You will see the sample page we created earlier.
Unixmen1.local test page:
Unixmen2.local test Page
If you want to access these sites from your remote system, you need to add the actual domain name records to your DNS server. However, I do not have a real domain name and DNS server, I just want to pass my local system test, then it just works as I wish.
cheers!
Ubuntu set Apache Virtual host