Search code examples
loopssshgrepfindxargs

how to export ssl key , crt and CA from httpd.conf apache to use it into nginx for all users


use custom setup that use nginx as web engine with cpanel need command to export ssl files to use it into nginx

cpanel now use AutoSSL powered by Comodo that give it free and will renew it automatic when any users domains ssl expire

example httpd.conf

<VirtualHost 4xx30:4433>
  ServerName xnxxsch.com
  <IfModule ssl_module>
 SSLCertificateFile /var/cpanel/ssl/installed/certs/xnh_com_d98c5_67ca3_150707$
    SSLCertificateKeyFile /var/cpanel/ssl/installed/keys/d98c5_67ca3_76c14a301e0260891bbe91504$
    SSLCACertificateFile /var/cpanel/ssl/installed/cabundles/cPanel_Inc__681917bfb43af6b642178$
  </IfModule>
</VirtualHost>

<VirtualHost 46.xx30:4433>
  ServerName xxxh.com
  <IfModule ssl_module>
 SSLCertificateFile /var/cpanel/ssl/installed/certs/xnah_com_d98c5_67ca3_150707$
    SSLCertificateKeyFile /var/cpanel/ssl/installed/keys/d98c5_67ca3_76c14a301e0260891bbe91504$
    SSLCACertificateFile /var/cpanel/ssl/installed/cabundles/cPanel_Inc__681917bfb43af6b642178$
  </IfModule>
</VirtualHost>

need to export every domains (ServerName)

as two files

SSLCertificateKeyFile as ServerName.key

and

SSLCertificateFile+ SSLCACertificateFile as ServerName.crt

from ssh

with

grep 'ServerName' /etc/apache2/conf/httpd.conf

i export all need to use at loop

to get SSLCertificateKeyFile under it

and copy it with name servername.crt to /etc/nginx/ssl/


Solution

  • I'm sure some efficiency nuts will choke on this, but it should work:

    #!/bin/bash
    # Look for ServerName, and extract the value.  Loop over results.
    for server in $( grep ServerName httpd.conf | sed 's/.*ServerName\s*//' ); do
        echo $server
        # Pull out the block of XML for that server
        block=$( grep -A5 "$server" httpd.conf)
    
        # Extract file names from the XML block
        SSLCertificateFile=$( echo "$block" | sed -n 's/.*SSLCertificateFile\s*//p')
        SSLCertificateKeyFile=$( echo "$block" | sed -n 's/.*SSLCertificateKeyFile\s*//p')
        SSLCACertificateFile=$( echo "$block" | sed -n 's/.*SSLCACertificateFile\s*//p')
    
        # Create files
        cp "$SSLCertificateKeyFile" "${server}.key"
        cat "$SSLCertificateFile" "$SSLCACertificateFile" > "${server}.crt"
    done
    # end of loop