WDL Demo Rss

Cómo configurar Apache Virtual Hosts en Ubuntu 12.04

Etiquetas: , ,

Vamos a explicar como crear un virtualhost en una pc para desarrollo.
Se tendrá como base un sistema operativo Ubuntu 12.04 con Apache y PHP

¿Qué son los Virtual Hosts?Los Virtual Hosts son usados normalmente para correr mas de un dominio sobre una misma dirección IP. Esto es muy util para los que necesitan correr varios sitios en un solo VPS. Los sitios de muestran segun a que dominio solicitaron. No hay limities en cuanto a Virtual Hosts se pueden agregar en un VPS

Set UpEn este paso vamos a requerir privilegios. Luego debemos tener instalado Apache en nuestro sistema o VPS:
sudo apt-get install apache2

Paso 1 - Creamos nuestro directorio

El primer paso será crear un directorio para nuestro Virtual Host donde alojaremos nuestro sitio.

Este será nuestro Document Root en el archivo de configuracion virtual de Apache. Adicionando el parámetro -p nos creara el directorio y los padres en caso de no existeir para nuestro nuevo directorio.
sudo mkdir -p /var/www/example.com/public_html

Ahora necesitaras designar unos DNS o una IP para testear que el virtual host funciona. En este tutorial usamos el dominio example.com como placeholder.

De todas formas, puedes usar un dominio inexistente para testear localmente el proceso, esto lo podremos ver en el Paso 7.

Paso 2 - PermisosNecesitamos dar el ownership del directorio al usuarioWe need to grant ownership of the directory to the user, instead of just keeping it on the root system.
 sudo chown -R $USER:$USER /var/www/example.com/public_html 

Ademas, es importante asegurarnos de que podamos acceder y leer los archivos
 sudo chmod -R 755 /var/www

Ahora si tenemos ya los permisos.

Paso 3 - Crear el sitio o la pagina

Con la configuración de nuestro directorio necesitamos crear un nuevo archivo llamado index.html
sudo nano /var/www/example.com/public_html/index.html

Podemos agregar algo de texto para poder visualizar cuando nos redireccione al virtual hostWe can add some text to the file so we will have something to look at when the IP redirects to the virtual host.
<html>
  <head>
    <title>www.example.com</title>
  </head>
  <body>
    <h1>Success: You Have Set Up a Virtual Host</h1>
  </body>
</html>

Save and Exit

Step Four—Create the New Virtual Host File


The next step is 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 directory:
 sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example.com

Step Five—Turn on Virtual Hosts


Open up the new config file:
 sudo nano /etc/apache2/sites-available/example.com

We are going to set up a virtual host in this file.
The first step is to insert a line for the ServerName under the ServerAdmin line.
  ServerName example.com 

The ServerName specifies the domain name that the virtual host uses. 

If you want to make your site accessible from more than one name (for example, with www in the URL), you can include the alternate 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
        ServerAlias www.example.com
  [...]

The next step is to fill in the correct Document Root. For this section, write in the extension of the new directory created in Step One. If the document root is incorrect or absent you will not be able to set up the virtual host.
The section should look like this:
 DocumentRoot /var/www/example.com/public_html 

You do not need to make any other changes to this file. Save and Exit.
The last step is to activate the host, with the built in apache shortcut:
 sudo a2ensite example.com

Step Six—Restart Apache


We’ve made a lot of the changes to the configuration, and the virtual host is set up. However none of the changes that we made will take effect until Apache is restarted. Use this command to restart apache:
 sudo service apache2 restart

You may see an error along the lines of
Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName 

The message is just a warning, and you will be able to access your virtual host without any further issues.

Optional Step Seven—Setting Up the Local Hosts


If you have pointed your domain name to your virtual private server’s IP address you can skip this step—you do not need to set up local hosts. Your virtual hosts should work. However, if want to try out your new virtual hosts without having to connect to an actual domain name, you can set up local hosts on your computer alone.

For this step, make sure you are on the computer itself, not your droplet.

To proceed with this step you need to know your computer’s administrative password, otherwise you will be required to use an actual domain name to test the virtual hosts.

If you are on a Mac or Linux, access the root user (su) on the computer and open up your hosts file:
nano /etc/hosts 

If you 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 line is there, directing your browser toward, say, example.com will give you all the virtual host details for the corresponding IP address.

# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost

#Virtual Hosts 
12.34.56.789    example.com

However, it may be a good idea to delete these made up addresses out of the local hosts folder when you are done to avoid any future confusion.

Step Eight—RESULTS: See Your Virtual Host in Action


Once you have finished setting up your virtual host, you can see how 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 Hosts


To add more virtual hosts, you can just repeat the process above, being careful to set up a new document root with the appropriate domain name, and then creating and activating the new virtual host file.

Fuente: How To Set Up Apache Virtual Hosts on Ubuntu 12.04 LTS

Comments (0)

Publicar un comentario