> ## Documentation Index
> Fetch the complete documentation index at: https://docs.futurex.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Configure Nginx Server

> Configuration steps for Nginx server to use HSM-backed TLS certificates and keys.

This section covers the following Nginx Server configuration tasks:

1. Set Futurex PKCS #11 environment variables
2. Configure Nginx certificates.
3. Configure Nginx to use the signed certificate and private key stored on the Vectera Plus.

The following sections provide detailed instructions.

## Set environment variables

In a terminal, run the following commands to set the required **FXPKCS11** environment variables, modifying the file path to match the location where the `libfxpkcs11.so` and `fxpkcs11.cfg` files are located on your system:

```shell expandable lines wrap title="Shell" theme={null}
export FXPKCS11_MODULE=/path/to/libfxpkcs11.so;
export FXPKCS11_CFG=/path/to/fxpkcs11.cfg;
```

## Configure Nginx certificates

Perform the following tasks to configure Nginx certificates:

1. Create a key pair on the Vectera Plus.
2. Generate a certificate signing request (CSR).
3. Create a self-signed root certificate authority (CA).
4. Sign the Nginx Server CSR.
5. Create a client certificate.

### Create a key pair

Perform the following steps to create a key pair on the Vectera Plus by using **pkcs11-tool**:

<Steps>
  <Step>
    In a terminal, run the following command to create a new RSA key pair on the Vectera Plus by using **pkcs11-tool**:

    ```shell expandable lines wrap title="Shell" theme={null}
    sudo pkcs11-tool --module $FXPKCS11_MODULE --login --keypairgen --key-type rsa:2048 -- label "Nginx_rsa_privatekey" --id "123456"
    ```
  </Step>

  <Step>
    When this **pkcs11-tool** command prompts for the PIN, enter the password of the identity configured in `fxpkcs11.cfg`.

    <Check>
      If successful, the command output lists the keys that pkcs11-tool created on the Vectera Plus.
    </Check>
  </Step>
</Steps>

### Generate a CSR

In a terminal, run the following command to generate a CSR from the private key that **pkcs11-tool** created on the Vectera Plus for Nginx in the preceding step. Ensure that the common name of the Nginx Server certificate matches the domain name or IP address of the virtual host for which you are configuring it.

```shell expandable lines wrap title="Shell" theme={null}
sudo openssl req -new -engine pkcs11 -keyform engine -key "pkcs11:object=Nginx_rsa_privatekey" -out Nginx-cert-req.pem
```

### Create a self-signed root CA

The following instructions show you how to create and use a self-signed root CA as a demonstration. In a production environment, use a secure certificate authority (such as the KMES Series 3) for all private key generation and certificate signing operations.

In a terminal, run the following commands to generate a root private key and self-signed certificate:

```shell expandable lines wrap title="Shell" theme={null}
sudo openssl genrsa -out ssl-ca-privatekey.pem 2048
sudo openssl req -new -x509 -key ssl-ca-privatekey.pem -out ssl-ca-cert.pem -days 365
```

### Sign the CSR

In a terminal, run the following command to issue a signed Nginx server certificate by using the self-signed root CA created in the preceding section:

```shell expandable lines wrap title="Shell" theme={null}
sudo openssl x509 -req -in Nginx-cert-req.pem -CA ssl-ca-cert.pem -CAkey ssl-ca-privatekey.pem -CAcreateserial -days 365 -out signed-Nginx-cert.pem -extensions v3_leaf
```

### Create a client certificate

Perform the following steps to create a client certificate for the browser that connects to Nginx:

<Steps>
  <Step>
    In a terminal, generate a client key pair by using the following command:

    ```shell expandable lines wrap title="Shell" theme={null}
    sudo openssl genrsa -out ssl-client-privatekey.pem 2048
    ```
  </Step>

  <Step>
    Run the following command to create a client certificate signing request:

    ```shell expandable lines wrap title="Shell" theme={null}
    sudo openssl req -new -key ssl-client-privatekey.pem -out ssl-client-req.pem -days 365
    ```
  </Step>

  <Step>
    Run the following command to sign the CSR with the CA certificate you created:

    ```shell expandable lines wrap title="Shell" theme={null}
    sudo openssl x509 -req -in ssl-client-req.pem -CA ssl-ca-cert.pem -CAkey ssl-ca-privatekey.pem -CAcreateserial -days 365 -out ssl-client-cert.pem -extensions v3_leaf
    ```
  </Step>

  <Step>
    Run the following command to convert the signed client certificate to PKCS #12 format for insertion into the browser:

    ```shell expandable lines wrap title="Shell" theme={null}
    sudo openssl pkcs12 -inkey ssl-client-privatekey.pem -in ssl-client-cert.pem -CAfile ssl-ca-cert.pem -export -out ssl-client-pkcs12.p12
    ```

    <Note>
      If you encounter SSL errors when connecting to your Nginx server in the web browser after completing this guide, ensure that you added the v3\leaf extensions properly. Nginx requires the SANS (added in as `IP.1` under the v3\leaf extension in your `openssl.cnf` file) in both the signed Nginx certificate and the browser client certificate.
    </Note>
  </Step>
</Steps>

## Configure Nginx

This section covers modifying the configuration file for an Nginx virtual host to use the signed certificate and private key stored on the Vectera Plus.

<Note>
  Refer to the documentation ([www.digitalocean.com/community/tutorial\collections/how-to-install-apache](http://www.digitalocean.com/community/tutorialcollections/how-to-install-apache)) specific to your operating system if you have not already configured a virtual host. Configuring a virtual host is outside the scope of this guide.
</Note>

<Steps>
  <Step>
    Before making any changes, stop your Nginx server with the following commands:

    ```shell expandable lines wrap title="Shell" theme={null}
    sudo systemctl stop Nginx
    sudo service Nginx stop
    ```
  </Step>

  <Step>
    In a text editor, open the configuration file in your `conf.d` folder in the Nginx directory for the virtual host you want to configure HTTPS for and modify it as shown in the following example:

    <Note>
      Modify the location of the signed Nginx certificate specified in the ssl\certificate define according to where it is stored on your system.

      The object name of the Nginx private key specified in the ssl\certificate\_key define must match the label you set in the pkcs11-tool command.
    </Note>

    ```none expandable lines wrap title="None" theme={null}
    server {
            listen 443 ssl;
    	server_name www.example.com;
            ssl_certificate /usr/local/bin/fxpkcs11/signed-Nginx-cert.pem;
            ssl_certificate_key "engine:pkcs11:pkcs11:token=Futurex;object=Nginx_rsa_privatekey";
    	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
            root /var/www/html;
            index index.html index.htm index.Nginx-debian.html;
            access_log /var/log/Nginx/access.log;
            error_log /var/log/Nginx/error.log;
            #ssl_verify_client off;

            location / {
                    # First attempt to serve request as file, then
                    # as directory, then fall back to displaying a 404.
                    try_files $uri $uri/ =404;
    	 }
    }
    ```
  </Step>

  <Step>
    Restart your Nginx server by using the following command:

    ```shell expandable lines wrap title="Shell" theme={null}
    sudo Nginx -g 'daemon off;'
    ```

    <Note>
      This integration needs the daemon off startup parameter. Do not close the window during operation. If you get an error message on startup, check to ensure a service is not already running on port 443.
    </Note>
  </Step>
</Steps>
