How to install SSL certificate in Apache
Before starting the tutorial, don't you have a Cloud Linux server with Apache where you can install your SSL certificate? Don't worry! With just one click, you can easily deploy it with SW Hosting.
To install an SSL certificate in Apache, we will need the following files:
- Certificate (.crt)
- Private key (.key)
- Certification Authority (.ca)
Do you have them? Let's begin:
-
Copy the three files to the server. In this manual, we assume that you have uploaded the three files via FTP, but you can do it in any other way.
-
Connect to the server via SSH and access the directory where we will host the certificates:
cd /etc/apache2/ssl/
- Create the directory corresponding to your domain and access it:
mkdir minuevaweb.es
cd minuevaweb.es
- We move the certificates into the created directory. Assuming that you have uploaded them to the directory 'web' via FTP, we will move them as follows:
mv /var/www/minuevaweb.es/datos/web/certificado.crt /etc/apache2/ssl/minuevaweb.es/
mv /var/www/minuevaweb.es/datos/web/certificado.key /etc/apache2/ssl/minuevaweb.es/
mv /var/www/minuevaweb.es/datos/web/certificado.ca /etc/apache2/ssl/minuevaweb.es/
- We change the owner and group of the certificates to root:
chown root:root /etc/apache2/ssl/minuevaweb.es/certificado.crt
chown root:root /etc/apache2/ssl/minuevaweb.es/certificado.key
chown root:root /etc/apache2/ssl/minuevaweb.es/certificado.ca
We have now moved the certificates into the corresponding directory.
Next, we will proceed to configure the VirtualHost block in Apache:
- Access the vhosts configuration files directory:
cd /etc/apache2/sites-enabled/
- Edit the configuration file corresponding to the website where you are installing the SSL certificate. In this example, we use the nano text editor, but you can use any other:
cd /etc/apache2/sites-enabled/
- We make a copy of the existing VirtualHost block below it, leaving it duplicated (we will make changes later, don't worry):
<VirtualHost *:80>
ServerName minuevaweb.es
ServerAlias www.minuevaweb.es
DocumentRoot '/var/www/minuevaweb.es/datos/web'
CustomLog /var/www/minuevaweb.es/logs/20190307.log common
ErrorLog /var/www/minuevaweb.es/logs/error_20190307.log
</VirtualHost>
(the original file)
<VirtualHost *:80>
ServerName minuevaweb.es
ServerAlias www.minuevaweb.es
DocumentRoot '/var/www/minuevaweb.es/datos/web'
CustomLog /var/www/minuevaweb.es/logs/20190307.log common
ErrorLog /var/www/minuevaweb.es/logs/error_20190307.log
</VirtualHost>
<VirtualHost *:80>
ServerName minuevaweb.es
ServerAlias www.minuevaweb.es
DocumentRoot '/var/www/minuevaweb.es/datos/web'
CustomLog /var/www/minuevaweb.es/logs/20190307.log common
ErrorLog /var/www/minuevaweb.es/logs/error_20190307.log
</VirtualHost>
(the file with the original copy)
-
In the second VirtualHost block, we modify the port from 80 to 443:
<VirtualHost *:443> -
In the second VirtualHost block, we add the following lines before the end with the paths corresponding to our certificates:
...
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/minuevaweb.es/certificado.crt
SSLCertificateKeyFile /etc/apache2/ssl/minuevaweb.es/certificado.key
SSLCACertificateFile /etc/apache2/ssl/minuevaweb.es/certificado.ca
</VirtualHost>
- It will look as follows:
<VirtualHost *:80>
ServerName minuevaweb.es
ServerAlias www.minuevaweb.es
DocumentRoot '/var/www/minuevaweb.es/datos/web'
CustomLog /var/www/minuevaweb.es/logs/20190307.log common
ErrorLog /var/www/minuevaweb.es/logs/error_20190307.log
</VirtualHost>
<VirtualHost *:443>
ServerName minuevaweb.es
ServerAlias www.minuevaweb.es
DocumentRoot '/var/www/minuevaweb.es/datos/web'
CustomLog /var/www/minuevaweb.es/logs/20190307.log common
ErrorLog /var/www/minuevaweb.es/logs/error_20190307.log
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/minuevaweb.es/certificado.crt
SSLCertificateKeyFile /etc/apache2/ssl/minuevaweb.es/certificado.key
SSLCACertificateFile /etc/apache2/ssl/minuevaweb.es/certificado.ca
</VirtualHost>
-
We save the changes made. In the nano text editor, you must press the following key combination:
Ctrl + Xto save and exitY keyto confirm that you want to overwriteEnter key -
We check that the configuration is correct:
apache2ctl -tSyntax OK should appear. If any error appears, review the previous steps. -
We reload the Apache configuration so that the changes are applied:
/etc/init.d/apache2 graceful
Done! Access your website with the https:// prefix to verify that it works correctly.
Remember that if you do not yet have a Cloud Linux server where you can install Apache and your SSL certificate, you can easily deploy it with SW Hosting.