Помощни статии
Инсталиране на SSL сертификат на Debian
Коментирано от Lyubomir Markov Включен 14 October 2014 16:52

Инсталиране на SSL сертификат на Debian

Като за начало е нужно да проверим дали е инсталиран следния пакет openssl и да инсталираме ssl модула за apache:

root@~# dpkg --list | grep openssl ii openssl 0.9.8o-4squeeze1
root@debain-icn:~# a2enmod ssl
Enabling module ssl. Run '/etc/init.d/apache2 restart' to activate new configuration!

След проверката и инсталацията на ssl модула създаваме една директория ssl в директорията на уеб сървъра /etc/apache2/. В тази директория ще генерираме домейн ключа както и csr-a, след което ще заложим сертификата и ca-bandle:

[root@/]# mkdir /etc/apache2/ssl
[root@/]# cd /etc/apache2/ssl

Създали сме директория ssl и сме в нея, време е да генерираме домейн ключа и csr-a: 

root@:/etc/apache2/ssl# openssl genrsa -out www.domain.com.pem 2048

Пример:
Generating RSA private key, 2048 bit long modulus .........................+++ ......+++ e is 65537 (0x10001)
root@:/etc/apache2/ssl# openssl req -new -key www.domain.com.pem -out www.domain.com.csr
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]:BG
State or Province Name (full name) [Some-State]:Sofia
Locality Name (eg, city) []:Sofia
Organization Name (eg, company) [Internet Widgits Pty Ltd]:ICN.Bg
Organizational Unit Name (eg, section) []:IT
Common Name (eg, YOUR name) []:www.domain.com
Email Address []:admin@domain.com
Please enter the following 'extra' attributes to be sent with your certificate request
A challenge password []:
An optional company name []:

Моля обърнете внимание на „Common Name“ много е важно какъв hostname ще подадете, тъй като сертификатът няма да работи коректно, ако е грешен. Може да не подавате „challenge password“. Разполагаме с домейн ключа и csr-a можем вече да закупим своя сертификат, за което е нужно да подадете на доставчика csr-a, за да Ви предостави сертификата. След като Ви е предоставен сертификата и ca-bandle трябва да ги копирате на сървъра.

Добавяме сертификатът на сървъра, използвайките текстов редактор:

root@:/etc/apache2/ssl# pico www.dom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HR0cDovL2FpYS5zdGFy dHNzbC5jb20vY2VydHMvc3ViLmNsYXNzMS5zZXJ2ZXIuY2EuY3J0MCMGA1UdEgQc MBqGGGh0dHA6Ly93d3cuc3RhcnRzc2wuY29tLzANBgkqhkiG9w0BAQUFAAOCAQEA ZBmk5S1AzwmOMkQH1cPota1/YC2uNBQCUQQCh5wP0+hmFTkr8Acq8qN8M0wUkONf lFeghsMulk9+XSK5x0IDLlNtABSAVJpLwietL8KdFcbujmMbsNoWDM2cZhS56NBB jg4a46OIm+t3Wdf5A02uHpCa3JTM8393DbX2ts43hYHn0zpp1a/v4RqSMHU8u4Du gKaUs+/Wo+2wQ7gQVj9hecoivtkvr6OEy+RzQr+Lf1dJ/v2bZKkjHnDWb8t1ZoXT
3Vkb5i8IpRHllHLboAEZMzmQ3q4F+yqPkD/1izs/Adu5wkwoC8yy2gLpi3wUX2Zg
Q4Z9hYzAqNmC0jutbwvaLA==
-----END CERTIFICATE-----

След като сме добавили сертификата добавяме и cabandle отново с текстов редактор:

root@:/etc/apache2/ssl# pico www.domain.com.cabundle

Пример:
-----BEGIN CERTIFICATE-----
MIIHyTCCBbGgAwIBAgIBATANBgkqhkiG9w0BAQUFADB9MQswCQYDVQQGEwJJTDEW MBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMiU2VjdXJlIERpZ2l0YWwg Q2VydGlmaWNhdGUgU2lnbmluZzEpMCcGA1UEAxMgU3RhcnRDb20gQ2VydGlmaWNh dGlvbiBBdXRob3JpdHkwHhcNMDYwOTE3MTk0NjM2WhcNMzYwOTE3MTk0NjM2WjB9 MQswCQYDVQQGEwJJTDEWMBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMi U2VjdXJlIERpZ2l0YWwgQ2VydGlmaWNhdGUgU2lnbmluZzEpMCcGA1UEAxMgU3Rh cnRDb20gQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwggIiMA0GCSqGSIb3DQEBAQUA A4ICDwAwggIKAoICAQ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FCRsI3FU34oH7N4RDYiDK51ZLZer+bMEkkySh
NOsF/5oirpt9P/FlUQqmMGqz9IgcgA38corog14=
-----END CERTIFICATE-----

Сега е нужно да проверим в настройките на apache2, първо дали порта 443 е отворен:

root@:/etc/apache2/ssl# pico /etc/apache2/ports.conf


# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from # Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
# README.Debian.gz NameVirtualHost *:80 Listen 80
# 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 Listen 443

Забелязваме, че всичко е коректно сега е нужно да добавим виртуалния хост като в него опишем домейн ключа, сертификата и cabandle:

След като сме създали /etc/apache2/ssl директорията и сме създали файловете с описани cabundle, crt, pem и csr е нужно да създадем Vhost-a в директорията

/etc/apache2/sites-available

Там файла трябва да се казва

domaina.com-ssl 

root@:/etc/apache2/ssl# pico /etc/apache2/sites-available/domaina.com-ssl

Пример:

<VirtualHost 192.168.1.101:443> --- като 192.168.1.101 е IP на което рабтои уеб сървъра Ви
ServerAdmin webmaster@domain.com
DocumentRoot /var/www
ServerName domain.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/httpd/ssl/domain.com.crt
SSLCertificateKeyFile /etc/httpd/ssl/domain.com.pem
SSLCACertificateFile /etc/httpd/ssl/domain.com.cabundle
SSLEngine on
</VirtualHost>

След това е необходимо да се изпълни следната команда:

a2ensite domaina.com-ssl

като заменяте домейна с домейн името за сертификата, както сте кръстили файла. Тази команда създава symlink от sites-available към sites-enabled.

Може да направите проверка на инсталирания сертификат като отворите адреса https://domaina.com. Ако той пренасочва към http://domaina.com, създайте един файл с phpinfo фунцкията в директорията на сайта:

:#pico info.php

<?php phpinfo(); ?>

и отворете https://domaina.com/info.php, за да видите дали info.php ще се отвори с ssl. Ако се отвори, това означава че приложението не е конфигурирано да работи с ssl.

След като сме добавили виртуалния хост, в който сме описали сертфиката, ключа и cabandle-a е нужно само да рестартираме уеб сървъра, за да приеме новите настройки.