Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Web Servers»Nginx»How To SetUp Nginx Virtual Hosts on Ubuntu 18.04 & 16.04 LTS

    How To SetUp Nginx Virtual Hosts on Ubuntu 18.04 & 16.04 LTS

    By RahulJune 3, 20192 Mins Read

    Virtual Host (Known as Server Blocks in Nginx) is used for hosting multiple websites on the single server. In this tutorial, you will get details about creating Virtual Host (Server Block) in Nginx web server on Ubuntu servers.

    Advertisement

    SetUp Nginx Virtual Hosts

  • Read: Set Up Virtual Hosts in Apache2 on Ubuntu
  • Nginx Installation

    We are assuming that you already have Nginx installed on your system but in case you don’t have installed it already, Use the following command to install it.

    sudo apt update
    sudo apt install nginx
    

    Create a Sample Project

    Now create a sample project to be configured with Virtual Host. Just create a directory to use as document root and put an index.html with demo content.

    sudo mkdir -p /var/www/xyz.com/httpdocs
    sudo echo "Hello World..." > /var/www/xyz.com/httpdocs/index.html
    

    Now set up proper file permissions, so that Nginx web server can access it. We are assuming that you are running Nginx with its default user www-data.

    sudo chown -R www-data:www-data /var/www/xyz.com
    sudo chmod -R 755 /var/www/xyz.com
    

    Create Virtual Hosts File

    Now set up virtual host configuration file for you domain xyz.com. You can simply make a copy of default configuration file and make required changes.

    sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/xyz.com.conf
    

    Edit new virtual host configuration in your favorite text editor and add the below configuration for HTTP and HTTPS settings. If you are not using HTTPS with your site just comment HTTPS Server Block section.

    vim /etc/nginx/sites-available/xyz.com.conf
    
    # HTTP Server Block
    #------------------------------------
    
    server {
        listen   80;
    
        root /var/www/xyz.com/httpdocs;
        index index.html index.htm;
    
        server_name example.com www.example.com;
    }
    
    
    # HTTPS Server Block
    #------------------------------------
    
    server {
    
        listen   443;
        server_name xyz.com www.xyz.com;
    
        root /var/www/xyz.com/httpdocs;
        index index.html index.htm;
    
        ssl on;
        ssl_certificate /etc/nginx/ssl/xyz.com/xyz.pem;
        ssl_certificate_key /etc/nginx/ssl/xyz.com/xyz.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
        ssl_prefer_server_ciphers on;
    }
    

    Enable Virtual Hosts

    Default Nginx reads configuration files located under /etc/nginx/sites-enabled directory. So simply create a soft link of original virtual host file to this directory.

    sudo ln -s /etc/nginx/sites-available/xyz.com.conf /etc/nginx/sites-enabled/xyz.com.conf
    

    After making all the configuration in your website Virtual host, Let’s restart Nginx service using the following command.

    sudo service nginx restart
    

    You have all done!

    nginx virtual host web-server website
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How To Set Up Nginx Reverse Proxy: A Step-By-Step Tutorial

    Configuring the Nginx Reverse Proxy in Front of Apache

    Configure CORS in NGINX

    How to Enable CORS in Nginx

    View 6 Comments

    6 Comments

    1. surya on January 12, 2021 7:58 pm

      Hi Rahul,
      How to configure https with nginx with a public ip with port 3000 without domain name.
      can you help me

      Reply
    2. surya on January 12, 2021 7:56 pm

      Hi Rahul,
      i want to attach valid SSL certificate to public IP with port no 3000 in nginx (ubuntu ) . so tester can go through the IP only not with domain. i tried but its showing invalid certificate due to IP. can you plz help me

      Reply
      • Rahul on January 13, 2021 5:17 am

        Hi Surya,

        You can’t generate or install a valid SSL for any IP address. You must have a domain name in order to configure valid ssl.

        Reply
    3. gopi on December 4, 2020 5:21 am

      please details configuration of create virtual host along with create two database in mongodb help me

      Reply
    4. Govind on June 17, 2019 7:15 am

      MY Question is
      stop IP address to access/show my ubuntu server’s landing page.(only access from Domain) using on nginx server
      link(Full Explanation):-
      https://askubuntu.com/questions/1151042/stop-ip-address-to-access-show-my-ubuntu-servers-landing-page-only-access-from

      Reply
    5. RunCloud on January 9, 2017 8:45 am

      Hi there, we’ve created a service called RunCloud to eliminate the tedious process of configuring NGINX and other server configs. Plus, we provide control panel to manage the server. We got 15 days free trial for you to play around. Do visit : http://runcloud.io to try it yourself today.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Install PHP 8.2-7.4 on RHEL & CentOS Stream 9
    • How to Install MySQL 8.0 on RHEL & CentOS Stream 9
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.