Score:0

Server migration: apache virtual host

mx flag

What is the best and fastest way to replicate apache virtual hosts to a new server?

When I need to change server I recreate all domains one by one with a procedure similar to this one:

sudo mkdir -p /var/www/html/sito.com
sudo chown -R $USER:$USER /var/www/html/sito.com
sudo chmod -R 755 /var/www/html/sito.com

sudo nano /etc/apache2/sites-available/sito.com.conf

------
<VirtualHost *:80>
    ServerAdmin [email protected]

    ServerName sito.com
    ServerAlias www.sito.com
    DocumentRoot /var/www/html/sito.com

        <Directory /var/www/html/sito.com>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                # New directive needed in Apache 2.4.3:
                Require all granted
        </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
-------

sudo a2ensite sito.com.conf
sudo systemctl reload apache2

and with 50, 100 domains this procedure is slow and tedious ...

Is there a fast and secure way to replicate the virtual hosts of the old server to a new server? Can there be problems with permissions and file owners?

I have ssh access and server root, ubuntu 20.04 server, apache, php8.1

raj avatar
cn flag
raj
I would copy the contents of directories `/etc/apache2/sites-available/` and `/etc/apache2/sites-enabled/` from old server to the new one - why recreating them manually? I would also copy the entire `/var/www/html/` but I guess that's what you are doing already? You can use for example `rsync` for that to preserve permissions.
in flag
So long as you're following standard Apache configuration practices, the `/sites-enabled` directory will contain symlinks of files in `/sites-available`, so you will only need to copy the files from `/sites-available`. Once on the new server, use `a2ensite {filename}` to enable the configuration file, which does the symlink creation on its own. Don't forget to restart/reload Apache afterwards
alebal avatar
mx flag
I've always been worried about creating problems with file permissions... isn't that a problem? Then in my virtulahost files there is some code added by certbot (redirect to https), but in order to request a new certificate from certbot (with the new server ip) the domain must be enabled and working... will it be a mess if i just copy?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.