Hosting multiple websites on a single VPS lets you run several domains or subdomains from one server, saving money and simplifying maintenance. You’ll configure virtual hosts (Apache) or server blocks (Nginx) so each domain serves its own files, with its own settings. This guide covers both web servers, step by step.
What You’ll Need
- A VPS with Linux (Ubuntu/Debian recommended) and root or sudo access.
- A registered domain name (or subdomain) for each site you want to host.
- DNS records pointing each domain to your VPS’s public IP address (A record).
- SSH client (like Terminal on macOS/Linux or PuTTY on Windows).
- Basic familiarity with the command line and file editing (nano or vim).
- Web server installed: either Nginx or Apache (install commands below if not already).
Step-by-Step
- Connect to your VPS via SSH:
ssh user@your-server-ip. Update the system:sudo apt update && sudo apt upgrade -y(for Debian/Ubuntu). - Install the web server if not already present. For Nginx:
sudo apt install nginx -y. For Apache:sudo apt install apache2 -y. Start and enable it (Nginx:sudo systemctl enable --now nginx, Apache:sudo systemctl enable --now apache2). - Create directory structure for each site. For example, for
example.comandexample.org:sudo mkdir -p /var/www/example.com/htmlandsudo mkdir -p /var/www/example.org/html. Set ownership to your user:sudo chown -R $USER:$USER /var/www/example.com/html(repeat for each). - Add a test index file for each site so you can verify later. For example,
echo '<h1>Welcome to Example.com</h1>' | sudo tee /var/www/example.com/html/index.html. Repeat for other domains with different text. - Create a configuration file for each site. For Nginx, create
/etc/nginx/sites-available/example.comwith a server block. For Apache, create/etc/apache2/sites-available/example.com.confwith a virtual host. See the examples below. - Enable the site. Nginx:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/. Apache:sudo a2ensite example.com.conf. - Test the configuration for syntax errors. Nginx:
sudo nginx -t. Apache:sudo apache2ctl configtest. If errors appear, fix them before proceeding. - Reload the web server to apply changes. Nginx:
sudo systemctl reload nginx. Apache:sudo systemctl reload apache2. - Repeat steps 3-8 for each additional domain you want to host.
- Test in a browser by visiting
http://example.comandhttp://example.org. You should see the respective test pages. - Set up SSL (optional but recommended) using Let’s Encrypt. Install Certbot:
sudo apt install certbot python3-certbot-nginx(orpython3-certbot-apachefor Apache). Runsudo certbot --nginx -d example.com -d www.example.com(or--apache). Follow prompts; Certbot will automatically configure HTTPS and renewals.
Example Nginx server block (/etc/nginx/sites-available/example.com):
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
}
Example Apache virtual host (/etc/apache2/sites-available/example.com.conf):
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/html
<Directory /var/www/example.com/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Common Mistakes
- Forgetting to enable the site – creating a config file but not symlinking (Nginx) or running a2ensite (Apache) means the site won’t load.
- Wrong file permissions – if the web server can’t read your files, you’ll get a 403 Forbidden error. Ensure the directory permissions are at least 755 and files are 644, with ownership by the web server user (www-data) or your user.
- Duplicate server_name – if two configs use the same domain, the server will pick one arbitrarily. Each domain must be unique.
- Not testing config before reload – a syntax error can crash the server. Always run
nginx -torapache2ctl configtestfirst. - DNS not propagated – if you just changed DNS, it can take up to 48 hours. Use
digor an online tool to check.
Tips
- Use separate log files for each site to make debugging easier. In Nginx, add
access_log /var/log/nginx/example.com.access.log;inside the server block. In Apache, useCustomLog ${APACHE_LOG_DIR}/example.com.access.log combined. - Keep your configs organized – name files after the domain and keep them in the sites-available directory. Use comments to remind yourself what each block does.
- Set up a firewall (like UFW) and allow ports 80 and 443:
sudo ufw allow 'Nginx Full'orsudo ufw allow 'Apache Full'. - Use PHP-FPM if you need PHP. For Nginx, install
php-fpmand add alocation ~ \.php$block. For Apache, installlibapache2-mod-php. - Back up your configs before making major changes. Copy the entire sites-available directory to a safe location.
Troubleshooting
- Site shows “404 Not Found” – Check that the root directory exists and contains an index file. Also ensure the server_name matches the domain exactly.
- Site shows “403 Forbidden” – This is usually a permissions issue. Check that the web server user can traverse the directory:
sudo chmod 755 /var/www/example.comandsudo chmod 644 /var/www/example.com/html/index.html. - Site shows the default page – This happens when the server can’t find a matching server block. Verify that the config file is enabled and that the server_name is correct. For Apache, also check that
a2dissite 000-default.confis not interfering. - Server won’t start after config change – Run
sudo nginx -torsudo apache2ctl configtestto see the error. Common issues: missing semicolons (Nginx), missing closing tags (Apache), or duplicate listen directives. - SSL certificate fails – Ensure your domain resolves to the server’s IP before running Certbot. Also make sure ports 80 and 443 are open in your firewall.
- Changes not taking effect – Always reload the server after changes:
sudo systemctl reload nginxorsudo systemctl reload apache2. If you edited files, check that you saved them correctly.
Frequently Asked Questions
Can I host both Nginx and Apache on the same VPS?
Yes, but they can’t both listen on the same port (80/443) simultaneously. You could run Apache on port 8080 and Nginx on 80, but it’s simpler to pick one. You can also use Nginx as a reverse proxy in front of Apache for different domains.
How many websites can I host on one VPS?
There’s no hard limit; it depends on your VPS’s resources (CPU, RAM, bandwidth) and traffic. A small VPS can easily handle a dozen low-traffic sites. Monitor resource usage and scale up if needed.
Do I need a separate IP address for each site?
No. With name-based virtual hosting, you can host unlimited sites on one IP address. The server uses the Host header from the HTTP request to determine which site to serve.
How do I add a new site later?
Simply create a new directory, a new config file, enable it, test, and reload. The process is the same as the initial setup.
Can I use subdomains like blog.example.com?
Yes. Just create a DNS A record for the subdomain pointing to your VPS IP, and use that subdomain as the server_name in a new config file.
What if my domain uses www and non-www?
Include both in the server_name (Nginx) or use ServerAlias (Apache). You can also set up a redirect from one to the other to avoid duplicate content.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Channel TLD Websites: What They Are & How to Get One5.00 min/km
- Django Class Based Views CRUD REST API
- Most Secure OS for Privacy: 3 Picks by Device
- Ryobi One+ Battery System: Is the Switch Worth It?
- Tmux Terminal Upgrade: Is It Worth It?
- Refurbished Tech Buying Checklist