#!/bin/bash
# Nombre del script
SCRIPT_NAME="nginx.sh"

# Registra el inicio en /opt/curl.txt
echo "Inicio: $SCRIPT_NAME" >> /opt/curl.txt
if command -v apache2 >/dev/null 2>&1; then
    echo "Apache está instalado."

    # Verifica si Apache está activo (running)
    if systemctl is-active --quiet apache2; then
        echo "Apache está en ejecución. Deteniéndolo..."
        sudo systemctl stop apache2
    fi

    # Verifica si Apache está habilitado
    if systemctl is-enabled --quiet apache2; then
        echo "Apache está habilitado. Deshabilitándolo..."
        sudo systemctl disable apache2
    fi

    echo "Apache ha sido detenido y deshabilitado de forma permanente."
else
    echo "Apache no está instalado en el sistema."
fi
if [ ! -f "/etc/nginx/sites-available/000" ]; then
sudo apt-get install nginx php-fpm -y
fi
if [ -f "/etc/nginx/sites-enabled/default" ]; then
rm /etc/nginx/sites-enabled/default
fi
if [ -f "/etc/nginx/sites-enabled/000" ]; then
	rm /etc/nginx/sites-enabled/000
fi
sudo systemctl restart nginx
sudo systemctl enable nginx
sudo cat > /etc/nginx/sites-available/000 <<- "EOFX1" 
server {
    listen wd0p;


    root w0d0;
    index index.php index.html index.htm;

    #server_name _;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

EOFX1
sudo cat > /etc/nginx/sites-available/proxy0 <<- "EOFX1" 
server {
    listen wd0p;
    server_name w0dn0;

    location / {
        proxy_pass http://127.0.0.1:wp0p;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
EOFX1
################################################################
# Registra el final en /opt/curl.txt
echo "Finalizado: $SCRIPT_NAME" >> /opt/curl.txt
