Home > Article > Operation and Maintenance > 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
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.
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
sudo mkdir /var/www/virtual_host
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:
a2445d15a37241fe08bac1f84a382b54
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>
ee672f0beb03b42be69279368a66a410
Save and close the file.
sudo chown -R apache:apache /var/ www/virtual_host/example.com
sudo chmod -R 755 /var/www/virtual_host/example.com
sudo service httpd restart
At this point, the configuration of the virtual host is completed.
Code example:
sudo vi /var/www/virtual_host/example.com/index.html
Paste the following into the file:
8b05045a5be5764f313ed5b9168a17e6
07e6e06e0dc95dc83bb70d14dca11cbe
93f0f5c25f18dab9d176bd4f6de5d30e
<title>Welcome to example.com!</title>
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
<h1>Welcome to example.com!</h1> <p>This is the default web page for the domain example.com.</p>
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
Save and close the file.
Summary:
This article introduces the techniques for domain name binding and virtual host configuration on CentOS. By modifying the hosts file, configuring DNS resolution and related configuration of Apache, it is possible to point the domain name to Correct directory purpose. At the same time, code examples for creating the virtual host directory and homepage are also provided to facilitate readers to practice and test.
I hope this article will help you with domain name binding and virtual host configuration when building a web server on CentOS.
The above is the detailed content of Domain name binding and virtual host configuration skills for building a web server on CentOS. For more information, please follow other related articles on the PHP Chinese website!