miércoles, 1 de agosto de 2007

Configurar el protocolo SSL en Apache 2.2 con Ubuntu Feisty Fawn

Hola a todos, el dia de hoy voy a documentar el como instalar en nuestro ubuntu feisty fawn el protocolo SSL (Secure Socket Layer) para nuestro Apache 2.2.

A diferencia de lo que pensaba antes de hecharlo a andar, es muy facil instalarlo.

Pues comencemos.

1. Primero que nada necesitamos instalar el servidor web apache que en ubuntu y cualquier distribucion basada en debian es cosa de un simple apt-get.

# apt-get install apache2

2. Ahora nos toca habilitar el modulo de SSL en nuestro recien instalado apache.

#a2enmod ssl

3. Aqui deberiamos de poder crear el certificado ssl mediante el comando apache2-ssl-certificate, pero para nuestra desgracia, al parecer en esta versión de ubuntu no se incluyo y necesitamos realizar unos pasos adicionales para subsanar dicho error :( , y quedaria asi.

Necesitamos descargar el siguiente archivo: apache2-ssl.tar.gz y ya una vez descargado procedemos a descomprimirlo, lo cual nos deja dos archivos en nuestro directorio. Uno con el nombre de ssleay.cnf el cual deberemos copiar en /usr/share/apache2 y otro con nombre apache2-ssl-certificate que copiaremos a /usr/sbin, ahora solo falta crear el directorio en donde residira el certificado ssl y el cual es /etc/apache2/ssl.

Listo, con esto ya tenemos el comando para generar el certificado de autenticidad, y podemos proceder a configurar nuestro Apache2 con SSL :) .

4. Creamos nuestro certificado de la siguiente manera:

# apache2-ssl-certificate

El comando nos arroja la siguiente salida esperando algunas respuestas:


creating selfsigned certificate
replace it with one signed by a certification authority (CA)

enter your ServerName at the Common Name prompt

If you want your certificate to expire after x days call this programm
with -days x
Generating a 1024 bit RSA private key
........++++++
....................++++++
writing new private key to '/etc/apache2/ssl/apache.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:MX
State or Province Name (full name) [Some-State]:Distrito Federal
Locality Name (eg, city) []:Mexico
Organization Name (eg, company; recommended) []:Sistemas y Conectividad
Organizational Unit Name (eg, section) []:Sistemas y Conectividad
server name (eg. ssl.domain.tld; required!!!) []:localhost
Email Address []:webmaster@localhost


5. Ahora necesitamos crear algunos archivos de configuracion para apache y para eso nos valemos de cualquier editor de textos como emacs o vi.

Nos basamos en los archivos de configuracion estandar de apache en nuestro ubuntu de la siguiente manera:


cp /etc/apache2/sites-available/default /etc/apache2/sites-available/ssl
ln -s /etc/apache2/sites-available/ssl /etc/apache2/sites-enabled/ssl


Ahora modifiquemos el archivo /etc/apache2/sites-enabled/ssl con cualquier editor de textos y agreguemos el numero de puerto de escucha de Apache2 SSL y algunas cosas mas como habilitar la directiva SSLEngine a On y fijar la ruta de SSLCertificateFile a /etc/apache2/ssl/apache.pem:


# Agregar puerto 443 default para SSL en NameVirtualHost
NameVirtualHost *:443

ServerAdmin webmaster@localhost

# Modificar el DocumentRoot hacia el directorio
# que querramos para SSL
DocumentRoot /home/www/ssl/htdocs

Options Indexes FollowSymLinks MultiViews
AllowOverride All


# Opciones de configuracion de nuestro directorio SSL

Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
#RedirectMatch ^/$ /apache2-default/


ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all


ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined
ServerSignature On

# Aqui agregamos el soporte SSL para nuestro apache asi como la ruta
# del certificado creado anteriormente.
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/apache.pem

Alias /doc/ "/usr/share/doc/"

Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128






6. Ahora necesitamos modificar el archivo: /etc/apache2/sites-enabled/000-default agregandole el puerto 80 al servidor web para las conexiones no seguras. El cambio quedaria asi:


# Puerto 80 predeterminado para el servidor web sin SSL
NameVirtualHost *:80

# Tambien aqui le agregamos el puerto 80

ServerAdmin webmaster@localhost

DocumentRoot /home/www/

Options Indexes FollowSymLinks MultiViews
AllowOverride All


Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# This directive allows us to have apache2's default start page
# in /apache2-default/, but still have / go to the right place
#RedirectMatch ^/$ /apache2-default/


ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all


ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined
ServerSignature On

Alias /doc/ "/usr/share/doc/"

Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128






Esos son los unicos cambios que se deben realizar en el archivo de default.

7. Ahora solo nos falta agregar el nuevo puerto (443) en el archivo /etc/apache2/ports.conf para que apache realice un fork() y cree el servidor a la escucha en ese puerto. El archivo ya modificado quedaria asi:


juliocs@andromeda:~$ cat /etc/apache2/ports.conf
Listen 80
Listen 443


8 Ya con esto solo nos falta reiniciar el apache con el comando:


# /etc/init.d/apache2 force-reload


Aqui muestro la salida del comando nmap contra localhost mostrando a nuestro apache con SSL habilitado:


juliocs@andromeda:~$ nmap localhost

Starting Nmap 4.20 ( http://insecure.org ) at 2007-08-01 05:53 CDT
Interesting ports on localhost (127.0.0.1):
Not shown: 1684 closed ports
PORT STATE SERVICE
21/tcp open ftp
22/tcp open ssh
80/tcp open http
111/tcp open rpcbind
139/tcp open netbios-ssn
443/tcp open https
445/tcp open microsoft-ds
631/tcp open ipp
902/tcp open iss-realsecure-sensor
977/tcp open unknown
2049/tcp open nfs
3306/tcp open mysql
5432/tcp open postgres

Nmap finished: 1 IP address (1 host up) scanned in 0.247 seconds



Ya por mi parte es todo y nos vemos pronto.



No hay comentarios.:

Bahia desde el balcon...

Bahia desde el balcon...
Ixtapa Zihuatanejo