How to Create a Website on a CentOS 7 Cloud Server Using Apache?

19-01-2024 05:21:50

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:

  • DocumentRoot and Directory: The root directory path of the virtual host
  • ServerName: The main domain name of the virtual host
  • ServerAlias: Other domain names of the virtual host
  • ServerAdmin: The administrator email of the virtual host
  • Require all granted: Allows access from all sources

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