The steps below can be used to move a domain that exists in DirectAdmin to a different domain and/or user.
If you need to move the site to a different domain, but keep the same user, then just use the same username is the commands below. If you need to move the site to a new domain under a new user, then follow the syntax of the commands below as provided.
These instructions assume that you will be working as the root user while logged into your DirectAdmin server via SSH.
The following assumes that you will be working as root via SSH. Knownhost uses port 2200 by default for SSH. Use the following syntax to log in as root via SSH, but replace IP.IP.IP.IP with your server's actual IP:
ssh root@IP.IP.IP.IP -p2200
If you've not already created the new DirectAdmin account/domain that you'd like to use, do this first.
Now that we have created an account, we can use the following sequence of steps to create a copy of a site's files/directories at a different site's document root and update the ownership accordingly:
cp -a /home/old/domains/olddomain.tld/public_html/* /home/new/domains/newdomain.tld/public_html/ cp -a /home/old/domains/olddomain.tld/public_html/.htaccess /home/new/domains/newdomain.tld/public_html/.htaccess chown -R new.new /home/new/domains/newdomain.tld/public_html/
' Note that you can use the same steps for creating a staging or dev subdomain simply by changing the paths to the new docroot for the intended subdomain. For example, after having created the dev subdomain in the DirectAdmin panel, you could use the following for the dev subdomain:
cp -a /home/old/domains/olddomain.tld/public_html/* /home/new/domains/newdomain.tld/public_html/dev/ cp -a /home/old/domains/olddomain.tld/public_html/.htaccess /home/new/domains/newdomain.tld/public_html/dev/.htaccess chown -R new.new /home/new/domains/newdomain.tld/public_html/dev/
First, you may need to know the database's name. You can get it like so:
grep DB_NAME /home/old/domains/olddomain.tld/public_html/wp-config.php
Now that you know the database name, you can make a backup of it using the following command:
mysqldump old_website > old_website.sql
Make a new database user, username, and password in new's DirectAdmin. You can do this via DirectAdmin's MySQL Management interface (when logged in as the user, navigate Dashboard / MySQL Management / Create Database), or you can use the mysql commands via the root ssh shell like so:
mysqladmin create new_db echo "CREATE USER new_dbuser IDENTIFIED BY 'newpassword';" | mysql; echo "GRANT ALL PRIVILEGES ON new_db.* TO new_dbuser IDENTIFIED BY 'newpassword';" | mysql
Make sure to replace newpassword with a strong, secure password, new_dbuser with your desired database username (prefixed with the DirectAdmin username), and new_db with the new database name (also prefixed with the DirectAdmin username).
Use the following command to do this:
mysql new_db < old_website.sql
Replace the following values with those you used in Step 5 to create the new database, database user, and database user password:
Make sure this is done before you perform Step 8, otherwise you'll end up making changes to the old database instead of the new database!
You will find useful commands online often to update the siteurl and home values in the wp_options table, but the following should take care of these and so much more.
Let's use WP-CLI's search-replace tool to first perform a dry-run and see how many replacements of the old domain with the new domain will occur:
I would then review the output from the following, which would show all findings of the old url that would be replaced with the new url when using wp-cli:
su -s /bin/bash -c "/usr/local/bin/wp search-replace old.com new.com --dry-run" new
Above, new represents the new DirectAdmin user's username.
Run the following to proceed with replacing the old url with the new url in the newly created database containing the import of the sql backup I old from the old site.
su -s /bin/bash -c "/usr/local/bin/wp search-replace old.com new.com --skip-columns=guid" new
Now, WordPress warns against changing the 1)GUID. This mainly applies to RSS feeds and feed-readers. These feeds are enabled by default. If you have users that use a RSS feed for getting updates from your site, if we change the GUID to the new URL, they will be flooded with old posts as if they were new. This is why WordPress warns against changing the GUID column.
Depending on how many there are, you may just do this manually, or write a sed command to accomplish it like so:
sed -i.bak 's/old.com/new.com/g' .htaccess
The command above should make a backup .htaccess before applying the changes. The backup .htaccess will have .bak appended to it like so:
Lastly, I would make a backup of the old account before deleting it just in case. You can do this via DirectAdmin for the account 'new' using the command below:
echo "action=backup&append%5Fto%5Fpath=nothing&database%5Fdata%5Faware=yes&email%5Fdata%5Faware=yes&local%5Fpath=%2Fhome%2Fadmin%2Fadmin%5Fbackups&owner=admin&select%30=new&type=admin&value=multiple&when=now&where=local" >> /usr/local/directadmin/data/task.queue; /usr/local/directadmin/dataskque d200
wp search-replace foo bar --export=database.sql
Using this command could combine steps noted above (particularly steps 6 and 8), further speeding up and simplifying the process.
If you have any questions about this process, or require assistance, please provide the link to this wiki article, the source and desired destination domains and usernames, and your support request to your KnownHost support team. :)