Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Last active February 23, 2023 00:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/f193c98dfc255ef9bb059978a076dd65 to your computer and use it in GitHub Desktop.
Save nfsarmento/f193c98dfc255ef9bb059978a076dd65 to your computer and use it in GitHub Desktop.
bash script to create mariadb database, create nginx virtual host, setup WordPress and install SSL.
#!/bin/bash
# -------------------------------------------------
# Make site directory
# Download WP and install WP to site directory
# Set WP configuration
# Configure NGINX for new domain-name
# -------------------------------------------------
#
# Requirments:
#
# - NGINX
# - MARIA DB
# - PHP 7
# - python3-certbot-nginx
#
# How install https://www.nuno-sarmento.com/install-wordpress-on-ubuntu-20-04/
#
# -------------------------------------------------
clear
if [[ `id -u` != 0 ]]; then
echo >&2 "Must be root to run script";
exit;
fi
echo "-----------------------------------------------------"
echo "STEP #0/9 (setup database)";
echo "-----------------------------------------------------"
read -p "1/6. DOMANIN_NAME: " DOMAIN_NAME;
read -p "2/6. DB_USER_NAME: " DB_USER_NAME;
read -p "3/6. DB_USER_PASS: " DB_USER_PASS;
read -p "4/6. DB_NAME (example: db_demo): " DB_NAME;
read -p "5/6. DB_ROOT_USER_NAME: " DB_ROOT_USER_NAME;
read -p "6/6. DB_ROOT_USER_PASS: " DB_ROOT_USER_PASS;
if [[ $DOMAIN_NAME == "" ]]; then
echo >&2 "DOMAIN_NAME is empty";
exit;
fi
if [[ $DB_ROOT_USER_NAME == "" ]]; then
echo >&2 "DB_ROOT_USER_NAME is empty";
exit;
fi
if [[ $DB_ROOT_USER_PASS == "" ]]; then
echo >&2 "DB_ROOT_USER_PASS is empty";
exit;
fi
if [[ $DB_USER_NAME == "" ]]; then
echo >&2 "DB_USER_NAME is empty";
exit;
fi
if [[ $DB_USER_PASS == "" ]]; then
echo >&2 "DB_USER_PASS is empty";
exit;
fi
if [[ $DB_NAME == "" ]]; then
echo >&2 "DB_NAME is empty";
exit;
fi
echo "-----------------------------------------------------"
echo "STEP #1/9 (make directories)"
echo "-----------------------------------------------------"
mkdir -p /var/www/$DOMAIN_NAME/public;
chown -R www-data:www-data /var/www/$DOMAIN_NAME/public;
chmod -R 755 /var/www;
echo "-----------------------------------------------------"
echo "STEP #2/9 (download WP)"
echo "-----------------------------------------------------"
rm -rf /tmp/wordpress
cd /tmp
wget -c -q http://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
echo "-----------------------------------------------------"
echo "STEP #3/9 (install WP)"
echo "-----------------------------------------------------"
cp -a /tmp/wordpress/. /var/www/$DOMAIN_NAME/public/
chown www-data:www-data -R /var/www/$DOMAIN_NAME/public/*
mkdir /var/www/$DOMAIN_NAME/public/wp-content/uploads
chmod 775 /var/www/$DOMAIN_NAME/public/wp-content/uploads
echo "-----------------------------------------------------"
echo "STEP #4/9 (configure DB)"
echo "-----------------------------------------------------"
mysql -u$DB_ROOT_USER_NAME -p$DB_ROOT_USER_PASS <<-EOF
CREATE DATABASE $DB_NAME DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
GRANT ALL ON $DB_NAME.* TO '$DB_USER_NAME'@'localhost' IDENTIFIED BY '$DB_USER_PASS';
FLUSH PRIVILEGES;
EOF
echo "-----------------------------------------------------"
echo "STEP #5/9 (configure WP 'DB connection')"
echo "-----------------------------------------------------"
cp /var/www/$DOMAIN_NAME/public/wp-config-sample.php /var/www/$DOMAIN_NAME/public/wp-config.php
# define( 'DB_NAME', 'database_name_here' );
# define( 'DB_USER', 'username_here');
# define( 'DB_PASSWORD', 'password_here' );
sed -i -e "s/database_name_here/$DB_NAME/g" /var/www/$DOMAIN_NAME/public/wp-config.php
sed -i -e "s/username_here/$DB_USER_NAME/g" /var/www/$DOMAIN_NAME/public/wp-config.php
sed -i -e "s/password_here/$DB_USER_PASS/g" /var/www/$DOMAIN_NAME/public/wp-config.php
echo "-----------------------------------------------------"
echo "STEP #6/9 (configure NGINX)"
echo "-----------------------------------------------------"
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/$DOMAIN_NAME
cat << EOF > /etc/nginx/sites-available/$DOMAIN_NAME
server {
listen 80;
listen [::]:80;
server_name $DOMAIN_NAME www.$DOMAIN_NAME;
root /var/www/$DOMAIN_NAME/public;
index index.php;
# log files
access_log /var/log/nginx/$DOMAIN_NAME.access.log;
error_log /var/log/nginx/$DOMAIN_NAME.error.log;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
EOF
ln -s /etc/nginx/sites-available/$DOMAIN_NAME /etc/nginx/sites-enabled/
echo "-----------------------------------------------------"
echo "STEP #7/8 (test NGINX configuration and restart NGINX)\n"
echo "-----------------------------------------------------"
nginx -t
echo "RESTART NGINX"
systemctl restart nginx
echo "-----------------------------------------------------"
echo "STEP #8/9 (Install letsencrypt SSL)\n"
echo "-----------------------------------------------------"
sudo certbot --nginx -d $DOMAIN_NAME -d www.$DOMAIN_NAME
echo "-----------------------------------------------------"
echo "STEP #9/9 (Fix permissions)\n"
echo "-----------------------------------------------------"
cd /var/www/$DOMAIN_NAME/public
find -type d -exec chmod 755 {} \;
find -type f -exec chmod 644 {} \;
chown -R www-data:www-data *
echo "-----------------------------------------------------"
echo "Site Created for $DOMAIN_NAME"
echo "-----------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment