Configure Apache2 Self-Signed SSL Certificate on Ubuntu 8.04.2 AMD64 LTS
tonyn | May 8, 2009Install Apache2 and OpenSSL packages:
apt-get install apache2 openssl ssl-cert
Create SSL directory under /etc/apache2
mkdir /etc/apache2/ssl
Generate SSL certificate:
openssl req $@ -new -x509 -days 3652 -nodes -out /etc/apache2/ssl/unix86.pem \ -keyout /etc/apache2/ssl/unix86.pem
Generating a 1024 bit RSA private key .................++++++ ..........++++++ writing new private key to '/etc/apache2/ssl/unix86.pem' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:HK State or Province Name (full name) [Some-State]:Hong-Kong Locality Name (eg, city) []:Hongkong Organization Name (eg, company) [Internet Widgits Pty Ltd]:UNIX86.COM Organizational Unit Name (eg, section) []:IT Common Name (eg, YOUR name) []:mail.unix86.com Email Address []:tonyn@unix86.com
Change the SSL Cert file permission
chmod 600 /etc/apache2/ssl/unix86.pem
Create SSL virtual hosts
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/ssl
vi /etc/apache2/sites-available/ssl #------------------------------------------------------------------------------- NameVirtualHost *:443 <VirtualHost *:443> SSLEngine On SSLCertificateFile /etc/apache2/ssl/unix86.pem ServerAdmin webmaster@unix86.org 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 /var/log/apache2/error-unix86.org.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/access-unix86.org.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> #-------------------------------------------------------------------------------
Modify /etc/apache2/ports.conf
vi /etc/apache2/ports.conf #------------------------------------------------------------------------------- Listen 80
<IfModule mod_ssl.c> Listen 443 </IfModule> #-------------------------------------------------------------------------------
Enable SSL Virtual Host
a2ensite ssl a2enmod ssl
Mod rewrite
It’s always good to force users to access things like webmail via https, this can be accomplished with mod_rewrite.
Enable the mod_rewrite module
a2enmod rewrite
Then add the following to /etc/apache2/sites-available/default
vi /etc/apache2/sites-available/default
#-------------------------------------------------------------------------------
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^/webmail(.*)$ https://%{SERVER_NAME}/webmail$1 [L,R]
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 2
#-------------------------------------------------------------------------------
Force an SSL connection and redirect all traffic to port 80 to port 443 (HTTPS), use this instead:
vi /etc/apache2/sites-available/default
#-------------------------------------------------------------------------------
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]
#-------------------------------------------------------------------------------
Restart Apache2:
/etc/init.d/apache2 restart
Sources:
https://help.ubuntu.com/8.04/serverguide/C/certificates-and-security.html
https://help.ubuntu.com/community/forum/server/apache2/SSL
http://twpug.net/modules/newbb/viewtopic.php?topic_id=2893
http://www.tc.umn.edu/~brams006/selfsign_ubuntu.html
http://www.linode.com/wiki/index.php/Apache2_SSL_in_Ubuntu
http://alephzarro.com/blog/2007/01/07/installation-of-subversion-on-ubuntu-with-apache-ssl-and-basicauth/
http://davidlaing.com/2008/12/27/self-cert-ssl-certificate-for-apache2-on-ubuntu-804lts/