


Domain name binding and virtual host configuration skills for building a web server on CentOS
Domain name binding and virtual host configuration skills for building a web server on CentOS
Introduction:
When building a web server, domain name binding and virtual host configuration are a very important step. This article will introduce how to configure domain name binding and virtual host on CentOS, and provide corresponding code examples.
1. Domain name binding
- Modify the hosts file
First, you need to add the mapping relationship between the domain name and the IP address in the CentOS hosts file. Find and open the hosts file, usually located in /etc/hosts:
sudo vi /etc/hosts
Add the following line at the end of the file, where "www.example.com" is the customized domain name, "192.168.0.100" is the IP address of the server:
192.168.0.100 www.example.com
Save and close the file.
- Configure DNS resolution
If your domain name is registered and DNS resolution has taken effect, you can skip this step. Otherwise, you need to point the domain name to the server's IP address in the domain name registrar's control panel. - Configuring Apache
Next, you need to configure the Apache server to point the domain name to the correct directory. Open Apache's main configuration file httpd.conf:
sudo vi /etc/httpd/conf/httpd.conf
Find and modify the following line, replace "www.example.com "Change to the domain name you want to bind:
ServerName www.example.com:80
Save and close the file.
Restart the Apache service to make the configuration take effect:
sudo service httpd restart
2. Virtual host configuration
- Create virtual host directory
The configuration of the virtual host requires a separate directory to store website files. First, create a directory to store the files of the virtual host:
sudo mkdir /var/www/virtual_host
- Modify the Apache configuration file
Open Apache Virtual host configuration file httpd-vhosts.conf:
sudo vi /etc/httpd/conf.d/httpd-vhosts.conf
At the end of the file, add the following content and replace "example.com" is your domain name, "/var/www/virtual_host/example.com" is the directory path you just created:
ServerName example.com DocumentRoot /var/www/virtual_host/example.com <Directory /var/www/virtual_host/example.com> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
Save and close the file.
- Set permissions
In order to ensure that website files can be accessed by Apache, you need to modify the permissions of the virtual host directory:
sudo chown -R apache:apache /var/ www/virtual_host/example.com
sudo chmod -R 755 /var/www/virtual_host/example.com
- Restart the Apache service
Restart the Apache service to make the configuration take effect:
sudo service httpd restart
At this point, the configuration of the virtual host is completed.
Code example:
- Create index.html file
In the virtual host directory, create an index.html file as the homepage of the website:
sudo vi /var/www/virtual_host/example.com/index.html
Paste the following into the file:
< html>
<title>Welcome to example.com!</title>
<h1>Welcome to example.com!</h1> <p>This is the default web page for the domain example.com.</p>