Beruflich Dokumente
Kultur Dokumente
now we generate the certicates, for 3 years (1095 days) under the folder we created above.
sudo openssl req -x509 -nodes -days 1095 -newkey rsa:2048 -out
/etc/apache2/ssl/server.crt -keyout /etc/apache2/ssl/server.key
that will show the following, and ask you some questions.
Common Name, it should match the internet name FQDN (here demo.hallard.me)
Now we install the SSL mod for apache, this instruction pre configure the file
/etc/apache2/ports.conf with some line and the important one that say Listen 443
Now we edit the file default-ssl (or default-ssl.conf for new version) we have just enabled
Edit October 2014 : on new apache2 version, configuration files need to have .conf
extension, so in this case the two previous commands are now :
sudo ln -s /etc/apache2/sites-available/default-ssl.conf
/etc/apache2/sites-enabled/000-default-ssl.conf
sudo nano /etc/apache2/sites-enabled/000-default-ssl.conf
End of Edit
SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key
now you can go with your favorite browser, in my example https://demo.hallard.me, the
browser will warn you because it is a self signed certificate, but if you accept it you will now
have the same famous “It works!” but with encryption. To avoid warning by browser, you can
add the certificate to Trusted Root Certificate Authority of your computer. The procedure to
to this depends on browser and operating system, so google is your friend.
Now it is safe that you force SSL encryption on each page that require authentication.
For example, for WordPress, add the following two lines (just after the other existing define
lines in the file wp-config.php (located in wordpress installation dir)
Introduction
TLS, or transport layer security, and its predecessor SSL, which stands for secure sockets
layer, are web protocols used to wrap normal traffic in a protected, encrypted wrapper.
Using this technology, servers can send traffic safely between servers and clients without the
possibility of messages being intercepted by outside parties. The certificate system also assists
users in verifying the identity of the sites that they are connecting with.
A self-signed certificate will encrypt communication between your server and any clients.
However, because it is not signed by any of the trusted certificate authorities included with
web browsers, users cannot use the certificate to validate the identity of your server
automatically.
A self-signed certificate may be appropriate if you do not have a domain name associated
with your server and for instances where an encrypted web interface is not user-facing. If you
do have a domain name, in many cases it is better to use a CA-signed certificate.
TLS/SSL works by using a combination of a public certificate and a private key. The SSL key
is kept secret on the server. It is used to encrypt content sent to clients. The SSL certificate is
publicly shared with anyone requesting the content. It can be used to decrypt the content
signed by the associated SSL key.
We can create a self-signed key and certificate pair with OpenSSL in a single command:
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/apache-
selfsigned.key -out /etc/ssl/certs/apache-selfsigned.crt
You will be asked a series of questions. Before we go over that, let’s take a look at what is
happening in the command we are issuing:
openssl: This is the basic command line tool for creating and managing OpenSSL
certificates, keys, and other files.
req: This subcommand specifies that we want to use X.509 certificate signing request
(CSR) management. The “X.509” is a public key infrastructure standard that SSL and
TLS adheres to for its key and certificate management. We want to create a new X.509
cert, so we are using this subcommand.
-x509: This further modifies the previous subcommand by telling the utility that we
want to make a self-signed certificate instead of generating a certificate signing
request, as would normally happen.
-nodes: This tells OpenSSL to skip the option to secure our certificate with a
passphrase. We need Apache to be able to read the file, without user intervention,
when the server starts up. A passphrase would prevent this from happening because
we would have to enter it after every restart.
-days 365: This option sets the length of time that the certificate will be considered
valid. We set it for one year here.
-newkey rsa:2048: This specifies that we want to generate a new certificate and a new
key at the same time. We did not create the key that is required to sign the certificate
in a previous step, so we need to create it along with the certificate. The rsa:2048
portion tells it to make an RSA key that is 2048 bits long.
-keyout: This line tells OpenSSL where to place the generated private key file that we
are creating.
-out: This tells OpenSSL where to place the certificate that we are creating.
As we stated above, these options will create both a key file and a certificate. We will be
asked a few questions about our server in order to embed the information correctly in the
certificate.
Fill out the prompts appropriately. The most important line is the one that requests the
Common Name (e.g. server FQDN or YOUR name). You need to enter the domain name
associated with your server or, more likely, your server’s public IP address.
We have created our key and certificate files under the /etc/ssl directory. Now we just need
to modify our Apache configuration to take advantage of these.
3. (Recommended) We will modify the unencrypted Virtual Host file to automatically redirect
requests to the encrypted Virtual Host.
First, we will create an Apache configuration snippet to define some SSL settings. This will
set Apache up with a strong SSL cipher suite and enable some advanced features that will
help keep our server secure. The parameters we will set can be used by any Virtual Hosts
enabling SSL.
Create a new snippet in the /etc/apache2/conf-available directory. We will name the file
ssl-params.conf to make its purpose clear:
To set up Apache SSL securely, we will be using the recommendations by Remy van Elst on
the site. This site is designed to provide easy-to-consume encryption settings for popular
software.
Before we go any further, let’s back up the original SSL Virtual Host file:
Inside, with most of the comments removed, the Virtual Host file should look something like
this by default:
As it stands now, the server will provide both unencrypted HTTP and encrypted HTTPS
traffic. For better security, it is recommended in most cases to redirect HTTP to HTTPS
automatically. If you do not want or need this functionality, you can safely skip this section.
To adjust the unencrypted Virtual Host file to redirect all traffic to be SSL encrypted, we can
open the /etc/apache2/sites-available/000-default.conf file:
Inside, within the VirtualHost configuration blocks, we need to add a Redirect directive,
pointing all traffic to the SSL version of the site:
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
. . .
If you have the ufw firewall enabled, as recommended by the prerequisite guides, you might
need to adjust the settings to allow for SSL traffic. Luckily, Apache registers a few profiles
with ufw upon installation.
Output
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
If you allowed only regular HTTP traffic earlier, your output might look like this:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
To additionally let in HTTPS traffic, we can allow the “Apache Full” profile and then delete
the redundant “Apache” profile allowance:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
Now that we’ve made our changes and adjusted our firewall, we can enable the SSL and
headers modules in Apache, enable our SSL-ready Virtual Host, and restart Apache.
We can enable mod_ssl, the Apache SSL module, and mod_headers, needed by some of the
settings in our SSL snippet, with the a2enmod command:
Next, we can enable our SSL Virtual Host with the a2ensite command:
We will also need to enable our ssl-params.conf file, to read in the values we set:
At this point, our site and the necessary modules are enabled. We should check to make sure
that there are no syntax errors in our files. We can do this by typing:
If everything is successful, you will get a result that looks like this:
Output
AH00558: apache2: Could not reliably determine the server's fully qualified
domain name, using 127.0.1.1. Set the 'ServerName' directive globally to
suppress this message
Syntax OK
The first line is just a message telling you that the ServerName directive is not set globally. If
you want to get rid of that message, you can set ServerName to your server’s domain name or
IP address in /etc/apache2/apache2.conf. This is optional as the message will do no harm.
If your output has Syntax OK in it, your configuration file has no syntax errors. We can safely
restart Apache to implement our changes:
Open your web browser and type https:// followed by your server’s domain name or IP
into the address bar:
https://server_domain_or_IP
This is expected and normal. We are only interested in the encryption aspect of our certificate, not
the third party validation of our host’s authenticity. Click “ADVANCED” and then the link provided to
proceed to your host anyways
Find the Redirect line we added earlier. Add permanent to that line, which changes the
redirect from a 302 temporary redirect to a 301 permanent redirect:
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
. . .
. . .
</VirtualHost>
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from
# Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
# README.Debian.gz
NameVirtualHost *:80
Listen 80
<IfModule mod_ssl.c>
# If you add NameVirtualHost *:443 here, you will also have to change
# the VirtualHost statement in /etc/apache2/sites-available/default-ssl
# to <VirtualHost *:443>
# Server Name Indication for SSL named virtual hosts is currently not
# supported by MSIE on Windows XP.
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
default reads:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
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>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Access Control:
# With SSLRequire you can do per-directory access control based
# on arbitrary complex boolean expressions containing server
# variable checks and other lookup directives. The syntax is a
# mixture between C and Perl. See the mod_ssl documentation
# for more details.
#<Location />
#SSLRequire ( %{SSL_CIPHER} !~ m/^(EXP|NULL)/ \
# and %{SSL_CLIENT_S_DN_O} eq "Snake Oil, Ltd." \
# and %{SSL_CLIENT_S_DN_OU} in {"Staff", "CA", "Dev"} \
# and %{TIME_WDAY} >= 1 and %{TIME_WDAY} <= 5 \
# and %{TIME_HOUR} >= 8 and %{TIME_HOUR} <= 20 ) \
# or %{REMOTE_ADDR} =~ m/^192\.76\.162\.[0-9]+$/
#</Location>
# o StdEnvVars:
# This exports the standard SSL/TLS related `SSL_*' environment
variables.
# Per default this exportation is switched off for performance
reasons,
# because the extraction step is an expensive operation and is
usually
# useless for serving static content. So one usually enables
the
# exportation for CGI and SSI requests only.
# o StrictRequire:
# This denies access when "SSLRequireSSL" or "SSLRequire"
applied even
# under a "Satisfy any" situation, i.e. when it applies access
is denied
# and no other module can change it.
# o OptRenegotiate:
# This enables optimized SSL connection renegotiation handling
when SSL
# directives are used in per-directory context.
#SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
</IfModule>
After making all the changes I was sure to restart apache (using service apache2 restart,
apchectl -k restart, sudo /etc/init.d/apache2 restart - I've used them all at some point, though I
don't know if there is a difference).
After all this, HTTPS does not work: If I try to go to https://fileserver with my browser
(Chromium) I get an error:
first step in the simple and user-friendly process remains to activate SSL on the droplet. This
can be done by using the following command:
The above step needs to be followed up by restarting Apache, by using the following
command:
The second step entails creation of a new directory to store the server key and certificate. The
following command shall help users achieve that:
The above command shall lead to the creation of a new directory, as required. You are now
ready to move on to the next step.
When creating a new SSL certificate, one needs to specify the duration validity of the same by
changing the value 365 (as appearing in the message below) to the preferred number of days.
It is important to mention here that the certificate so created stands to auto-expire upon
completion of one year.
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout
/etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
The above command is rather versatile, and lets users create both the self-signed SSL
certificate and the server key to safeguard it, in addition to placing both of these into the new
directory. The command shall prompt the terminal to display a long list of important fields
that need to be supplied with correct details (as outlined below):
Execution of Step 1 through 3 shall ensure availability of all the requisite components of the
finished certificate. Consequently, users need to set up the virtual hosts that can display the
new certificate. This can be done by opening up the SSL configuration file, using the
following command:
nano /etc/apache2/sites-available/default-ssl
You need to locate the section that begins with on the window the above command shall lead
you to, and make the following changes in a swift manner. Subsequently, you need to add a
line with your unique server name right underneath the Server Admin email (as shown below)
ServerName sample.com:443
Here, users must replace - sample.com - with their unique DNS approved domain name/IP
address of the server. An important point to note here is that the unique domain name/server
IP address on the certificate must correspond to that supplied by the user under the field
‘common name) as part of Step 3 above.
Further, users need to locate the following three lines on the screen, and ensure an appropriate
match with the extensions below:
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
Once this is done, all you need to do is save and exit out of the file, which completes Step 4
for you.
Prior to the activation of the website that will appear on the 443 port, it is important to enable
that Virtual Host: The following command shall help you do that:
With the activation of the new Virtual Host using the above command, you are all set. All you
need to do is restart the Apache server to reload it with all the incorporated changes as per the
aforementioned steps. The following command shall lead you there:
Conclusion
You have configured your Apache server to use strong encryption for client connections. This
will allow you serve requests securely, and will prevent outside parties from reading your
traffic.