How to Migrate Your Website to a New VPS: A Complete Guide from an Experienced Developer
Hey there, fellow developer! Migrating a website to a new VPS is a task every server-side person has faced. It seems simple, but in reality, you can get bogged down for days troubleshooting errors and nuances. In this guide, I, an experienced developer, will share my experience and tell you how to do everything quickly, painlessly, and most importantly, without data loss. Grab a coffee, be patient – we’re starting! I hope this guide will be your lifeline in the stormy sea of website migration.
Before you begin, you need to carefully plan everything. First – determine what type of website you have: WordPress, custom-built, or on another engine. This will determine your further plan of action. You will need SSH access to both servers – old and new. Write down all the necessary data: IP addresses, logins, passwords (of course, keep them in a safe place!). Also, find out the PHP versions, MySQL/MariaDB versions, and the web server version (Nginx or Apache) on your current server. This will help you configure the new VPS the same way as the old one, avoiding compatibility issues. Check that the new VPS has enough resources (RAM, CPU, disk space) for your website. You don’t want to face performance issues after the migration, do you?
# Checking versions on the old server
php -v
mysql --version
nginx -v
Don’t forget to check the domain name availability. If you are using your provider’s DNS servers, you will need to update the DNS records, pointing to the new IP address of your VPS. This is where things often go wrong. Remember: checking DNS records is a critical step!
# Checking DNS records (example with dig)
dig example.com
Data Backup
Look, I’ve been there… data loss is a disaster. Therefore, *always*, **always**, back up your data *before* any critical changes. For WordPress, there are many plugins that allow you to create full website backups (database and files). For custom-built websites, you will have to use other methods. You can copy all website files using `rsync` or `scp`, and the database using the `mysqldump` utility. By the way, `mysqldump` is my best friend, saving me countless times.
# Backing up the MySQL database
mysqldump -u your_username -p your_database_name > backup.sql
# Copying website files using rsync (example)
rsync -avz -e ssh user@old_server:/path/to/website/ /path/to/backup/
Pro tip: store backups in multiple locations – on an external hard drive, in cloud storage. Redundancy is the key to peace of mind. I usually do backups every few hours, just in case. Plus, this helps revert to a previous version if necessary.
Setting Up the New VPS
Now let’s configure the new VPS. Install the necessary software: a web server (Nginx or Apache), PHP, MySQL/MariaDB. For Ubuntu/Debian, this can be done using `apt`. For CentOS/RHEL – `yum` or `dnf`. It’s crucial to install the same versions as on the old server. Mismatched versions can lead to unexpected problems. I often used `apt-get`, but `apt` is a more modern analogue.
After making changes, don’t forget to restart the web server: `sudo systemctl restart nginx`. This is very important! I forgot to do this once – spent half a day debugging… Don’t repeat my mistakes!
Transferring Website Files
Now, let’s transfer the website files to the new VPS. You can use `rsync`, `scp`, or FTP. `rsync` is my personal favorite because it’s fast and allows you to synchronize files efficiently. Don’t forget to specify the correct path to your website directory on the new server. I often use `/var/www/` or `/home/user/public_html`, depending on the configuration.
After transferring the files, check their integrity. Compare the sizes of folders and files on the old and new servers. Discrepancies may indicate transfer errors. If something goes wrong, you can always use the backup. This is *real* backup magic. No cap!
# Checking directory size
du -sh /var/www/example.com/
Database Setup
Let’s migrate the database. You can use `mysql` or other MySQL clients. Import the database backup (the `backup.sql` file) we created earlier. During the import process, an error may occur related to database access rights or the absence of a user. This is easily solved, but sometimes this step requires some effort.
# Connecting to MySQL
mysql -u your_username -p
# Importing the database
mysql -u your_username -p your_database_name < backup.sql
Don’t forget to update your website’s configuration files, specifying the correct data for connecting to the new database. Usually, this data is stored in the `wp-config.php` file for WordPress or in the corresponding configuration files of your framework/CMS. Double-check the database settings to avoid unpleasant surprises. Been there, done that!
Verification and Testing
The final chord! After you have transferred the files and the database, check if your website is working. Open it in a browser. If everything is fine, congratulations! You did it! But don’t rush to celebrate. Conduct thorough testing: check all website functions, see if all pages are displayed correctly. Use performance testing tools such as GTmetrix or PageSpeed Insights.
# Checking web server status
systemctl status nginx
If something doesn’t work, don’t panic. Check the web server and PHP logs. They contain valuable information that will help you find and fix errors. I’ve saved the day more than once by analyzing the logs. It’s like reading a detective story, only more mysterious. Ever wonder why this happens? Often the problem lies in small things, such as an incorrectly specified path or incorrect access rights.
After successful testing, update the DNS records, pointing to the new IP address of your VPS. Boom! That’s it! Now your website is running on the new VPS. Hooray! I hope this guide helped you get through this process easier. Remember to keep backups and stay tuned for more helpful tips!
“Website migration is always stressful, but with proper preparation and a plan of action, everything becomes significantly easier.”
Ivan Ivanov, Senior DevOps Engineer
“Always back up your data before making any major changes. It’s the only way to ensure you can recover from any problems.”