Created
May 29, 2018 14:28
-
-
Save flavienbwk/0c5130f7264768050c53c56659781c48 to your computer and use it in GitHub Desktop.
Automatically creates a fresh Wordpress install.
This file contains hidden or 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
#!/bin/bash | |
# Variables | |
log_path="error.log" | |
log_path_custom="install.log" | |
master_domain="mywebchef.org" | |
domain="wp.mywebchef.org" | |
ip_address="xxx.xxx.xxx.xxx" | |
wp_email="[email protected]" | |
wp_user=$(whoami) | |
wp_title="My Web Chef" | |
# Root database password. | |
# Leave empty if empty. | |
db_root_password="" | |
# SSL Certificate information | |
country="FR" | |
state="somewhere" | |
locality="somewhere" | |
organization="MyCompany" | |
organizationalunit="MyCompany" | |
password="testpassword" | |
# Colors | |
RED='\033[0;31m' | |
BLUE='\033[0;34m' | |
GREEN='\033[0;32m' | |
ORANGE='\033[0;33m' | |
NC='\033[0m' | |
# Log functions | |
function setSuccessLog() { | |
log_content="(`date`) : $1" | |
echo $log_content >> "$log_path_custom" | |
echo -e "${GREEN}$log_content${NC}" | |
} | |
function setErrorLog() { | |
log_content="(`date`) : $1" | |
echo $log_content >> "$log_path" | |
echo -e "${RED}$log_content${NC}" | |
} | |
function setLog() { | |
log_content="(`date`) : $1" | |
echo $log_content >> "$log_path_custom" | |
echo -e "${ORANGE}$log_content${NC}" | |
} | |
# === | |
# Beginning the program. | |
# === | |
echo -e "${BLUE}Welcome to myChef.sh, automatical WordPress installer.${NC}" | |
# Removing last execution log files. | |
rm -rf "$log_path" | |
rm -rf "$log_path_custom" | |
touch "$log_path" | |
touch "$log_path_custom" | |
if ! [[ $domain =~ ([a-z0-9|-]+\.)*[a-z0-9|-]+\.[a-z]+ ]] | |
then | |
setErrorLog "The domain name you've specified is not valid." | |
exit | |
fi | |
if [ "$EUID" -ne 0 ] | |
then | |
setErrorLog "Please run this script as root (sudo)." | |
fi | |
setLog "Updating..." | |
apt-get update >> "${log_path_custom}" 2> "${log_path}" | |
setSuccessLog "OK." | |
if grep -Fxq "deb https://packages.sury.org/php/ $(lsb_release -sc) main" "/etc/apt/sources.list.d/php.list" | |
then | |
setLog "deb package already in sources.list, skipping." | |
else | |
setLog "Installing deb for PHP 7.2." | |
apt-get install apt-transport-https lsb-release ca-certificates -y --force-yes >> "${log_path_custom}" 2> "${log_path}" | |
wget --no-check-certificate -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg | |
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list | |
apt-get update >> "${log_path_custom}" 2> "${log_path}" | |
fi | |
apt install apache2 -y >> "${log_path_custom}" 2> "${log_path}" | |
if [ $(dpkg-query -W -f='${Status}' apache2 2>/dev/null | grep -c "ok installed") -eq 0 ] | |
then | |
setErrorLog "Failed to install apache2." | |
exit | |
else | |
a2enmod rewrite | |
service apache2 restart | |
fi | |
if ! [ -x "$(command -v php)" ] | |
then | |
setLog "PHP not installed, installing the PHP 7.2." | |
apt install php7.2 -y >> "${log_path_custom}" 2> "${log_path}" | |
apt install php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-gd php7.2-xml php7.2-mysql php7.2-cli -y >> "${log_path_custom}" 2> "${log_path}" | |
if [ -x "$(command -v php)" ] | |
then | |
apt-get -y install libapache2-mod-php7.2 | |
fi | |
fi | |
if ! [ -x "$(command -v php)" ] | |
then | |
setErrorLog "Failed to install PHP 7.2." | |
setLog "Installing PHP 5.6..." | |
apt-get install apt-transport-https lsb-release ca-certificates -y >> "${log_path_custom}" 2> "${log_path}" | |
apt-get -y install php5 libapache2-mod-php5 >> "${log_path_custom}" 2> "${log_path}" | |
service apache2 restart | |
apt-get -y install php5-mysqlnd php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl >> "${log_path_custom}" 2> "${log_path}" | |
if [ $(dpkg-query -W -f='${Status}' php 2>/dev/null | grep -c "ok installed") -eq 0 ] | |
then | |
setErrorLog "Failed to install PHP 5.6." | |
exit | |
fi | |
fi | |
apt install mysql-client -y >> "${log_path_custom}" 2> "${log_path}" | |
apt install mysql-server -y >> "${log_path_custom}" 2> "${log_path}" | |
if [ $(dpkg-query -W -f='${Status}' mysql-client 2>/dev/null | grep -c "ok installed") -eq 0 ] | |
then | |
setErrorLog "Failed to install mysql-client." | |
exit | |
fi | |
if ! [ -x "$(command -v wp --info)" ] | |
then | |
setLog "Installing WP-CLI for an easier configuration..." | |
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar | |
if ! [ -x "$(command -v php wp-cli.phar --info)" ] | |
then | |
setErrorLog "Failed to download a valid wp-cli.phar." | |
exit | |
fi | |
chmod +x wp-cli.phar | |
mv wp-cli.phar /usr/local/bin/wp | |
if ! [ -x "$(command -v wp --info)" ] | |
then | |
setErrorLog "Failed to install WP-CLI." | |
exit | |
fi | |
setSuccessLog "OK." | |
fi | |
if ! [ -e "/var/www" ] | |
then | |
setLog "Creating /var/www directory." | |
mkdir "/var/www" >> "${log_path_custom}" 2> "${log_path}" | |
fi | |
# Checking if the file is downloaded. | |
if [ ! -e "/tmp/latest.tar.gz" ] | |
then | |
wget http://wordpress.org/latest.tar.gz | |
if [ ! -e "latest.tar.gz" ] | |
then | |
setErrorLog "Impossible to download the WordPress file." | |
exit | |
else | |
mv latest.tar.gz /tmp | |
fi | |
else | |
setLog "Using WordPress from temporary files." | |
fi | |
if [ ! -e "/tmp/wordpress" ] | |
then | |
setLog "Extracting..." | |
tar -zxf "/tmp/latest.tar.gz" --directory "/tmp" >> "${log_path_custom}" 2> "${log_path}" | |
fi | |
if [ ! -e "/tmp/wordpress" ] | |
then | |
setErrorLog "Impossible to extract the WordPress files." | |
exit | |
else | |
#seed=$( date +%T ) | |
seed=$domain | |
fi | |
wordpress_install_path="/var/www/wp_$seed" | |
if [ -e "$wordpress_install_path" ] | |
then | |
setLog "Updating WordPress..." | |
wp core update --path="$wordpress_install_path" --allow-root | |
if (($? > 0)); then | |
setErrorLog "Update failed to install." | |
exit | |
else | |
setSuccessLog "OK. Success." | |
exit | |
fi | |
fi | |
setLog "Moving inside ${wordpress_install_path}..." | |
mv "/tmp/wordpress" "$wordpress_install_path" | |
if [ ! -e "$wordpress_install_path" ] | |
then | |
setErrorLog "Impossible to move the WordPress files." | |
exit | |
else | |
setSuccessLog "Moved." | |
fi | |
setLog "Attributing rights... " | |
chown -R www-data:www-data /var/www | |
chmod -R 775 $wordpress_install_path | |
setSuccessLog "OK." | |
config_file="wp.mywebchef.org.conf" | |
config_path="/etc/apache2/sites-available" | |
if [ ! -e "$config_file/$config_file" ] | |
then | |
echo -e "\ | |
<VirtualHost *:80>\n \ | |
ServerName $domain\n \ | |
DocumentRoot ${wordpress_install_path}\n \ | |
Redirect / https://$domain\n \ | |
ErrorLog /var/log/apache2/error-wordpress.log\n \ | |
CustomLog /var/log/apache2/custom-wordpress.log combined\n \ | |
</VirtualHost>\n \ | |
<VirtualHost *:443>\n \ | |
SSLEngine On\n \ | |
SSLCertificateFile /etc/ssl/private/${domain}.crt\n \ | |
SSLCertificateKeyFile /etc/ssl/private/${domain}.key\n \ | |
SSLCACertificateFile /etc/ssl/private/${domain}.crt\n \ | |
ServerName $domain\n \ | |
ServerAdmin webmaster@$domain\n \ | |
DocumentRoot ${wordpress_install_path}\n \ | |
ErrorLog /var/log/apache2/error-wordpress.log\n \ | |
CustomLog /var/log/apache2/custom-wordpress.log combined\n \ | |
</VirtualHost>\n" > "$config_path/$config_file" | |
setLog "Disabling default apache configuration file." | |
rm "/etc/apache2/sites-enabled/000-default.conf" | |
else | |
setLog "Skipping apache2 configuration file : already exists." | |
fi | |
ln -s "/etc/apache2/sites-available/$config_file" "/etc/apache2/sites-enabled" >> "${log_path_custom}" 2> "${log_path}" | |
if [ ! -L "/etc/apache2/sites-enabled/$config_file" ] | |
then | |
setErrorLog "Impossible to establish the symbolic link (/etc/apache2/sites-enabled/$config_file)." | |
exit | |
else | |
setLog "Successfuly established the symbolic link." | |
fi | |
apt install pwgen -y >> "${log_path_custom}" 2> "${log_path}" | |
wp_db_name="${seed//./_}" | |
wp_db_user="${seed//./_}""_user" | |
wp_db_password=$(pwgen 8 1) | |
if [ -d "/var/lib/mysql/$seed" ] | |
then | |
setLog "Database exists. Skipping configuration." | |
else | |
setLog "Configuring database..." | |
if ! [ -z $db_root_password ] | |
then | |
db_root_password="-p'${db_root_password}'" | |
fi | |
(mysql -u root "${db_root_password}" --execute="CREATE DATABASE ${seed//./_};") | |
(mysql -u root "${db_root_password}" --execute="DROP USER '${wp_db_user}'@'localhost';") | |
(mysql -u root "${db_root_password}" --execute="FLUSH PRIVILEGES;") | |
(mysql -u root "${db_root_password}" --execute="CREATE USER '${wp_db_user}'@'localhost' IDENTIFIED BY '${wp_db_password}';") | |
(mysql -u root "${db_root_password}" --execute="GRANT ALL PRIVILEGES ON * . * TO '${wp_db_user}'@'localhost';") | |
(mysql -u root "${db_root_password}" --execute="FLUSH PRIVILEGES;") | |
setLog "Changing wp-config.php..." | |
cp "$wordpress_install_path/wp-config-sample.php" "$wordpress_install_path/wp-config.php" | |
if [ -e "$wordpress_install_path/wp-config.php" ] | |
then | |
sed -i -re "s/database_name_here/$wp_db_name/g" "$wordpress_install_path/wp-config.php" | |
sed -i -re "s/username_here/$wp_db_user/g" "$wordpress_install_path/wp-config.php" | |
sed -i -re "s/password_here/$wp_db_password/g" "$wordpress_install_path/wp-config.php" | |
sed -i -re "s/put your unique phrase here/$(pwgen 16 1)/g" "$wordpress_install_path/wp-config.php" | |
echo "define('COOKIE_DOMAIN', $_SERVER['HTTP_HOST'] );" >> "$wordpress_install_path/wp-config.php" | |
else | |
setErrorLog "Impossible to copy the wp-config.php file." | |
setErrorLog "Removing database and database users created." | |
mysql -u root"${db_root_password}" --execute="DROP DATABASE $seed;" | |
exit | |
fi | |
setSuccessLog "OK." | |
fi | |
setLog "Installing website..." | |
wp core install --allow-root --path="$wordpress_install_path" --url="$domain" --title="$wp_title" --admin_user="$wp_user" --admin_password="$wp_db_password" --admin_email="$wp_email" | |
if (($? > 0)); then | |
setErrorLog "Failed to install WordPress." | |
exit | |
fi | |
setSuccessLog "OK." | |
setLog "Preparing SSL certificates..." | |
openssl req -new -nodes -sha256 -newkey rsa:2048 -keyout "/etc/ssl/private/${domain}.key" -out "/etc/ssl/private/${domain}.csr" -subj "/emailAddress=$wp_email/CN=$domain/O=$organization/OU=$organizationalunit/C=$country/ST=$state/L=$locality" >> "${log_path_custom}" 2> "${log_path}" | |
if [[ -e "/etc/ssl/private/${domain}.csr" ]] | |
then | |
setLog "Creating self signed certificate..." | |
openssl x509 -req -days 365 -in "/etc/ssl/private/${domain}.csr" -signkey "/etc/ssl/private/${domain}.key" \ | |
-out "/etc/ssl/private/${domain}.crt" >> "${log_path_custom}" 2> "${log_path}" | |
else | |
setErrorLog "Failed SSL configuration." | |
exit | |
fi | |
a2enmod ssl | |
a2ensite "$config_file" | |
service apache2 restart | |
if (($? > 0)); then | |
setErrorLog "Failed to install SSL configuration for WordPress." | |
exit | |
fi | |
setSuccessLog "OK." | |
setSuccessLog "[WordPress credentials] For user $wp_user, password is $wp_db_password" | |
if false; then | |
setLog "Installing AWStats for monitoring your server..." | |
apt-get install awstats -y >> "${log_path_custom}" 2> "${log_path}" | |
if ! [ -x "$(command -v awstats)" ] | |
then | |
setErrorLog "Failed to install awstats." | |
exit | |
fi | |
awstats_config_file="awstats.${master_domain}.conf" | |
awstats_config_file_domain="awstats.${domain}.conf" | |
awstats_config_path="/etc/awstats/${awstats_config_file}" | |
awstats_config_path_domain="/etc/awstats/${awstats_config_file_domain}" | |
cp "/etc/awstats/awstats.conf" "$awstats_config_path" | |
if ! [[ -e "$awstats_config_path" ]] | |
then | |
setErrorLog "Impossible to copy your awstats config file for ${master_domain}" | |
exit | |
fi | |
sed -i -re "s/SiteDomain=\"\"/${master_domain}/g" "$awstats_config_path" | |
sed -i -re "s/LogFile=\"/var/log/apache2/access.log\"//var/log/apache2/${master_domain}-access.log/g" "$awstats_config_path" | |
sed -i -re "s/HostAliases=\"localhost 127.0.0.1\"/HostAliases=\"${ip_address} ${master_domain}\"/g" "$awstats_config_path" | |
/usr/lib/cgi-bin/awstats.pl -config=${master_domain} -update | |
if (($? > 0)); then | |
setErrorLog "Failed to update the awstats configuration." | |
exit | |
fi | |
setLog "Processing apache2 configuration..." | |
if grep -Fxq "Alias /awstats-icon/ /usr/share/awstats/icon/" ${master_domain} | |
then | |
setLog "Config already set. Skipping." | |
else | |
setLog "Adding AWSTATS access configuration to apache2..." | |
echo -e "Alias /awstats-icon/ /usr/share/awstats/icon/\n \ | |
<Directory /usr/share/awstats/icon>\n \ | |
Options None\n \ | |
AllowOverride None\n \ | |
Order allow,deny\n \ | |
Allow from all\n \ | |
</Directory>\n" >> "/etc/apache2/apache2.conf" | |
service apache2 restart | |
if (($? > 0)); then | |
setErrorLog "Failed to initialize apache2 configuration for AWSTATS." | |
exit | |
fi | |
setSuccessLog "AWSTATS configuration for apache2 successfuly installed." | |
fi | |
setLog "Processing seed configuration." | |
cp "/etc/awstats/awstats.conf" "$awstats_config_path_domain" | |
if ! [[ -e "$awstats_config_path_domain" ]] | |
then | |
setErrorLog "Impossible to copy your awstats config file for ${domain}" | |
exit | |
fi | |
sed -i -re "s/SiteDomain=\"\"/${master_domain}/g" "$awstats_config_path_domain" | |
sed -i -re "s/LogFile=\"/var/log/apache2/access.log\"//var/log/apache2/custom-wordpress.log/g" "$awstats_config_path_domain" | |
sed -i -re "s/HostAliases=\"localhost 127.0.0.1\"/HostAliases=\"${ip_address} ${domain}\"/g" "$awstats_config_path_domain" | |
setLog "Processing cron configuration for awstats..." | |
if grep -Fxq "0,10,20,30,40,50 * * * * root /bin/sh /root/awstats.sh" "/etc/cron.d/awstats" | |
then | |
setLog "Config already set for /etc/cron.d/awstats. Skipping." | |
else | |
echo "0,10,20,30,40,50 * * * * root /bin/sh /root/awstats.sh" >> "/etc/cron.d/awstats" | |
fi | |
setLog "Processing sh cron configuration for awstats..." | |
touch "/root/awstats.sh" | |
if grep -Fxq "perl /usr/lib/cgi-bin/awstats.pl -config=${domain}" "/root/awstats.sh" | |
then | |
setLog "Config already set for /etc/cron.d/awstats. Skipping." | |
else | |
echo "perl /usr/lib/cgi-bin/awstats.pl -config=${domain} -update > /dev/null" >> "/root/awstats.sh" | |
chmod +x "/root/awstats.sh" | |
fi | |
setSuccessLog "OK." | |
else | |
setLog "Skipping awstats configuration." | |
fi | |
status_domain="server.${master_domain}" | |
status_path="${config_path}/${status_domain}" | |
setLog "Configuring status-info pages @ ${status_domain}..." | |
a2enmod status | |
a2enmod info | |
service apache2 restart | |
mkdir "/var/www/${status_domain}" >> "${log_path_custom}" 2> "${log_path}" | |
echo -e "\ | |
<VirtualHost *:80>\n \ | |
ServerName $status_domain\n \ | |
ServerAdmin webmaster@$status_domain\n \ | |
DocumentRoot /var/www/${status_domain}\n \ | |
ErrorLog /var/log/apache2/${status_domain}-error.log\n \ | |
CustomLog /var/log/apache2/${status_domain}-access.log combined\n \ | |
</VirtualHost>\n" > "${status_path}.conf" | |
if ! [[ -e "${status_path}.conf" ]] | |
then | |
setErrorLog "Status-info path on apache2 configuration failed." | |
exit | |
fi | |
echo -e "<Location /status>\n \ | |
SetHandler server-status\n \ | |
</Location>\n" >> "${status_path}.conf" | |
echo -e "<Location /info>\n \ | |
SetHandler server-info\n \ | |
</Location>\n" >> "${status_path}.conf" | |
ln -s "${status_path}.conf" "/etc/apache2/sites-enabled/" >> "${log_path_custom}" 2> "${log_path}" | |
if ! [[ -e "/etc/apache2/sites-enabled/${status_domain}.conf" ]] | |
then | |
setErrorLog "Status-info syslink on apache2 configuration failed." | |
exit | |
fi | |
service apache2 restart | |
setSuccessLog "Succesfuly installed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment