Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/bin/bash -e
clear
echo "============================================"
echo "Nginx configuration files"
echo "============================================"
# Check if the global directory exists
DIRECTORY="/etc/nginx/global"
if [ ! -d "$DIRECTORY" ]; then
# Create the directory
echo "Creating $DIRECTORY directory..."
mkdir $DIRECTORY
else
echo "$DIRECTORY directory already exist."
fi
echo "Checking if common.conf exists..."
if [ ! -f /etc/nginx/global/common.conf ]; then
echo "Creating common.conf"
echo "# Global configuration file." > /etc/nginx/global/common.conf
echo "# ESSENTIAL : Configure Nginx Listening Port" >> /etc/nginx/global/common.conf
echo "listen 80;" >> /etc/nginx/global/common.conf
echo "# ESSENTIAL : Default file to serve. If the first file isn't found," >> /etc/nginx/global/common.conf
echo "index index.php index.html index.htm;" >> /etc/nginx/global/common.conf
echo "# ESSENTIAL : no favicon logs" >> /etc/nginx/global/common.conf
echo "location = /favicon.ico {" >> /etc/nginx/global/common.conf
echo -e "\tlog_not_found off;" >> /etc/nginx/global/common.conf
echo -e "\taccess_log off;" >> /etc/nginx/global/common.conf
echo "}" >> /etc/nginx/global/common.conf
echo "# ESSENTIAL : robots.txt" >> /etc/nginx/global/common.conf
echo "location = /robots.txt {" >> /etc/nginx/global/common.conf
echo -e "\tallow all;" >> /etc/nginx/global/common.conf
echo -e "\tlog_not_found off;" >> /etc/nginx/global/common.conf
echo -e "\taccess_log off;" >> /etc/nginx/global/common.conf
echo "}" >> /etc/nginx/global/common.conf
echo "# ESSENTIAL : Configure 404 Pages" >> /etc/nginx/global/common.conf
echo "error_page 404 /404.html;" >> /etc/nginx/global/common.conf
echo "# ESSENTIAL : Configure 50x Pages" >> /etc/nginx/global/common.conf
echo "error_page 500 502 503 504 /50x.html;" >> /etc/nginx/global/common.conf
echo "location = /50x.html {" >> /etc/nginx/global/common.conf
echo -e "\troot /usr/share/nginx/www;" >> /etc/nginx/global/common.conf
echo "}" >> /etc/nginx/global/common.conf
echo "# SECURITY : Deny all attempts to access hidden files .abcde" >> /etc/nginx/global/common.conf
echo "location ~ /\. {" >> /etc/nginx/global/common.conf
echo -e "\tdeny all;" >> /etc/nginx/global/common.conf
echo "}" >> /etc/nginx/global/common.conf
echo "# PERFORMANCE : Set expires headers for static files and turn off logging." >> /etc/nginx/global/common.conf
echo "location ~* ^.+\.(js|css|swf|xml|txt|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)$ {" >> /etc/nginx/global/common.conf
echo -e "\taccess_log off; log_not_found off; expires 30d;" >> /etc/nginx/global/common.conf
echo "}" >> /etc/nginx/global/common.conf
echo "common.conf has been created!"
fi
echo "Checking if wordpress.conf exists..."
if [ ! -f /etc/nginx/global/wordpress.conf ]; then
echo "# WORDPRESS : Rewrite rules, sends everything through index.php and keeps the appended query string intact" > /etc/nginx/global/wordpress.conf
echo "location / {" >> /etc/nginx/global/wordpress.conf
echo -e "\ttry_files $uri $uri/ /index.php?q=$uri&$args;" >> /etc/nginx/global/wordpress.conf
echo "}" >> /etc/nginx/global/wordpress.conf
echo "# SECURITY : Deny all attempts to access PHP Files in the uploads directory" >> /etc/nginx/global/wordpress.conf
echo "location ~* /(?:uploads|files)/.*\.php$ {" >> /etc/nginx/global/wordpress.conf
echo -e "\tdeny all;" >> /etc/nginx/global/wordpress.conf
echo "}" >> /etc/nginx/global/wordpress.conf
echo "# REQUIREMENTS : Enable PHP Support" >> /etc/nginx/global/wordpress.conf
echo "location ~ \.php$ {" >> /etc/nginx/global/wordpress.conf
echo -e "\t# SECURITY : Zero day Exploit Protection" >> /etc/nginx/global/wordpress.conf
echo -e "\ttry_files $uri =404;" >> /etc/nginx/global/wordpress.conf
echo -e "\t# ENABLE : Enable PHP, listen fpm sock" >> /etc/nginx/global/wordpress.conf
echo -e "\tfastcgi_split_path_info ^(.+\.php)(/.+)$;" >> /etc/nginx/global/wordpress.conf
echo -e "\tfastcgi_pass unix:/var/run/php5-fpm.sock;" >> /etc/nginx/global/wordpress.conf
echo -e "\tfastcgi_index index.php;" >> /etc/nginx/global/wordpress.conf
echo -e "\tinclude fastcgi_params;" >> /etc/nginx/global/wordpress.conf
echo "}" >> /etc/nginx/global/wordpress.conf
echo "# PLUGINS : Enable Rewrite Rules for Yoast SEO SiteMap" >> /etc/nginx/global/wordpress.conf
echo "rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;" >> /etc/nginx/global/wordpress.conf
echo "rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;" >> /etc/nginx/global/wordpress.conf
echo "wordpress.conf has been created!"
fi
echo "Website url (www.mywebsite.com):"
read -e websiteurl
suburl=$(echo "$websiteurl" | cut -d'.' -f 2-)
echo "Creating the website configuration file..."
# Website specific configuration file
echo "server {" > /etc/nginx/sites-available/$suburl
echo "# URL: Correct way to redirect URL's" >> /etc/nginx/sites-available/$suburl
echo -e "\tserver_name $suburl;" >> /etc/nginx/sites-available/$suburl
echo -e 'rewrite ^/(.*)$ http://'$websiteurl'/$1 permanent;' >> /etc/nginx/sites-available/$suburl
echo "}" >> /etc/nginx/sites-available/$suburl
echo "server {" >> /etc/nginx/sites-available/$suburl
echo -e "\tserver_name $websiteurl;" >> /etc/nginx/sites-available/$suburl
echo -e "\troot /usr/share/nginx/www/$suburl/wordpress;" >> //etc/nginx/sites-available/$suburl
echo -e "\taccess_log /var/log/nginx/$suburl.access.log;" >> /etc/nginx/sites-available/$suburl
echo -e "\terror_log /var/log/nginx/$suburl.error.log;" >> /etc/nginx/sites-available/$suburl
echo -e "\tinclude global/common.conf;" >> /etc/nginx/sites-available/$suburl
echo -e "\tinclude global/wordpress.conf;" >> /etc/nginx/sites-available/$suburl
echo "}" >> /etc/nginx/sites-available/$suburl
echo "Create the symbolic link ..."
ln -s /etc/nginx/sites-available/$suburl /etc/nginx/sites-enabled/$suburl
echo "Restarting services..."
service nginx restart
service php5-fpm restart
echo "============================================"
echo "MySQL Configuration"
echo "============================================"
echo "Database Name: "
read -e dbname
echo "Database User: "
read -e dbuser
echo "Database Password: "
read -s dbpass
MYSQL=`which mysql`
Q1="CREATE DATABASE IF NOT EXISTS $dbname;"
Q2="GRANT USAGE ON *.* TO $dbuser@localhost IDENTIFIED BY '$dbpass';"
Q3="GRANT ALL PRIVILEGES ON $dbname.* TO $dbuser@localhost;"
Q4="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}${Q4}"
$MYSQL -uroot -p -e "$SQL"
echo "Database $dbname has been created!"
echo "============================================"
echo "WordPress Install Script"
echo "============================================"
echo "run install? (y/n)"
read -e run
if [ "$run" == n ] ; then
exit
else
echo "============================================"
echo "Installing wordpress..."
echo "============================================"
echo "Downloading WordPress..."
#download wordpress
curl -O https://wordpress.org/latest.tar.gz
#unzip wordpress
echo "Extracting WordPress..."
tar -zxvf latest.tar.gz
#create wp config
echo "Editing wp-config.php..."
cp wordpress/wp-config-sample.php wordpress/wp-config.php
#set database details with perl find and replace
perl -pi -e "s/database_name_here/$dbname/g" wordpress/wp-config.php
perl -pi -e "s/username_here/$dbuser/g" wordpress/wp-config.php
perl -pi -e "s/password_here/$dbpass/g" wordpress/wp-config.php
echo "Changing permissions..."
#change the permissions on the wordpress folder
chown www-data:www-data wordpress
echo "Cleaning up..."
#remove zip file
rm latest.tar.gz
#remove bash script
rm wpinstall.sh
echo "Restarting services..."
service nginx restart
service php5-fpm restart
echo "========================="
echo "Happy blogging!."
echo "========================="
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment