How to create a subdomain in cPanel shared hosting
Learn the exact steps to add a subdomain like mail.example.com in cPanel. This guide covers creating the subdomain account, pointing the DNS, and configuring the domain home directory for cPanel 118.x and WHM.
You will create a functional subdomain like mail.example.com or blog.example.com in your cPanel account. These steps apply to cPanel 118.x and WHM 80.x on standard shared hosting or VPS environments.
Prerequisites
- Root or Reseller access to WHM (for creating subdomains) or a standard cPanel account with subdomain creation privileges.
- Access to the DNS Zone Editor in WHM or your domain registrar's control panel.
- Knowledge of the primary domain name (e.g., example.com).
- Whitelist the domain in WHM if you are using a managed hosting environment.
Step 1: Access WHM and the Subdomains Tool
Log in to your WHM interface using your root credentials. Navigate to the Home > Subdomains section to access the creation tool. You must be logged in as root or a reseller with the appropriate permission set.
http://your-server-ip:2087
Once logged in, locate the Subdomains link in the left-hand navigation menu under the Home section. Click on it to open the creation form.
Step 2: Enter the Subdomain Details
Type the subdomain name you want to create in the first field. Enter the parent domain name in the second field. Choose the number of accounts to create. The default is one, but you can create multiple if needed.
Subdomain: blog
Parent domain: example.com
Number of accounts: 1
Click the Create button. The system will process the request and generate the necessary directory structure and account files.
Step 3: Configure the Subdomain Account
After creation, the system will prompt you to set up the account. Enter a username for the subdomain account. Set a password for the account. You can also assign a specific email address to the subdomain if desired.
Username: bloguser
Password: ********
Email: blog@example.com
Select the option to create a home directory. This is critical for the subdomain to function correctly. Click Finish to complete the setup.
Step 4: Point the DNS Records
Log in to your domain registrar or use the DNS Zone Editor in WHM. Locate the Zone Editor for your primary domain. Add an A record for the subdomain pointing to your server IP address.
Type: A
Name: blog
Value: 192.0.2.1
TTL: 3600
Save the changes. DNS propagation can take up to 48 hours, but it usually resolves within a few minutes. You can check the status using a tool like dig or nslookup.
Step 5: Set Up the Web Application
Log in to the subdomain account in cPanel. Navigate to the Files section. Upload your website files to the public_html directory. Ensure the index.php or index.html file is present in the root.
cd public_html
ls -la
Verify that the directory permissions are set to 755. Check that the file permissions are set to 644 for standard files and 755 for directories.
Verify the installation
Open a web browser and navigate to the subdomain URL. You should see the content you uploaded to the public_html directory. If you see a default cPanel welcome page, the setup is successful.
https://blog.example.com
Check the server logs in WHM to ensure there are no errors related to the subdomain. Look for the access log in the subdomain's home directory.
Troubleshooting
Error: "Subdomain already exists" This occurs if you try to create a subdomain with the same name as an existing one. Check the Subdomains list in WHM. Delete the old entry or use a different subdomain name.
Error: "Permission denied when creating directory" This happens if the user does not have write access to the parent directory. Ensure the user has the necessary permissions. Check the file ownership with ls -la.
Error: "DNS record not resolving" The A record might not be propagating. Wait a few minutes and try again. Use nslookup to verify the IP address matches your server.
Error: "403 Forbidden on subdomain" This indicates incorrect file permissions. Set the directory permissions to 755 and file permissions to 644. Ensure the index file is present.
Error: "500 Internal Server Error" Check the error logs in the subdomain's home directory. Look for PHP syntax errors or missing dependencies. Ensure the web server configuration is correct.