here is a detailed, step-by-step tutorial on how to buy multiple domains, point them to your VPS, and create subdomains. For this tutorial, we’ll use two demo domains and point them to a VPS using Apache as the web server.
Step 1: Buy Domain Names
- Choose a Domain Registrar:
- Popular registrars include GoDaddy, Namecheap, Google Domains, and others. For this tutorial, we’ll use Namecheap.
- Purchase Domains:
- Go to Namecheap.com (or your chosen registrar).
- Search for your desired domain names (e.g.,
example1.comandexample2.com). - Add the domains to your cart and complete the purchase process.
Step 2: Point Domains to Your VPS
- Find Your VPS IP Address:
- Log in to your VPS provider (e.g., DigitalOcean, AWS, etc.) and locate the IP address of your VPS.
- Update DNS Records:
- Log in to your domain registrar account.
- Navigate to the DNS management section for
example1.com. - Add an A record with the following details:
- Host:
@ - Value:
[Your VPS IP address]
- Host:
- Add another A record for
www:- Host:
www - Value:
[Your VPS IP address]
- Host:
- Repeat the above steps for
example2.com.
Step 3: Set Up Apache on Your VPS
- Install Apache:
- SSH into your VPS.
- Update the package lists and install Apache:
sudo apt update
sudo apt install apache2Enable Apache Virtual Hosts:
sudo a2enmod vhost_alias
sudo systemctl restart apache2Step 4: Create Directories for Domains
- Create Web Directories:
sudo mkdir -p /var/www/example1.com/public_html
sudo mkdir -p /var/www/example2.com/public_html
sudo mkdir -p /var/www/sub.example1.com/public_htmlSet Permissions:
sudo chown -R $USER:$USER /var/www/example1.com/public_html
sudo chown -R $USER:$USER /var/www/example2.com/public_html
sudo chown -R $USER:$USER /var/www/sub.example1.com/public_html
sudo chmod -R 755 /var/www
Step 5: Create Virtual Host Files
- For
example1.com:
sudo nano /etc/apache2/sites-available/example1.com.conf
Add the following content:
<VirtualHost *:80>
ServerAdmin admin@example1.com
ServerName example1.com
ServerAlias www.example1.com
DocumentRoot /var/www/example1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example1.com_error.log
CustomLog ${APACHE_LOG_DIR}/example1.com_access.log combined
</VirtualHost>
For example2.com:
sudo nano /etc/apache2/sites-available/example2.com.confAdd the following content:
<VirtualHost *:80>
ServerAdmin admin@example2.com
ServerName example2.com
ServerAlias www.example2.com
DocumentRoot /var/www/example2.com/public_html
ErrorLog ${APACHE_LOG_DIR}/example2.com_error.log
CustomLog ${APACHE_LOG_DIR}/example2.com_access.log combined
</VirtualHost>
For sub.example1.com:
sudo nano /etc/apache2/sites-available/sub.example1.com.conf
Add the following content:
<VirtualHost *:80>
ServerAdmin admin@example1.com
ServerName sub.example1.com
DocumentRoot /var/www/sub.example1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/sub.example1.com_error.log
CustomLog ${APACHE_LOG_DIR}/sub.example1.com_access.log combined
</VirtualHost>
Step 6: Enable the New Virtual Hosts
- Enable the Virtual Host Files:
sudo a2ensite example1.com.conf
sudo a2ensite example2.com.conf
sudo a2ensite sub.example1.com.conf
sudo systemctl restart apache2
Step 7: Verify Your Configuration
- Create Test HTML Files:
echo "<html><head><title>Example1</title></head><body><h1>Welcome to Example1</h1></body></html>" | sudo tee /var/www/example1.com/public_html/index.html
echo "<html><head><title>Example2</title></head><body><h1>Welcome to Example2</h1></body></html>" | sudo tee /var/www/example2.com/public_html/index.html
echo "<html><head><title>Subdomain Example1</title></head><body><h1>Welcome to Subdomain Example1</h1></body></html>" | sudo tee /var/www/sub.example1.com/public_html/index.html
Test in a Browser:
- Open your web browser and navigate to
http://example1.comandhttp://www.example1.com. - Navigate to
http://example2.comandhttp://www.example2.com. - Navigate to
http://sub.example1.com.
You should see the respective welcome messages for each domain and subdomain.