Skip to content

Instantly share code, notes, and snippets.

@carlHandy
Created August 13, 2020 04:03
Show Gist options
  • Save carlHandy/a6b6eacd1b44f85d62d220b9dd5933b4 to your computer and use it in GitHub Desktop.
Save carlHandy/a6b6eacd1b44f85d62d220b9dd5933b4 to your computer and use it in GitHub Desktop.
Installs and configures wordpress nginx server block.
#!/bin/bash
read -p 'Enter domain name: ' domain
# Functions
ok() { echo -e '\e[32m'$domain'\e[m'; } # Green
die() { echo -e '\e[1;31m'$domain'\e[m'; exit 1; }
NGINX_AVAILABLE='/etc/nginx/sites-available'
NGINX_ENABLED='/etc/nginx/sites-enabled'
WEB_DIR='/var/www'
WEB_USER='www-data:www-data'
# Sanity check
[ $(id -g) != "0" ] && die "Script must be run as root."
# Create nginx config
cat > $NGINX_AVAILABLE/$domain <<EOF
server {
## Remove server tokens (hide server version)
server_tokens off;
## Your website name goes here.
server_name $domain www.$domain;
## Your only path reference.
root $WEB_DIR/$domain/wordpress;
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
EOF
# Configure directory
cd $WEB_DIR
mkdir $domain
cd $domain
# Connfigure WordPress
echo 'Database Name: '
read -e dbname
echo 'Database User: '
read -e dbuser
echo 'Database Password: '
read -e dbpass
echo 'Database Host: '
read -e dbhost
echo 'run install? (y/n)'
read -e run
if ["$run" == n] ; then
exit
else
echo 'A robot is now installing WordPress for you'
# download wordpress
curl -O https://wordpress.org/latest.tar.gz
# unzip wordpress
tar -zxvf latest.tar.gz
# remove zip file
rm -rf latest.tar.gz
cd wordpress
mv wp-config-sample.php wp-config.php
# set database details with perl find and replace
perl -i -pe "s/database_name_here/$dbname/g" wp-config.php
perl -i -pe "s/username_here/$dbuser/g" wp-config.php
perl -i -pe "s/password_here/$dbpass/g" wp-config.php
perl -i -pe "s/localhost/$dbhost/g" wp-config.php
fi
echo 'Installation is complete'
cd $WEB_DIR
chown -R $WEB_USER $domain
chmod -R 775 $domain
# Setup symlink
ln -s $NGINX_AVAILABLE/$domain $NGINX_ENABLED/$domain
systemctl restart nginx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment