How to install ssl certificate in local server for different-different vhost?

How to install ssl certificate on localhost

Most of the time we create virtualhost in our local server to run our developing project and it is widely common practice in our industries. When we install PHP, Apache, MySql in to create our local server, most cases we forget one thing that is ssl. In short for development purpose we don't care about it. BUT, sometime this ignorance will kill your day like when you want to access webcam through your browser and you are trying with Chrome! ee... There might have chance to become angry on yourself if you don't know how to make it your own localhost!

Lets Start

  • Create a folder in your favourite location first, for my case it is /home/myname/ssl. ssl is my newly created folder where I'll store all the certificates. you can store it any where in your PC, my case it is in /home/myname.

  • Make that folder read+write+executable sudo chmod -R 777 /home/myname/ssl so that when we run command for certificate creation, it can easily do that.
    $ sudo openssl req -x509 -days 365 -newkey rsa:2048 -keyout /home/myname/ssl/localhost.key -out /home/myname/ssl/localhost.crt
      Enter PEM pass phrase: 123456 [type your own password]
      Verifying - Enter PEM pass phrase: 123456 [retype your own password]
      Country Name []:CN [type your own country code]
      State Name []:SN [type your own state]
      Locality Name []: LN [type your own locality]
      Organization Name []: company-name [type your own company]
      Common Name []: localhost [type your own common name]
      Email Address []:myadmin@localhost.com [type your own email]
  • Now open your vhost 
                     $ sudo gedit /etc/apache2/sites-available/localhost.conf

  • And Edit
                  <VirtualHost *:443>

                        ServerName localhost.local
                        ServerAdmin myadmin@localhost.com

                        ServerAlias www.localhost.local
                        DocumentRoot /var/www/html/localhost.local

                        SSLEngine on
                        SSLCertificateFile "/home/myname/ssl/localhost.crt"
                        SSLCertificateKeyFile "/home/myname/ssl/localhost.key"

                        <Directory /var/www/html/localhost.local/>
                                Options Indexes FollowSymLinks MultiViews
                                AllowOverride All
                                Order allow,deny
                                allow from all
                    </Directory>

                    ErrorLog ${APACHE_LOG_DIR}/localhost_error.log
                    CustomLog ${APACHE_LOG_DIR}/localhost_access.log combined
            </VirtualHost>
  • virtualhost port must be 443, this is for ssl port. And you have to confirm the bellow code in you vhost
                   SSLEngine on
                   SSLCertificateFile "/home/myname/ssl/localhost.crt"
                   SSLCertificateKeyFile "/home/myname/ssl/localhost.key"
  • Now Enable Site By
                    $ sudo a2ensite /etc/apache2/sites-available/localhost.conf
  • Check that your apache ssl module is running on your local server if not then enable it for your
                    $ sudo a2enmod ssl

  • Almost done, just one more thing to do. Restart your server at this moment. When you restart your server this time you have to enter the ssl password with your sudo password. ssl password would be the same what you setup before.
                sudo service apache2 restart
                Enter PEM pass phrase: 123456 [type your own password]

    

    It's DONE!








    Post a Comment

    Previous Post Next Post