Skip to content

Nginx

sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
/var/www/html
/etc/nginx/nginx.conf
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/

Setting up Nginx on Ubuntu is a rite of passage for many developers. It’s lightweight, fast, and remarkably stable once you get the syntax right.

Here is the step-by-step process to get a basic website live.

1. Install Nginx

First, update your local package index and install the server software.

sudo apt update
sudo apt install nginx

Once installed, Nginx starts automatically. You can verify it's running by visiting your server's IP address in a web browser. You should see the "Welcome to nginx!" page.


2. Set Up the Directory Structure

While Nginx has a default site folder at /var/www/html, it is best practice to create a specific folder for your domain. Replace example.com with your actual domain name:

sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com

Next, create a simple index.html file so you have something to look at:

nano /var/www/example.com/html/index.html

Type "Hello World!" in the file, then save and exit (Ctrl+O, Enter, Ctrl+X).


3. Create a Server Block (Virtual Host)

This is the most critical step. You need to tell Nginx to "look" for your domain and point it to the right folder.

Create a new configuration file:

sudo nano /etc/nginx/sites-available/example.com

Paste in this standard configuration:

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

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

    server_name example.com www.example.com;

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

4. Enable the Configuration

Nginx uses a "symbolic link" system. You create the file in sites-available and "link" it to sites-enabled to turn it on.

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Crucial Step: Always test your syntax before restarting, or your server might crash.

sudo nginx -t

If it says "syntax is ok," restart Nginx:

sudo systemctl restart nginx

5. Adjust the Firewall (UFW)

Ubuntu comes with a firewall that might block web traffic by default. You need to allow "Nginx Full" (which allows both HTTP and HTTPS).

sudo ufw allow 'Nginx Full'

Summary Checklist

Task Command
Check Status systemctl status nginx
Config Location /etc/nginx/sites-available/
Web Files Location /var/www/your_domain/html/
Error Logs /var/log/nginx/error.log

Pro Tip: If you plan on using WordPress (as you mentioned previously), you'll also need to install PHP-FPM and update your server block to handle .php files, as Nginx doesn't process PHP natively.

Would you like the specific code to add PHP support to this Nginx configuration?

How to install nginx

sudo apt update
sudo apt install nginx


sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com

$USER:$USER  ubuntu:ubuntu


sudo vi /etc/nginx/sites-available/example.com
example.com
server {
    listen 80;
    listen [::]:80;

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

    server_name example.com www.example.com;

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

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Using Certbot with Let's Encrypt

sudo apt update
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

ssh-keygen -t ed25519 -C "github-actions"