Virtual Host Setup with SSL Certificate in Your Localhost

virtual-host-setup-with-ssl-certificate-in-your-localhost

Image description

In this article we will learn how we can run our project from a custom url example.com instead of localhost in our local LAMP Server on Ubuntu.

First open your terminal and create a New file called example.com.conf with root privileges.

touch /etc/apache2/sites-available/example.com.conf

Open the new file with root privileges.
sudo nano /etc/apache2/sites-available/example.com.conf

Add below lines in the example.com.conf file.


  ServerName example.com
        DocumentRoot /var/www/html/example-project
  Redirect permanent / https://example.com


  ServerAdmin example@example.com
        DocumentRoot /var/www/html/example-project
  ServerName example.com

  SSLEngine on
  SSLCertificateFile /var/www/html/ssl/server.crt
  SSLCertificateKeyFile /var/www/html/ssl/server.key
  SSLCertificateChainFile /var/www/html/ssl/rootCA.pem

  
    Options Indexes FollowSymLinks
    AllowOverride All
        Require all granted
  

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

As you can see we have added http with port 80 and https with port 443. We have also added redirection to example.com to https://example.com

For SSL certificate, Please create a ssl certificate and then add the crt, key and pem type files in a ssl folder under /var/www/html folder. Please note that we have mentioned path for the crt, key and pem files for VirtualHost port 443 above. Finally, we enabled htaccess by “AllowOverride All”.

Now save the file and it’s time to add our custom host to the host file.

Open the host file in terminal by the command
sudo nano /etc/hosts

Add our example.com to the end of file.

127.0.0.1   localhost
.....
.....
127.0.1.1   example.com // add this line

To enable SSL and new vhost config file use these commands
sudo a2enmod ssl // to enable ssl
sudo a2ensite example.com.conf // to enable vhost config

Now restart the server to apply the changes
sudo systemctl restart apache2

Test it now by visiting example.com

If we face any problem then we will be able to see in /var/log/apache2/error.log file.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
top-5-remote-support-software-solutions-(free-&-paid)

Top 5 Remote Support Software Solutions (Free & Paid)

Next Post
kick-scrooge-to-the-curb-to-make-your-holiday-marketing-numbers-soar!

Kick Scrooge to the Curb to Make Your Holiday Marketing Numbers Soar!

Related Posts