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

PHPz
PHPzOriginal
2023-08-05 19:57:072089browse

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

  1. 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.

  1. 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.
  2. 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

  1. 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

  1. 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:

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.

  1. 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

  1. 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:

  1. 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:

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.

  1. Test website
    Enter your domain name in the browser (such as: http://www.example.com), if you see the page showing "Welcome to example.com!", It means that the domain name binding and virtual host configuration are successful.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn