Skip to content

Instantly share code, notes, and snippets.

@cballou
Last active November 5, 2018 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cballou/c3219856b5aab455264a to your computer and use it in GitHub Desktop.
Save cballou/c3219856b5aab455264a to your computer and use it in GitHub Desktop.
Handle a near auto-installation of Wordpress on Ubuntu 14.04.
#!/bin/bash
# MySQL database defaults
dbname="wordpress"
dbuser="wordpress"
# Gather user info
read -r -p "Enter your domain, e.g. mywebsite.co (no http://, no www.): " wpURL
read -r -p "Enter a secure MySQL root password to use (save this somewhere): " rootpass
read -r -p "Enter a secure MySQL WordPress password to use: " userpass
# Update the machine
apt-get update
apt-get upgrade -y
# Ensure MySQL installation doesn't ask for input
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password ${rootpass}"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password ${rootpass}"
# Install LAMP and WP requirements
apt-get install -y lamp-server^
apt-get install -y \
curl \
php-apc \
php-pear \
php5-cli \
php5-curl \
php5-dev \
php5-gd \
php5-imagick \
php5-imap \
php5-mcrypt \
php5-mysqlnd \
php5-pspell \
php5-tidy \
php5-xmlrpc \
php5enmod \
apcu \
curl \
gd \
imagick \
imap \
mcrypt \
mysqlnd \
pspell \
tidy \
xmlrpc
# Create our Wordpress database
echo "CREATE DATABASE $dbname default character set utf8;" | mysql -u root -p$rootpass
echo "CREATE USER '$dbuser'@'localhost' IDENTIFIED BY '$userpass';" | mysql -u root -p$rootpass
echo "GRANT ALL PRIVILEGES ON $dbname.* TO '$dbuser'@'localhost';" | mysql -u root -p$rootpass
echo "FLUSH PRIVILEGES;" | mysql -u root -p$rootpass
echo "New MySQL database is successfully created"
# Ensure we have certain apache modules loaded
a2enmod expires headers rewrite
# Download, unpack and configure WordPress
wget -q -O - "http://wordpress.org/latest.tar.gz" | tar -xzf - -C /var/www
chown www-data: -R /var/www/wordpress && cd /var/www/wordpress
cp wp-config-sample.php wp-config.php
chmod 640 wp-config.php
mkdir uploads
sed -i "s/database_name_here/$dbname/;s/username_here/$dbuser/;s/password_here/$userpass/" wp-config.php
# Update the default hashes and salts
SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
STRING='put your unique phrase here'
printf '%s\n' "g/$STRING/d" a "$SALT" . w | ed -s /var/www/wordpress/wp-config.php
# Set proper permissions of wordpress
cd /var/www/wordpress
chown www-data:www-data -R *
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;
# Create our Apache virtual host
echo "
<VirtualHost *:80>
ServerName $wpURL
ServerAlias www.$wpURL
DocumentRoot /var/www/wordpress
DirectoryIndex index.php
<Directory "/var/www/wordpress">
Options FollowSymLinks
AllowOverride All
</Directory>
ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
" > /etc/apache2/sites-available/wordpress.conf
# create a default .htaccess file
echo "
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
" > /var/www/wordpress/.htaccess
# Enable the site
a2dissite 000-default
a2ensite wordpress
# Turn off some of the unnecessary response headers.
sed -i "s/^ServerTokens\s.*/ServerTokens Prod/" /etc/apache2/conf-available/security.conf
sed -i "s/^ServerSignature\s.*/ServerSignature Off/" /etc/apache2/conf-available/security.conf
# Turn off the expose_php setting.
sed -i "s/expose_php =.*/expose_php = Off/" /etc/php5/apache2/php.ini
# Restart Apache
service apache2 restart
# Install and configure monit to keep the site alive
apt-get install -y monit
cat > /etc/monit/conf.d/apache2 <<EOF
check process apache2 with pidfile /var/run/apache2.pid
group www
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
if failed host localhost port 80 protocol http
with timeout 10 seconds
then restart
if failed host localhost port 443 type tcpssl protocol http
with timeout 10 seconds
then restart
if 5 restarts within 5 cycles then timeout
EOF
cat > /etc/monit/conf.d/mysql <<EOF
check process mysqld with pidfile /var/run/mysqld/mysqld.pid
group database
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"
if failed host localhost port 3306 protocol mysql then restart
if 5 restarts within 5 cycles then timeout
EOF
# restart monit with our new configuration settings
service monit restart
# Output
WPVER=$(grep "wp_version = " /var/www/wordpress/wp-includes/version.php |awk -F\' '{print $2}')
echo -e "\nWordPress version $WPVER is successfully installed!"
echo -en "\aPlease visit http://www.$wpURL to complete your installation\n"
# Cleanup our history (dont show pws)
history -cw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment