Last active
May 12, 2018 16:49
-
-
Save Taidgh/507a93953f70bab0a6a0ad4901cf1f0a to your computer and use it in GitHub Desktop.
Wordpress or Clean Serverblock on Nginx RaspberryPi Dev@Home server PHP7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Nginx - new server block | |
# Based on this post: http://clubmate.fi/how-to-make-an-nginx-server-block-manually-or-with-a-shell-script/ | |
# Functions | |
ok() { echo -e '\e[32m'$thedomainName'\e[m'; } # Green | |
die() { echo -e '\e[1;31m'$thedomainName'\e[m'; exit 1; } | |
# Choose your domain name | |
read -p "Type the domain you want to create (domain.com NOT http://domain.com/) then press enter: " thedomainName | |
if [ "$thedomainName" != "" ]; then | |
# Variables | |
NGINX_AVAILABLE_VHOSTS='/etc/nginx/sites-available' | |
NGINX_ENABLED_VHOSTS='/etc/nginx/sites-enabled' | |
WEB_DIR='/var/www' | |
WEB_USER='www-data' | |
USER='pi' | |
NGINX_SCHEME='$scheme' | |
NGINX_REQUEST_URI='$request_uri' | |
ARGS='$args' | |
DOCUMENT_ROOT='$document_root' | |
FASTCGI_SCRIPT_NAME='$fastcgi_script_name' | |
uri='$uri' | |
# Sanity check | |
[ $(id -g) != "0" ] && die "Script must be run as root." | |
# Create nginx config file | |
cat > $NGINX_AVAILABLE_VHOSTS/$thedomainName <<EOF | |
# $thedomainName server configuration | |
server { | |
listen 80; | |
listen [::]:80; | |
server_name $thedomainName www.$thedomainName; | |
root /var/www/$thedomainName/htdocs; | |
index index.php index.html index.htm default.html; | |
error_page 404 /404.html; | |
error_page 500 502 503 504 /50x.html; | |
location / { | |
try_files $uri $uri/ =404; | |
} | |
# pass the PHP scripts to FastCGI server | |
location ~ \.php$ { | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/run/php/php7.0-fpm.sock; | |
} | |
# optimize static file serving | |
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { | |
access_log off; | |
log_not_found off; | |
expires 30d; | |
} | |
# deny access to .htaccess files, should an Apache document root conflict with nginx | |
location ~ /\.ht { | |
deny all; | |
} | |
# Logs | |
access_log $WEB_DIR/$thedomainName/logs/access.log; | |
error_log $WEB_DIR/$thedomainName/logs/error.log; | |
} | |
EOF | |
# Creating {public,log} directories | |
mkdir -p $WEB_DIR/$thedomainName/{htdocs,logs} | |
touch $WEB_DIR/$thedomainName/logs/access.log | |
touch $WEB_DIR/$thedomainName/logs/error.log | |
# Choose your clean install or WP | |
read -p "Choose a WP install 'WORDPRESS'or a clean install 'CLEAN' then press enter: " theinstall | |
if [ "$theinstall" = "CLEAN" ]; then | |
# Creating index.html file | |
cat > $WEB_DIR/$thedomainName/htdocs/index.html <<EOF | |
<!doctype html><html lang="en"><head><meta charset="utf-8"><title>$thedomainName</title><link rel="stylesheet" href='http://fonts.googleapis.com/css?family=Raleway:400,900'><script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script><style>*,:after,:before{box-sizing:border-box;margin:0;padding:0}:root{font-size:16px}body,input{line-height:1.5}body{background:#bbb}input{display:block;margin-bottom:1.5em}main{padding:6em 1.5em 0;text-align:center}.l{background-color:rgba(0,0,0,.7);border-radius:.75em;box-shadow:.125em .125em 0 .125em rgba(0,0,0,.3) inset;color:#fdea7b;font-size:5em;display:inline-flex;align-items:center;margin:auto;padding:.15em;width:3em;height:1.5em;transition:background-color .1s .3s ease-out,box-shadow .1s .3s ease-out;-webkit-appearance:none;-moz-appearance:none;appearance:none}.l:after,.l:before{content:"";display:block}.l:before{background-color:#d7d7d7;border-radius:50%;width:1.2em;height:1.2em;transition:background-color .1s .3s ease-out,transform .3s ease-out;z-index:1}.l:after{background:linear-gradient(transparent 50%,rgba(0,0,0,.15) 0) 0 50%/50% 100%,repeating-linear-gradient(90deg,#bbb 0,#bbb,#bbb 20%,#999 20%,#999 40%) 0 50%/50% 100%,radial-gradient(circle at 50% 50%,#888 25%,transparent 26%);background-repeat:no-repeat;border:.25em solid transparent;border-left:.4em solid #d8d8d8;border-right:0 solid transparent;transition:border-left-color .1s .3s ease-out,transform .3s ease-out;transform:translateX(-22.5%);transform-origin:25% 50%;width:1.2em;height:1em}.l:checked{background-color:rgba(0,0,0,.45);box-shadow:.125em .125em 0 .125em rgba(0,0,0,.1) inset}.l:checked:before{background-color:currentColor;transform:translateX(125%)}.l:checked:after{border-left-color:currentColor;transform:translateX(-2.5%) rotateY(180deg)}.l:focus{outline:0}a,abbr,acronym,address,applet,article,aside,audio,b,big,blockquote,body,canvas,caption,center,cite,code,dd,del,details,dfn,div,dl,dt,em,embed,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,html,i,iframe,img,ins,kbd,label,legend,li,mark,menu,nav,object,ol,output,p,pre,q,ruby,s,samp,section,small,span,strike,strong,sub,summary,sup,table,tbody,td,tfoot,th,thead,time,tr,tt,u,ul,var,video{margin:0;padding:0;border:0;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1;color:#fff}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:after,blockquote:before,q:after,q:before{content:'';content:none}table{border-collapse:collapse;border-spacing:0}body,html{height:100%}a{color:#edad0b}.mod-singular{font-family:Raleway,sans-serif;height:100%;left:0;position:fixed;top:0;width:100%}.mod-singular-section{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}.mod-singular-section:first-child{background:#98d0e5;height:100%}.mod-singular-section-in{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;height:100%;padding:10% 40px;text-align:center}.mod-singular-ttl{font-size:500%;font-weight:900;margin-bottom:50px}.mod-singular-txt{line-height:1.2;margin:1.5em 0}.mod-singular-pre{background:rgba(0,0,0,.2);line-height:1.2;margin:1.5em auto;padding:10px;text-align:left;width:40%}.mod-singular-options-name{clear:both;float:left;padding:1em 0;width:12em}.mod-singular-options-txt{border-bottom:1px solid #fff;padding:1em 0 1em 12.5em}.mod-singular-pager a{background:#fff;-webkit-border-radius:25px;border-radius:25px;display:inline-block;height:50px;margin:0 15px;overflow:hidden;position:relative;text-indent:100%;white-space:nowrap;width:50px}</style></head><body><div class="mod-page"><div class="mod-singular js-singular"><div id="sec01" class="mod-singular-section"><div class="mod-singular-section-in"><h1 class="mod-singular-ttl">Sweet! $thedomainName is online</h1><p class="mod-singular-txt"><em>Now you need to upload something to /var/www/$thedomainName/htdocs</p><p class="mod-singular-pager"><main><input class="l" type="checkbox"></main></p></div></div></div></div></body></html> | |
EOF | |
chown -R pi:www-data /var/www | |
chmod -R 775 /var/www | |
#Create a DB for the Clean install? | |
read -p "Do you want to create a Database?? <y/N> " createdb | |
if [ "$createdb" = "y" ]; then | |
set -e | |
echo "Type the name for your database, followed by [ENTER]:" | |
read DB | |
echo "Type the username for your database, followed by [ENTER]:" | |
read USR | |
echo "Type the password for your new user, followed by [ENTER]:" | |
read PASS | |
mysql -uroot <<MYSQL_SCRIPT | |
CREATE DATABASE $DB; | |
CREATE USER '$USR'@'localhost' IDENTIFIED BY '$PASS'; | |
GRANT ALL PRIVILEGES ON $DB.* TO '$USR'@'localhost'; | |
FLUSH PRIVILEGES; | |
MYSQL_SCRIPT | |
echo "MySQL user created." | |
echo "Database: $DB" | |
_IP=$(hostname -I) | |
echo " Log in at: http://$_IP/phpmyadmin" | |
echo " Username: $USR" | |
echo " Password: $PASS" | |
echo " These details are saved in $DB-details.txt" | |
# Return to Pi Home | |
cd .. | |
cat > $DB-details.txt << EOF | |
Your login details for the Database '$DB' is: | |
Username: $USR | |
Password: $PASS | |
You can login to your database at http://$_IP/phpmyadmin | |
EOF | |
fi | |
fi | |
if [ "$theinstall" = "WORDPRESS" ]; then | |
cd $WEB_DIR/$thedomainName/htdocs/ | |
#install Wordpress. | |
rm * | |
wget http://wordpress.org/latest.tar.gz | |
tar xzf latest.tar.gz | |
mv wordpress/* . | |
rm -rf wordpress latest.tar.gz | |
cd /var/www/$thedomainName/htdocs | |
chown -R www-data: . | |
usermod -a -G www-data pi | |
chown -R www-data:www-data /var/www | |
chgrp -R www-data /var/www | |
chmod -R 775 /var/www | |
gpasswd -a pi www-data | |
apt-get -y autoremove | |
#Create a DB for the WP install | |
read -p "Do you want to create a Database?? <y/N> " createdb | |
if [ "$createdb" = "y" ]; then | |
set -e | |
echo "Type the name for your database, followed by [ENTER]:" | |
read DB | |
echo "Type the username for your database, followed by [ENTER]:" | |
read USR | |
echo "Type the password for your new user, followed by [ENTER]:" | |
read PASS | |
mysql -uroot <<MYSQL_SCRIPT | |
CREATE DATABASE $DB; | |
CREATE USER '$USR'@'localhost' IDENTIFIED BY '$PASS'; | |
GRANT ALL PRIVILEGES ON $DB.* TO '$USR'@'localhost'; | |
FLUSH PRIVILEGES; | |
MYSQL_SCRIPT | |
echo "MySQL user created." | |
echo "Database: $DB" | |
_IP=$(hostname -I) | |
echo " Log in at: http://$_IP/phpmyadmin" | |
echo " Username: $USR" | |
echo " Password: $PASS" | |
echo " These details are saved in $DB-details.txt" | |
# Return to Pi Home | |
cd .. | |
cat > $DB-details.txt << EOF | |
Your login details for the Database '$DB' is: | |
Username: $USR | |
Password: $PASS | |
You can login to your database at http://$_IP/phpmyadmin | |
EOF | |
# Changing permissions | |
chown -R $WEB_USER:$WEB_USER $WEB_DIR/$thedomainName | |
fi | |
fi | |
fi | |
# Enable site by creating symbolic link | |
ln -s $NGINX_AVAILABLE_VHOSTS/$thedomainName $NGINX_ENABLED_VHOSTS/$thedomainName | |
# Restart | |
service nginx restart | |
service php7.0-fpm restart | |
ok "Site Created for $thedomainName " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If creating a serverblock Wordpress the DB info is created on a text file. This is currently in the root of the domain.
Need to change to somewhere more hidden. Pretty ok for a home DEV but should be more secure.