How to Apply an SSL on Apache

Introduction

In this article, we will guide you through the steps of installing an SSL certificate on the Apache web server in the CentOS/Ubuntu Operation System.
 
We assume that you have Apache installed on your server and have certificate files(including .key and .crt files).
 

Upload the .key and .crt files

Upload the .key and .crt files to your Linux Server.
 

Configure the <VirtualHost> block

Open the configuration file of the website.
 
Configurate the <VirtualHost> file block for the site. Refer to <VirtualHost *:80> to create a VirtualHost for 443, and add the following code.
<VirtualHost *:443>

        SSLEngine on

        SSLCertificateFile /path/to/your_domain_name.crt

        SSLCertificateKeyFile /path/to/your_private.key

</VirtualHost>
 

Enable ssl module  

For CentOS server, use "httpd -M | grep ssl" to check if the SSL module is installed. If it’s not, install it through "yum install mod_ssl ".
 
For Ubuntu server, use "apache2 -M | grep ssl" to check if the SSL module is installed. If it’s not, install it through "apt install mod_ssl ".
 

Test your Apache configuration file

For CentOS and Ubuntu server, please run "apachectl configtest". If "Syntax OK" is displayed, the configuration file setup was successful.
 

Restart Apache 

For CentOS server, restart the Apache Service by using " systemctl restart httpd ".
 
For Ubuntu server, restart the Apache Service by using " systemctl restart apache2 ".
 

Test the HTTPs of your website

You can access your website via https://your_domain_name now.
 

Add redirection from HTTP to HTTPS

7.1 Install redirect module

For CentOS server, please refer to the following:
yum install mod_rewrite
 
For Ubuntu server, please refer to the following:
apt install mod_rewrite
 

7.2 Edit configuration file

Modify the “/etc/httpd/conf/httpd.conf” file and add the following at the end of the file.
 
RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
 

Restart Apache 

For CentOS server, restart the Apache Service by using " systemctl restart httpd ".
 
For Ubuntu server, restart the Apache Service by using " systemctl restart apache2 ".

Add Feedback