+3 votes
646 views
by
How to configure Apache Virtualhost Ubuntu 14.04 LTS?

1 Answer

+4 votes
by (40.6k points)

Introduction

The Apache web server is one of the most popular website to provide Internet content. It has more than half of all active web sites on the net and is extremely powerful and flexible.

Apache functionality and components divided into separate units which can be configured independently. The basic unit describing an individial site or the virtual domain named host.

These mappings allow the administrator to use a server to host multiple domains or sites on a single IP interface or using a mechanism matches. This is relevant for anyone looking for accommodation for more than one site in a single VPS.

Each domain is configured target visitors to a specific folder that contains the information from the site, he never indicated that the same server is responsible for other sites. This scheme is unlimited expandable software as well as the server can handle the load.

In this guide, we'll tell you how you can configure your Apache virtual hosts on your VPS with Ubuntu 14.04. During this process, you will learn how to set up different content to different visitors depending on the domain that request.

Prerequisites
Before starting this tutorial, you should create a non-root user by following steps 1 to 4 in this guide.

You also need to have Apache installed to continue the following steps. If you have not already, you can install Apache on your server using apt-get:

sudo apt-get update
sudo apt-get install apache2
After completing these steps, we can espezar.

For purposes of this guide, my settings to create a virtual host and one for pruebas.com ejemplo.com. reference to them will be made in this guide, but you should replace them with your own domains in the process.

To learn how to configure your domains with DigitalOcean, follow this link. If you do not have to use available domains, you can use any values.

We'll show you how to edit your local then test files setting if you are using any values. This allows you to test your setup at home, even if your content is not available through the domain to other visitors.

Step One - Create the directory structure
The first step we need to create the directory structure to keep the information on our site.

Our document root (the home directory in which Apache seeks to show content) will be set on individual folders within the / var / www. Here we will create directories for the two virtual hosts we want to configure.

Within each of these directories we will create a directory called public_html which will keep the public information of the site and their respective files. This will give us more flexibility in our accommodations.

To be sure, for each of our sites, we will create directories as follows:
For instance, for our sites, we're going to make our directories like this:

sudo mkdir -p /var/www/ejemplo.com/public_html
sudo mkdir -p /var/www/pruebas.com/public_html
The red marks represent the domain that we hope our VPS.

Step Two - Grant Permissions
Now we have the directory structure for Neustr files, but the root user owns them. If we want our regular user can modify the files in our web directory, we need to change the owner doing the following:

sudo chown -R $ USER: $ USER /var/www/ejemplo.com/public_html
sudo chown -R $ USER: $ USER /var/www/pruebas.com/public_html
$ USER variable will take the value of the user with which you are currently identified. In doing so, our regular user now owns the public_html directories where our content will be stored.

We must also change the permissions a little to make sure that the Read permission can be applied to files and directories so that the pages can be deployed successfully:

sudo chmod -R 755 / var / www
Your server will now have permission to display the contents, and the user will be able to create content in directories as needed.

Step Three - Create a test page for each Virtual Host
Currently we have the structure in place. We will create content to display.

We're just going to do a show, so our pages are very simple. Just create an index.html file for each site.

Let's start with ejemplo.com. We can open an index.html file by writing editor:

nano /var/www/ejemplo.com/public_html/index.html
In this file, create a simple HTML document indicating that the site is connected. My file was as follows:

<Html>
  <Head>
    <Title> Welcome to Ejemplo.com! </ Title>
  </ Head>
  <Body>
    <H1> Success! The Virtual Host ejemplo.com is working! </ H1>
  </ Body>
</ Html>
Save and close the file when finished.

We can copy this file and use it as the basis for our second site by typing:

cp /var/www/ejemplo.com/public_html/index.html /var/www/pruebas.com/public_html/index.html
Now we can open the file and modify the relevant information:

nano /var/www/pruebas.com/public_html/index.html
<Html>
  <Head>
    <Title> Welcome to Pruebas.com! </ Title>
  </ Head>
  <Body>
    <H1> Success! The Virtual Host pruebas.com is working! </ H1>
  </ Body>
</ Html>
Save and close as in previous case. Now you have enough pages to test your settings.

Step Four - Create New Virtual Host Files
The Virtual Host files are files that contain information and settings specific to the domain and that tell the Apache server and respond to requests from multiple domains.

Apache includes a default virtual host file called 000-default.conf we can use to skip the point. We will make a copy to work on it and create our Virtual Host for each domain.

We begin with a domain, configure it, copy it to the second domain, and then make the necessary adjustments. The default Ubuntu configuration requires that each configuration file Virtual Host ends in .conf.

Create the Virtual Host File

Starting copy the file to the first domain:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ejemplo.com.conf

Open the new file in your editor as root:

sudo nano /etc/apache2/sites-available/ejemplo.com.conf
This file will look something like this (I removed comments here to make them more readable):

<VirtualHost *: 80>
    ServerAdmin webmaster @ localhost
    DocumentRoot / var / www / html
    ErrorLog $ {} /error.log APACHE_LOG_DIR
    CustomLog $ {} /access.log combined APACHE_LOG_DIR
</ VirtualHost>
As you can see, there is not much here. We will customize the data here for our first domain and add some additional directives. This section of the Virtual Host matches any request that is requested to port 80, the default port for HTTP.

First, we need to change the ServerAdmin directive to a site administrator email you can receive mail.

ServerAdmin [email protected]
After this, we need to add two directives. The first call ServerName, which establishes the basis of the domain must match this virtual host. This will be like your domain. The second, called ServerAlias ​​determines future that can match names and served as the base or primary domain name. This is useful for host type www:

ServerName ejemplo.com
ServerAlias ​​www.ejemplo.com
What remains to be changing for the basic configuration of a Virtual Host is the location of the root directory for the domain. We have created what we need, so we just need to modify DocumentRoot to point to the directory that we created:

DocumentRoot /var/www/ejemplo.com/public_html
In total, our Virtual Host file should look like this:

<VirtualHost *: 80>
    ServerAdmin [email protected]
    ServerName ejemplo.com
    ServerAlias ​​www.ejemplo.com
    DocumentRoot /var/www/ejemplo.com/public_html
    ErrorLog $ {} /error.log APACHE_LOG_DIR
    CustomLog $ {} /access.log combined APACHE_LOG_DIR
</ VirtualHost>
Save and close the file.

Copy the first Virtual Host File and change to the second domain

Now that we have our first Virtual Host file setup we can create the second copying the first and making the necessary changes.

Let's start by copying:

sudo cp /etc/apache2/sites-available/ejemplo.com.conf /etc/apache2/sites-available/pruebas.com.conf
Open the new file with root privileges on your editor:

sudo nano /etc/apache2/sites-available/pruebas.com.conf
Now we have to change all the pieces of information to refer to the second domain. When finished, it should look something like this:

<VirtualHost *: 80>
    ServerAdmin [email protected]
    ServerName pruebas.com
    ServerAlias ​​www.pruebas.com
    DocumentRoot /var/www/pruebas.com/public_html
    ErrorLog $ {} /error.log APACHE_LOG_DIR
    CustomLog $ {} /access.log combined APACHE_LOG_DIR
</ VirtualHost>
Save and close at the end.

Step Five - Enables new Virtual Host Files
Now we have created our virtual hosts file, we must enable them. Apache includes tools that allow us to do this.

We can use the tool to enable a2ensite each of our sites doing this:

sudo a2ensite ejemplo.com.conf
sudo a2ensite pruebas.com.conf
When you have finished, you must restart Apache to make sure your changes to take effect:

sudo service apache2 restart
You should receive a message similar to this information:

* Restarting web server apache2
 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive to suppress esta message globally
This message does not affect the site.

Step Six - Sets Local Files (Optional)
If you are not using your domain name owned and used for this procedure such domains in place, you can at least make functional tests of this process temporarily modifying the hosts file on your local computer.

This intercept any requests to domains that you configure and point to your VPS, only if you are using domains registered.

This only works over your computer, and it's just useful for testing purposes.

Sure to be working on your local computer to the next steps and not in your VPS. You must know the password of the administrator or a member of the administrative group.

If you're on a Mac or Linux computer, your local file editing privileges by typing:

sudo nano / etc / hosts
If you're on a Windows machine, you can find the instructions to modify your hosts file here.

The details you need to add are the public IP of your VPS followed by the domain you want to point to the VPS.

For domains that we use in this guide, we will assume that our VPS IP is 111.111.111.111, we can add the following lines to the end of the hosts file:

127.0.0.1 localhost
127.0.1.1 guest-desktop
111.111.111.111 ejemplo.com
111.111.111.111 pruebas.com
This directly target any request for ejemplo.com and pruebas.com on our computer and send them to our server 111.111.111.111. This is what we want if we are not owners of these domains yet, only for testing purposes for our Virtual Hosts.

Save and close the file.

Step Sierte - Test your Tesultados
Now that we have our Virtual Hosts configured, we can test setup simply by visiting the domain you have configured through your web browser:

http://ejemplo.com
You should see something like this:

Apache Virtual Host Example

Similarly, if we visit the second page:

http://pruebas.com
You can see the file you created to the second site:

Testing Apache Virtual Host

If both sites works well, then you have two Virtual Hosts configured correctly on the same server.

If you need to adjust the hosts file on your computer, you will probably just delete the lines you added and verify that your configuration works. This prevents your hosts file is full of entries that are not needed.

If you need constant access to these Virtual Host, consider purchasing domains for each site and configured to point to your VPS.

Conclusion
If you followed me, you must have a response to two separate domains server. Now you can expand this process following the steps that have filled up to create additional Virtual Hosts.

No software limit on the number of domains that Apache can handle, so you're free to add as many as your server can handle.

6.5k questions

8.2k answers

191 comments

71 users

Please leave a comment if the solution works or not.
(with device model) [X]Close
Ask a Question
Welcome to Wikiwhy Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...