Harish Kumar

How to Set Up Nginx in a New Linux Server Instance

Published on 29 Apr 2023 · Nginx

How to Set Up Nginx in a New Linux Server Instance

Nginx is a powerful web server that is used by millions of websites around the world. It is known for its high performance, scalability, and stability, and is often used in conjunction with other web technologies like PHP, MySQL, and Node.js. In this tutorial, we will show you how to set up Nginx in a new Linux server instance.

Step 1: Create a New Linux Server Instance

The first step is to create a new Linux server instance. You can do this by signing up for a cloud hosting service like Amazon Web Services (AWS), DigitalOcean, or Linode. Once you have created a new server instance, you can connect to it using SSH.

Step 2: Install Nginx

The next step is to install Nginx on your server. You can do this by running the following command in your server's terminal:

                    
                    sudo apt-get update
                    sudo apt-get install nginx
                    
                    

This will install Nginx and all its dependencies on your server.

Step 3: Configure Nginx

Once Nginx is installed, you need to configure it to serve your website or web application. The configuration file for Nginx is located at /etc/nginx/nginx.conf. You can open this file using a text editor like nano:

                    
                    sudo nano /etc/nginx/nginx.conf
                    
                    

In this file, you can configure various aspects of Nginx like the server blocks, upstream servers, and HTTP settings. For example, to configure Nginx to serve a website located at /var/www/html, you can add the following server block to your configuration file:

                    
                    server {
                        listen 80;
                        server_name example.com;
                        root /var/www/html;
                        index index.html;
                    }
                    
                    

This tells Nginx to listen on port 80 (the default HTTP port), serve requests for the domain example.com, and serve files from the /var/www/html directory.

Step 4: Test Nginx

After you have configured Nginx, you should test it to make sure it is working correctly. You can do this by running the following command in your server's terminal:

sudo nginx -t
                    

This will test your Nginx configuration and report any errors or warnings. If there are no errors, you can start Nginx by running the following command:

                    
                        sudo systemctl start nginx
                    
                    

You can now access your website by navigating to http://your-server-ip-address in your web browser.

Step 5: Set Up SSL/TLS

If you want to secure your website with SSL/TLS, you can use a free service like Let's Encrypt to obtain an SSL/TLS certificate. Once you have obtained a certificate, you can configure Nginx to use it by adding the following server block to your configuration file:

                    
                    server {
                        listen 443 ssl;
                        server_name example.com;
                        root /var/www/html;
                        index index.html;
                        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
                        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
                    }
                    
                    

This tells Nginx to listen on port 443 (the default HTTPS port), serve requests for the domain example.com, and use the SSL/TLS certificate located at /etc/letsencrypt/live/example.com/fullchain.pem and the private key located at /etc/letsencrypt/live/example.com/privkey.pem.

Step 6: Restart Nginx

After you have made changes to your Nginx configuration file, you need to restart nginx

                    
                    sudo systemctl restart nginx