Domain name binding and virtual host configuration skills for building a web server on CentOS

PHPz
Release: 2023-08-05 19:57:07
Original
2088 people have browsed it

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:

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>
Copy after login

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:


< html>

<title>Welcome to example.com!</title>
Copy after login


<h1>Welcome to example.com!</h1>
<p>This is the default web page for the domain example.com.</p>
Copy after login


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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template