To access your Apache server using a domain name (e.g., yourdomain.com) instead of the server's IP address, you'll need to follow these steps:
Step 1: Point the Domain to Your Server’s IP
- You need to create an A Record in your domain’s DNS settings.
- Add an A Record with the following details:
Host: @ (or www if you want to add the www version)
Value: Your server’s IP address
TTL: Set it to Automatic or 300 seconds
Step 2: Create a Virtual Host Configuration
You need to configure Apache to recognize your domain.
- Create a new virtual host file:
sudo nano /etc/httpd/conf.d/yourdomain.com.conf
- Add the following configuration:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog /var/log/httpd/yourdomain_error.log
CustomLog /var/log/httpd/yourdomain_access.log combined
</VirtualHost>
Note: Dont forget to create the directory
sudo mkdir /var/www/vhosts/<domain_name>
Step 3: Enable the Virtual Host
After creating the virtual host, enable it.
- Apache automatically reads the configuration file, so just restart Apache:
sudo systemctl restart httpd
Step 4: Test the Domain
After DNS propagation and Apache configuration:
- Open your browser.
- Visit: http://yourdomain.com
You should see the Apache default page or your website content.
Bonus: Instead of creating individual virtual host files for each domain, you can use mod_vhost_alias
to dynamically handle multiple domains with a single configuration file. This is ideal when you have many domains and want a simpler, scalable setup.
- Enable
mod_vhost_alias
:sudo yum install mod_ssl -y
- Restart Apache:
sudo systemctl restart httpd
- To use dynamic virtual hosts, you need to create folders corresponding to each domain:
- For example:
sudo mkdir /var/www/vhosts/<domain_name>
- You can repeat the same steps by creating virtual host and adding content into file:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerAlias *.codetowebsite.com
VirtualDocumentRoot "/var/www/%0/public_html"
<Directory "/var/www">
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/dynamic_error.log
CustomLog /var/log/httpd/dynamic_access.log combined
</VirtualHost>