Práctica: Servidor web seguro con Apache
La idea es que cuando accedamos al dominio https://www.dominio1.com nos aparezca el candado en el navegador indicando que se trata de un servidor seguro.
1.- Creamos el certificado seguro y la clave
Generamos la clave
apt-get install openssl
openssl genrsa -des3 -out server.key 1024
Generating RSA private key, 1024 bit long modulus ..............++++++ ...++++++ e is 65537 (0x10001) Enter pass phrase for server.key: Enter pass phrase for server.key: Verifying - Enter pass phrase for server.key:
Generamos el certificado:
openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key: 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) [AU]:ES State or Province Name (full name) [Some-State]:Alava Locality Name (eg, city) []:Vitoria-Gasteiz Organization Name (eg, company) [Internet Widgits Pty Ltd]:Egibide Organizational Unit Name (eg, section) []:Informatica Common Name (e.g. server FQDN or YOUR name) []:www.dominio1.com Email Address []:aitor@kaixo.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:dinux An optional company name []:
Quitamos la palabra de paso a la clave:
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
Enter pass phrase for server.key.org: writing RSA key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok subject=/C=ES/ST=Alava/L=Vitoria-Gasteiz/O=Egibide/OU=Informatica/CN=www.dominio1.com/emailAddress=aitor@kaixo.com Getting Private key
Copiamos y renombrado el certificado y la clave generadas a /etc/apache2/ssl
cp server.crt /etc/apache2/ssl/ssl.crt
cp server.key /etc/apache2/ssl/ssl.key
3.- Colocamos por defecto el certificado en todo el servidor en el fichero /etc/apache2/sites-available/default-ssl
SSLEngine on SSLCertificateFile /etc/apache2/ssl/ssl.crt SSLCertificateKeyFile /etc/apache2/ssl/ssl.key
Comprobamos que tenemos el puerto 443 habilitado en /etc/apache2/ports.conf
NameVirtualHost *:80 Listen 80 <IfModule mod_ssl.c> # If you add NameVirtualHost *:443 here, you will also have to change # the VirtualHost statement in /etc/apache2/sites-available/default-ssl # to# Server Name Indication for SSL named virtual hosts is currently not # supported by MSIE on Windows XP. Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule>
4.- O podemos añadirlo a un servidor virtual:
<VirtualHost *:80> DocumentRoot "/var/www/dominio2" ServerName www.dominio2.com <Directory "/var/www/dominio2"> allow from all Options +Indexes </Directory> </VirtualHost> <VirtualHost *:443> DocumentRoot "/var/www/dominio2" ServerName www.dominio2.com <Directory "/var/www/dominio2"> allow from all Options +Indexes </Directory> SSLEngine On SSLCertificateFile /etc/apache2/ssl/ssl.crt SSLCertificateKeyFile /etc/apache2/ssl/ssl.key </VirtualHost>
5.- Añadimos el sitio y el módulo de servidor seguro
sudo a2ensite default-ssl
sudo a2enmod ssl
Nota: Este comando es equivalente a hacer:
ln -s ../mods-available/ssl.load ssl.load
ln -s ../mods-available/ssl.conf ssl.conf
6.- Probamos la configuración correcta del servidor.
Podemos comprobar que nuestra configuración es correcta antes de reiniciar el servidor con el siguiente comando. Si reiniciamos el servidor con un error el servicio dejará de funcionar hasta que solucionemos el problema.
apachectl configtest
7.- Reiniciamos el servidor y probamos
/etc/init.d/apache2 restarty accedemos con el navegador a
https://www.dominio1.com