{"id":121,"date":"2010-03-30T20:52:14","date_gmt":"2010-03-31T01:52:14","guid":{"rendered":"http:\/\/www.linuxpoweruser.com\/?p=121"},"modified":"2010-03-30T20:52:14","modified_gmt":"2010-03-31T01:52:14","slug":"ubuntu-apache-lamp-server-quick-howto-part-2-ssl-websites","status":"publish","type":"post","link":"https:\/\/www.linuxpoweruser.com\/?p=121","title":{"rendered":"Ubuntu Apache LAMP Server Quick Howto &#8211; Part 2 &#8211; SSL Websites"},"content":{"rendered":"<p>Hi &#8211; Welcome back to the Ubuntu Apache LAMP Server Quick Howto.\u00a0 This second part of the series will cover configuring SSL for use with Apache for securing website content.\u00a0 SSL stands for Secure Sockets Layer, and is a method of providing transport layer encryption or TLS (transport layer security).\u00a0 SSL typically uses X.509 digital certificates to perform authentication and encryption functions.\u00a0\u00a0 X.509 is based on public key infrastructure or PKI.\u00a0 PKI is beyond the scope of this article (sounds like a good topic for a future article), but we will at least create a self-signed certificate for instructional purposes here.<\/p>\n<p>SSL adds a few caveats to Apache configuration.\u00a0 One of those is name based virtual hosting.\u00a0 SSL does not allow the use of name based virtual hosts.\u00a0 From the apache SSL\/TLS Strong Encryption Guide:<\/p>\n<h3><em><a id=\"vhosts2\" name=\"vhosts2\">&#8220;Why is it not possible to use Name-Based Virtual Hosting to identify different SSL virtual hosts?<\/a><\/em><\/h3>\n<p><em>Name-Based Virtual Hosting is a very popular method of identifying     different virtual hosts. It allows you to use the same IP address and     the same port number for many different sites. When people move on to     SSL, it seems natural to assume that the same method can be used to have     lots of different SSL virtual hosts on the same server.<\/em><\/p>\n<p><em>It comes as rather a shock to learn that it is impossible.<\/em><\/p>\n<p><em> The reason is that the SSL protocol is a separate layer which     encapsulates the HTTP protocol. So the SSL session is a separate      transaction, that takes place before the HTTP session has begun.      The server receives an SSL request on IP address X and port Y      (usually 443). Since the SSL request does not contain any Host:      field, the server has no way to decide which SSL virtual host to use.     Usually, it will just use the first one it finds, which matches the      port and IP address specified&#8221;<\/em><\/p>\n<p>My experience is that Apache will reject the configuration if you attempt to use named based virtual hosts with SSL.<\/p>\n<p>So, with this in mind, if I have multiple &#8220;sites&#8221; to serve with SSL, I either use different IP addresses for each site, or I serve one site with multiple directories of content that can be referenced by http:\/\/&lt;site fqdn&gt;\/directory.\u00a0 For example, if I had a site called www.stuff.com and it used SSL, I would add content for different functions\/site at https:\/\/www.stuff.com\/systeminfo or http:\/\/www.stuff.com\/shopping.<\/p>\n<p>Anyway, so, how do we set up SSL?\u00a0 First thing is to make sure its enabled on your LAMP server.\u00a0 To turn on SSL use the following command:<br \/>\n<code><br \/>\nsudo a2enmod ssl<br \/>\n<\/code><br \/>\nfollowed by<br \/>\n<code><br \/>\nsudo \/etc\/init.d\/apache2 restart<br \/>\n<\/code><br \/>\nAlso &#8211; we should create a specific document root directory for the SSL Apache to use.\u00a0 This way, if you have both SSL and non-SSL sites on the same server, not all the non-ssl sites will be published automatically by the SSL definition.<br \/>\n<code><br \/>\nsudo mkdir \/var\/www-ssl<br \/>\n<\/code><br \/>\nCreating a self signed certificate:<\/p>\n<p>In order to use SSL with Apache you must have a certificate.\u00a0 Here is how to make a self signed certificate.\u00a0 Note:\u00a0 Self Signed certs are fine for testing and lab servers, but are not recommended for production environments or internet facing servers.<\/p>\n<p>First, you will need to create a private key file:<br \/>\n<code><br \/>\nsudo openssl genrsa 1024 &gt; host.key<br \/>\nsudo chmod 400 host.key<br \/>\n<\/code><br \/>\nNext, you will need to create the certificate:<br \/>\n<code><br \/>\nsudo openssl req -new -x509 -nodes -sha1 -days 365 -key host.key &gt; host.cert<br \/>\n<\/code><br \/>\nAfter hitting enter, you will be prompted for several pieces of information that will be encoded into the certificate.\u00a0 Here is an example output:<br \/>\n<code><br \/>\nYou are about to be asked to enter information that will be incorporated<br \/>\ninto your certificate request.<br \/>\nWhat you are about to enter is what is called a Distinguished Name or a DN.<br \/>\nThere are quite a few fields but you can leave some blank<br \/>\nFor some fields there will be a default value,<br \/>\nIf you enter '.', the field will be left blank.<br \/>\n-----<br \/>\nCountry Name (2 letter code) [AU]:<span style=\"color: #ff0000;\">US<\/span><br \/>\nState or Province Name (full name) [Some-State]:<span style=\"color: #ff0000;\">Kansas<\/span><br \/>\nLocality Name (eg, city) []:<span style=\"color: #ff0000;\">Topeka<\/span><br \/>\nOrganization Name (eg, company) [Internet Widgits Pty Ltd]:<span style=\"color: #ff0000;\">Test.com<\/span><br \/>\nOrganizational Unit Name (eg, section) []:<span style=\"color: #ff0000;\">IT<\/span><br \/>\nCommon Name (eg, YOUR name) []:<span style=\"color: #ff0000;\">www.test.com<\/span><br \/>\nEmail Address []:<span style=\"color: #ff0000;\">admin@test.com<\/span><br \/>\n<\/code><br \/>\nNote, the Common Name should be the fully qualified domain name of your web server.\u00a0 Your cert and key will be in the current directory, and will be called host.cert (cert) and host.key (key).\u00a0 Both of these files will be needed for the next step.\u00a0 As root or using sudo, copy the key file to the \/etc\/ssl\/private directory on your LAMP server, and copy the cert file (host.cert) to the \/etc\/ssl\/certs directory.<\/p>\n<p>Next go to the \/etc\/apache2\/sites-available and edit the default-ssl file.\u00a0 This file controls the default SSL virtual host configuration.\u00a0 We will be modifying the following some the lines in the file.\u00a0 As root, find the beginning stanza in the file (with your favorite editor) and it will look something like this:<\/p>\n<pre><span style=\"color: #0000ff;\"><em><span style=\"color: #3366ff;\">&lt;IfModule mod_ssl.c&gt;<\/span>\n<span style=\"color: #3366ff;\">&lt;VirtualHost _default_:443&gt;\n            ServerAdmin webmaster@localhost\n\n            DocumentRoot \/var\/www\n            &lt;Directory \/&gt;\n                         Options FollowSymLinks\n                         AllowOverride None\n            &lt;\/Directory&gt;\n            &lt;Directory \/var\/www&gt;\n                        Options Indexes FollowSymLinks MultiViews\n                        AllowOverride None\n                        Order allow,deny\n                        allow from all\n            &lt;\/Directory&gt;<\/span><\/em>\n<\/span><\/pre>\n<p>Change the webmaster@localhost to an email address of the person responsible for the server (this isn&#8217;t required, but is probably good practice).<\/p>\n<p>Also, you should probably add a ServerName directive right after the ServerAdmin directive.\u00a0 The ServerName directive should reflect the fully qualified domain name as the server.<\/p>\n<p>The DocumentRoot directive should be changed to \/var\/www-ssl &#8211; the directory we made earlier.\u00a0 Lastly, the Directory \/var\/www directive should be changed to Directory \/var\/www-ssl.<\/p>\n<p>Here is the same code snip, modified for a server called www.test.com:<\/p>\n<pre><span style=\"color: #3366ff;\"><em>&lt;IfModule mod_ssl.c&gt;\n&lt;VirtualHost _default_:443&gt;\n           ServerAdmin admin@test.com\n           ServerName www.test.com\n           DocumentRoot \/var\/www-ssl\n           &lt;Directory \/&gt;\n                  Options FollowSymLinks\n                  AllowOverride None\n           &lt;\/Directory&gt;\n           &lt;Directory \/var\/www-ssl&gt;\n                  Options Indexes FollowSymLinks MultiViews\n                  AllowOverride None\n                  Order allow,deny\n                  allow from all\n           &lt;\/Directory&gt;<\/em><\/span><\/pre>\n<p>Next, lets find the following couple of lines:<br \/>\n<code><br \/>\nSSLCertificateFile\u00a0\u00a0\u00a0 \/etc\/ssl\/certs\/ssl-cert-snakeoil.pem<br \/>\nSSLCertificateKeyFile \/etc\/ssl\/private\/ssl-cert-snakeoil.key<br \/>\n<\/code><br \/>\nThese lines tell Apache where to find the SSL certificate to use for the site.\u00a0 We are going to replace these with the certificate and private key we created earlier.\u00a0 We will change them to read:<br \/>\n<code><br \/>\nSSLCertificateFile \u00a0 \u00a0\/etc\/ssl\/certs\/host.cert<br \/>\nSSLCertificateKeyFile \/etc\/ssl\/private\/host.key<br \/>\n<\/code><br \/>\nOk &#8211; make sure you save your default-ssl file with the changes we have detailed above.\u00a0 After the file is saved, run the following command:<br \/>\n<code><br \/>\na2ensite default-ssl<br \/>\n<\/code><br \/>\nfollowed by:<br \/>\n<code><br \/>\n\/etc\/init.d\/apache2 restart<br \/>\n<\/code><\/p>\n<p>Now, its time to test to see if you can connect to the server with SSL.  You might want to create an index.html file at the \/var\/www-ssl directory first though.  To do this simply create a file with &#8220;It works, SSL!&#8221; in it as root and save it to the \/var\/www-ssl directory with the name of index.html.  Afterwards, open Firefox, and go to https:\/\/&lt;server name or IP address&gt;.  You probably get a certificate error, since we created a self-signed cert for our tutorial.  You should tell Firefox to trust the certificate.  If all goes well, you should see: It works, SSL! at the top of the page.<\/p>\n<p>If all went well, we should have a working Apache server with SSL.  At this point, you can place content directly under \/var\/www-ssl or in folders under that directory.  If you have some folders under the non-ssl Document root (normally \/var\/www) simply sim-linking those directories to \/var\/www-ssl will publish them via SSL as well.<\/p>\n<p>So, what if it doesn&#8217;t work?  Generally, this is the result of either SSL not being enabled, or an errror in your default-ssl file.  Double check that you have enabled SSL with the a2enmod command, and make sure the default-ssl file is correct, and that it has been enabled with the a2ensite command.  Also note, that when enabling SSL for the first time, make sure you restart Apache with the command:<br \/>\n<code><br \/>\n\/etc\/init.d\/apache2 restart<br \/>\n<\/code><br \/>\nTo make sure SSL support is enabled.  You can check that Apache is listening for SSL connection by doing a<br \/>\n<code><br \/>\nnetstat -tan<br \/>\n<\/code><br \/>\nand looking for an entry like this:<\/p>\n<pre><span style=\"color: #3366ff;\"><em>Active Internet connections (servers and established)\nProto Recv-Q Send-Q Local Address           Foreign Address     State\ntcp        0      0 0.0.0.0:443             0.0.0.0:*           LISTEN\n\n<\/em><\/span><span style=\"color: #3366ff;\">\u00a0<\/span><\/pre>\n<p>If that line is not present, then double check your configuration.<\/p>\n<p>Next time we will focus on the Mysql portion of a LAMP server &#8211; look for it soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi &#8211; Welcome back to the Ubuntu Apache LAMP Server Quick Howto.\u00a0 This second part of the series will cover configuring SSL for use with Apache for securing website content.\u00a0 SSL stands for Secure Sockets Layer, and is a method of providing transport layer encryption or TLS (transport layer security).\u00a0 SSL typically uses X.509 digital [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,10],"tags":[11,36,40,64,71,73],"_links":{"self":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts\/121"}],"collection":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=121"}],"version-history":[{"count":0,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxpoweruser.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}