Automated Scripted Install of WordPress on CentOS/RHEL Linux

wordpress-4

Many WordPress users have enjoyed WordPress using an Internet host provider or on an XAMPP installation on a Windows installation. This exercise is a combination of several guides with some personal tweaks to install WordPress 3.5.1 on a fresh install of of CentOS 5/6 or Red Hat Enterprise Linux (RHEL) 5/6 using PHP 5.2.4+.

Install Packages

Using yum install MySQL, Apache, and PHP. Then make Apache and MySQL autostart, and start the two processes.

CentOS 5

yum -y install mysql-server httpd php php-mysql
rpm -Uvh http://repo.webtatic.com/yum/centos/5/latest.rpm
yum --enablerepo=webtatic update php -y
chkconfig mysqld on && service mysqld start

CentOS 6

yum -y install mysql-server httpd php php-mysql
rpm -Uvh http://repo.webtatic.com/yum/el6/latest.rpm
yum update php -y
chkconfig mysqld on && service mysqld start

Create a Database for WordPress with a MySQL User
Substitute password with your unique password. Consider changing the database name and database user from WordPress to unique names.

echo 'CREATE DATABASE wordpress;' | mysql
echo "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost' IDENTIFIED BY 'password'" | mysql
echo "FLUSH PRIVILEGES;" | mysql

WordPress
Download, install, remove temporary directories, and configure Apache cache.

cd /var/www/html
curl -O https://wordpress.org/latest.tar.gz
tar -C --strip-components=1 -zxvf latest.tar.gz
/bin/rm latest.tar.gz
mkdir wp-content/uploads wp-content/cache
chown apache:apache wp-content/uploads wp-content/cache

Configure WordPress
Copy (cp) the sample configuration to the default configuration. Substitute the word password with your unique password.

cp wp-config-sample.php  wp-config.php
sed -ie 's@database_name_here@wordpress@' wp-config.php
sed -ie 's@username_here@wordpress@' wp-config.php
sed -ie 's@password_here@password@' wp-config.php
curl https://api.wordpress.org/secret-key/1.1/salt/ >> wp-config.php

Configure Apache

echo "# BEGIN WordPress" >> .htaccess
echo "<IfModule mod_rewrite.c>" >> .htaccess
echo "RewriteEngine On" >> .htaccess
echo "RewriteBase /" >> .htaccess
echo "RewriteRule ^index\.php$ - [L]" >> .htaccess
echo "RewriteCond %{REQUEST_FILENAME} !-f" >> .htaccess
echo "RewriteCond %{REQUEST_FILENAME} !-d" >> .htaccess
echo "RewriteRule . /index.php [L]" >> .htaccess
echo "</IfModule>" >> .htaccess
echo "# END WordPress" >> .htaccess
chmod 666 .htaccess
chkconfig httpd on && service httpd start

Automated Process
Create a bash script to do all of the above tasks. Should only take a couple of minutes to get WordPress installed and running. Don’t forget to change the username (optional/recommended), database name (optional/recommended), and password (highly recommended). Copy and paste the code below into a file, for example, install-wordpress.sh. use chmod +x install-wordpress.sh to make it executable. Then run ./install-wordpress.sh

#!/bin/bash
# install-wordpress.sh
#
# Description: This script will download, configure and install WordPress for CentOS/RHEL 6.x Linux.
#

yum -y install mysql-server httpd php php-mysql
chkconfig mysqld on && service mysqld start
echo 'CREATE DATABASE wordpress;' | mysql
echo "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'localhost' IDENTIFIED BY 'password';" | mysql
echo "FLUSH PRIVILEGES;" | MySQL
cd /var/www/html
curl -O https://wordpress.org/latest.tar.gz
tar -C --strip-components=1 -zxvf latest.tar.gz
/bin/rm latest.tar.gz
mkdir wp-content/uploads wp-content/cache
chown apache:apache wp-content/uploads wp-content/cache
cp wp-config-sample.php wp-config.php
sed -ie 's@database_name_here@wordpress@' wp-config.php
sed -ie 's@username_here@wordpress@' wp-config.php
sed -ie 's@password_here@password@' wp-config.php
curl https://api.wordpress.org/secret-key/1.1/salt/ >> wp-config.php
echo "# BEGIN WordPress" >> .htaccess
echo "<IfModule mod_rewrite.c>" >> .htaccess
echo "RewriteEngine On" >> .htaccess
echo "RewriteBase /" >> .htaccess
echo "RewriteRule ^index\.php$ - [L]" >> .htaccess
echo "RewriteCond %{REQUEST_FILENAME} !-f" >> .htaccess
echo "RewriteCond %{REQUEST_FILENAME} !-d" >> .htaccess
echo "RewriteRule . /index.php [L]" >> .htaccess
echo "</IfModule>" >> .htaccess
echo "# END WordPress" >> .htaccess
chmod 666 /var/www/html/.htaccess
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
service iptables save
sed -i "/^<Directory \"\/var\/www\/html\">/,/^<\/Directory>/{s/AllowOverride None/AllowOverride All/g}" /etc/httpd/conf/httpd.conf
chkconfig httpd on && service httpd start

The automatic writing of the .htaccess and the chmod 666 may be redundant, as WordPress will create the .htaccess entries when making changes to the permalink settings. The chmod 666 ensures that WordPress can write to the .htaccess file. This example does both. Permalinks are handled with the sed command to edit /etc/httpd/conf/httpd.conf

Sources