Shortest possible shell script to install WordPress on Nginx server?

The following script is the one I use on Ubuntu-Nginx environments, to get a WordPress webapp up and running.

I run it with a domain as an argument:

bash ~/nwsm.sh example.com

My question is what could be shorten in that script, maybe via some WP-CLI automation, or other automation tool, to get the shortest script possible.

As for now it’s 35 lines of code, I assume it could go down to <=30 lines?

#!/bin/sh

domain="$1"
read -sp "What's your DB root password?" dbrootp
read -sp "What's your DB user password?" dbuserp
cd ${drt}

cat <<-WEBAPPBASE > /etc/nginx/sites-available/${domain}.conf
    server {
        root ${drt}/${domain}; 
        server_name ${domain} www.${domain};
        location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {expires 365d;}
    }
WEBAPPBASE
ln -s /etc/nginx/sites-available/${domain}.conf /etc/nginx/sites-enabled/
certbot --nginx -d ${domain} -d www.${domain} # HTTP/2

cat <<-DBSTACK | mysql -u root -p"${dbrootp}"
    CREATE USER "${domain}"@"localhost" IDENTIFIED BY "${dbuserp}";
    CREATE DATABASE "${domain}";
    GRANT ALL PRIVILEGES ON ${domain}.* TO "${domain}"@"localhost";
DBSTACK

mkdir ${domain} && curl -L http://wordpress.org/latest.tar.gz | tar -zx -C ${domain}/
cp ${domain}/wp-config-sample.php ${domain}/wp-config.php
sed -ir "s/username_here|database_name_here/${domain}/g ; s/password_here/${dbuserp}/g" ${domain}"/ ${domain}/wp-config.php

chown -R www-data:www-data ${domain}/*
chmod -R a-x,a=rX,u+w ${domain}/*
systemctl restart nginx.service

1 Answer
1

Having the “shortest” script is a fairly pointless goal here I think, besides for entertainment purposes. But if you are looking for something that is very minimal and easy to understand, you can check out Bash scripts like my own free SlickStack project on GitHub, which is geared toward LEMP stack newbies.

Keep mind that if you integrate something like WP-CLI, it might appear to be “shorter” commands, but the backend is a rather complicated system of scripts and aliases. And by the time you’ve installed WP-CLI and ran through all the necessary commands, it might not save much time…

You also seem to skip over PHP-FPM and MySQL installation which WordPress requires, not to mention other common things like MU (Must-Use) plugins and drop-in plugins e.g. object-cache.php which is pretty mainstream these days, also wp-config.php configuration, and so forth.

I spent years putting SlickStack together, employing an extremely user-friendly and minimal approach, and yet it still ended up containing several different Bash scripts… ultimately, anytime you are getting into server management and DevOps tools, it is probably going to require more than 30 lines of code! In the case of SlickStack, the ss-config file is where you can modify all your stack variables and options, before installation, meaning that you don’t have to edit any of the actual Bash scripts.

You can also search public Gists on GitHub, which tend to be smaller in size than repos.

Leave a Comment