How to Add a New Website to an Existing Nginx Setup?

2 minutes read

Adding a new website to an existing Nginx setup can seem daunting, but with careful planning and execution, it becomes a manageable task. This guide will walk you through the necessary steps to expand your server’s capabilities by hosting multiple sites.

Table of Contents

Prerequisites

Before you begin, ensure that you have:

  • A running instance of Nginx. If you need guidance on Nginx installation, you might find the link helpful.
  • Access to server terminal and permissions to view and edit configuration files.
  • A basic understanding of server and web technologies.

Step 1: Set Up the Directory Structure

The first step is organizing the files and directories where your website’s files will be stored. It’s crucial to maintain a standardized directory structure for easy management.

1
2
3
sudo mkdir -p /var/www/yournewsite.com/html
sudo chown -R $USER:$USER /var/www/yournewsite.com/html
sudo chmod -R 755 /var/www/yournewsite.com

Replace yournewsite.com with your actual domain name. Ensure the permissions allow Nginx to serve the files while maintaining system security.

Step 2: Configure Nginx

Next, you need to create a configuration file. This tells Nginx how to treat requests for your new site.

  1. Create a new configuration file in /etc/nginx/sites-available/:

    1
    
    sudo nano /etc/nginx/sites-available/yournewsite.com
    
  2. Add the following basic server configuration:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    server {
        listen 80;
        listen [::]:80;
    
    
        server_name yournewsite.com www.yournewsite.com;
    
    
        root /var/www/yournewsite.com/html;
        index index.html index.htm;
    
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
    

Make sure to replace yournewsite.com with your actual domain name. This configuration is a minimal setup sufficient for a static site, and it may be expanded as required for dynamic content or other technologies.

  1. Create a symbolic link between sites-available/ and sites-enabled/ to enable the configuration:

    1
    
    sudo ln -s /etc/nginx/sites-available/yournewsite.com /etc/nginx/sites-enabled/
    
  2. Use Nginx’s testing utility to ensure that your configuration is correct:

    1
    
    sudo nginx -t
    

Step 3: Test and Reload Nginx

Once the configuration has been added, tested, and found correct, reload Nginx to apply the changes:

1
sudo systemctl reload nginx

You can now navigate to http://yournewsite.com in your browser to verify that your site is live. If it doesn’t work as expected, inspect the Nginx error logs found at /var/log/nginx/error.log for issues.

Conclusion

Congratulations, you have successfully added a new website to your existing Nginx setup! This simple guide introduces the basics, but Nginx offers rich functionality to explore further as your needs grow. For more comprehensive tasks such as deploying in cloud environments, consider exploring Nginx deployment.

Further Reading

  • For advanced configurations like redirects from HTTP to HTTPS, check out this guide on Nginx redirects.

By following best practices and organizing your server layout effectively, you’ll maintain a scalable and managed hosting environment with Nginx.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

Do you need to setup wholesale pricing in WooCommerce for bulk purchases and wholesale consumers? By default, WooCommerce solely lets you set regular pricing for a product. On this article, we are going to present you the way to simply setup WooCommerce whole...
Do you wish to correctly set up and setup WP Rocket plugin in WordPress? WP Rocket is the perfect WordPress caching plugin out there as a result of it gives essentially the most complete set of instruments to assist pace up your web site. Nevertheless, many fr...
To install WordPress on an existing database, you will need to follow these steps:Download the latest version of WordPress from the official website (https://wordpress.org).Extract the downloaded ZIP file to a specific location on your computer.Connect to your...
To set up WooCommerce on WordPress, you need to follow a few steps:Install WooCommerce: Login to your WordPress dashboard and go to the "Plugins" section. Click on "Add New" and search for "WooCommerce." Install the plugin and activate ...
To configure a proxy in popular web servers like Apache and Nginx, you need to modify their configuration files. Here is a general explanation of the steps involved without listing them:Apache: Locate and open the Apache configuration file (typically named htt...
Monitoring website traffic and performance on web hosting is essential for any website owner to ensure that their website is running smoothly and efficiently. There are several ways to effectively monitor website traffic and performance. Here are some key meth...