Skip to content

Instantly share code, notes, and snippets.

@reitermarkus
Last active April 24, 2019 05:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save reitermarkus/e9b1c9e979e829469fa6 to your computer and use it in GitHub Desktop.
Save reitermarkus/e9b1c9e979e829469fa6 to your computer and use it in GitHub Desktop.
Install WordPress on DigitalOcean CentOS Droplet with PHP 7
#!/bin/sh
DATABASE_NAME='wordpress'
DATABASE_USER='wordpress'
ROOT_MYSQL_PASSWORD=`dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev`
WORDPRESS_MYSQL_PASSWORD=`dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev`
# Write Passwords to File.
echo "Root MySQL Password: $ROOT_MYSQL_PASSWORD" >> /root/passwords.txt
echo "Wordpress MySQL Password: $WORDPRESS_MYSQL_PASSWORD" >> /root/passwords.txt
# Add PHP 7 repos.
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
# Update packages.
yum -y update
# Install packages.
yum -y install httpd
yum -y install mariadb mariadb-server
yum -y install php70w php70w-cli php70w-common php70w-mysql php70w-opcache
# Start Services
systemctl enable firewalld.service
systemctl start firewalld
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
systemctl enable httpd.service
systemctl start httpd
systemctl enable mariadb.service
systemctl start mariadb
# Set up Database User
mysqladmin -u root -h localhost create $DATABASE_NAME
mysqladmin -u root -h localhost password $ROOT_MYSQL_PASSWORD
mysql -uroot -p$ROOT_MYSQL_PASSWORD -e "CREATE USER $DATABASE_USER@localhost IDENTIFIED BY '"$WORDPRESS_MYSQL_PASSWORD"'"
mysql -uroot -p$ROOT_MYSQL_PASSWORD -e "GRANT ALL PRIVILEGES ON $DATABASE_NAME.* TO $DATABASE_USER@localhost"
# Install WordPress
wget https://wordpress.org/latest.tar.gz -O /tmp/wordpress.tar.gz
cd /tmp/ && tar xf wordpress.tar.gz && cp wordpress/wp-config-sample.php wordpress/wp-config.php
sed -i "s/'database_name_here'/'$DATABASE_NAME'/g" /tmp/wordpress/wp-config.php
sed -i "s/'username_here'/'$DATABASE_USER'/g" /tmp/wordpress/wp-config.php
sed -i "s/'password_here'/'$WORDPRESS_MYSQL_PASSWORD'/g" /tmp/wordpress/wp-config.php
for i in $(seq 1 8); do
wp_salt=$(</dev/urandom tr -dc 'a-zA-Z0-9!@#$%^&*()\-_ []{}<>~`+=,.;:/?|' | head -c 64 | sed -e 's/[\/&]/\\&/g')
sed -i "s/put your unique phrase here/$wp_salt/g" /tmp/wordpress/wp-config.php
done
cp -Rf /tmp/wordpress/* /var/www/html/
rm -f /var/www/html/index.html
chown -Rf apache:apache /var/www/html
# Create Swapfile
fallocate -l 512M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile swap swap sw 0 0' >> /etc/fstab
echo 'vm.swappiness = 10' >> /etc/sysctl.conf
echo 'vm.vfs_cache_pressure = 50' >> /etc/sysctl.conf
reboot -h now
@CharlyRipp
Copy link

Greetings! Great yet simple -- I'm doing this on AWS and thought I'd leave a few findings.

I opted to go to PHP7.2 and some dependencies were not on a base CentOS 7 box (at least in AWS)

yum -y install epel-release http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php72
yum -y update
yum -y install wget firewalld httpd \
   mariadb mariadb-server \
   php72 php72-php-cli php72-php-common php72-php-mysql php72-php-opcache

For random password/key generation, I standardized using the dd command and made a small helper function

rando() {
  echo `dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev | sed 's#/#_#g' `
}
ROOT_MYSQL_PASSWORD=`rando`
WORDPRESS_MYSQL_PASSWORD=`rando`

Note the replacement of / with _ as forward slashes were causing issues in sed commands replacing passwords and salts.

This was also used in the salt generation, which your sed command replaces globally, setting all salts to the first generated salt in the for loop. I fixed this and streamlined it:

for i in $(seq 1 8); do
  sed -i "0,/put your unique phrase here/s//`rando`/" /var/www/html/wp-config.php
done

Finally the swapfile - I was having an issue where fallocate wasn't physically allocating the space, causing swapon to fail. Switching to using dd to establish the file seemed to fix it. See https://unix.stackexchange.com/questions/294600/i-cant-enable-swap-space-on-centos-7

dd if=/dev/zero of=/swapfile count=512 bs=1MiB
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile swap swap sw 0 0' >> /etc/fstab
echo 'vm.swappiness = 10'         >> /etc/sysctl.conf
echo 'vm.vfs_cache_pressure = 50' >> /etc/sysctl.conf

I created a fork incase anyone was interested in the full file - https://gist.github.com/CharlyRipp/667addcf7113b162ee99aadf8efc7a01
Tried to piece it back together to closely resemble the original - my end product is more split out to have an ephemeral AMI that spins up, syncs with existing instances, and joins the autoscaling group.

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