wordpress-3.png

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 on a fresh install of Linux. More specifically an install of CentOS or Red Hat Enterprise Linux (RHEL).

March 9, 2013: This process was originally written two years ago. The WordPress minimum requirements for PHP has changed and has now been taken into account. The script has been developed further, tweaked, and simplified.

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

Beginning with WordPress 3.2, the minimum requirements for PHP changed to version 5.2.4 or greater. The CentOS 5.x repositories as of March 9, 2013 do not go beyond 5.1.x. The extra step of enabling an additional repository is added to update PHP version beyond the minimum requirement. This script has successfully been tested with WordPress 3.5.1 on CentOS 5.8.
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
The CentOS 6.x repositories use a different link and a slight variation to the syntax.
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
http://blog.adlibre.org/technology/how-to-install-wordpress-on-centos-5-in-five-minutes-flat.html
http://blog.fantasticmf.com/2011/01/28/install-wordpress-on-centosrhel-in-five-minutes-flat-remix/
http://www.e-romagnoli.com/2008/07/wordpress-permalink-on-centos-5-tutorial/
http://www.webhostingtalk.com/showthread.php?t=1178639
http://www.webtatic.com/projects/yum-repository/
http://unix.stackexchange.com/questions/77549/replacing-text-between-two-html-comments