Now Reading: Install Nginx on an Ubuntu VPS server and set up multiple websites with separate configuration files for each website

Loading

Install Nginx on an Ubuntu VPS server and set up multiple websites with separate configuration files for each website

svgFebruary 7, 2023NgnixServerUbuntuCodeStackGuide

Here are the steps to install Nginx on an Ubuntu VPS server and set up multiple websites with separate configuration files for each website:

  1. Update the package index:
    Before installing Nginx, update the package index on your server:
sudo apt update
  1. Install Nginx:
    Use the following command to install Nginx:
sudo apt install nginx
  1. Create a new file for each website: In the
    /etc/nginx/sites-available/ directory,
    create a new file for each website, using the domain name as the file name. For example, if your domain is example.com, create a file named example.com.
  2. Configure each website: In each file, add the Nginx configuration for that website.
    The configuration should specify the server name and document root, as well as any other required options, such as the port number, location of SSL certificates, etc.
  3. Create symbolic links:
    To enable each website, create a symbolic link from the website configuration file in
    /etc/nginx/sites-available/ to /etc/nginx/sites-enabled/
    You can use the following command to create the symbolic link:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
  1. Point domains to your server:
    To associate your domains with your Nginx server, you need to update their DNS records to point to the IP address of your server.
  2. Restart Nginx:
    After making changes to the Nginx configuration files, you need to restart Nginx for the changes to take effect. You can use the following command to restart Nginx:
sudo systemctl restart nginx

If you are not familiar with the nginx configuration file, here’s an example

Here is an example Nginx configuration file for a single website:

server {
    listen 80;
    listen [::]:80;
    server_name example.com;

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

    location / {
        try_files $uri $uri/ /index.html;
    }

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

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

    location /api/ {
        proxy_pass http://127.0.0.1:8000;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        internal;
    }
}

In this example, the server is configured to listen on both IPv4 and IPv6 addresses, at port 80. The domain name is set to “example.com”. The root directory for the website is set to “/var/www/example.com”, and the default index file is “index.html”.

The location blocks specify how Nginx should handle different types of requests, such as serving static files from the “/static/” and “/images/” directories, and forwarding requests to an API server running on “127.0.0.1:8000”.

Finally, the error_page directive is used to specify the location of a custom error page for HTTP status codes 500, 502, 503, and 504, which are commonly used for server errors.

Note that this is just an example, and you will need to modify it to match your specific needs.

1 People voted this article. 1 Upvotes - 0 Downvotes.
svg

What do you think?

Show comments / Leave a comment

Leave a reply

svg
Quick Navigation
  • 01

    Install Nginx on an Ubuntu VPS server and set up multiple websites with separate configuration files for each website