Skip to main content

How To Set Up Multiple SSL Certificates with Nginx on Ubuntu



You can host multiple SSL certificates on one IP Address using Server Name Identification (SNI).

SNI ?

Although hosting several sites on a single virtual private server is not a challenge with the use of virtual hosts, providing separate SSL certificates for each site traditionally required separate IP addresses. The process has recently been simplified through the use of Server Name Indication (SNI), which sends a site visitor the certificate that matches the requested server name.

SNI can only be used for serving multiple SSL sites from your web server and is not likely to work at all on other daemons, such as mail servers, etc. There are also a small percentage of older web browsers that may still give certificate errors. Wikipedia has an updated list of software that does and does not support this TLS extension.

Setting Up

SNI does need to have registered domain names in order to serve the certificates.

The steps in this tutorial require the user to have root privileges. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4.

Nginx should already be installed and running on your VPS.

If this is not the case, you can download it with this command:

sudo apt-get install nginx

You can make sure that SNI is enabled on your server:

nginx -V

After displaying the nginx version, you should see the line:

TLS SNI support enabled

SNI does need to have registered domain names in order to serve the certificates.


The steps in this tutorial require the user to have root privileges. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4.

Nginx should already be installed and running on your VPS.

If this is not the case, you can download it with this command:

sudo apt-get install nginx

You can make sure that SNI is enabled on your server:

nginx -V

After displaying the nginx version, you should see the line:

TLS SNI support enabled



Step One—Create Your SSL Certificate Directories

For the purposes of this tutorial, both certificates will be self-signed. We will be working to create a server that hosts both test.com and test.org.

The SSL certificate has 2 parts main parts: the certificate itself and the public key. To make all of the relevant files easy to access, we should create a directory for each virtual host’s SSL certificate.
mkdir -p /etc/nginx/ssl/test.com

mkdir -p /etc/nginx/ssl/test.org

Step Two—Create the Server Key and Certificate Signing Request

First, create the SSL certificate for test.com.

Switch into the proper directory:

cd /etc/nginx/ssl/test.com

Start by creating the private server key. During this process, you will be asked to enter a specific passphrase. Be sure to note this phrase carefully, if you forget it or lose it, you will not be able to access the certificate.

sudo openssl genrsa -des3 -out server.key 1024

Follow up by creating a certificate signing request:

sudo openssl req -new -key server.key -out server.csr

This command will prompt terminal to display a lists of fields that need to be filled in.

The most important line is "Common Name". Enter your official domain name here or, if you don't have one yet, your site's IP address. Leave the challenge password and optional company name blank.

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]:US

State or Province Name (full name) [Some-State]:New York

Locality Name (eg, city) []:NYC

Organization Name (eg, company) [Internet Widgits Pty Ltd]:Awesome Inc

Organizational Unit Name (eg, section) []:Dept of Merriment

Common Name (e.g. server FQDN or YOUR name) []:test.com

Email Address []:webmaster@awesomeinc.com
Step Three—Remove the Passphrase

We are almost finished creating the certificate. However, it would serve us to remove the passphrase. Although having the passphrase in place does provide heightened security, the issue starts when one tries to reload nginx. In the event that nginx crashes or needs to reboot, you will always have to re-enter your passphrase to get your entire web server back online.

Use this command to remove the password:

sudo cp server.key server.key.org

sudo openssl rsa -in server.key.org -out server.key


Step Four—Sign your SSL Certificate

Your certificate is all but done, and you just have to sign it.


Keep in mind that you can specify how long the certificate should remain valid by changing the 365 to the number of days you prefer. As it stands this certificate will expire after one year.

sudo openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

You are now done making the certificate for your first host.

To create the second certificate, switch into the second directory:

cd /etc/nginx/ssl/test.org

Repeat the previous three steps for the second certificate. Once both are squared away, you can start adding the certificates to your virtual hosts.


Step Five—Create the Virtual Hosts

Once you have the certificates saved and ready, you can add in your information in the virtual host file.

Although it’s not required, we can create two virtual host files to store virtual hosts in a separate files.

sudo nano /etc/nginx/sites-available/test.com

Each file will then contain the virtual host configuration (make sure to edit the server_name, ssl_certificate, and ssl_certificate_key lines to match your details):


server {


listen 443;

server_name test.com;

root /usr/share/nginx/www;

index index.html index.htm;

ssl on;

ssl_certificate /etc/nginx/ssl/test.com/server.crt;

ssl_certificate_key /etc/nginx/ssl/test.com/server.key;

}

You can then put in the appropriate configuration into the other virtual host file.

sudo nano /etc/nginx/sites-available/test.org

server {

listen 443;

server_name test.org;

root /usr/share/nginx/www;

index index.html index.htm;

ssl on;

ssl_certificate /etc/nginx/ssl/test.org/server.crt;

ssl_certificate_key /etc/nginx/ssl/test.org/server.key;

}
Step Six—Activate the Virtual Hosts

The last step is to activate the hosts by creating a symbolic link between the sites-available directory and the sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enabled/test.com

sudo ln -s /etc/nginx/sites-available/test.org /etc/nginx/sites-enabled/test.org

With all of the virtual hosts in place, restart nginx.

sudo service nginx restart

You should now be able to access both sites, each with its own domain name and SSL certificate.

You can view the sites both with and without the signed SSL certificates by typing in just the domain (eg. test.com or test.org) or the domain with the https prefix (https://test.com or https://test.org).

Comments

Popular posts from this blog

Why Dozer Framework (Bean Manipulation)

Why Dozer ? Let us think about a situation that you have a source bean which contains lot of fields and the source bean belongs to a different project or module. Now you want to expose the bean to  the outside world as a part of your web service REST service development. It is not advisable to do it. There may be the following reasons. The source bean is not serialized and a final class. The source system does not allow doing it because of security breach. The source bean is very heavy and contains lot of nested beans. The source bean has fields of different types which may not be required for other system. The source bean has lot of fields; some of them are not required. Scenario to use Dozer Suppose You want to make a REST call or web service call to get the minimal account details of a person. But the source system has a bean called “Acc0untBean” which contains many sensitive information like person’s internet banking passw0rd, PAN no or social sec...

Difference between Micro Service and Web Services

Micro web services and Web services are two different concepts of application development architecture, Which can be differentiated from it's development style and layered architecture.In This article I will explain the difference between Web Services and Micro Services Web Services ? Web services are services that are made available from a business's Web server for Web users or other Web-connected programs. it is a way to expose the functionality of an application to other application, without a user interface. It is a service which exposes an API over HTTP. Web Services allow applications developed in different technologies to communicate with each other through a common format like XML, Jason, etc.  Web services are not tied to any one operating system or programming language. For example, an application developed in Java can be used in C#, Android, Php etc., and vice versa.  Web Service is a connection technology, a way to connect services together into a ...

JAVA_OPTS Variable Details

Memory Available to the Java JVM Increasing the memory available to the Java JVM JAVA_OPTS="-Xmx1024m -Xms256m" export JAVA_OPT Options description: -Xmx sets the maximum amount of memory that can be allocated to the JVM heap; here it is being set to 1024 megabytes. -Xms sets the initial amount of memory allocated to the JVM heap; here it is being set to 256 megabytes. Run Java JVM in Server Mode The Java JVM can optimize a number of things for server environments. You can explicitly select the Java HotSpot Server VM with the -server option. JAVA_OPTS="-Xmx1024m -Xms256m -server" export JAVA_OPT What the option means: -server instructs the launcher to use the Java HotSpot Server VM. PermGen Memory If you start getting java.lang.OutOfMemoryError: PermGen space error messages. You may want to include a "-XX:MaxPermSize" option in your JAVA_OPTS. JAVA_OPTS="-Xmx1024m -Xms256m -server -XX:MaxPermSize=128m" export...