{"id":6353,"date":"2021-06-26T16:15:46","date_gmt":"2021-06-26T10:45:46","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=6353"},"modified":"2021-06-26T16:55:44","modified_gmt":"2021-06-26T11:25:44","slug":"how-to-install-nginx-and-lets-encrypt-ssl-with-html-docker-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-install-nginx-and-lets-encrypt-ssl-with-html-docker-ubuntu-20-04\/","title":{"rendered":"How to Install Nginx and Let\u2019s Encrypt SSL with HTML + Docker \u2013 Ubuntu 20.04"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>How to Install Nginx and Let\u2019s Encrypt SSL with HTML + Docker \u2013 Ubuntu 20.04<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Date: 26-06-2021<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Prerequisites:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">1. Install Docker on your server.<br>2. Install Docker Compose on your server.<br>3. Configure DNS to point the domain to the server to install SSL.<br>Note: In this document, I use <strong>ramesh.pheonixsolutions.com<\/strong> domain name.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 1: Create Docker Compose YML file:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Login into the server via ssh and create a directory <strong>\u201cnginx-ssl\u201d <\/strong>by using the below command.<br><strong># sudo mkdir nginx-ssl\u00a0<\/strong><br>Move inside the above directory and create a <strong>docker-compose.yml<\/strong> file and paste the below configurations inside the file.<br><strong>Note: <\/strong>\/public:\/var\/www\/html &#8211; Application\/Website data directory.<br>=======================================================<br><code>version: \"3.0\"<br>services:<br>    web:<br>         image: nginx:latest\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<br>         restart: always<br>         volumes:<br>             - .\/public:\/var\/www\/html<br>             - .\/conf.d:\/etc\/nginx\/conf.d<br>             - .\/certbot\/conf:\/etc\/nginx\/ssl<br>             - .\/certbot\/data:\/var\/www\/certbot<br>         ports:<br>             - 80:80<br>             - 443:443<br>    certbot:<br>         image: certbot\/certbot:latest<br>         command: certonly --webroot --webroot-path=\/var\/www\/certbot --email pheonixsolutionshelpdesk@gmail.com --agree-tos --no-eff-email -d ramesh.pheonixsolutions.com<br>         volumes:<br>              - .\/certbot\/conf:\/etc\/letsencrypt<br>              - .\/certbot\/logs:\/var\/log\/letsencrypt<br>              - .\/certbot\/data:\/var\/www\/certbot<\/code><br>========================================================<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-24-at-9.32.01-AM.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"330\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-24-at-9.32.01-AM-1024x330.png\" alt=\"\" class=\"wp-image-6368\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-24-at-9.32.01-AM-1024x330.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-24-at-9.32.01-AM-300x97.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-24-at-9.32.01-AM-768x248.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-24-at-9.32.01-AM-1536x495.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-24-at-9.32.01-AM-850x274.png 850w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2021\/06\/Screen-Shot-2021-06-24-at-9.32.01-AM.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 2: Configure Nginx:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We will create a directory as mentioned in the docker-compose file as <strong>\u201cconf.d\u201d<\/strong> so create this directory inside the nginx-ssl directory.<strong><br># sudo mkdir nginx-ssl\/conf.d<\/strong><br>Create a configuration file with the <strong>ramesh.pheonixsolutions.com.conf<\/strong> extension in the conf.d directory and paste the below configurations inside the file.<br>============================================<br><code>server {<br>     listen [::]:80;<br>     listen 80;<br>     server_name ramesh.pheonixsolution.com;<br>     location ~ \/.well-known\/acme-challenge {<br>         allow all;<br>          root \/var\/www\/certbot;<br>     }<br>}<\/code><br>===============================================<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><img decoding=\"async\" width=\"624\" height=\"165\" src=\"https:\/\/lh3.googleusercontent.com\/KGke2T1_b7xdIq7uYw-u77nXOdfyKLO288DcdvywXrn_s9C123DY6nGUZGW9y8DgWvNhtZzn29TjME9NpVxlO5eTjgf8rCNPNEy5GQ5VVma6zGX5rV-7a6xzXCvMSJwAslBeoZxB\"><\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 3: Start Docker Containers:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can start the containers using the following command to receive the SSL certificates.<br><strong># docker-compose up -d<\/strong><br><br>You will see an output similar to the below one.<br><img decoding=\"async\" width=\"624\" height=\"715\" src=\"https:\/\/lh3.googleusercontent.com\/vZzehHOUtwOY7aOJj6q5J0HWoINAiqBciG0wvpAwUWq0O8wEVUR8nyItyUGJ6DUwyf5fcjwxPUtiQ4Li3zoosTRyotXtfTTPwpsA9i6xfTTABq9BtpZ24wtPOsVy_jZlXrSy3EQk\"><br>This output indicates the Nginx and Certbot images are pulled from Docker hub and the containers are created successfully.<br><br>Use the below command to view the container&#8217;s status. You will get below output.<br><strong># docker-compose ps<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"143\" src=\"https:\/\/lh4.googleusercontent.com\/acd3L5x1XE8eD4R-xKnbvin5WiUM092vvVeizF62_PDb8kikwbfCUe--cnLEenXCDRO5oOIDfMoDYuq2Jd5rXrcY1h_coRDUwuMhasFylzuF_vOgb0dTZsndhfWKcB2dpr6aTJHh\"><br><strong>Note: <\/strong>The state <strong>Exit 0<\/strong> indicates the setup is completed without any error.<br><br>Now you check your work directory, there will be new directories created as \u201c<strong>certbot\u201d and \u201cpublic\u201d. <\/strong>You can see the SSL certificate synced in the certbot directory.<br><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"248\" src=\"https:\/\/lh6.googleusercontent.com\/np1wV-1xXA51WAi_8Fs3PmEfxHnURBnnqxY_A1Q0QW2AjWc99w4Gsf6SmTf-GeJD2OJNUSZ0RF2Z5yRPFnxOnuTQg7m0-xBFuMLgRVr7WHEWF6ywy7ybMeGe3crM-Nll7c7zKFzw\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 4: Configure SSL with Nginx:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Now you have received the Let\u2019s Encrypt SSL certificate. You can configure HTTPS and set up redirection to HTTPS.<br><br>Edit the <strong>ramesh.pheonixsolutions.com.conf<\/strong> file and make the following changes.<br>==============================================<br><code>server {<br>     listen [::]:80;<br>     listen 80;<br>     server_name ramesh.pheonixsolution.com;<br>     location ~ \/.well-known\/acme-challenge {<br>                         allow all;<br>     root \/var\/www\/certbot;<br>     }<br><br>  # redirect http to https<br>  return 301 https:\/\/ramesh.pheonixsolutions.com$request_uri;<br>}<br><br>server {<br>     listen [::]:443 ssl http2;<br>     listen 443 ssl http2;<br>     server_name ramesh.pheonixsolutions.com;<br>      root \/var\/www\/html;<br><br>    # SSL code<br>    ssl_certificate \/etc\/nginx\/ssl\/live\/ramesh.pheonixsolutions.com\/fullchain.pem;<br>    ssl_certificate_key \/etc\/nginx\/ssl\/live\/ramesh.pheonixsolutions.com\/privkey.pem;<br>    location \/ {<br>          index index.html;<br>    }<br>}<\/code><br>======================================<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"547\" src=\"https:\/\/lh3.googleusercontent.com\/XUTBn8QXbQ2gnQdqH1qUWW5tJ1Vb8lTn4KTT2COjngfHFVxP1Nsx0yMwzKAREjka1IR0LXg-zWL82NnvVUj_b0KTdnBU9U8c9mTxQflTXVHJt1AptZV9dYS6O94qBXpHEeAH8IJT\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 5: Create index.html file:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create the index.html file inside the <strong>\u201cpublic\u201d<\/strong> directory which then syncs to the directory configured.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/rk4NMj-AoFl5iGVfoBW32RGSfnD3yxBg67FNXWS2hjABxVQVOs_jbLDp1C2Wo5e4cMMMKrs3VHQkz0KcTEjPr3ZjZ2uGSw54GEPUoO8r5sWSx4Ha7VeIJ7tMMyHT_lnPt_EmdCcY\" width=\"624\" height=\"229\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 6: Restart the containers:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Restart the containers to load the new configurations by using below command.<br># <strong>docker-compose restart<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"71\" src=\"https:\/\/lh3.googleusercontent.com\/jOaMMGJJaMYspgQMk255WX-RbA1Aw6NJXApEvKJSx94jsqeBo_Gg_jzr5EDCyKNVDHYE5cIASR_gaKFWQ9GUhQgkRo2s5mKu3ktLYJDihYGKv-6bVRJGpyhvXv6VQwMk60_EqB9_\"><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can check the domain name in the browser.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"220\" src=\"https:\/\/lh4.googleusercontent.com\/mQPJ5XzLM2W46SB56Mrvq2yEnDmtNkfC2PXVCuy87K7lzzWHiOXIYJl7t04cwFEMm-B7M0227vvIXFIowElPLxYyqa9Vc7fpsGBLdTTjnYTMG8uQ5aVssz0Hb8kMCyFrtHjzR-SA\"><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Install Nginx and Let\u2019s Encrypt SSL with HTML + Docker \u2013 Ubuntu 20.04 Date: 26-06-2021 Prerequisites: 1. Install [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1024],"tags":[304,520,876,495,425,271,875],"class_list":["post-6353","post","type-post","status-publish","format-standard","hentry","category-security","tag-docker","tag-docker-container","tag-docker-with-ssl","tag-html","tag-lets-encrpyt-ssl","tag-nginx","tag-sample-app-with-docker-over-ssl","psol-cat-security"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1Et","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=6353"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6353\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=6353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=6353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=6353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}