
{"id":15,"date":"2026-01-23T05:37:47","date_gmt":"2026-01-23T05:37:47","guid":{"rendered":"http:\/\/wordpress.web.dsslit.com\/?p=15"},"modified":"2026-01-23T05:37:47","modified_gmt":"2026-01-23T05:37:47","slug":"creating-a-virtual-host-in-nginx","status":"publish","type":"post","link":"https:\/\/dsslit.ddns.net\/wordpress\/?p=15","title":{"rendered":"Virtual Host in NGINX"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hosting multiple websites efficiently on a single server is a common requirement, and NGINX, a powerful and high-performance web server, offers a solution through its virtual host feature. Also known as server blocks, virtual hosts allow you to host multiple domains on a single server instance. In this guide, we will walk you through the process of setting up a virtual host using NGINX on a Linux system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding, ensure that you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A server running Linux.<\/li>\n\n\n\n<li>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sudo or root privileges on the server.<\/li>\n\n\n\n<li>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A domain name pointed to your server&#8217;s public IP address.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Install NGINX<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If NGINX is not already installed on your server, you can install it using the package manager of your Linux distribution. For Ubuntu or Debian-based systems, use the following commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt update<br>sudo apt install nginx<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>For CentOS or RHEL-based systems, use:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install epel-release<br>sudo yum install nginx<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Start and Enable NGINX<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Ensure NGINX is running and set to start on boot using the systemctl commands:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl start nginx<br>sudo systemctl enable nginx<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Create a Directory for Your Website<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a directory structure for your website files. This is where you will store your website&#8217;s content. Use your actual domain name in place of &#8216;your_domain&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo mkdir -p \/var\/www\/your_domain\/html<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Assign ownership and set the correct permissions:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo chown -R $USER:$USER \/var\/www\/your_domain\/html<br>sudo chmod -R 755 \/var\/www<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Create a Sample HTML Page (Optional)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To test your setup, create a sample&nbsp;<code>index.html<\/code>&nbsp;file in the directory you just created:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo \"&lt;html><br>\u00a0 &lt;head><br>\u00a0\u00a0\u00a0 &lt;title>Welcome to Your_domain!&lt;\/title><br>\u00a0 &lt;\/head><br>\u00a0 &lt;body><br>\u00a0\u00a0\u00a0 &lt;h1>Success! The Your_domain virtual host is working!&lt;\/h1><br>\u00a0 &lt;\/body><br>&lt;\/html><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Create a New Configuration File for Your Website<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new configuration file in the&nbsp;<code>\/etc\/nginx\/sites-available\/<\/code>&nbsp;directory. This file will contain the server block settings for your domain:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nano \/etc\/nginx\/sites-available\/your_domain<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Insert the following configuration, ensuring you replace &#8216;your_domain&#8217; with your actual domain name and adjust the document root if necessary:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">server {<br>\u00a0\u00a0\u00a0 listen 80;<br>\u00a0\u00a0\u00a0 server_name your_domain www.your_domain;<br>\u00a0\u00a0\u00a0 root \/var\/www\/your_domain\/html;<br><br>\u00a0\u00a0\u00a0 index index.html;<br>\u00a0\u00a0\u00a0 location \/ {<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try_files $uri $uri\/ =404;<br>\u00a0\u00a0\u00a0 }<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Enable the File by Creating a Link<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Activate the server block by linking the file you just created to the&nbsp;<code>sites-enabled<\/code>&nbsp;directory:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ln -s \/etc\/nginx\/sites-available\/your_domain \/etc\/nginx\/sites-enabled\/<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 7: Test NGINX Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s crucial to test your NGINX configuration for any syntax errors before restarting the service:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nginx -t<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 8: Restart NGINX<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the configuration test is successful, restart NGINX to apply the changes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart nginx<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 9: Adjust the Firewall (If Necessary)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you&#8217;re using a firewall, ensure that HTTP and HTTPS traffic are permitted. For UFW users:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ufw allow 'Nginx HTTP'<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For firewalld users:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo firewall-cmd --permanent --zone=public --add-service=http<br>sudo firewall-cmd --permanent --zone=public --add-service=https<br>sudo firewall-cmd --reload<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 10: Access Your Website<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open a web browser and navigate to your domain:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/your_domain<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see the sample HTML page or the content you&#8217;ve uploaded to your website&#8217;s directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations! You&#8217;ve successfully set up a virtual host in NGINX. This configuration allows you to host multiple websites on a single server, optimizing resources and management. Remember to replace&nbsp;<code>your_domain<\/code>&nbsp;with your actual domain name and ensure that your domain&#8217;s DNS settings are pointing to your server&#8217;s IP address. By following these steps, you can expand your server&#8217;s capabilities and host various websites efficiently and effectively.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hosting multiple websites efficiently on a single server is a common requirement, and NGINX, a powerful and high-performance web server, offers a solution through its virtual host feature. Also known as server blocks, virtual hosts allow you to host multiple domains on a single server instance. In this guide, we will walk you through the &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15"}],"version-history":[{"count":0,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions"}],"wp:attachment":[{"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dsslit.ddns.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}