Setting Up and Installing Nginx on Ubuntu Server

Setting Up and Installing Nginx on Ubuntu Server

  1. Prerequisites Before starting the installation process, ensure that you have the following:
  • Ubuntu Server installed on your machine.
  • sudo privileges or access to the root user.
  • Basic understanding of the terminal/command line.

Installing Nginx

Update Package Index:

sudo apt update

Install Nginx

sudo apt install nginx

Start Nginx

sudo systemctl start nginx

Enable Nginx to start on boot:

sudo systemctl enable nginx

Adjusting the Firewall

Check the available application profiles:

sudo ufw app list

Allow Nginx HTTP and HTTPS connections:

sudo ufw allow 'Nginx Full'

Verify the changes:

sudo ufw status

Checking Your Web Server

Open your web browser and enter your server's IP address. You should see the default Nginx landing page if everything is configured correctly.

Managing the Nginx Process

To stop Nginx:

sudo systemctl stop nginx

To restart Nginx:

sudo systemctl restart nginx

To reload Nginx to apply configuration changes:

sudo systemctl reload nginx

Deployment of a Simple React Web Server

Create a my-app.conf file:

sudo nano /etc/nginx/sites-available/my-app.conf

Add the following configuration:

server {
    listen 80;
    server_name your_domain.com;

    location / {
        root /path/to/your/react/app;
        index index.html;
        try_files $uri $uri/ /index.html;
    }
}

Create a symbolic link to enable the site:

sudo ln -s /etc/nginx/sites-available/my-app.conf /etc/nginx/sites-enabled/

Test the Nginx configuration:

sudo nginx -t

If the test is successful, reload Nginx:

sudo systemctl reload nginx

Avoiding Possible Hash Bucket Memory Problem

By default, Nginx allocates memory based on the size of hash buckets. To avoid memory issues:

Edit the nginx.conf file:

sudo nano /etc/nginx/nginx.conf

Add or modify the following line in the http block:


http {
    ...
    server_names_hash_bucket_size 64;
    ...
}

Getting Familiar with Important Nginx Files and Directories

  • /etc/nginx/sites-available/: Contains configuration files for available sites.
  • /etc/nginx/sites-enabled/: Symbolic links to the files in sites-available that are enabled.
  • /etc/nginx/nginx.conf: Main Nginx configuration file. This guide should help you set up and configure Nginx on your Ubuntu server, along with deploying a simple React web server.

PARTNER WITH US TO CREATE A COMPELLING NARRATIVE
FOR YOUR BRAND!

Let's bring your ideas to life, start collaborating with our creative agency and turn your vision into reality.