The following script creates a webapp substrate for WordPress webapps/websites on Ubunu-Nginx with PHP-FPM and Mysql environments, with Certbot, while all software is uncustomized.
Such substrate includes, based on the 1 or more domains given as an argument (
/opt/nwsm.sh domain1.tld domain2.tld
):
/etc/nginx/sites-available/example.com.conf
.etc/nginx/sites-enabled/
symlink.- Appropriate DB user and instance, named
example.com
. - Suitable
wp-config.php
file.
I especially hope to learn how I could shorten this code in at least 10 rows, if at all.
Installing the script
Just copy and paste in Bash and it willl be created under /opt/nwsm.sh
.
cat <<-"NWSM" > /opt/nwsm.sh
#!/bin/sh
for domain; do
cat <<-WEBAPPCONF > "/etc/nginx/sites-available/${domain}.conf"
server {
root /var/www/html/${domain};
server_name ${domain} www.${domain};
location ~* \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
expires 365d;
}
location / {
index index.php index.html index.htm fastcgi_index;
try_files $uri $uri =404 $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
WEBAPPCONF
ln -s /etc/nginx/sites-available/${domain}.conf /etc/nginx/sites-enabled/
echo "Please enter database user password for user ${domain}: "
read -s waps
echo "Please enter database root password"
mysql -u root -p <<-MYSQL
create user "${domain}"@"localhost" identified by "${waps}";
create database ${domain};
GRANT ALL PRIVILEGES ON ${domain}.* TO ${domain}@localhost;
MYSQL
certbot --nginx -d ${domain} -d www.${domain}
done
cd /var/www/html/
wget http://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz && rm latest.tar.gz
mv wordpress ${domain}
cp /var/www/html/${domain}/wp-config-sample.php /var/www/html/${domain}/wp-config.php
echo "1/1: Please enter the password of the site's DB user." && read -s dbup
sed -i "s/database_name_here/${domain}"/g /var/www/html/${domain}/wp-config.php
sed -i "s/username_here/${domain}"/g /var/www/html/${domain}/wp-config.php
sed -i "s/password_here/${dbup}"/g /var/www/html/${domain}/wp-config.php
chown www-data:www-data /var/www/html/* -R
find /var/www/html/* -type d -exec chmod 755 {} \;
find /var/www/html/* -type f -exec chmod 644 {} \;
systemctl restart nginx.service
NWSM
chmod +x /opt/nwsm.sh
Using the script
Call the script with a domain and tld as an argument, say:
/opt/nwsm.sh example.com
cat <<-"NWSM" > /opt/nwsm.sh
which will fail when run on a machine that doesn't have opt, or by a user that doesn't have write permissions to it? \$\endgroup\$/root/bin
,/opt/bin
,/usr/local/bin
,/opt/scripts
,/home/jenny/bin
, ... \$\endgroup\$