GNU/Linux >> Linux Esercitazione >  >> Panels >> Panels

Host virtuale Nginx – Blocco del server Nginx

Ti mostreremo come creare un host virtuale Nginx, noto anche come blocco server Nginx. In uno dei nostri tutorial precedenti abbiamo spiegato come installare e configurare il server LNMP (Nginx, MySQL e PHP) su un VPS Debian 6 (squeeze), ora vedremo come impostare un nuovo blocco server (host virtuale ) per ogni nuovo dominio.

Nota:"VirtualHost" è un termine Apache. Nginx non ha host virtuali, ha "Server Blocks" che usano il server_name e le direttive listen per collegarsi ai socket TCP.

Il seguente script può essere utilizzato per impostare un nuovo blocco server sul tuo server Nginx.

#!/usr/bin/env bash
#
# Nginx - new server block
# http://rosehosting.com

# Functions
ok() { echo -e '\e[32m'$1'\e[m'; } # Green
die() { echo -e '\e[1;31m'$1'\e[m'; exit 1; }

# Variables
NGINX_AVAILABLE_VHOSTS='/etc/nginx/sites-available'
NGINX_ENABLED_VHOSTS='/etc/nginx/sites-enabled'
WEB_DIR='/var/www'
WEB_USER='www-data'

# Sanity check
[ $(id -g) != "0" ] && die "Script must be run as root."
[ $# != "1" ] && die "Usage: $(basename $0) domainName"

# Create nginx config file
cat > $NGINX_AVAILABLE_VHOSTS/$1 <<EOF
server {
  server_name $1;
  listen 80;
  root $WEB_DIR/$1/public_html;
  access_log $WEB_DIR/$1/logs/access.log;
  error_log $WEB_DIR/$1/logs/error.log;
  index index.html index.php;
  location / {
    try_files \$uri \$uri/ @rewrites;
  }
  location @rewrites {
    rewrite ^ /index.php last;
  }
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }
  location ~ /\.ht {
    deny  all;
  }
  location ~ \.php {
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
  }
}
EOF

# Creating {public,log} directories
mkdir -p $WEB_DIR/$1/{public_html,logs}

# Creating index.html file
cat > $WEB_DIR/$1/public_html/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
        <title>$1</title>
        <meta charset="utf-8" />
</head>
<body class="container">
        <header><h1>$1<h1></header>
        <div id="wrapper"><p>Hello World</p></div>
        <footer>© $(date +%Y)</footer>
</body>
</html>
EOF

# Changing permissions
chown -R $WEB_USER:$WEB_USER $WEB_DIR/$1

# Enable site by creating symbolic link
ln -s $NGINX_AVAILABLE_VHOSTS/$1 $NGINX_ENABLED_VHOSTS/$1

# Restart
echo "Do you wish to restart nginx?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) /etc/init.d/nginx restart ; break;;
        No ) exit;;
    esac
done

ok "Site Created for $1"

Ecco cosa fa, in poche parole:

  1. Crea una nuova directory per il sito (/var/www/DOMAIN.COM/public_html)
  2. Crea una nuova directory per i file di registro (/var/www/DOMAIN.COM/logs)
  3. Imposta il proprietario/gruppo corretto.
  4. Crea un semplice file index.html per mostrare che il sito funziona.
  5. Richiede il riavvio.

Per utilizzare il tipo di script:

./nginx_vhost.sh  newdomain.com

Lo script dovrebbe funzionare su Debian, Ubuntu e distribuzioni strettamente correlate.

Se utilizzi uno dei nostri piani di hosting VPS Ubuntu, non devi creare da solo Nginx Virtual Host, puoi semplicemente chiedere ai nostri esperti amministratori Linux di creare un blocco server Nginx per te. Sono disponibili 24 ore su 24, 7 giorni su 7 e si prenderanno immediatamente cura della tua richiesta.

PS. Se ti è piaciuto questo post, su come creare un host virtuale Nginx, condividilo con i tuoi amici sui social network utilizzando i pulsanti a sinistra o semplicemente lascia una risposta qui sotto. Grazie.


Panels
  1. nginx - 413 Entità richiesta troppo grande

  2. Configurazioni dell'host virtuale Apache – Linux

  3. Installa il server web Apache su Linux Mint 13 / Linux Mint 14

  4. Installa Nginx e configura l'host virtuale in Ubuntu 20.04

  5. Ospita WordPress in Ubuntu 20.04, Mysql 8, Ubuntu 20.04, Nginx

Come installare Nginx su Ubuntu 20.04

Come installare Nginx su CentOS 8

Installazione di Nginx su Ubuntu 14.04 e 16.04

Installa Nginx su CentOS 7 / RHEL 7

Come ospitare un sito Web su NGINX Web Server

Come configurare un host virtuale Nginx