Skip to content

Instantly share code, notes, and snippets.

@dncpax
Last active August 12, 2020 11:56
Show Gist options
  • Save dncpax/0b34f7dc7136b1f7ce08d518098098d3 to your computer and use it in GitHub Desktop.
Save dncpax/0b34f7dc7136b1f7ce08d518098098d3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Functions
ok() { echo -e '\e[32m'$domain'\e[m'; } # Green
die() { echo -e '\e[1;31m'$domain'\e[m'; exit 1; }
# Sanity check
[ $(id -g) != "0" ] && die "Script must be run as root."
# Connfigure WordPress
# Site Variables
echo "Site URL (ie, www.youraddress.com):"
read -e domain
export domain
echo 'Database Name: '
read -e dbname
export dbname
echo 'Database User: '
read -e dbuser
export dbuser
echo 'Database Password: '
read -e dbpass
export dbpass
echo 'Database Host: '
read -e dbhost
export dbhost
#Checking packages
echo -e "Checking packages...${NC}"
echo -e "List of required packeges: nano, zip, unzip, mc, apache2 & php, mysql, php curl, wget, curl"
read -r -p "Do you want to check packeges? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
apt install --yes nano
apt install --yes zip
apt install --yes unzip
apt install --yes wget
apt install --yes curl
apt install --yes nginx
apt install --yes php-fpm php-mysql php-curl
apt install --yes mysql-server
;;
*)
echo -e "Packeges check is ignored!
Please be aware, that apache2, mysql, phpmyadmin and other software may not be installed!
${NC}"
;;
esac
#criacao e setup da base de dados
# Path to MySQL
MYSQL='/usr/bin/mysql'
# Setup DB & DB User
#Isto pede a pwd 2 x!! como evitar?
#$MYSQL -h$dbhost -P3306 -uroot -e"UPDATE mysql.user SET password = PASSWORD('$dbpass') WHERE user = 'root';"
$MYSQL -uroot -p$dbpass -e "CREATE USER '$dbuser'@'$dbhost' IDENTIFIED BY '$dbpass';"
$MYSQL -uroot -p$dbpass -e "CREATE DATABASE IF NOT EXISTS $dbname; GRANT ALL ON $dbname.* TO '$dbuser'@'$dbhost'; FLUSH PRIVILEGES; "
#Config do Nginx
#read -p 'Enter domain name: ' domain
NGINX_AVAILABLE='/etc/nginx/sites-available'
NGINX_ENABLED='/etc/nginx/sites-enabled'
WEB_DIR='/var/www'
WEB_USER='www-data:www-data'
# 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;
## 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;
}
# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ \.php$ {
# Zero-day exploit defense.
# http://forum.nginx.org/read.php?2,88845,page=3
# Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
# Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine. And then cross your fingers that you won't get hacked.
try_files $uri =404;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#para evitar mudar o fix_pathinfo: https://security.stackexchange.com/a/177370
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_pass php;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
EOF
#Instalacao propriamente do wp
# Configure directory
cd $WEB_DIR
mkdir $domain
cd $domain
# Connfigure WordPress
echo 'Database Name: ' $dbname
echo 'Database User: ' $dbuser
echo 'Database Password: ' $dbpass
echo 'Database Host: ' $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
@dncpax
Copy link
Author

dncpax commented Aug 7, 2020

Install wordpress from scratch, with nginx and mysql. Includes db creation and config, and config of nginx. It's a mix&match of several scripts found online...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment