How To Set Up nginx Virtual Hosts (Server Blocks) on Ubuntu 12.04 LTS

How To Set Up nginx Virtual Hosts on Ubuntu 12.04 LTS

How To Set Up nginx Virtual Hosts (Server Blocks) on Ubuntu 12.04 LTS

Introduction

In this tutorial, we will learn how to set up nginx virtual hosts on Ubuntu 12.04 LTS. Virtual hosts, also known as server blocks, allow you to run multiple websites on a single server with different domain names and configurations.

Prerequisites

To follow this tutorial, you will need:

  • An Ubuntu 12.04 LTS server with nginx installed.
  • A registered domain name or a local hosts file entry for each virtual host you want to set up.

Step 1 - Creating the Virtual Host Directory Structure

The first step is to create the directory structure for each virtual host. We will create a separate directory for each virtual host in the /var/www directory.

Run the following command to create the directory for your first virtual host:

sudo mkdir -p /var/www/example.com/html

Replace "example.com" with your domain name.

Repeat this command for each virtual host you want to set up.

Step 2 - Creating the Virtual Host Configuration Files

The next step is to create the virtual host configuration files in the /etc/nginx/sites-available directory. These files will contain the configuration for each virtual host.

Run the following command to create a new configuration file for your first virtual host:

sudo nano /etc/nginx/sites-available/example.com

Replace "example.com" with your domain name.

Copy and paste the following configuration into the file:

server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/html;
    index index.html index.htm;

    server_name example.com www.example.com;

    location / {
        try_files $uri $uri/ =404;
    }
}
		

Save and close the file.

Repeat this step for each virtual host you want to set up.

Step 3 - Enabling the Virtual Hosts

After creating the virtual host configuration files, we need to enable them.

Run the following command to create a symbolic link from the sites-available directory to the sites-enabled directory:

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

Replace "example.com" with your domain name.

Repeat this command for each virtual host you want to enable.

Step 4 - Testing the Configuration

After enabling the virtual hosts, we need to test the configuration to make sure there are no syntax errors.

Run the following command:

sudo nginx -t

If there are no

Комментарии

Популярные сообщения из этого блога

How To Modify CSS Classes in JavaScript

How To Backup MySQL Databases on an Ubuntu VPS

How To Backup PostgreSQL Databases on an Ubuntu VPS