iocage-plugin-wordpress/post_install.sh

132 lines
5.3 KiB
Bash
Executable File

#!/bin/sh
# Enable the service
sysrc -f /etc/rc.conf nginx_enable="YES"
sysrc -f /etc/rc.conf mysql_enable="YES"
sysrc -f /etc/rc.conf php_fpm_enable="YES"
# Install fresh wordpress.conf if user hasn't upgraded
CPCONFIG=0
if [ -e "/usr/local/etc/nginx/conf.d/wordpress.conf" ] ; then
# Confirm the config doesn't have user-changes. Update if not
if [ "$(md5 -q /usr/local/etc/nginx/conf.d/wordpress.conf)" = "$(cat /usr/local/etc/nginx/conf.d/wordpress.conf.checksum)" ] ; then
CPCONFIG=1
fi
else
CPCONFIG=1
fi
# Copy over the nginx config template
if [ "$CPCONFIG" = "1" ] ; then
cp /usr/local/etc/nginx/conf.d/wordpress.conf.template /usr/local/etc/nginx/conf.d/wordpress.conf
md5 -q /usr/local/etc/nginx/conf.d/wordpress.conf > /usr/local/etc/nginx/conf.d/wordpress.conf.checksum
fi
cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
# Modify opcache settings in php.ini according to Wordpress documentation (remove comment and set recommended value)
# https://docs.nextcloud.com/server/15/admin_manual/configuration_server/server_tuning.html#enable-php-opcache
sed -i '' 's/.*opcache.enable=.*/opcache.enable=1/' /usr/local/etc/php.ini
sed -i '' 's/.*opcache.enable_cli=.*/opcache.enable_cli=1/' /usr/local/etc/php.ini
sed -i '' 's/.*opcache.interned_strings_buffer=.*/opcache.interned_strings_buffer=8/' /usr/local/etc/php.ini
sed -i '' 's/.*opcache.max_accelerated_files=.*/opcache.max_accelerated_files=4000/' /usr/local/etc/php.ini
sed -i '' 's/.*opcache.memory_consumption=.*/opcache.memory_consumption=128/' /usr/local/etc/php.ini
sed -i '' 's/.*opcache.save_comments=.*/opcache.save_comments=1/' /usr/local/etc/php.ini
sed -i '' 's/.*opcache.revalidate_freq=.*/opcache.revalidate_freq=60/' /usr/local/etc/php.ini
# recommended value of 512MB for php memory limit (avoid warning when running occ)
sed -i '' 's/.*memory_limit.*/memory_limit=512M/' /usr/local/etc/php.ini
# recommended value of 10 (instead of 5) to avoid timeout
sed -i '' 's/.*pm.max_children.*/pm.max_children=10/' /usr/local/etc/php-fpm.d/wordpress.conf
# Nextcloud wants PATH environment variable set.
echo "env[PATH] = $PATH" >> /usr/local/etc/php-fpm.d/wordpress.conf
# Start the service
service nginx start 2>/dev/null
service php-fpm start 2>/dev/null
service mysql-server start 2>/dev/null
cd /tmp
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
#https://docs.nextcloud.com/server/13/admin_manual/installation/installation_wizard.html do not use the same name for user and db
USER="dbadmin"
DB="wordpress"
WPUSER="wpadmin"
# Save the config values
echo "$DB" > /root/dbname
echo "$USER" > /root/dbuser
echo "$WPUSER" > /root/wpuser
export LC_ALL=C
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1 > /root/dbpassword
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1 > /root/wppassword
PASS=`cat /root/dbpassword`
WPPASS=`cat /root/wppassword`
if [ -e "/root/.mysql_secret" ] ; then
# Mysql > 57 sets a default PW on root
TMPPW=$(cat /root/.mysql_secret | grep -v "^#")
echo "SQL Temp Password: $TMPPW"
# Configure mysql
mysql -u root -p"${TMPPW}" --connect-expired-password <<-EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '${PASS}';
CREATE USER '${USER}'@'localhost' IDENTIFIED BY '${PASS}';
GRANT ALL PRIVILEGES ON *.* TO '${USER}'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON ${DB}.* TO '${USER}'@'localhost';
FLUSH PRIVILEGES;
CREATE DATABASE ${DB};
EOF
else
# Mysql <= 56 does not
# Configure mysql
mysql -u root <<-EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '${PASS}';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.db WHERE Db='test' OR Db='test_%';
CREATE USER '${USER}'@'localhost' IDENTIFIED BY '${PASS}';
GRANT ALL PRIVILEGES ON *.* TO '${USER}'@'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON ${DB}.* TO '${USER}'@'localhost';
FLUSH PRIVILEGES;
CREATE DATABASE ${DB};
EOF
fi
# If on NAT, we need to use the HOST address as the IP
if [ -e "/etc/iocage-env" ] ; then
IOCAGE_PLUGIN_IP=$(cat /etc/iocage-env | grep HOST_ADDRESS= | cut -d '=' -f 2)
echo "Using NAT Address: $IOCAGE_PLUGIN_IP"
fi
# create sessions tmp dir outside nextcloud installation
mkdir -p /usr/local/www/wordpress-sessions-tmp >/dev/null 2>/dev/null
chmod o-rwx /usr/local/www/wordpress-sessions-tmp
chown -R www:www /usr/local/www/wordpress-sessions-tmp
chmod -R o-rwx /usr/local/www/wordpress
#updater needs this
chown -R www:www /usr/local/www/wordpress
#Use wp to complete the Wordpress installation
cd /usr/local/www/wordpress
su -m www -c "wp core config --dbname=\"${DB}\" --dbuser=\"${USER}\" --dbpass=\"${PASS}\""
su -m www -c "wp core install --title=\"${DB}\" --admin_user=\"${WPUSER}\" --admin_password=\"${WPPASS}\" --admin_email=\"ken@blkdoor.com\""
#restart the services to make sure we have pick up the new permission
service php-fpm restart 2>/dev/null
#nginx restarts to fast while php is not fully started yet
sleep 5
service nginx restart 2>/dev/null
echo "Database Name: $DB" > /root/PLUGIN_INFO
echo "Database User: $USER" >> /root/PLUGIN_INFO
echo "Database Password: $PASS" >> /root/PLUGIN_INFO
echo "Wordpress Admin User: $WPUSER" >> /root/PLUGIN_INFO
echo "Wordpress Admin Password: $WPPASS" >> /root/PLUGIN_INFO