Basic Nginx server environment creation script Here's a script I wrote for raising up a basic Nginx server environment.

The script includes an internal script named csb (create Sblock) which is used to create Sblocks in a comfortable way. As I'm new to Linux, I might have had about 2-3 mistakes, especially in that internal script.

#!/bin/bash -x
function nginx {

    apt-get update -y &amp;&amp; apt-get upgrade -y
    apt-get install unattended-upgrades tree zip unzip -y
    dpkg-reconfigure --priority=low unattended-upgrades
    mkdir /root/backups /root/backups/db /root/backups/dirs

    ufw app list # Orient your firewall for Nginx.
    cd /usr/src &amp;&amp; rm -fv csf.tgz
    wget https://download.configserver.com/csf.tgz &amp;&amp; tar -xzf csf.tgz
    cd csf &amp;&amp; sh install.sh
    sed -i 's/TESTING = "1"/TESTING = "0"/g' /etc/csf/csf.conf
    csf -r &amp;&amp; perl /usr/local/csf/bin/csftest.pl

    cd /usr/local/src &amp;&amp; wget http://www.rfxn.com/downloads/maldetect-current.tar.gz &amp;&amp; tar -xzf maldetect-current.tar.gz
    cd maldetect-* &amp;&amp; bash ./install.sh &amp;&amp; cd ~

    # systemctl stats/stop/start/restart/reload/disable/enable # disable/enable --- at boot. # nginx -t
    apt-get install nginx mysql-server php-fpm php-mysql
    sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.0/fpm/php.ini
    sed -i 's/server_names_hash_bucket_size ..;/server_names_hash_bucket_size ..;/g' /etc/nginx/nginx.conf
    restart php7.0-fpm
    # http://ip_address

    cat &lt;&lt;-'CSB' &gt; /etc/nginx/sites-available/csb
        #!/bin/sh
        for domain; do
        &gt; "/etc/nginx/sites-available/${domain}.conf" cat &lt;&lt;EOF
        server {
            listen 80;
            listen [::]:80;
            root /var/www/html/${domain};
            index index.php index.html index.htm index.nginx-debian.html;
                server_name ${domain} www.${domain};

            location / {
                try_files $uri $uri/ =404;
            }

            location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            }

            location ~ /\.ht {
                deny all;
            }
        }
        EOF
    ln -s /etc/nginx/sites-available/${domain} /etc/nginx/sites-enabled/
        done
        systemctl restart nginx.service
    CSB
    chmod +x /etc/nginx/sites-available/csb

    cat &lt;&lt;-'BASHRC' &gt; /etc/bash.bashrc
    alias csb="bash /etc/nginx/sites-available/csb"
    BASHRC

    reboot
} nginx


Note: For some reason I can't indent the code under ln, here in Code Review.
Error handling

The script misses error handling at several places.
The downloads could fail or be corrupt,
but the script will continue until the end,
and finding the failure point can be difficult.
Worse, if a failure is quiet enough,
it might not even get noticed.

Checking the exit code of every single command can be tedious.
An alternative is to add this near the top of the script:

set -euo pipefail


This will cause the script abort on the first failure,
and also treat suspicious operations as failure.

Long lines

Long lines are harder to read.
Instead of this:


cd /usr/local/src &amp;&amp; wget http://www.rfxn.com/downloads/maldetect-current.tar.gz &amp;&amp; tar -xzf maldetect-current.tar.gz



It would be easier to read with one line per command:

cd /usr/local/src \
  &amp;&amp; wget http://www.rfxn.com/downloads/maldetect-current.tar.gz \
  &amp;&amp; tar -xzf maldetect-current.tar.gz


Changing directories in scripts

Be careful with changing directories in scripts.
If an error happens in the middle of the operation,
the next operation may find itself in an unexpected directory (in the absence of adequate error handling).

Changing directories can become safer using (...) sub-shells.
For example instead of this, with a cd ~ at the end:


cd /usr/local/src &amp;&amp; wget http://www.rfxn.com/downloads/maldetect-current.tar.gz &amp;&amp; tar -xzf maldetect-current.tar.gz
cd maldetect-* &amp;&amp; bash ./install.sh &amp;&amp; cd ~



You could do this, without the need for the cd ~ at the end:

(
    cd /usr/local/src &amp;&amp; wget http://www.rfxn.com/downloads/maldetect-current.tar.gz &amp;&amp; tar -xzf maldetect-current.tar.gz
    cd maldetect-* &amp;&amp; bash ./install.sh
)


Function declaration style

This is the recommended writing style:

nginx() {
    # ...
}


That is, no function keyword, and () after the name.

I would also give this function a more descriptive name.

Tips

Instead of:


mkdir /root/backups /root/backups/db /root/backups/dirs



You could write more compactly as:

mkdir -p /root/backups/{db,dirs}


Instead of cd ~, you could write simply cd.