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



# 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;


# 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, and make the file executable

$ chmod +x

Run the file by executing something along the lines of the following:

$ ./