Apache2 SSL in Ubuntu: From Linodewiki
Apache2 SSL in Ubuntu: From Linodewiki
http://www.linode.com/wiki/index.php/Apache2_SSL_in_...
Contents
1 Apache2 SSL 1.1 Install packages 1.2 Generate the certicate 1.3 Enable the SSL module 1.4 Listen to port 443 1.5 Create and enable the SSL site 1.6 Mod rewrite
Apache2 SSL
This guide will help you setup SSL with apache2. Note: This manual applies to ubuntu versions prior to Ubuntu Feisty (7.04). The apache2-ssl-certicate script used in this manual isn't included anymore starting from Feisty. Please check the ocial Ubuntu documentation: https://help.ubuntu.com/8.04/serverguide/C/httpd.html for instructions how to setup apache2 with SSL.
Install packages
First make sure all needed packages are installed.
sudo apt-get install apache2 libapache-mod-ssl
1 of 4
10/07/12 09:12
http://www.linode.com/wiki/index.php/Apache2_SSL_in_...
1. Use pem instead of key in order not to be prompted for password. 2. Point where your crt and pem is stored as well.
SSLCertificateFile /etc/apache2/ssl/certs/apache.crt SSLCertificateKeyFile /etc/apache2/apache-ssl/apache.pem DocumentRoot /var/www/ <directory /> Options FollowSymLinks AllowOverride None </directory> <directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all # This directive allows us to have apache2's default start page # in /apache2-default/, but still have / go to the right place # Commented out for Ubuntu #RedirectMatch ^/$ /apache2-default/ </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>
2 of 4
10/07/12 09:12
http://www.linode.com/wiki/index.php/Apache2_SSL_in_...
# Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/access.log combined ServerSignature On 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>
...and enable it
sudo a2ensite ssl
Mod rewrite
It's always good to force users to access things like webmail via https, this can be accomplished with mod_rewrite. First you'll have to enable the module
sudo a2enmod rewrite
If you want to force an SSL connection and redirect all trac from port 80 to port 443 (HTTPS), use this instead:
RewriteEngine RewriteCond RewriteRule on %{SERVER_PORT} ^80$ ^(.*)$ https://%{SERVER_NAME}$1 [L,R]
I had trouble with the above working. I ended up with a 302 Found response with a bad URL. Instead, I found this achieved what I was looking for. I put this at the top of the le to force my whole server to use SSL:
RewriteEngine RewriteCond on %{HTTPS} !=on
3 of 4
10/07/12 09:12
http://www.linode.com/wiki/index.php/Apache2_SSL_in_...
For those of you that are having issues using either of the above rewrite-rules (eg: a normal http:// url with say a sub-folder, being returned to the root), try this one. This literally takes the whole URL and simply redirects it to HTTPS.
RewriteEngine RewriteCond RewriteRule on %{SERVER_PORT} ^80$ ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
Additionally, using force-reload seems to be having issues with 2.2.x, so just do a straight restart if you can aord a couple seconds of down time on your web server.
4 of 4
10/07/12 09:12