Skip to content

Instantly share code, notes, and snippets.

@gpolanco
Created June 7, 2020 09:40
Show Gist options
  • Save gpolanco/44ce47f9b461445d9ad2e9e6fc7a4516 to your computer and use it in GitHub Desktop.
Save gpolanco/44ce47f9b461445d9ad2e9e6fc7a4516 to your computer and use it in GitHub Desktop.
Semi-automating Nginx server block and demo public content
#!/bin/bash
domain=$1
server_block="/etc/nginx/sites-available/$domain"
document_root="/var/www/$domain"
public_html="$document_root/public_html"
index_public_html="$public_html/inde.html"
#===================================================
#================= SERVER BLOCK ===================
#===================================================
# Create the Nginx server server_block file:
sudo tee $server_block > /dev/null <<EOF
server {
listen 80;
listen [::]:80;
root $public_html;
index index.html index.htm;
server_name $domain www.$domain;
location / {
try_files $uri $uri/ =404;
}
}
EOF
# Link to make it available
sudo ln -s $server_block /etc/nginx/sites-enabled/
# Test configuration and reload if successful
sudo nginx -t && sudo service nginx reload
#====================================================
#================== DOCUMENT ROOT ===================
#====================================================
# Create the Document Root directory
sudo mkdir -p $public_html
# Assign ownership to your regular user account
sudo chown -R $USER:$USER $public_html
# Add permissions
sudo chmod -R 755 $root
# Create inde with demo content
sudo tee $index_public_html > /dev/null <<EOF
<html>
<head>
<meta charset="UTF-8">
<title>Bienvenido a $domain!</title>
</head>
<body>
<h1>¡El bloque de servidor de $domain está funcionando!</h1>
</body>
</html>
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment