DEV Community

Cover image for How to install ssl certificate (or make http to https) in local server for different-different vhost?
Md. Noor A Alam Siddique
Md. Noor A Alam Siddique

Posted on

How to install ssl certificate (or make http to https) in local server for different-different vhost?

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!

Here, I'll try to make an demonastration for this. I'll make it of one vhost but by following the way you can make as much as you want. I should mention here that I tested it for Apache not sure about nginx or others.

Our Target

Our System Configuration

  • ubuntu version >= 14.04
  • php version >= 5.6.*

Lets Start

  • Create a folder in your favorate location first, for my case it is /home/siddique/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/siddique.
  • Make that folder read+write+executable sudo chmod -R 777 /home/siddique/ssl so that when we run command for certificate creation, it can easily do that.
  • Now, lets start
$ sudo openssl req -x509 -days 365 -newkey rsa:2048 -keyout /home/siddique/ssl/localhost.key -out /home/siddique/ssl/localhost.crt
  Enter PEM pass phrase: 123456 [type your own password]
  Verifying - Enter PEM pass phrase: 123456 [retype your own password]
  Country Name []: BD [type your own country code]
  State Name []: Dhaka [type your own state]
  Locality Name []: Bangladeshi [type your own locality]
  Organization Name []: company-name [type your own company]
  Common Name []: localhost [type your own common name]
  Email Address []: admin@localhost.com [type your own email]
  • Now open your virtualhost
$ sudo gedit /etc/apache2/sites-available/localhost.conf
  • And edit
<VirtualHost *:443>

    ServerName localhost.local
    ServerAdmin admin@localhost.com

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

    SSLEngine on
    SSLCertificateFile "/home/siddique/ssl/localhost.crt"
    SSLCertificateKeyFile "/home/siddique/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/siddique/ssl/localhost.crt"
   SSLCertificateKeyFile "/home/siddique/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!

  • Now, open your browse and browse https://localhost.local
  • You may have to check Proceed to localhost.local (unsafe) from browser for the first time visit.

It's done, No more talk, he he ... bye

Github

Latest comments (0)