Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Apache2: setup https using self sign certificate

05 Oct 2021 » apache2, linux

I have pre-install apche2 and openssl service on my linux machin

Enable mode ssl and rewrite fist.


sudo a2enmod rewrite
sudo a2enmod ssl

To enable https on apache follow the steps.

add bellow code in /etc/apache2/apache2.conf


<Directory /var/www/html>
AllowOverride All
</Directory>

Create certificate directory in /etc/apache2/ and go inside it.


openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out apache-certificate.crt -keyout apache.key

It will generate certificate file automaticaly it will ask some questions like region, state, city and most importent common name, for comon name you need to provide domain name here.

Set generated certificate path in conf file

go to site-available and edit conf file. for ex. we use adminer.local.conf


<VirtualHost *:80>
        RewriteEngine On
        RewriteCond %{HTTPS} !=on
        RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</virtualhost>
<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/adminer.local
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SSLEngine on
        SSLCertificateFile /etc/apache2/certificate/apache-certificate.crt
        SSLCertificateKeyFile /etc/apache2/certificate/apache.key
</VirtualHost>