Skip to content

Instantly share code, notes, and snippets.

@kevashcraft
Last active June 20, 2017 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevashcraft/3812c316de79d5ca1c83 to your computer and use it in GitHub Desktop.
Save kevashcraft/3812c316de79d5ca1c83 to your computer and use it in GitHub Desktop.
Create a WordPress Site with WP CLI
#!/bin/bash
#
# addsite
# a bash script to create a new directory, download wordpress, create the database and user, and create wp-config and the initial site install with wp cli
# by Kevin Ashcraft (@kevashcraft)
# last updated on November, 8th 2014
#
# * expects a template file at /etc/nginx/conf.d/template
# * expects wp cli executable as wp
# * expects that this is not really just a dream
#
#
# ask for sql password
read -p "SQL Passwd: " SQLPASS
# exit if the site's directory already exits
if [ -d /srv/$1 ]; then
exit "Directory already exits"
fi
mkdir /srv/$1
cd /srv/$1
wp core download
wpperms /srv/$1
#clean the domain name for use as a db name
DB=$(echo "$1" | tr -cd '[[:alnum:]]' )
DB=$(echo ${DB:0:14})
#create sql pass from urandom
NEWSQLPASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1)
mysql -u root -p$SQLPASS -e "CREATE DATABASE $DB"
if [ $? -ne 0 ]; then
rm -rf /srv/$1
exit "Could not create DB"
fi
mysql -u root -p$SQLPASS -e "GRANT ALL ON $DB.* TO $DB@localhost IDENTIFIED BY '$NEWSQLPASS'"
if [ $? -ne 0 ]; then
mysql -u root -p$SQLPASS -e "DROP DATABASE $DB"
rm -rf /srv/$1
exit "Could not create User"
fi
ADMINR=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 7 | head -n 1)
ADMINP=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1)
ADMIN="@@Admin@$ADMINR"
wp core config --dbname=$DB --dbuser=$DB --dbpass=$NEWSQLPASS
wp core install --url=http://$1 --title='A New LogicDudes WordPress Site' --admin_user=$ADMIN --admin_password=$ADMINP --admin_email=kevin@logicdudes.com
cp /etc/nginx/conf.d/template /etc/nginx/conf.d/$1.conf
sed -i "s/SERVERNAME/$1/gI" /etc/nginx/conf.d/$1.conf
systemctl reload nginx
echo "Admin User: $ADMIN"
echo "Admin Password: $ADMINP"
echo "All done!"
listen :80;
server {
include listen;
server_name www.SERVERNAME;
return 301 http://SERVERNAME$request_uri;
}
server {
server_name SERVERNAME;
root /srv/SERVERNAME;
include listen;
include wordpress;
}
set $skip_cache 1;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
#rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location /wp-admin {
auth_basic "What is the magic word?";
auth_basic_user_file /etc/nginx/passes;
# pagespeed off;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# try_files $uri =404;
fastcgi_index index.php;
# fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
# fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root";
include fastcgi_params;
fastcgi_read_timeout 900;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache FASTCACHE;
fastcgi_cache_valid 200 60m;
}
location ~ /purge(/.*) {
fastcgi_cache_purge FASTCACHE "$scheme$request_method$host$request_uri";
}
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}
location ~* ^.+\.(js|css)$ {
expires max;
}
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\. { deny all; access_log off; log_not_found off; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment