Over the weekend we migrated our WordPress blog to CentOS 7 from 6.x. The process was pretty straightforward and this brief tutorial is going to show you what we did so you can avoid the pitfalls when migrating your own sites.
We didn’t intend to migrate to CentOS 7 so soon. We had been running version 6.x and weren’t running into any problems. CentOS 6.x is a very stable OS and I don’t see a lot of webmasters migrating to 7 anytime soon.
The main reasons we migrated were unrelated to CentOS 6. We migrated because we made a previous mistake when we initially provisioned resources for our webserver.
We wrote few weeks ago that our blog was running on Google Compute Engine (Cloud Infrastructure). GCE allows anyone to run powerful websites on their virtual instances. It’s great and you only pay for the services you use. Using GCE we cut our hosting bills by half and improve our website speed greatly.
The way we setup our instance at GCE wasn’t optimal and once you setup an instance with image installed, you can’t just go back and undo all the changes you made. We couldn’t do that, so we decided to create a separate instance from scratch and rebuild our VM and that’s why we migrated to CentOS 7.
Below are the things we did and you’re welcome to use and apply in your own environments.
- Preparing the Virtual Instance
We chose to go with the single CUP unit with 3.8GB RAM instance. This seems the be the right fit for our blog. For the image, we selected CentOS 7 on a SSD Persistent disk (10GB).
Then we added additional 250GB storage to store your blog content. The 250GB was partitioned and formatted with Ext4 filesystem into two disks.
- 100GB will be mounted at /var/www and will store our WordPress content (Images, videos and other data)
- 50GB will by mounted at /var/lib/mysql and will store our WordPress database content (links, references and other data)
Storing your WordPress critical data onto a separate drive that can be snapshot is cool. In this way when your server crashes, you will be able to mount the drive onto a new machine and keep blogging.
You can use fdisk to create new partitions. Then use mkfs.ext4 to create Ext4 filesystem on the drive.
Here’s how our drives are mounted in the /etc/fstab file.
UUID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1 /var/www ext4 defaults 0 0
UUID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2 /var/lib/mysql ext4 defaults 0 0
Next, setup your server timezone. To do that, create a symbolic link to the localtime file in the /etc/ directory.
sudo mv /etc/localtime /etc/localtime.bak
sudo ln -s /usr/share/zoneinfo/US/Central /etc/localtime
Continue below to install new repositories
- Installing Additional Repositories
By default when you installed CentOS 7, you won’t have access to all sort of important packages. To get them, you must install trusted third-party repositories like Epel Releases and Remi.
Installing these additional repositories will give you access to great packages maintained by the community. To install the, run the commands below.
- Installing Epel Release (latest version)
sudo yum install epel-release
- Installing Remi
cd /tmp/ && wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
sudo rpm -Uvh remi-release-7*.rpm
- Installing Nginx Webserver
As you probably already know, we use Nginx webserver here. We switched to Nginx from Apache2 over a year ago and we’re impressed with our site performance.
If you use Apache2, in your environment, then go and install it. After installing the webserver, CentOS 7 will automatically create a root directory on the /var/www mount point.
To install the latest version of Nginx, add its repository to CentOS 7
sudo vi /etc/yum.repos.d/nginx.repo
Then add the below repository content in the file and save it.
Next, run the commands below to install Nginx
sudo yum -y install nginx
Next, enable Nginx to always start up everytime your system reboots
sudo systemctl enable nginx
sudo systemctl start nginx
- Installing MariaDB Database server
The next item to install and configure is the database server. We were running MariaDB instead of MySQL. If you’re doing it the other way around, then continue and install the database server for your site.
To install MariaDB server (the latest), run the commands below to add its repository.
sudo vi /etc/yum.repos.d/MariaDB.repo
Then add the block below into the file and save it.
# MariaDB 10.0 CentOS repository list - created 2014-11-03 19:01 UTC
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos7-amd64
After that, run the commands below to install MariaDB database server
sudo yum install MariaDB-server MariaDB-client
If you get an error that there’s a conflict with package mariadb-libs-1:5.5.37-1.el7_0.x86_64 package, run the commands below to remove it.
sudo yum remove mariadb-libs-1:5*
The commands above also removes postfix package. After that, go and rerun the above commands.
Next, run the commands below to allow the database server to always start up everytime you restart or your system reboots.
sudo systemctl enable mariadb
sudo systemctl start mariadb or /etc/init.d/mysql start
Next, run the commands below to secure the database server.
Next, choose Yes for the rest of the prompts until you’re done.
- Enter current password for root (enter for none): press Enter
- Set root password? Y
- New password: Type new root password
- Re-enter new password: Confirm the password
- Remove anonymous users? Y
- Disallow root login remotely? Y
- Remove test database and access to it? Y
- Reload privilege tables now? Y
- Installing PHP And PHP Modules
The next step is to install PHP package and other modules. To do that, run the commands below. Sometime you’ll run into conflict issues with MariaDB 10… If that’s the case, install the PHP stuff first, then remove the mariadb-lib package and the install MariaDB.
sudo yum install php php-fpm php-mbstring php-mysql php-soap php-gd php-xml php-xmlrpc php-cli php-curl php-pear php-dev php-common php-cgi php-imap php-mcrypt php-opcache php-pspell
Next, run the commands below to always start up php-fpm after your system reboots
sudo systemctl enable php-fpm
sudo systemctl start php-fpm
- Installing Memcached
We also use memcached on this blog. To install memcached, run the commands below
sudo yum install memcached libmemcached-devel
Then open Memcached config file and make the changes to look like the one below.
sudo vi /etc/sysconfig/memcached
Add the lines below
Save the file and close out.
- Allow HTTP/HTTPS through the Firewall
To complete the server configuration run the commands below to allow HTTP/HTTPS traffic through the firewall.
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
With memcached installed, you may also want to enable it through SELinux. Run the commands below to make sure Memcached isn’t blocked via SELinux.
setsebool -P httpd_can_network_memcache 1
Restart your webserver or the machine. At this point, you should have your server setup.
With everytime setup, all I had to do was to create a backup of our live site content, including the database. To backup the website content, run the commands below.
sudo tar -cvf webcontent.tar /var/www/html/
This will create a webcontent.tar zipped file. You’ll then copy over this to the new server and extract it.
Do the same the the database.
sudo mysqldump -u root -p database_name > database_name.sql
Copy the database content to the new server and import it.
I also backup the /etc/nginx, /etc/php-fpm.d/, and other directories and copied them over the new server. I extracted all the folder and put them in the same location as the old server.
Reboot the machine and everything worked.
The final thing I did was change the host IP on our domain provider portal to point to the new server. That was it!