WordPress site Migration from CentOS7 to Ubuntu24

Phase 1: Preparation on CentOS 7 (Source Server)

First, you need to back up your data. Log in to your CentOS server via SSH.

1、Backup the Database
Run this command to export your database to a SQL file.
(Replace db_name, db_user with your actual database details)

mysqldump -u db_user -p db_name > wordpress_backup.sql

2、Backup WordPress Files
Compress your website files into a single archive to make the transfer easier.
(Assuming your site is at /var/www/html or /usr/share/nginx/html)

tar -czf wordpress_files.tar.gz /var/www/html

Phase 2: Setup Ubuntu 24.04 (Destination Server)

Log in to your new Ubuntu 24.04 server. You need to install the LEMP stack (Linux, Nginx, MySQL, PHP).

1、Update and Install Nginx & MySQL

sudo apt update && sudo apt upgrade -y
sudo apt install nginx mysql-server ghostscript -y

    2、Install PHP and Extensions
    Ubuntu 24.04 defaults to PHP 8.3. Install PHP-FPM and the required WordPress extensions:

    sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-zip php-intl php-bcmath php-soap -y

    3、Configure MySQL
    Create a new database and user for your migrated site.

    sudo mysql

    Inside the MySQL shell:

    CREATE DATABASE wordpress_db;
    CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'strong_password';
    GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;

    Phase 3: Transfer Data

    You can pull the files from your old CentOS server directly to your new Ubuntu server using rsync. Run these commands on the Ubuntu server:

    (Replace root@centos_ip with your CentOS server’s user and IP)

    1、Transfer the Files

    rsync -avz root@centos_ip:/home/root/wordpress_files.tar.gz /tmp/
    rsync -avz root@centos_ip:/home/root/wordpress_backup.sql /tmp/

    Phase 4: Restore on Ubuntu 24.04

    1、Restore Files
    Create the web root directory and extract your files.

    sudo mkdir -p /var/www/yourdomain.com
    sudo tar -xzf /tmp/wordpress_files.tar.gz -C /var/www/yourdomain.com

    2、Fix Permissions
    CentOS often uses nginx or apache as the user, but Ubuntu uses www-data. You must update ownership:

    sudo chown -R www-data:www-data /var/www/yourdomain.com
    sudo find /var/www/yourdomain.com -type d -exec chmod 755 {} \;
    sudo find /var/www/yourdomain.com -type f -exec chmod 644 {} \;

    3、Restore Database
    Import the SQL dump into the new database you created in Phase 2.

    sudo mysql wordpress_db < /tmp/wordpress_backup.sql

    4、Update wp-config.php
    Edit your configuration file to match the new database credentials.

    sudo nano /var/www/yourdomain.com/wp-config.php

    Update these lines:

    define( 'DB_NAME', 'wordpress_db' );
    define( 'DB_USER', 'wp_user' );
    define( 'DB_PASSWORD', 'strong_password' );
    define( 'DB_HOST', 'localhost' );

    Phase 5: Nginx Configuration

    Ubuntu stores Nginx configs in /etc/nginx/sites-available/. Create a new config file:

    1、Create Config File

    sudo vim /etc/nginx/sites-available/yourdomain.com

    2、Paste the Configuration
    Note: Ubuntu 24.04 uses php8.3-fpm.sock by default. Ensure the fastcgi_pass line matches this.

    server {
        listen 80;
        server_name yourdomain.com www.yourdomain.com;
        root /var/www/yourdomain.com;
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }

    3、Enable the Site
    Link the file to sites-enabled and test the config.

    sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
    sudo nginx -t

    (If you see “syntax is ok”, proceed. If not, fix the errors shown).

    4、Restart Nginx

    sudo systemctl restart nginx

    Phase 6: Final Steps

    1、Update DNS: Point your domain’s A record to the new Ubuntu server’s IP address.

    2、Install SSL (HTTPS): Once DNS propagates, secure your site with Let’s Encrypt.

    sudo apt install certbot python3-certbot-nginx
    sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

    Your site should now be live on Ubuntu 24.04!

    Leave a Reply

    Your email address will not be published. Required fields are marked *