# Instalare Nginx

Ai nevoie de un server web rapid? Nginx este perfect pentru serverul tău. Este ușor, gestionează mult trafic și este ușor de configurat. Hai să-l pornim cu mai multe site-uri web.

### Pasul 1: Actualizează Sistemul

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

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

### Pasul 2: Instalează 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
```

### Pasul 3: Pornește și Activează

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

Ar trebui să arate "active (running)".

### Pasul 4: Configurează Firewall-ul

Permite HTTP și 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
```

### Pasul 5: Testează Instalarea

Vizitează în browser:

```
http://adresa-ip-server
```

Ar trebui să vezi pagina de bun venit Nginx.

### Pasul 6: Structura Directorilor Nginx

Directoare importante:

* `/etc/nginx/` - Fișiere de configurare
* `/var/www/html/` - Rădăcina web implicită
* `/var/log/nginx/` - Fișiere log
* `/usr/share/nginx/html/` - Fișiere implicite (CentOS)

### Pasul 7: Creează Primul Tău Site Web

#### 1. Creează directorul site-ului:

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

#### 2. Setează permisiuni:

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

#### 3. Creează pagină de test:

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

Adaugă:

```html
<!DOCTYPE html>
<html>
<head>
    <title>Bun venit la Exemplu.com!</title>
</head>
<body>
    <h1>Succes! Nginx funcționează.</h1>
    <p>Acesta este primul tău site web pe ITITAN Hosting.</p>
</body>
</html>
```

### Pasul 8: Creează Host Virtual (Server Block)

#### Ubuntu/Debian:

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

#### CentOS/RHEL:

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

Adaugă această configurație:

```nginx
server {
    listen 80;
    listen [::]:80;
    
    server_name exemplu.com www.exemplu.com;
    root /var/www/exemplu.com/html;
    index index.html index.htm;
    
    location / {
        try_files $uri $uri/ =404;
    }
    
    # Refuză accesul la fișiere .htaccess
    location ~ /\.ht {
        deny all;
    }
}
```

#### Activează site-ul (doar Ubuntu/Debian):

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

### Pasul 9: Testează și Reîncarcă

```bash
# Testează configurația
sudo nginx -t

# Dacă sintaxa este OK, reîncarcă
sudo systemctl reload nginx
```

### Pasul 10: Testează Site-ul Tău

Vizitează `http://adresa-ip-server` (sau domeniul tău dacă DNS-ul este setat).

### Pasul 11: Configurează PHP cu Nginx (Opțional)

#### Instalează PHP-FPM:

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

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

#### Configurează Nginx pentru PHP:

Editează configurația site-ului tău, adaugă:

```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;  # Ajustează versiunea
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
```

#### Testează PHP:

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

Vizitează `http://adresa-ip-server-tau/info.php` (șterge după test!).

### Probleme Comune

#### "Nginx nu pornește"

```bash
# Verifică log-urile de eroare
sudo tail -f /var/log/nginx/error.log

# Testează configurația
sudo nginx -t
```

#### "403 Forbidden"

```bash
# Verifică permisiunile
sudo ls -la /var/www/exemplu.com/

# Repară proprietatea
sudo chown -R www-data:www-data /var/www/exemplu.com  # Ubuntu
sudo chown -R nginx:nginx /var/www/exemplu.com        # CentOS
```

#### "502 Bad Gateway"

```bash
# Verifică PHP-FPM
sudo systemctl status php-fpm

# Verifică socket-ul
ls -la /var/run/php/
```

### Comenzi Utile

#### Verifică versiunea Nginx:

```bash
nginx -v
```

#### Reîncarcă configurația (fără downtime):

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

#### Verifică conexiuni active:

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

#### Vezi log-urile de acces:

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

### Sfaturi de Securitate

1. Îndepărtează pagina implicită Nginx
2. Ascunde versiunea Nginx în headers
3. Setează permisiuni corecte pentru fișiere
4. Actualizări regulate: `sudo apt upgrade nginx`

Gata! Serverul tău Nginx este pregătit să găzduiască site-uri web. 🎉


---

# 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/romana/vps/linux/instalare-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.
