https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-with-a-free-signed-ssl-certificate-on-a-vps
Note: You may want to consider using Let's Encrypt instead of the StartSSL.com process below. Let's Encrypt is a new certificate authority that provides a free and easy way of creating SSL/TLS certificates that are trusted in most web browsers. Check out the tutorial to get started: How To Secure Apache with Let's Encrypt on Ubuntu 14.04
Prerequisites
Before we get started, here are the web tools you need for this tutorial:
- Google Chrome browser
- Apache installed on your VPS (cloud server)
- A domain name you own
- Access to an email address at that domain, either:
- postmaster@duable.co
- hostmaster@duable.co
- webmaster@duable.co
StartSSL.com offers completely free verified (your users won't have to see those scary red screens saying "this site isn't trusted" anymore) SSL certificates that you can use on your website. This is a great deal as most companies charge $50-$60 for similar services. The free version is a bit tricky to set up, but it's well worth it.
To get started, browse to StartSSL.com and using the toolbar on the left, navigate to StartSSL Products and then to StartSSL™ Free. Choose the link for Control Panel from the top of the page.
Make sure you are using Google Chrome
- Choose the Express Signup. option
- Enter your personal information, and click continue.
- You'll get an email with a verification code inside it shortly. Copy and paste that email into the form on StartSSL's page.
- They will review your request for a certificate and then send you an email with the new info. This process might take as long as 6 hours though, so be patient.
- Once the email comes, use the link provided and the new authentication code (at the bottom of the email) to continue to the next step.
- They will ask you to Generate a private key and you will be provided with the choice of "High" or "Medium" grade. Go ahead and choose "High".
- Once your key is ready, click Install.
- Chrome will show a popdown that says that the certificate has been succesfully installed to Chrome.
This means your browser is now authenticated with your new certificate and you can log into the StartSSL authentication areas using your new certificate. Now, we need to get a properly formatted certificate set up for use on your VPS. Click on the Control panel link again, and choose the Authenticate option. Chrome will show a popup asking if you want to authenticate and will show the certificate you just installed. Go ahead and authenticate with that certificate to enter the control panel.
You will need to validate your domain name to prove that you own the domain you are setting up a certificate for. Click over to the Validations Wizard in the Control panel and set Type to Domain Name Validation. You'll be prompted to choose from an email at your domain, something like [email]postmaster@yourdomain.com[/email].
Check the email inbox for the email address you selected. You will get yet another verification email at that address, so like before, copy and paste the verification code into the StartSSL website.
Next, go to the Certificates Wizard tab and choose to create a Web Server SSL/TLS Certificate.
Hit continue and then enter in a secure password, leaving the other settings as is.
You will be shown a textbox that contains your private key. Copy and paste the contents into a text editor and save the data into a file called ssl.key.
When you click continue, you will be asked which domain you want to create the certificate for:
Choose your domain and proceed to the next step.
You will be asked what subdomain you want to create a certificate for. In most cases, you want to choose www here, but if you'd like to use a different subdomain with SSL, then enter that here instead:
StartSSL will provide you with your new certificate in a text box, much as it did for the private key:
Again, copy and paste into a text editor, this time saving it as ssl.crt.
You will also need the StartCom Root CA and StartSSL's Class 1 Intermediate Server CA in order to authenticate your website though, so for the final step, go over to the Toolbox pane and choose StartCom CA Certificates:
At this screen, right click and Save As two files:
- StartCom Root CA (PEM Encoded) (save to ca.pem)
- Class 1 Intermediate Server CA (save to sub.class1.server.ca.pem)
For security reasons, StartSSL encrypts your private key (the ssl.key file), but your web server needs the unencrypted version of it to handle your site's encryption. To unencrypt it, copy it onto your server, and use the following command to decrypt it into the file private.key:
openssl rsa -in ssl.key -out private.key
OpenSSL will ask you for your password, so enter it in the password you typed in on StartSSL's website.
At this point you should have five files. If you're missing any, double-check the previous steps and re-download them:
- ca.pem - StartSSL's Root certificate
- private.key - The unencrypted version of your private key (be very careful no one else has access to this file!)
- sub.class1.server.ca.pem - The intermediate certificate for StartSSL
- ssl.key - The encrypted version of your private key (does not need to be copied to server)
- ssl.crt - Your new certificate
You can discard the ssl.key file. If you haven't already copied the others onto your server you upload them there now:
scp {ca.pem,private.key,sub.class1.server.ca.pem,ssl.crt} YOURSERVER:~ 1 c$ h, I) M1 x/ m9 R' q1 ^
Activating the certificate in Apache
Having a certificate isn't any good if you can't actually use it. This section explains how to configure Apache to use your new SSL certificate. These instructions are for Apache running on recent versions of Ubuntu VPS. For other Linux-based distros or web servers, you'll have to adjust accordingly.
First, create the folders where we'll store the keys. Enable Apache's SSL module, and restart Apache.
sudo a2enmod ssl
! P5 ? ~2 u6 h/ asudo service apache2 restart
4 R7 X) H9 q. ^1 C# Csudo mkdir -p /etc/apache2/ssl( x' D# w$ P: s5 ~3 X4 t; Z* B
Copy the files you set up in the previous section into the /etc/apache2/ssl folder on your VPS.
sudo mkdir -p /etc/apache2/ssl
& o' w" d9 ]! I* Q; d# W% O% [cp ~/{ca.pem,private.key,sub.class1.server.ca.pem,ssl.crt} /etc/apache2/ssl2 b* J( h4 W5 X M
Execute:
ls /etc/apache2/ssl
And it should return:
ca.pem/ \5 Q5 | E5 i' ]
ssl.crt
@& O5 n8 B3 i, E9 Sprivate.key
* L8 G* N" t5 S) ?/ F$ ~& Zsub.class1.server.ca.pem
Now, open your apache2 configuration file. Unless you've already modified the default configuration, input:
nano /etc/apache2/sites-enabled/000-default
It should look something like this:
<VirtualHost *:80>
8 b h( L+ @( E/ `0 c7 O, g ServerAdmin webmaster@localhost3 g; f$ ?6 w+ Y* j- ?
Y8 E- i! k/ a/ I7 p% s9 R( V
DocumentRoot /var/www
* F7 x3 X; ~1 I' g- Q% e$ V <Directory />- v' q# s) Q% H ]
Options FollowSymLinks1 a9 `5 r7 A# W, k5 `
AllowOverride None
* E" l: k; y! F$ a |. [: h! \" M </Directory>: Y/ D* E4 ~& }, h
<Directory /var/www/>. {: K; R1 H2 u2 n8 L0 D
Options Indexes FollowSymLinks MultiViews
5 c9 n/ S5 _ b, x; m) ^ AllowOverride None( p8 X% e8 `) s$ J) b4 R
Order allow,deny
5 J( b/ p7 e) E: ~" } allow from all0 Q2 Z. p2 c% A/ M8 h
</Directory>* D: e7 ^0 O- K* R! O, J
3 h) B2 r2 F9 | ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
' d) |8 i7 H* L7 y% L <Directory "/usr/lib/cgi-bin">
$ p; @& `- C) w. f9 ? AllowOverride None
5 n7 z1 b1 L3 k) Z9 I! c Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Y5 y; w J D. r- S1 x$ O9 @# N1 M Order allow,deny9 e9 P% S# u7 }' D* j
Allow from all
* t% W: S: s- _4 f5 J9 p3 v0 l </Directory>. K4 w: s/ {9 L G% i' m+ {' p6 k
8 _0 e! u3 F% X) j
ErrorLog ${APACHE_LOG_DIR}/error.log
) }: z" ]5 [4 H1 [
, y0 @0 V& C' W; w! g # Possible values include: debug, info, notice, warn, error, crit,5 \9 y1 q# A0 ?! ~
# alert, emerg.
- r- I, K. _2 p4 }# F LogLevel warn
4 z% X% ~9 j3 u9 Z
5 `3 w+ V% w& _: c! @' [7 u7 M" V CustomLog ${APACHE_LOG_DIR}/access.log combined
+ U m2 {& f/ V( z/ d# ]* R* [
+ f; E0 W" j6 K6 ^6 @ M7 q+ T Alias /doc/ "/usr/share/doc/"
$ E$ v- c: j. v% O <Directory "/usr/share/doc/">$ J9 i* I+ U. M1 y: W2 {0 ]
Options Indexes MultiViews FollowSymLinks+ {3 T' C- F9 }1 P( u
AllowOverride None
$ p9 j7 R8 D0 k" | Order deny,allow
+ Z c8 C* z/ d" l6 S Deny from all' M/ t0 C- G1 X
Allow from 127.0.0.0/255.0.0.0 ::1/1286 u$ j$ E1 c5 E0 F7 X
</Directory>6 A, n1 i8 A" J8 I: }6 |: y
4 r2 H8 ~) g$ P" ?& M</VirtualHost>
9 C0 U# k9 x& w- i4 F
Copy the entire script above (from <VirtualHost *:80> to </VirtualHost>), paste it below the existing one, and change the top line from:
<VirtualHost *:80>
to
<VirtualHost *:443>
And add the following lines after the <VirtualHost *:443> line:
SSLEngine on
$ Z/ f/ C- u/ q+ i& [0 S/ ]SSLProtocol all -SSLv2 ) c8 D, F) C0 n* b1 m0 i7 t
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM ' f% J7 ~9 O( j2 A K6 Y) E: a# n5 S
3 M/ N0 `5 w3 p" ^+ r2 b0 e
SSLCertificateFile /etc/apache2/ssl/ssl.crt
. X A' d' ~/ W* g6 c, T! p8 F2 h$ M# KSSLCertificateKeyFile /etc/apache2/ssl/private.key + r5 f. F1 f; X# N2 W& h, x
SSLCertificateChainFile /etc/apache2/ssl/sub.class1.server.ca.pem
The end result should look like this:
<VirtualHost *:80>
5 V0 p4 o) |$ U3 N ServerAdmin webmaster@localhost# o" J+ ~! {7 h) s
0 ~2 O0 t8 @+ ~, L
DocumentRoot /var/www
8 ^$ W; ]. `8 }% S2 ]5 F <Directory />
1 A' l% _, O$ E# [ Options FollowSymLinks
( y5 z0 B4 A( Q* y AllowOverride None
$ e6 ]) H; i# T' a0 l6 t; \, C </Directory>
8 Z6 D6 g8 T5 G/ E4 k: M) n <Directory /var/www/>
3 s: Q6 c; V1 ]; ~6 E$ u Options Indexes FollowSymLinks MultiViews
3 H1 L( C1 I% A F8 {/ [ AllowOverride None
( w) {. s/ r; A; x3 I9 k: y Order allow,deny2 f8 h$ g: V5 P2 _
allow from all
d; I; ?2 c: y+ T </Directory>
1 Y2 s/ u6 F( `
/ |* ] X3 ?3 k* R# V/ H ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
4 z* j1 q- D8 Q4 H% x0 Y <Directory "/usr/lib/cgi-bin">! I$ ^4 \0 e- [9 j/ O4 H
AllowOverride None
" B3 w0 D; e. ?/ j1 Q) ?, ] Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
) x7 j o8 r3 L8 r5 x$ n1 ~* X Order allow,deny
0 \: b( S7 J, b0 _/ D Allow from all: f1 ~1 _7 _( u
</Directory>
) R/ m# C( j, o6 S/ m3 p' i
) `7 l A+ w" L* w! Y4 A1 p ErrorLog ${APACHE_LOG_DIR}/error.log: y4 j. D' o& i8 A( G0 U% H
. O0 _5 R; d6 e5 U( u) T4 l # Possible values include: debug, info, notice, warn, error, crit,
: O$ b. d! T( N7 p' J6 K8 b # alert, emerg.
6 a* X! j, R4 X8 I LogLevel warn- H. K$ T n6 C
u; K$ @8 ^4 o% B3 q3 ] CustomLog ${APACHE_LOG_DIR}/access.log combined; U2 j! K- v5 b, }2 K0 v$ O( ?
s7 ~# D5 k ^# \, z5 C Alias /doc/ "/usr/share/doc/"
' S1 P6 J. y! c w1 ^; [ <Directory "/usr/share/doc/">
- _/ `3 m9 d- L2 M6 r6 j: R5 q Options Indexes MultiViews FollowSymLinks2 [! ]. C0 q' z: Q4 l+ o; V! b x, v3 {
AllowOverride None
& X% F+ Y% M! K2 l, W( ^ Order deny,allow
6 h7 P4 f. _( S& T Deny from all8 b& @8 l2 R) J, v" V. z! j
Allow from 127.0.0.0/255.0.0.0 ::1/128
0 T' j* f; E* Y) j5 a </Directory>
' j) A- _9 n0 H9 K4 Y/ _2 n
( [& c2 A4 O! a' V7 s! ^7 }</VirtualHost>( T+ e4 X; f+ I; i2 x
P7 | |7 _' k( M; Q1 y1 X<VirtualHost *:443>
K" v. ?0 P; H9 h* P. ?2 t9 i9 h SSLEngine on & {! D3 ^2 k4 z) y6 q4 `/ w
SSLProtocol all -SSLv2 5 N$ O0 ^7 \9 [/ V5 k. k8 P
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM
, E8 A7 s# I: X9 _7 }9 m7 Q$ x' k& S9 [2 T% Z1 v% Z; l5 L
SSLCertificateFile /etc/apache2/ssl/ssl.crt : p* M. b- y P0 ^, U
SSLCertificateKeyFile /etc/apache2/ssl/private.key
! {5 U- q$ j' r& G$ z* r SSLCertificateChainFile /etc/apache2/ssl/sub.class1.server.ca.pem
1 D$ ?4 d$ S7 A. P. B8 C2 | ServerAdmin webmaster@localhost
/ [% B6 `# i9 B
$ ~( Q8 E; e U4 S- r( r DocumentRoot /var/www
& }) l7 k" F4 ?! Z* Y# |" Q6 z2 e <Directory />( C( R+ @, G+ d* q
Options FollowSymLinks
6 b0 l7 S( }8 P2 s# r$ Y2 e2 ^ AllowOverride None/ B1 y5 |& _* ?0 S1 U% m
</Directory>
* W0 _% d* M% ?: r9 C9 |, S <Directory /var/www/>
) y2 ?" j8 k$ W; T Options Indexes FollowSymLinks MultiViews
. i. k4 B, v' _! v: e- @ AllowOverride None
9 ]7 t/ p8 u4 s$ Y. K* i2 g Order allow,deny
A8 o" a. Q6 Y. R4 p3 z2 k' B/ A+ ~" y allow from all6 f/ A7 G( |4 U% Q% p' \
</Directory>
$ @5 Z0 W/ s" v- A7 T1 i0 ]
. c: Z+ T# `8 c+ G$ @" s7 g! w ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/5 Q8 ?4 A A' l
<Directory "/usr/lib/cgi-bin">
+ Q5 t( f6 _/ i3 j5 l AllowOverride None9 }* W; ]* V% A8 h. V4 F
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch i r4 ~ { v: U5 m: N8 p, o
Order allow,deny: ?1 d( o: q# v( G* {+ ^1 m. m2 [1 j
Allow from all. l) H: z# V+ j: {$ b: C
</Directory>
) h2 _4 U, e4 v* s4 k% t; D5 U
3 F) R* k5 P# p, v. a ErrorLog ${APACHE_LOG_DIR}/error.log
) O' S6 w' j1 A( t5 W# g9 L2 \. K" r+ w% J
# Possible values include: debug, info, notice, warn, error, crit,& _1 h7 S8 o P
# alert, emerg.
& u; K- U, [6 G LogLevel warn2 R3 \' m% g0 u% O
0 X5 x' y. l6 Q6 k. L; ?: j
CustomLog ${APACHE_LOG_DIR}/access.log combined/ ?' C5 f+ R: j$ f" i' s
2 G- D0 E% L2 Q6 x8 h8 Q: q
Alias /doc/ "/usr/share/doc/": b9 B" t: [+ [/ @& e! F
<Directory "/usr/share/doc/">; B0 i3 U1 [' \, o j- M9 J7 G a
Options Indexes MultiViews FollowSymLinks
6 Q0 K8 r. z2 p2 j9 d" ?5 P3 Z AllowOverride None; @9 e: y2 C# i4 X0 w, l L, [0 n: }% ?6 H
Order deny,allow9 H/ s/ w! N# @3 G8 H; B1 p& n
Deny from all. v8 W; C1 A2 V1 e; X
Allow from 127.0.0.0/255.0.0.0 ::1/128. m& d& ?% t; ~, y7 ], z
</Directory>
' L. J4 s7 F2 x! n5 W( l/ g' H; C5 R. t" s
</VirtualHost>
Save your files and restart Apache with:
sudo service apache2 restart
You can check Apache's log files to see if there are any show stopping errors with this command:
cat /var/log/apache2/error.log
8 ~) j" _) L7 d$ e5 Z& N- R; \
If everything looks good, try accessing your site in your web browser using an HTTPS URL (e.g. https://www.YOURSITE.com). When your site loads, you should see a little green padlock icon next to the URL. Click on it and you should see the following. The connections tab should show that the site's identity has been verified by StartCom.
Congratulations! You are all set!
Reference Links:
Here are some of the other posts I consulted when putting this together. If you run into any problems they might be a source of inspiration on how to fix them: