What the Red MeansThe lines that's user needs to enter or customize'll be in red in this tutorial!
The rest should mostly be copy-and-pastable.
About Virtual HostsVirtual Hosts are used to run more than one domain off the a single IP address. This is especially useful to people who need to run several sites off of one virtual private server. The sites display different information to the visitors and depending on with which the users accessed the site. There is no limit to the number of virtual hosts so can be added to a VPS.
Set upThe steps in this tutorial require is the user to have root privileges. You can have a Initial Server Setup. You can implement whatever username suits.
Additionally, you are need to have Apache already installed and running on your virtual server
If It is not the case, can download it and this command:
sudo apt-get install apache2
Step one-create a New DirectoryThe "the" in creating a virtual host is to a create a directory where we'll keep the new website ' s information.
This location'll is your Document Root in the Apache virtual configuration file later on. By adding a-p to the line of code, the command automatically generates all the parents for the new directory.
sudo mkdir-p/var/www/example.com/public_html
You'll need to designate the actual DNS approved domain, or an IP addresses, to test this a virtual host is working. In this tutorial we'll use example.com as a placeholder for a correct domain name.
However, should you want to use a unapproved domain name to test the process you'll find information in how to make it Work on your The local computer into step Seven.
Step two-grant PermissionsWe need to grant ownership of the directory to the user, instead of just keeping it on the root system.
Additionally, it is important to make sure that everyone would be able to read our new files.
sudo chmod-r 755/var/www
Now you are are all do with permissions.
Step three-create the PageWithin Our configurations directory, we are need to create a new file called index.html
sudo nano/var/www/example.com/public_html/index.html
We can add some text to the ' file so we'll have something to look in when the IP redirects to the virtual host.
Save and Exit
Step four-create The New Virtual Host FileThe next step are to set up the Apache configuration. We ' re going to work off a duplicate-go ahead and make a copy of the file (naming it after your domain name) in the same di Rectory:
sudo cp/etc/apache2/sites-available/default/etc/apache2/sites-available/example.com
Step Five-turn on Virtual HostsOpen up the new config file:
sudo nano/etc/apache2/sites-available/example.com
We are going to set up a virtual host with this file.
The ' The ' the ' is ' to insert ' a line ' for the ServerName under ' ServerAdmin line.
The ServerName specifies the domain name that virtual host uses.
If you are want to make your site accessible from more than one name (for example, with www. URL), you can include the A Lternate names in your virtual host file by adding a serveralias line. The beginning of your virtual host file would then look like this:
<virtualhost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
www.example.com
[...]
The next step are to fill in the correct Document Root. For this section, write in the extension's new directory created in step one. If the document root is incorrect or absent you are not being able to set up the virtual host.
The section should look like this:
You don't need to make any other changes to this file. Save and Exit.
The last of the step was to activate the host and the built in Apache shortcut:
sudo a2ensite example.com
Step Six-restart ApacheWe ' ve made a lot of the the changes to the configuration, and the virtual host was set up. However None of the changes that we made'll take effect until Apache is restarted. Use the command to restart Apache:
sudo service apache2 restart
You may have an error along the lines of
The message is just a warning, and you are able to access your virtual host without any further issues.
Optional Step seven-setting up the local HostsIf you are have pointed your domain name to your virtual private server's IP address can skip this step-you does not need to Set up the local hosts. Your virtual hosts should work. However, if want to try out your new virtual hosts without have to connect to a actual domain name, can set up Loca L hosts on your computer alone.
For this step, make sure your are on the computer itself, not your droplet.
To proceed with this step for need to know your computer ' s administrative password, otherwise An actual domain name to test the virtual hosts.
If you are are on a Mac or Linux, access the root user (SU) in the computer and open up your Hosts file:
If you are are on a Windows Computer, you can find the directions to alter the host file on the Microsoft site
You can add the local hosts details to this file, as seen in the example below. As long as that's there, directing your browser toward, say, example.com'll give you all the virtual host details For the corresponding IP address.
# Host Database
#
localhost is used to configure the loopback interface ' The
system is booting. Do don't change this entry.
# #
127.0.0.1 localhost
#Virtual Hosts
12.34.56.789 example.com
However, it may is a good idea to delete this made up addresses out of the The local hosts folder as you are do to avoid Any future confusion.
Step eight-results:see Your Virtual Host ActionOnce you have finished setting up your virtual host, you can have a it looks online. Type your IP address into the browser (ie. http://12.34.56.789)
It should look somewhat similar to my handy screenshot
Good job!
creating more Virtual HostsTo add more virtual hosts, your can just repeat the process above, being careful to set up a new document root with the app Ropriate domain name, and then creating and activating the new virtual host file.
MoreOnce you have set up your virtual hosts, you can proceed to Create a SSL certificate for your site or Install a FTP serve R