After Apache is installed, some initial settings are required before creating a website. Here, we demonstrate the initial setup and website creation process using a CentOS 7 cloud server as an example.
Initial Setup
Remove the default welcome page:
mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak
systemctl reload httpd
Disable directory listing:
vi /etc/httpd/conf/httpd.conf
Remove 'Indexes' from the line 'Options Indexes FollowSymLinks':
Options FollowSymLinks
Restart Apache to apply the changes:
systemctl reload httpd
Now, when we access the IP address, the page displayed indicates that the initial Apache setup is complete.
Website Creation
An Apache server is divided into multiple independently configured units called virtual hosts, which are the core configuration files for websites. Virtual hosting technology allows multiple websites to be created on a single server using one IP address, with each website having its independent domain name, unaffected by others. Theoretically, the number of websites that can be created is unlimited, depending on the server's performance configuration and resource usage.
We'll demonstrate by creating a virtual host for the domain name zhaomu.com. Replace this with your own domain name for actual operations.
Create a Directory
Use the following command to create a directory:
mkdir /home/zhaomu.com
Create a Page
Create a default homepage for this website:
vi /home/zhaomu.com/index.html
The content is as follows:
<html>
<head>
<title>zhaomu.com</title>
</head>
<body>
<h1>zhaomu.com virtual host !</h1>
</body>
</html>
Save and exit using the ':wq' command.
Create a Configuration File
Create a configuration file using the following command:
vi /etc/httpd/conf.d/zhaomu.com.conf
The content is as follows:
<VirtualHost *:80>
DocumentRoot "/home/zhaomu.com"
ServerName zhaomu.com
ServerAlias www.zhaomu.com
ServerAdmin admin@zhaomu.com
<Directory "/home/zhaomu.com">
Require all granted
</Directory>
</VirtualHost>
The meanings of the directives in the configuration file are as follows:
Restart Apache to apply the changes:
systemctl reload httpd
Now, when we access the domain name, the page displayed indicates the successful creation of the virtual host.
Note: If there is an error when accessing the domain name, the issue can be identified by checking Apache's error logs.
tail /var/log/httpd/error_log
23-02-2024 02:02:07
22-02-2024 03:19:32
22-02-2024 03:16:03
22-02-2024 03:14:03
22-02-2024 03:11:58