Hosting websites on a Cubes Hosting Ubuntu VPS requires web server software to handle incoming HTTP/HTTPS requests and serve web pages to visitors.
By using Virtual Hosts (known as Server Blocks in Nginx or VirtualHosts in Apache), you can host multiple independent websites (such as domain1.com and domain2.com) on a single VPS server, each with its own document root directory, configuration files, and free SSL certificates!
This complete guide will show you how to set up custom Virtual Hosts on Nginx or Apache2 on Ubuntu 22.04 / 24.04 LTS.
Nginx vs. Apache2: Key Differences
Before choosing which web server to install, review how Nginx and Apache compare:
| Feature | Nginx (Recommended) | Apache2 |
|---|---|---|
| Architecture | Event-driven, asynchronous (ultra-low RAM usage) | Process/thread per connection (higher RAM under load) |
| Static Performance | Extremely fast (HTML, CSS, images, video streaming) | Fast, but uses more memory for static files |
| Reverse Proxy | Excellent native reverse proxy for Node.js, Python, & APIs | Requires extra modules (mod_proxy) |
| Per-Directory Config | ❌ No .htaccess (central server block configs only) | ✅ Supports .htaccess files for per-folder overrides |
| PHP Execution | Requires PHP-FPM | Supports mod_php or PHP-FPM |
- Choose Nginx if: You want maximum speed, low resource consumption, reverse proxy capabilities, or host high-traffic sites.
- Choose Apache if: Your application or CMS specifically requires
.htaccessfile rewrites.
Option 1: Setting Up Nginx with Custom Server Blocks (Recommended)
Nginx is an event-driven, ultra-fast web server ideal for high-traffic websites, APIs, and reverse proxies.
Step 1: Install Nginx
sudo apt update && sudo apt install nginx -y
Step 2: Create Web Directory Structure
Create a dedicated folder for your domain's web files under /var/www/:
# Replace example.com with your actual domain name
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com/html
Create a sample index.html file to test:
echo "<h1>Welcome to example.com on Nginx!</h1>" > /var/www/example.com/html/index.html
Step 3: Create Nginx Server Block File
Create a new configuration file in sites-available:
sudo nano /etc/nginx/sites-available/example.com
Paste the following Server Block configuration:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/example.com_access.log;
error_log /var/log/nginx/example.com_error.log;
}
(Save and close: CTRL + O, Enter, CTRL + X).
Step 4: Enable the Nginx Site & Test
Create a symbolic link to activate your site configuration:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
# Test Nginx syntax for errors
sudo nginx -t
# Reload Nginx to apply changes
sudo systemctl reload nginx
Option 2: Setting Up Apache2 with Custom VirtualHosts
Apache2 is a modular, feature-rich web server widely used for WordPress and PHP applications utilizing .htaccess rewrite rules.
Step 1: Install Apache2
sudo apt update && sudo apt install apache2 -y
Step 2: Create Web Directory Structure
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_html
sudo chmod -R 755 /var/www/example.com/public_html
echo "<h1>Welcome to example.com on Apache!</h1>" > /var/www/example.com/public_html/index.html
Step 3: Create Apache VirtualHost File
Create a new VirtualHost file:
sudo nano /etc/apache2/sites-available/example.com.conf
Paste the following VirtualHost configuration:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
Step 4: Enable Apache Site & Reload
# Enable your new site configuration
sudo a2ensite example.com.conf
# Optional: Disable the default Apache fallback page
sudo a2dissite 000-default.conf
# Test Apache syntax for errors
sudo apache2ctl configtest
# Reload Apache
sudo systemctl reload apache2
Step 5: Open Firewall Ports & Install Free SSL (Certbot)
Whether you chose Nginx or Apache, secure your websites with free SSL certificates using Certbot (Let's Encrypt).
1. Allow HTTP and HTTPS in UFW Firewall:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
2. Issue Free Let's Encrypt SSL Certificates:
- For Nginx:
sudo apt install certbot python3-certbot-nginx -y sudo certbot --nginx -d example.com -d www.example.com - For Apache:
sudo apt install certbot python3-certbot-apache -y sudo certbot --apache -d example.com -d www.example.com
Follow the on-screen prompts to enter your email and enable automatic HTTPS redirects!
Conclusion
Setting up Nginx Server Blocks or Apache VirtualHosts allows you to scale your Cubes Hosting Ubuntu VPS to host multiple domain names effortlessly. Open your web ports, secure your sites with Certbot SSL, and manage your web infrastructure with total developer freedom!
