Configuring Domain Name Resolution for Apache2 on CentOS
To set up domain name resolution for Apache2 on CentOS, you need to configure both the Apache virtual hosts (to associate domains with website directories) and the DNS/hosts file (to map domains to your server’s IP address). Below is a step-by-step guide:
Run the following command to install the httpd package (Apache2’s package name in CentOS):
sudo yum install httpd -y
Start the Apache service and enable it to launch at boot:
sudo systemctl start httpd
sudo systemctl enable httpd
For each domain, create a dedicated directory to store website files. For example, for example.com:
sudo mkdir -p /var/www/example.com/html
Set the correct ownership (Apache runs as the apache user) and permissions:
sudo chown -R apache:apache /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com
Apache uses virtual hosts to serve multiple domains from a single server. On CentOS, the recommended location for virtual host configurations is /etc/httpd/conf.d/.
Create a new configuration file for your domain (replace example.com with your actual domain):
sudo vi /etc/httpd/conf.d/example.com.conf
Add the following content (modify ServerName, DocumentRoot, and ServerAlias as needed):
<VirtualHost *:80>
ServerName example.com # Primary domain
ServerAlias www.example.com # Optional: alias for the primary domain
DocumentRoot /var/www/example.com/html # Path to website files
<Directory /var/www/example.com/html>
Options Indexes FollowSymLinks
AllowOverride All # Allows .htaccess files to override settings
Require all granted # Grants access to all users
</Directory>
ErrorLog /var/log/httpd/example.com-error_log # Error log path
CustomLog /var/log/httpd/example.com-access_log combined # Access log format
</VirtualHost>
Save and close the file.
Apache automatically includes all .conf files in /etc/httpd/conf.d/. To apply the new configuration, restart the Apache service:
sudo systemctl restart httpd
For Apache to recognize your domain, you need to map the domain to your server’s IP address. There are two methods:
Edit the hosts file on your local machine (not the CentOS server) to map the domain to the server’s IP. Replace your_server_ip with your CentOS server’s public/ private IP:
sudo vi /etc/hosts
Add the following line:
your_server_ip example.com www.example.com
Save and close the file. This will route requests for example.com to your server.
For production, configure your domain’s DNS records (via your domain registrar or DNS provider) to point to your server’s IP. Add an A record:
@ (for the root domain) or www (for the www subdomain)A192.0.2.1)Open a browser and navigate to your domain (e.g., http://example.com). If configured correctly, you should see the default Apache page or your website content.
To verify Apache’s configuration syntax (before restarting), run:
sudo apachectl configtest
The output should be Syntax OK.
apache:apache) and permissions (755).hosts file (for testing) or DNS records (for production). Use nslookup example.com to verify resolution./var/log/httpd/error_log) for clues.By following these steps, you can successfully configure domain name resolution for Apache2 on CentOS, allowing multiple domains to coexist on a single server.