Migrating the data

With the server set up, the remaining step was moving the data from the old server to the new one. This is a delicate process where the goal is to keep the service up as much as possible (minimizing downtime). To do that, I put together the following workflow:

Source server

  • First, the old server needs SSH access to the new one. I used ssh-keygen and then added the generated public key to ~/.ssh/authorized_keys on the destination server.
  • Copy the application data into a tar.gz:

    sudo tar -czf ~/transfers/project.tar.gz .

    Run it from inside the folder you want to compress so the internal directory structure is right. Otherwise it includes the absolute path and causes problems when extracting.
  • Make a dump of the database and compress it as much as possible:

    mysqldump --opt -u project_user -p project_db | gzip -9 > ~/transfers/project_db.sql.gz
  • I use rsync to sync the transfers directory:

    rsync -a ~/transfers/ ubuntu@new_host:~/transfers

Destination server:

  • Create a new virtual host:
sudo mkdir /var/www/project.com
sudo chown -R $USER:$USER /var/www/project.com/
sudo chmod -R 755 /var/www/project.com/
mkdir /var/www/project.com/public_html
sudo nano /etc/apache2/sites-available/project.com.conf
  • Put this content in the file:
<VirtualHost *:80>
    ServerName project.com
    ServerAlias www.project.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/project.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.project.log
    CustomLog ${APACHE_LOG_DIR}/access.project.log combined

    <Directory /var/www/project.com/public_html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
  • Run these commands to enable the site:
sudo apachectl configtest

sudo a2ensite project.com
sudo systemctl restart apache2
  • Import the database and the files themselves. I use pv, which you can install with apt-get and shows a progress bar:
cd /var/www/project.com
tar -zxf ~/transfers/project.tar.gz
sudo chmod -R 755 /var/www/project.com/

pv ~/transfers/project_db.sql.gz | gunzip | mysql -u project_user -p project_db
  • Next, update the DNS records to point to the new server, and then run this command to enable HTTPS:
sudo certbot

We also need to make sure the deployment system points to the new server. If something goes wrong, these files come in handy:

// edit the PHP configuration
sudo nano /etc/php/8.0/fpm/php.ini

// restart PHP
sudo systemctl restart php8.0-fpm

// if that fails, see the errors:
sudo journalctl -xeu php8.0-fpm.service

// PHP-FPM and pool configuration:
sudo nano /etc/php/8.0/fpm/php-fpm.conf
sudo nano /etc/php/8.0/fpm/pool.d/www.conf

// restart Apache:
sudo systemctl restart apache2

// Apache log location:
/var/log/apache2

// Apache configuration file:
/etc/apache2/apache2.conf

// If URLs and .htaccess don't work, you need to enable this:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

No comments yet

Leave a Reply

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