Skip to content

The Geek Worker

Random Computer Issues And Things That Fixed Them For Me

  • About this Website
  • Helpful Links

Enabling SSL in Apache2 on Ubuntu

June 14, 2011 by geekworker3 min read

I’m using my server for various admin interfaces and so want to SSL encrypt all traffic to the web server. This is easy enough to add to the default vhosts.

First, we need an SSL certificate. Create it by running:

openssl req -new -x509 -days 3650 -nodes
-out /etc/ssl/certs/apacheserver.pem
-keyout /etc/ssl/private/apacheserver.pem

This generates a self-signed certificate. For tests this is good enough; for production sites you WILL want to use a real purchased certificate. Since one of the jobs of SSL is not only to encrypt, but also to authenticate a site to the user, a self-signed certificate will cause browsers to pop up a warning. Users can (permanently) accept this for your site, but it’s probably not the impression you want to leave.

Ubuntu’s Apache2 will come with the SSL module installed by default, but it’s not used. As our next step, we need to enable it:

a2enmod ssl

Finally, we need to create an SSL virtual host. In my case, I want all http traffic to be simply redirected to https. Find the file /etc/apache2/sites-enabled/000-default and edit it.

Change the existing vhost to listen to Port 443; edit the “VirtualHost” line so it reads:

<VirtualHost *:443>
[…]
</VirtualHost>

(Where […] is of course the rest of your vhost configuration)

Now, inside the VirtualHost definition we’ll need to enable SSL and tell it where the certificate resides:

<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ephesus.pem
SSLCertificateKeyFile /etc/ssl/private/ephesus.pem
[…]
</VirtualHost>

Finally, add a new vhost for redirection:

<VirtualHost *:80>
RedirectPermanent / https://server.example.org/
</VirtualHost>

Naturally, https://server.example.org/ should point to your machine’s name.

Restart Apache:

/etc/init.d/apache2 restart

And that’s all. If you go to http://server.example.org/ you should now be redirected to https://server.example.org

For final reference, here’s my configuration. Note that I commented out the Ubuntu documentation (No need to make this available to the big wide world) and cgi-bin (I’m not using this).

<VirtualHost *:80>
RedirectPermanent / https://ephesus.pandemonium.de/
</VirtualHost>

<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ephesus.pem
SSLCertificateKeyFile /etc/ssl/private/ephesus.pem

ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

#ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
#<Directory “/usr/lib/cgi-bin”>
# AllowOverride None
# Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
# Order allow,deny
# Allow from all
#</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined
# Alias /doc/ “/usr/share/doc/”
# <Directory “/usr/share/doc/”>
# Options Indexes MultiViews FollowSymLinks
# AllowOverride None
# Order deny,allow
# Deny from all
# Allow from 127.0.0.0/255.0.0.0 ::1/128
# </Directory>

</VirtualHost>

Posted in ArticleTagged Apache, Linux, SSL, Ubuntu

Related Stories

  • February 8, 2025 · Article

    Inkscape: “Document Properties” Window Not Showing Up

    Inkscape sometimes opens the “Documents Properties” window outside the visible area of the desktop. This is how you get it back.

  • December 30, 2023 · Article

    Synology Drive Client does not connect on Apple MacOS

    Synology Drive Client on MacOS refused to connect. Deleting the appropriate logfiles helped.

  • September 13, 2023 · Article

    “The iTunes Store is temporarily unavailable. Please try again later” when trying to subscribe to Podcast

    Currently (September 2023), Apple’s iTunes store seems to have a bug where it’s impossible to subscribe to new podcasts. However, there is a workaround. Find your desired podcast on the iTunes store. Click the “get” button, on the right side of the episode list, for any episode of the podcast. Switch to your library. The […]

Previous story Moving eJabberd Next story Turning off Syntax Highlighting in vim

3 Comments

  1. Apache: ssl_error_rx_record_too_long Error | The Geek Worker says:
    August 2, 2017 at 10:40 pm

    […] not configured SSL properly – Firefox was attempting to parse http as https. So I set out to configure SSL and lo and behold, this solved the […]

    Reply
  2. bitsurs says:
    August 2, 2017 at 10:40 pm

    While I am building a RESTful app, I was looking hot-to setup my ubuntu dist. This article is great: clear in his scope and explanation.
    I had my SLL apache ready in 5 mins.
    Thanks
    PS:
    here is my conf:

    ServerAdmin webmaster@localhost
    DocumentRoot /media/usb_htdocs/taklia.pro/taklia.dev/services/trunk

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/apacheserver.pem
    SSLCertificateKeyFile /etc/ssl/private/apacheserver.pem

    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all

    ServerAlias services.taklia.dev
    RedirectPermanent / https://services.taklia.dev/

    Reply
  3. james White says:
    August 2, 2017 at 10:40 pm

    Thanks for the guide im in the process of creating a new local exchange server at are office for internal mail mostly but people will be accessing it from home aswell. So im creating it as a SSL, this has been really helpful almost got it setup and working now thanks to you

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Inkscape: “Document Properties” Window Not Showing Up
  • Synology Drive Client does not connect on Apple MacOS
  • “The iTunes Store is temporarily unavailable. Please try again later” when trying to subscribe to Podcast
  • “Access Denied” Errors with TortoiseSVN on Samba
  • Keyboard and Mouse Microstutters with a Dell Latitude Notebook

Recent Comments

  1. Bart on Gigabyte OSD_Sidekick: “Please check if your USB cable is connected”
  2. Jason on Sony Vegas and Sony Movie Studio: No Audio Tracks In mp4 video filefound
  3. DaReclama on OpenVPN: IP packet with unknown IP version seen
  4. vilva on Excel Formula to get Number of Days in a Month
  5. Michael (Micky) Avidan on Excel Formula to get Number of Days in a Month

Archives

  • February 2025
  • December 2023
  • September 2023
  • July 2022
  • June 2022
  • April 2022
  • November 2021
  • March 2021
  • January 2021
  • August 2020
  • July 2020
  • May 2020
  • January 2020
  • May 2019
  • November 2018
  • July 2018
  • January 2018
  • August 2017
  • January 2017
  • September 2016
  • July 2016
  • October 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • June 2014
  • April 2014
  • February 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • July 2013
  • June 2013
  • April 2013
  • February 2013
  • October 2012
  • August 2012
  • March 2012
  • January 2012
  • October 2011
  • September 2011
  • August 2011
  • June 2011
  • July 2007

Categories

  • Article

© 2026 The Geek Worker. Proudly powered by WordPress.