centos

centos apache2域名解析

小樊
39
2025-11-11 23:50:33
栏目: 云计算

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:

1. Install Apache2 (if not already installed)

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

2. Create Website Directories

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

3. Configure Apache Virtual Hosts

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.

4. Enable the Configuration

Apache automatically includes all .conf files in /etc/httpd/conf.d/. To apply the new configuration, restart the Apache service:

sudo systemctl restart httpd

5. Configure Domain Name Resolution

For Apache to recognize your domain, you need to map the domain to your server’s IP address. There are two methods:

Option A: Temporary Resolution (for Testing)

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.

Option B: Permanent Resolution (via DNS)

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:

6. Test the Configuration

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.

7. Troubleshooting Common Issues

By following these steps, you can successfully configure domain name resolution for Apache2 on CentOS, allowing multiple domains to coexist on a single server.

0
看了该问题的人还看了