# How to install Nginx

Need a fast web server? Nginx is perfect for your VPS. It's lightweight, handles lots of traffic, and is easy to configure. Let's get it running with multiple websites.

### Step 1: Update System

```bash
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y

# CentOS/RHEL
sudo yum update -y
```

### Step 2: Install Nginx

#### Ubuntu/Debian:

```bash
sudo apt install nginx -y
```

#### CentOS/RHEL 8+:

```bash
sudo dnf install nginx -y
```

#### CentOS/RHEL 7:

```bash
sudo yum install nginx -y
```

### Step 3: Start and Enable

```bash
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
```

Should show "active (running)".

### Step 4: Configure Firewall

Allow HTTP and HTTPS:

#### Ubuntu/Debian (UFW):

```bash
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status
```

#### CentOS/RHEL (firewalld):

```bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
```

### Step 5: Test Installation

Visit in browser:

```
http://your-server-ip
```

You should see the Nginx welcome page.

### Step 6: Nginx Directory Structure

Important directories:

* `/etc/nginx/` - Configuration files
* `/var/www/html/` - Default web root
* `/var/log/nginx/` - Log files
* `/usr/share/nginx/html/` - Default files (CentOS)

### Step 7: Create Your First Website

#### 1. Create website directory:

```bash
sudo mkdir -p /var/www/example.com/html
```

#### 2. Set permissions:

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

#### 3. Create test page:

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

Add:

```html
<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Example.com!</title>
</head>
<body>
    <h1>Success! Nginx is working.</h1>
    <p>This is your first website on ITITAN Hosting.</p>
</body>
</html>
```

### Step 8: Create Virtual Host (Server Block)

#### Ubuntu/Debian:

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

#### CentOS/RHEL:

```bash
sudo nano /etc/nginx/conf.d/example.com.conf
```

Add this configuration:

```nginx
server {
    listen 80;
    listen [::]:80;
    
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html index.htm;
    
    location / {
        try_files $uri $uri/ =404;
    }
    
    # Deny access to .htaccess files
    location ~ /\.ht {
        deny all;
    }
}
```

#### Enable site (Ubuntu/Debian only):

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

### Step 9: Test and Reload

```bash
# Test configuration
sudo nginx -t

# If syntax is OK, reload
sudo systemctl reload nginx
```

### Step 10: Test Your Website

Visit `http://your-server-ip` (or your domain if DNS is set).

### Step 11: Basic Performance Tuning

#### Edit main config:

```bash
sudo nano /etc/nginx/nginx.conf
```

Inside `http` block, add:

```nginx
# Basic performance settings
client_max_body_size 100M;
keepalive_timeout 65;
types_hash_max_size 2048;

# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript 
           application/javascript application/xml+rss application/json;
```

#### Restart:

```bash
sudo systemctl restart nginx
```

### Step 12: Set Up PHP with Nginx (Optional)

#### Install PHP-FPM:

```bash
# Ubuntu/Debian
sudo apt install php-fpm php-mysql -y

# CentOS/RHEL
sudo dnf install php-fpm php-mysqlnd -y
```

#### Configure Nginx for PHP:

Edit your site config, add:

```nginx
location ~ \.php$ {
    include snippets/fastcgi-php.conf;  # Ubuntu/Debian
    # include /etc/nginx/fastcgi_params;  # CentOS/RHEL
    
    fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;  # Adjust version
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
```

#### Test PHP:

```bash
echo "<?php phpinfo(); ?>" > /var/www/example.com/html/info.php
```

Visit `http://your-server-ip/info.php` (remove after testing!).

### Common Issues

#### "Nginx won't start"

```bash
# Check error logs
sudo tail -f /var/log/nginx/error.log

# Test config
sudo nginx -t
```

#### "403 Forbidden"

```bash
# Check permissions
sudo ls -la /var/www/example.com/

# Fix ownership
sudo chown -R www-data:www-data /var/www/example.com  # Ubuntu
sudo chown -R nginx:nginx /var/www/example.com        # CentOS
```

#### "502 Bad Gateway"

```bash
# Check PHP-FPM
sudo systemctl status php-fpm

# Check socket
ls -la /var/run/php/
```

### Useful Commands

#### Check Nginx version:

```bash
nginx -v
```

#### Reload configuration (no downtime):

```bash
sudo nginx -t && sudo systemctl reload nginx
```

#### Check active connections:

```bash
sudo netstat -anp | grep nginx
```

#### View access logs:

```bash
sudo tail -f /var/log/nginx/access.log
```

### Security Tips

1. Remove default Nginx page
2. Hide Nginx version in headers
3. Set up proper file permissions
4. Regular updates: `sudo apt upgrade nginx`

That's it! Your Nginx server is ready to host websites. 🎉


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ititanhosting.ro/docs/english/vps/linux/how-to-install-nginx.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
