{"id":9939,"date":"2021-09-20T10:13:39","date_gmt":"2021-09-20T10:13:39","guid":{"rendered":"https:\/\/linuxways.net\/?p=9939"},"modified":"2021-09-20T10:13:39","modified_gmt":"2021-09-20T10:13:39","slug":"how-to-configure-nginx-server-block-and-secure-nginx-with-lets-encrypt-ssl-on-rocky-linux-8-centos-8","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-configure-nginx-server-block-and-secure-nginx-with-lets-encrypt-ssl-on-rocky-linux-8-centos-8\/","title":{"rendered":"How to Configure Nginx Server Block and Secure Nginx with Let&#8217;s Encrypt SSL on Rocky Linux 8 \/ CentOS 8"},"content":{"rendered":"<p>An Nginx server block is the equivalent of an Apache virtual host. It gives users the flexibility to host numerous websites on the same server. This is a cost effective approach of hosting websites instead of setting up different servers and configuring them for different domains.<\/p>\n<p>In this walkthrough, we will demonstrate the configuration of an Nginx server block. Afterwards, we will show you how you can secure the webserver with Let\u2019s Encrypt SSL which is a free SSL .<\/p>\n<h2><strong>Prerequisites<\/strong><\/h2>\n<p>Before you move along, ensure that you have Nginx installed. We have a comprehensive tutorial that walls through the installation of <a href=\"https:\/\/linuxways.net\/de\/centos\/how-to-install-nginx-on-centos\/\">l Nginx on CentOS 8<\/a>. Also, confirm that your domain name points to your virtual server\u2019s public IP address. To ensure this, head over to your domain name vendor and configure the A record to point to the server\u2019s IP.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1177\" height=\"314\" class=\"wp-image-9940\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-145.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-145.png 1177w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-145-300x80.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-145-1024x273.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-145-768x205.png 768w\" sizes=\"auto, (max-width: 1177px) 100vw, 1177px\" \/><\/p>\n<p>Here, the IP address points to the domain called <strong>linuxtechgeek.info <\/strong>which we are going to use throughout this guide.<\/p>\n<h2><strong>Step 1: Create Document root for the domain<\/strong><\/h2>\n<p>We will start off by creating a directory for our domain that will store the website\u2019s files. So, run the command below to accomplish this.<\/p>\n<pre><strong>$ sudo mkdir -p \/var\/www\/linuxtechgeek.info\/html<\/strong><\/pre>\n<p>For demo purposes, we will create a sample <strong>index.html<\/strong> file.<\/p>\n<pre><strong>$ sudo vim \/var\/www\/linuxtechgeek.info\/html\/index.html<\/strong><\/pre>\n<p>Paste the following HTML lines.<\/p>\n<pre><strong>&lt;html&gt;<\/strong>\r\n\r\n<strong> &lt;head&gt;<\/strong>\r\n\r\n<strong> &lt;title&gt;Welcome to my domain.info&lt;\/title&gt;<\/strong>\r\n\r\n<strong> &lt;\/head&gt;<\/strong>\r\n\r\n<strong> &lt;body&gt;<\/strong>\r\n\r\n<strong> &lt;h1&gt;Hey fellaz!!.The server block is working.&lt;\/h1&gt;<\/strong>\r\n\r\n<strong> &lt;\/body&gt;<\/strong>\r\n\r\n<strong>&lt;\/html&gt;<\/strong><\/pre>\n<p>Of course, feel free to edit the content in the body to your preference. Next, configure the ownership of the domain\u2019s directory to avoid any permission glitches.<\/p>\n<pre><strong>$ sudo chown -R nginx \/var\/www\/linuxtechgeek.info\/<\/strong><\/pre>\n<p>And set the permissions as shown.<\/p>\n<pre><strong>$ sudo chmod -R 755 \/var\/www\/linuxtechgeek.info\/<\/strong><\/pre>\n<p>Great! Let\u2019s now configure the Server block file.<\/p>\n<h2><strong>Step 2: Create Nginx server block file<\/strong><\/h2>\n<p>We will configure the Nginx server block file in the <strong>\/etc\/nginx\/conf.d <\/strong>directory as follows.<\/p>\n<pre><strong>$ sudo vim \/etc\/nginx\/conf.d\/linuxtechgeek.info.conf<\/strong><\/pre>\n<p>Paste the following configuration<\/p>\n<pre><strong>server {<\/strong>\r\n\r\n<strong> listen 80;<\/strong>\r\n\r\n<strong> server_name linuxtechgeek.info www.linuxtechgeek.info;<\/strong>\r\n\r\n<strong> root \/var\/www\/linuxtechgeek.info\/html;<\/strong>\r\n\r\n<strong> index index.php index.html index.htm;<\/strong>\r\n\r\n<strong> access_log\/var\/log\/nginx\/linuxtechgeek.info.access.log;<\/strong>\r\n\r\n<strong> error_log \/var\/log\/nginx\/linuxtechgeek.info.error.log;<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<p>Save and exit. Once done, verify if all the configurations are sound.<\/p>\n<pre><strong>$ sudo nginx -t<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"718\" height=\"104\" class=\"wp-image-9941\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-146.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-146.png 718w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-146-300x43.png 300w\" sizes=\"auto, (max-width: 718px) 100vw, 718px\" \/><\/strong><\/p>\n<p>From the output, everything looks just fine. To apply all the changes made, restart Nginx webserver.<\/p>\n<pre><strong>$ sudo systemctl restart nginx<\/strong><\/pre>\n<p>And ensure that it is running.<\/p>\n<pre><strong>$ sudo systemctl status nginx<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"214\" class=\"wp-image-9942\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-147.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-147.png 838w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-147-300x77.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-147-768x196.png 768w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><\/strong><\/p>\n<p>Now browse your website and you will be directed to your server block index.html file.<\/p>\n<pre><a href=\"http:\/\/domain-name.come\"><strong>http:\/\/domain-name.come<\/strong><\/a><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"798\" height=\"122\" class=\"wp-image-9943\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-148.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-148.png 798w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-148-300x46.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-148-768x117.png 768w\" sizes=\"auto, (max-width: 798px) 100vw, 798px\" \/><\/strong><\/p>\n<h2><strong>Step 3: Secure Nginx with Let\u2019s Encyrpt SSL<\/strong><\/h2>\n<p>Our server block is already set up, but the web server is not encrypted yet. Encrypting the site using an SSL certificate is crucial in order to secure information sent to and from the web server. An SSL certificate will also enhance your site\u2019s Google rankings and boost interactions with your customers.<\/p>\n<p><a href=\"https:\/\/letsencrypt.org\/\">Let\u2019s Encrypt<\/a> is a free global CA ( Certificate Authority) that lets users obtain and secure their sites using a free SSL \/TLS certificate. We are going to install Certbot which will automate the installation of the free SSL certificate from Let\u2019s Encrypt.<\/p>\n<pre><strong>$ sudo dnf install certbot python3-certbot-nginx<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"901\" height=\"211\" class=\"wp-image-9944\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-149.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-149.png 901w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-149-300x70.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-149-768x180.png 768w\" sizes=\"auto, (max-width: 901px) 100vw, 901px\" \/><\/p>\n<p>Once installed, run cerbot as follows to install the Let\u2019s Encrypt SSL certificate<\/p>\n<pre><strong>$ sudo certbox --nginx<\/strong><\/pre>\n<p>Follow the prompts as indicated.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"952\" height=\"635\" class=\"wp-image-9945\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-150.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-150.png 952w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-150-300x200.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-150-768x512.png 768w\" sizes=\"auto, (max-width: 952px) 100vw, 952px\" \/><\/p>\n<h2><strong>Step 3: Manage certificate renewal<\/strong><\/h2>\n<p>Let\u2019s Encrypt certificate is valid up to 90 days. However, a notification will be sent to you 20 days before expiry and more notifications from 10 days to the last day.<\/p>\n<p>You can renew the certificate manually using the command:<\/p>\n<pre><strong>$ sudo certbot renew <\/strong><\/pre>\n<p>To automate the renewal, create a new cron job.<\/p>\n<pre><strong>$ crontab -e<\/strong><\/pre>\n<p>Append this line and save the changes.<\/p>\n<pre><strong>0 0 * * * \/usr\/bin\/certbot renew &gt; \/dev\/null 2&gt;&amp;1<\/strong><\/pre>\n<p>And that\u2019s it! That\u2019s how you can configure an Nginx server block and secure your web server using Let\u2019s Encrypt SSL.<\/p>","protected":false},"excerpt":{"rendered":"<p>An Nginx server block is the equivalent of an Apache virtual host. It gives users the flexibility to host numerous websites on the same server. This is a&hellip;<\/p>","protected":false},"author":1,"featured_media":10291,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,165],"tags":[93,149,436,432],"class_list":["post-9939","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-red-hat","tag-centos-8","tag-nginx","tag-rocky-linux-8","tag-ssl-certificate"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=9939"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9939\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/10291"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=9939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=9939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=9939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}