Virtual Host Apache2 Gnu/Linux

15 de feb. de 2018

hola a todos hoy hablo sobre lo que es un virtualhost en apache2 y como funciona para su respectivo funcionamiento.

bueno asi como el nombre lo dice virtualhost o tambien como en la documentacion oficial de apache2 lo llama host virtual el término Hosting Virtual se refiere a hacer funcionar más de un sitio web (tales como www.company1.com y www.company2.com) en una sola máquina. Los sitios web virtuales pueden estar "basados en direcciones IP", lo que significa que cada sitio web tiene una dirección IP diferente, o "basados en nombres diferentes", lo que significa que con una sola dirección IP están funcionando sitios web con diferentes nombres (de dominio). El hecho de que estén funcionando en la misma máquina física pasa completamente desapercibido para el usuario que visita esos sitios web.

VitualHost o Hosting Virtual

ejemplos :

<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 example.com
ServerAlias www.example.com

ServerAdmin correoAdmin@gmail.com

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>

bueno aca vemos el primer ejemplo de un virtual host basico o mas bien el que esta por defecto ahora veamos otros.

este ejemplo que vemos ahora es con reverse proxy:

<VirtualHost *:80>
    ServerName example.algo.org
    Redirect permanent / http://example.organizacion.algo.edu.bo
</VirtualHost>

<VirtualHost *:80>

ServerName example.organizacion.algo.edu.bo

ServerAdmin correoAdmin@organizacion.algo.edu.bo
#DocumentRoot /var/www/html

#HTTP proxy/gateway server
ProxyRequests off
ProxyPass / http://127.0.0.1:2368/
ProxyPassReverse / http:/127.0.0.1:2368/


ErrorLog ${APACHE_LOG_DIR}/example_organizacion_error.log
CustomLog ${APACHE_LOG_DIR}/example_organizacion_access.log combined


RewriteEngine on
RewriteCond %{SERVER_NAME} =organizacion.algo.edu.bo
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

ahora veamos otro ejemplo con reverse proxy:

<VirtualHost *:80>
  ServerName dominioalgo.info
  ServerAlias www.dominioalgo.info
  
  DocumentRoot /home/user/proyecto/public

  <Location /assets>
    ProxyPass !
  </Location>
  <Location /system>
    ProxyPass !
  </Location>

  ProxyPass / http://127.0.0.1:3000/
  ProxyPassReverse / http://127.0.0.1:3000/ 
</VirtualHost>

ahora veamos los ejemplos con ssl:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    # 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 example.organizacion.algo.edu.bo
    ServerAlias www.example.organizacion.algo.edu.bo

    ServerAdmin correoAdmin@organizacion.algo.edu.bo
    
    #DocumentRoot /var/www/html

    #HTTP proxy/gateway server
    ProxyRequests off
    ProxyPass / http://127.0.0.1:2368/
    ProxyPassReverse / http:/127.0.0.1:2368/

    # 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}/exampl_error.log
    CustomLog ${APACHE_LOG_DIR}/example_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
SSLCertificateFile /etc/letsencrypt/live/example.organizacion.algo.edu.bo/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.organizacion.algo.edu.bo/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

ahora veamos este otro ejemplo con ssl:

<IfModule mod_ssl.c>

<VirtualHost *:443>

ServerName <IfModule mod_ssl.c>

<VirtualHost *:443>

ServerName example.organizacion.algo.edu.bo
ServerAlias www.example.organizacion.algo.edu.bo

ServerAdmin correoAdmin@organizacion.algo.edu.bo

#DocumentRoot /var/www/html

#HTTP proxy/gateway server
ProxyRequests off
ProxyPass / http://127.0.0.1:2368/
ProxyPassReverse / http:/127.0.0.1:2368/


ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined

SSLCertificateFile /etc/letsencrypt/live/example.organizacion.algo.edu.bo/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.organizacion.algo.edu.bo/fullchain.pem/privkey.pem

Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

ServerAdmin correoAdmin@organizacion.algo.edu.bo
#DocumentRoot /var/www/html

#HTTP proxy/gateway server
ProxyRequests off
ProxyPass / http://127.0.0.1:2368/
ProxyPassReverse / http:/127.0.0.1:2368/


ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined

SSLCertificateFile /etc/letsencrypt/live/example.organizacion.algo.edu.bo/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.organizacion.algo.edu.bo/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

bueno eso es todo por este post hasta otro !!

¿Te gustó el contenido o lo que hacemos? ¡Cualquier colaboración es agradecida para mantener los servidores o crear proyectos!

Owen-Wilson

Siempre Aprendiendo de uno mismo y de los demás !!

Comentarios:

¡Genial! Te has suscrito con éxito.
¡Genial! Ahora, completa el checkout para tener acceso completo.
¡Bienvenido de nuevo! Has iniciado sesión con éxito.
Éxito! Su cuenta está totalmente activada, ahora tienes acceso a todo el contenido.