ThisCalculator

Everyday

Add a Domain to WordPress with Virtual Hosts

Updated · Standard formula, shown below

This guide walks you through hosting a second WordPress site on the same server by creating an Apache virtual host. You’ll learn how to configure the server, set up DNS, and avoid common pitfalls—no prior server admin experience required, but basic comfort with the command line helps.

What You’ll Need

  • Root or sudo access to a Linux server (Ubuntu/Debian commands shown; adjust for RHEL/Fedora).
  • Apache web server already installed and running.
  • A registered domain name pointing to your server’s public IP (you can use a temporary hosts file entry for testing).
  • WordPress files for the new site (you can download them fresh or copy an existing install).
  • A MySQL/MariaDB database and user for the new site (created via command line or phpMyAdmin).

Step-by-Step

  1. Create the directory structure. SSH into your server and run: sudo mkdir -p /var/www/example.com/public_html. Replace example.com with your actual domain. Set ownership so your web user can write: sudo chown -R $USER:$USER /var/www/example.com/public_html.
  2. Upload WordPress files. Either download a fresh copy: cd /var/www/example.com/public_html && sudo curl -O https://wordpress.org/latest.tar.gz && sudo tar -xzf latest.tar.gz --strip-components=1, or copy an existing install with rsync. Then set permissions: sudo chown -R www-data:www-data /var/www/example.com.
  3. Create the database. Log into MySQL: sudo mysql -u root -p. Run: CREATE DATABASE example_db; CREATE USER 'example_user'@'localhost' IDENTIFIED BY 'strong_password'; GRANT ALL PRIVILEGES ON example_db.* TO 'example_user'@'localhost'; FLUSH PRIVILEGES; EXIT;. Note the database name, user, and password for the WordPress install.
  4. Create a virtual host configuration file. Copy the default config: sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf. Edit it with sudo nano /etc/apache2/sites-available/example.com.conf.
  5. Edit the virtual host block. Replace the ServerAdmin, ServerName, ServerAlias, and DocumentRoot lines. A minimal working block looks like:
    <VirtualHost *:80>
        ServerAdmin admin@example.com
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com/public_html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
        

    Save and exit.

  6. Enable the new site and reload Apache. Run sudo a2ensite example.com.conf, then sudo systemctl reload apache2. If you get a syntax error, run sudo apachectl configtest first.
  7. Test locally. To test before DNS propagates, edit your local machine’s hosts file (e.g., /etc/hosts on Linux/Mac, C:\Windows\System32\drivers\etc\hosts on Windows) and add: your_server_ip example.com. Then visit http://example.com in a browser—you should see the WordPress installation screen.
  8. Complete WordPress installation. Follow the on-screen prompts, entering the database details you created. Set the site title, admin username, and password. If you moved an existing site, you may need to update the wp_options table’s siteurl and home values via phpMyAdmin or WP-CLI.
  9. Set up SSL (optional but recommended). Use Certbot: sudo apt install certbot python3-certbot-apache, then sudo certbot --apache -d example.com -d www.example.com. This automatically edits your virtual host to redirect HTTP to HTTPS.

Common Mistakes

  • Forgetting to enable the site. Creating the config file doesn’t activate it—you must run a2ensite.
  • Wrong DocumentRoot. Pointing to a directory that doesn’t exist or lacks an index.php will show a 403 or directory listing.
  • File permissions too strict. If Apache can’t read the files, you’ll get a 403. Ensure the web server user (www-data) has read access.
  • Hardcoding the IP in WordPress. If you copied an existing site, the database still has the old domain. Update siteurl and home.
  • Not restarting Apache after config changes. Reload or restart is required for changes to take effect.

Tips

  • Use a separate virtual host file per domain to keep configurations organized and easy to debug.
  • Start with a simple HTTP virtual host, verify it works, then add SSL—this isolates issues.
  • For high-traffic sites, consider enabling Apache modules like mod_rewrite (needed for pretty permalinks): sudo a2enmod rewrite.
  • Keep WordPress core, themes, and plugins updated to avoid security vulnerabilities.
  • Use a staging copy of your site to test changes before going live.

Troubleshooting

  • Site shows “Apache2 Default Page”. This means your virtual host isn’t being used. Check that ServerName matches your domain exactly and that the default site is disabled or has a different name.
  • 403 Forbidden. Check directory permissions: sudo chmod -R 755 /var/www/example.com and ensure the DocumentRoot path is correct. Also verify that Options -Indexes isn’t blocking access—you can temporarily set Options Indexes FollowSymLinks to test.
  • 404 Not Found. If the URL works but shows 404, your .htaccess may be missing or mod_rewrite isn’t enabled. Run sudo a2enmod rewrite and restart Apache.
  • “Error establishing a database connection”. Double-check the database name, user, and password in wp-config.php. Ensure the database exists and the user has privileges.
  • DNS not resolving. If you’re testing via hosts file, make sure you’re editing the correct file and that your browser isn’t caching old DNS. Try a different browser or incognito mode.
  • SSL certificate fails. Ensure your domain’s DNS is fully propagated before running Certbot. Also, port 80 must be open for the HTTP-01 challenge.

Frequently Asked Questions

Can I host multiple WordPress sites on one server?

Yes, Apache virtual hosts allow you to run multiple sites on a single IP address, each with its own domain and document root.

Do I need a dedicated IP for each domain?

No, virtual hosts work with name-based hosting, so multiple domains can share one IP address.

How do I point my domain to the server?

Log into your domain registrar or DNS provider and create an “A” record pointing to your server’s public IP address. For subdomains, create an “A” record with the subdomain name.

What if I already have WordPress installed in a subdirectory?

You can move the files to the new domain’s document root or create a virtual host that points to that subdirectory. Just ensure the WordPress URL settings are updated.

Is it better to use a subdomain or a separate domain?

Separate domains are better for distinct brands or products. Subdomains (e.g., blog.example.com) are easier to set up and share cookies with the main domain, which can be useful for tracking.

How do I remove a virtual host later?

Disable it with sudo a2dissite example.com.conf, then reload Apache. You can delete the config file and the document root if no longer needed.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.