This has been adapted from a script by a DigitalOcean mod by the name of Jellingwood. Script has been modified to take PHP into account. This assumes your www directory is /var/www and that you are running PHP 7.0
#!/bin/bash
domain=$1
root="/var/www/$domain"
block="/etc/nginx/sites-available/$domain"
# Create the Document Root directory
sudo mkdir -p $root
# Assign ownership to your regular user account
sudo chown -R $USER:$USER $root
# Create the Nginx server block file:
sudo tee $block > /dev/null <<EOF
server {
listen 80;
listen [::]:80;
root /var/www/$domain;
index index.php index.html index.htm;
server_name $domain www.$domain;
location / {
try_files \$uri \$uri/ =404;
}
location ~ \.php\$ {
include snippets/fastcgi-php.conf;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
EOF
# Link to make it available
sudo ln -s $block /etc/nginx/sites-enabled/
# Test configuration and reload if successful
sudo nginx -t && sudo systemctl reload nginx
Save the file as create-site.sh, and make the file executable
$ chmod +x create-site.sh
Run the file by executing something along the lines of the following:
$ ./create-site.sh test.ramin.io