Moving WordPress from Ubuntu to Red Hat Enterprise Linux


Unable to log into my Oracle Cloud Infrastructure account I chose to move this site and trainsandwellbeing.blog to Red Hat Enterprise Linux hosted by libvirt in my kitchen. There are differences in how Debian based systems like Ubuntu and Red Hat Enterprise Linux and derivatives manage web hosting. Here’s how my migration went which was successful in that you are reading this page.

Getting Stuff from Ubuntu

Login to the Debian based server and take a database dump which in my case looked like:

mysqldump --add-drop-table -h localhost wordpress > database.bak

I then took a backup of the WordPress site and the database.bak files so in my case:

tar -cvzf macintosh-rescue.net.tgz database.bak /srv/www/macintosh-rescue.net

I then transferred my .tgz file to my Mac with scp.

Setting Up the Red Hat Enterprise Linux Server for LAMP

I have already created a virtual machine through libvirt and that is covered #LINK TO VIRTUALISATION MIGRATION#. When installing Red Hat Enterprise Linux I selected the “Web Server” group within Anaconda (Red Hat’s Operating System Installer). At this stage it it is important to fix the new hosts IP address and set port forwarding on your router for ports 80 and 443. Here are the commands I used to setup my LAMP stack:

firewall-cmd --permanent --zone=public --add-service=http 
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

With the firewall setup now is a good time to test the Apache web server so:

systemctl start httpd
systemctl enable httpd

Now try connecting to the servers IP address through a browser, then try again outside of your network for example: turn Wi-Fi off on a cellular enabled mobile device and enter your external IP address in its browser to make sure you can see the servers test page. If that all worked you can install some WordPress dependencies some of which require Extra Packages for Enterprise Linux (EPEL)

dnf install php-mysqlnd php-fpm mariadb-server php-json php-pecl-zip php-gd php-pecl-imagick php-intl php-mbstring php-xml

Now to get MariaDB running do the following:

systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation

…when prompted for a root password this should be (none) so press return, then set a root password when prompted; these are unrelated to the root password for the Linux system.

The Red Hat Enterprise Linux LAMP server should now be ready to use so now it’s time to migrate the WordPress instance and import the database.

Setting Up an existing WordPress

Red Hat Enterprise Linux has SELinux set to Enforcing by default and will block websites served outside of /var/www without modification whereas an Ubuntu server might have stored sites in /srv/www . In my case I need to use name based virtual hosting. I also use certbot so had to remove certificate references so that certbot can setup the new server for me, so enable port 80 sites only at this stage. It is important to note that Ubuntu and Red Hat Enterprise Linux manage apache (httpd.conf) differently; rather than having virtual hosts setup through linked files Red Hat Enterprise Linux has all information stored in one file so I copied my virtual host .conf files from /etc/apache2/sites-available and put them into a single file with modifications to append to /etc/httpd/conf/httpd.conf file with the files and changes below:

<VirtualHost *:80>
    DocumentRoot /var/www/macintosh-rescue.net
    ServerName macintosh-rescue.net
    <Directory /var/www/macintosh-rescue.net>
        Options FollowSymLinks
        AllowOverride All
        DirectoryIndex index.php
        Require all granted
    </Directory>
    <Directory /var/www/macintosh-rescue.net/wp-content>
        Options FollowSymLinks
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/trainsandwellbeing.blog
    ServerName trainsandwellbeing.blog
    <Directory "/var/www/trainsandwellbeing.blog">
	Order allow,deny
	Allow from all
	Require all granted
    </Directory>
</VirtualHost>

With my two virtual host files combined into one and edited for new file locations I ran:

cat VirtualHosts >> /etc/httpd/conf/httpd.conf
service httpd restart

At this stage you need to have the dns entries for the site domain names pointing to the new server and the site files in place with apache:apache for recursive permissions and ownership. The WordPress site will likely not work as it will redirect to from http to https which will probably land you on the test page, however simple sites with no override files should work now over http only.

SSL Certificates with certbot

To setup free SSL certificates I use Let’s Encrypt via certbot. there are a number of ways to get certbot including Extra Packages for Enterprise Linux (EPEL) and through Canonical Snapcraft (recommended by EFF). Follow the linked instructions then secure your sites by running and following instructions given:

certbot

Importing the Database for WordPress

look at your wp-conf.php file in the WordPress sites folder to get database information for MariaDB: it will look contain something like the following:

/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' );

/** MySQL database username */
define( 'DB_USER', 'wordpressuser' );

/** MySQL database password */
define( 'DB_PASSWORD', 'password' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

To prepare MariaDB for the above example you would do the following:

mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5340 to server version: 3.23.54

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON wordpress.* TO "wordpressuser"@"localhost"
-> IDENTIFIED BY "password";
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec) 

mysql> EXIT
Bye

Now it should be possible to import your database from the backup file like:

mysql -u wordpressuser -p wordpress < database.bak

SELinux

Red Hat Enterprise Linux uses a security hardening system which can interfere with the normal operations of WordPress. In my experience the following commands should allow functionality of WordPress:

setsebool -P httpd_can_network_connect on
ausearch -c 'php-fpm' --raw | audit2allow -M my-phpfpm
semodule -X 300 -i my-phpfpm.pp

It may be necessary to label the WordPress site for SELinux with can be done with a command like:

restorecon -rv /var/www/macintosh-rescue.net

Done!

I’d recommend rebooting your server to make sure everything comes bak up as expected. The migration with any luck is now complete.