DEV Community

Felix Mukah
Felix Mukah

Posted on

Setting up and configuring NGINX on a fresh Ubuntu server

1. Introduction
This documentation outlines the steps taken to set up and configure NGINX on a fresh Ubuntu server. The goal was to create a functional web server capable of serving a simple HTML page.

2. Prerequisites

  • An AWS EC2 instance running Ubuntu 22.04 LTS.
  • SSH access to the server using a .pem key pair.
  • Basic familiarity with the Linux command line.

3. Step-by-Step Process
Step 1: Provision the Ubuntu Server

  1. Create an EC2 Instance:
  • Log in to the AWS Management Console.

  • Launch an EC2 instance with the following specifications:

    • AMI: Ubuntu Server 22.04 LTS
    • Instance Type: t2.micro (free tier eligible)
    • Storage: 8 GB (default)
  • Configure the security group to allow SSH (port 22) and HTTP (port 80) traffic.

  • Download the .pem key pair for SSH access.

2. Connect to the Server:

  • Use SSH to connect to the server:
ssh -i /path/to/your-key.pem ubuntu@your_server_public_ip
Enter fullscreen mode Exit fullscreen mode

Step 2: Install NGINX

1. Update the package list:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install NGINX:

sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

3. Verify the installation:

nginx -v
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure NGINX

1. Start and enable NGINX:

sudo systemctl start nginx
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

2. Check the status of NGINX:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

3. Open the default NGINX configuration file:

sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

4. Modify the configuration to serve a simple HTML page:

  • Update the root directive to /var/www/html.

  • Ensure the index directive includes index.html.
    Example configuration:

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

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

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Save and exit the editor (Ctrl+O, Enter, Ctrl+X)

Step 4: Create a Simple HTML Page

  1. Create the HTML directory:

    sudo mkdir -p /var/www/html
    
  2. Create an index.html file:

    sudo nano /var/www/html/index.html
    
  3. Add the following HTML content:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to My Server!</title>
    </head>
    <body>
    <p>Welcome to DevOps Stage 0 - Felix Mukah/Felix Mukah</p>
    </body>
    </html>
    
  4. Save and exit the editor.

Step 5: Test the Configuration

  1. Test the NGINX configuration for syntax errors:

    sudo nginx -t
    
  2. Reload NGINX to apply the changes:

    sudo systemctl reload nginx
    
  3. Open a web browser and navigate to your server’s public IP address:

http://your_server_public_ip
Enter fullscreen mode Exit fullscreen mode

You should see the HTML page you created.

4. Verification
NGINX Status: Confirmed NGINX is running using sudo systemctl status nginx.

Web Page: Verified the HTML page is accessible via the server’s public IP.

5. Challenges and Solutions
Challenge: Initially, the .pem file was not recognized by PuTTYgen on Windows.

Solution: Converted the .pem file to .ppk using PuTTYgen and successfully connected to the server.

6. Conclusion
This project successfully demonstrated the setup and configuration of NGINX on a fresh Ubuntu server. The server is now capable of serving a simple HTML page over HTTP. This foundational knowledge can be expanded to host dynamic websites, implement SSL/TLS, and explore advanced NGINX features.

7. References

Top comments (0)