How to Migrate a LAMP Website to Linode
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
This guide describes how to migrate a website running in a LAMP environment on another host to a new Linode. Read the Best Practices when Migrating to Linode guide prior to following this guide for more information about migrating your site.
This guide includes commands that need to be run at the command line of your current host, which may not be available if you have a shared hosting environment. Ubuntu 18.04 is used as the distribution for the new Linode deployment in this guide. If you’d like to choose another distribution, us the examples here as an approximation for the commands you’ll need to run.
Migrate Your System
Deploy Your Linode
Follow Linode’s Creating a Compute Instance guide and choose Ubuntu 18.04 as your Linux image when deploying. Choose a Linode plan with enough storage space to accommodate the website data from your current host.
Follow the Setting Up and Securing a Compute Instance guide and create a limited Linux user with
sudo
privileges. The examples below assume this user is namedlinode_user
.
Install LAMP
If you did not do so previously, update your software:
sudo apt update && sudo apt upgrade
Install and use
tasksel
to install thelamp-server
metapackage.sudo apt install tasksel sudo tasksel install lamp-server
Prepare and Back up Your Current Host
The data that needs to be transferred includes:
- Files from the website document root. This guide assumes this to be
/var/www/html/
, but your server may have it located in another directory. - MySQL database data (which will be exported using the
mysqldump
utility) - Apache’s configuration files. Specifically,
/etc/apache2/apache2.conf
and/etc/apache2/sites-available/example.com.conf
.- These and the files in the
sites-available
directory may have different names.
- These and the files in the
Your server may have relevant website data stored in other directories, but these are the common locations for most files in a LAMP deployment.
Perform a database dump needs on your MySQL process prior to transferring the data. This will result in a file on disk that encapsulates your database data which can then be copied over the network as a normal file.
CautionStopping services on your current host will temporarily disable your website.
Connect to your current host via SSH.
Stop the web server on your current host:
sudo systemctl stop apache2
This prevents new requests on your website, which will stop new writes to your database. This ensures that the MySQL backup that is taken does not result in an inconsistent dataset.
Perform a database dump of the entire database on your current host:
sudo mysqldump --all-databases --single-transaction --quick --lock-tables=false > full-backup-$(date +%F).sql -u root -p
This command results in a file called
full-backup-*.sql
with the current date inserted.(Optional) Restart your web server on the current host to continue serving visitors in the interim:
sudo systemctl start apache2
Note
If any new information is added to the database on the current host prior to fully transferring service to Linode, that new information is not included in the MySQL backup that was performed in this section.
Transfer Data to Your Linode
The following commands copy files into the home directory of your Linode’s Linux user, ~/document_root
in these examples. The section after ensures that these files are moved to the right locations on disk and that their file ownership is correct. This work is done in two steps because the Linux user is assumed to have limited privileges and is unable to write to certain locations on disk.
From your current host, upload the Apache configuration files to your new Linode:
rsync -az /etc/apache2/apache2.conf linode_user@linode_ip_address:/etc/apache2/ rsync -az /etc/apache2/sites-available/example.com.conf linode_user@linode_ip_address:~
Upload the website files to your Linode:
rsync -av /var/www/html/ linode_user@linode_ip_address:~/document_root
Upload the database dump file to your Linode:
rsync -az full-backup-*.sql linode_user@linode_ip_address:~
Finish Setting up Your Linode
From your Linode, move the transferred Apache configuration files to the appropriate locations:
cd ~ sudo mv apache2.conf /etc/apache2 sudo mv example.com.conf /etc/apache2/sites-available
Set
root
as the owner and group for the files:sudo chown root:root /etc/apache2/apache2.conf /etc/apache2/sites-available/example.com.conf
Move the website files and set the owner and group to
www-data
:sudo mv document_root/* /var/www/html sudo chown -R www-data:www-data /var/www/html
Caution
This will overwrite all current data in the MySQL database system of your Linode. It is not recommended that you perform this command on a Linode with other existing websites.Restore the database dump file. Replace
full-backup-*.sql
with the name of your file:sudo mysql -u root < full-backup-*.sql
Reload MySQL’s grant tables. This is necessary when a web application includes a user in the
mysql.users
table. If this is not performed, the application will not have permission to read from the database:sudo mysqladmin flush-privileges -u root -p
Disable the default Apache example site and enable yours:
sudo a2dissite 000-default.conf sudo ensite example.com.conf
Restart Apache:
sudo systemctl restart apache
Test Your New Environment
Go to your Linode’s IP address in a web browser. Your website should appear.
If your website does not load normally, one possible reason is that your IP address could be hard-coded in some areas of the website files or in the database. If this is the case, consult your PHP application framework’s documentation for ways to search for and replace those values. For example, WordPress’s WP-CLI interface and Drupal’s Drush interface provide methods that help with this task.
Another reason the site may not load is if your website configuration expects your domain name to be supplied in the HTTP headers of a web request. When you visit your IP directly, this information is not supplied in your request. The Previewing Websites Without DNS guide describes a workaround for this issue. When you have updated your DNS records, the workaround will no longer be necessary to view your site.
If you are seeing any other errors on your site, try reviewing Apache’s error logs for further clues. The locations for these logs will be listed in your /etc/apache2/apache2.conf
or /etc/apache2/sites-available/
files.
Migrating DNS Records
The last step required to migrate is to update your DNS records to reflect your new Linode’s IP. Once this is done, visitors will start loading the page from your Linode.
(Optional) Prepare Your Domain Name to Move
A recommended first step is to lower the Time to Live (TTL) setting for your domain so that the migration won’t have a negative impact on your site’s visitors. TTL tells DNS caching servers how long to save information about your domain. Because DNS addresses don’t often change server IP addresses, default TTL is typically about 24 hours.
When changing servers, however, make the TTL shorter to make sure that when you update your domain information, it takes effect quickly. Otherwise, your domain could resolve to your old server’s IP address for up to 24 hours.
Locate your current nameservers. If you’re not sure what your nameservers are, use a Whois Search tool. You will see several nameservers listed, probably all at the same company.
You can usually derive the website of your nameserver authority (the organization that manages your DNS) from the nameservers you find in the Whois report (e.g.
ns1.linode.com
corresponds withlinode.com
). Sometimes the labeling for the nameservers is not directly related to the organization’s website, and in those cases you can often find the website by plugging the nameserver into a Google search.Contact your nameserver authority for details on how to shorten the TTL for your domain. Every provider is a little different, so you may have to ask for instructions.
Most nameserver authorities will allow you to set the TTL on your domain or on individual records, but some do not allow this setting to be edited. Here are support documents from some common authorities that mention how they manage TTL:
Make a note of your current TTL. It will be listed in seconds, so you need to divide by 3600 to get the number of hours (e.g. 86,400 seconds = 24 hours). This is the amount of time that you need to wait between now and when you actually move your domain.
Adjust your TTL to its shortest setting. For example, 300 seconds is equal to 5 minutes, so that’s a good choice if it’s available.
Wait out the original TTL from Step 3 before actually moving your domain–otherwise, DNS caching servers will not know of the new, lower TTL yet.
Use Linode’s Nameservers
Follow Linode’s instructions on adding a domain zone to create DNS records at Linode for your domain. Recreate the DNS records listed in your current nameserver authority’s website, but change the IP addresses to reflect your Linode IPs where appropriate.
Locate your domain’s registrar, which is the company you purchased your domain from. If you’re not sure who your registrar is, you can find out with a Whois Search tool.
Your registrar may not be the same organization as your current nameserver authority, though they often are, as registrars generally offer free DNS services.
Log in to your domain registrar’s control panel and update the authoritative nameservers to be Linode’s nameservers:
ns1.linode.com
ns2.linode.com
ns3.linode.com
ns4.linode.com
ns5.linode.com
Wait the amount of time you set for your TTL for the domain to propagate. If you did not shorten your TTL, this may take up to 48 hours.
Navigate to your domain in a web browser. It should now show the website from Linode, rather than your old host. If you can’t tell the difference, use the DIG utility. It should show the IP address for your Linode.
Set reverse DNS for your domain. This is especially important if you are running a mail server.
Note
If you’re having trouble seeing your site at the new IP address, try visiting it in a different browser or in a private browsing session. Sometimes your browser will cache old DNS data, even if it has updated everywhere else.
This page was originally published on