Centos 7 apache 2.4.6 virtualhost 왜 없음

My server information is

Server version: Apache/2.4.6 (CentOS)
Server built:   Nov 19 2015 21:43:13

I am trying to configure virtual host for 2 different sites: biz.example.com and pin.example.com which are hosted on the same server. There are 2 different folders located under 'var/www/html/' named 'biz' and 'pin' with their respected project files for the above mentioned 2 websites. I am trying to configure it on the below way.

Within /etc/hosts below configuration

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

xxx.xxx.xxx.xxx biz.example.com
xxx.xxx.xxx.xxx pin.example.com

xxx.xxx.xxx.xxx is replaced by the server IP address.

Within /etc/httpd/conf/httpd.conf

IncludeOptional sites-enabled/*.conf

Now, under /etc/httpd/sites-available there are biz.conf and pin.conf file. I also have the folder sites-enabled under /etc/httpd which has 2 files that points to the biz.conf and pin.conf of sites-available folder using the below command

ln -s /etc/httpd/sites-available/biz.conf /etc/httpd/sites-enabled/biz.conf

ln -s /etc/httpd/sites-available/pin.conf /etc/httpd/sites-enabled/pin.conf

biz.conf has the followings

<VirtualHost *:80>
ServerName http://biz.example.com/
ServerAlias http://biz.example.com/
DocumentRoot "/var/www/html/biz"
<directory "/var/www/html/biz">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Allow from 127.0.0.1
</directory>
</VirtualHost>

And the configuration within pin.conf file is mentioned as

<VirtualHost *:80>
ServerName http://pin.example.com/
ServerAlias http://pin.example.com/
DocumentRoot "/var/www/html/pin"
<directory "/var/www/html/pin">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Allow from 127.0.0.1
</directory>
</VirtualHost>

On this setup, if i try to access http://biz.example.com/ , the correct website (biz website) is loading. But if i try to access http://pin.example.com/ , then also biz website is loading instead of pin website. Multiple configuration is not working together.

I also tried to merge the virtual configuration of biz.conf and pin.conf within a single file, biz.conf, but it didn't work as well.

Mar 29, 2022

Edgaras G.

2min Read

Centos 7 apache 2.4.6 virtualhost 왜 없음

If you want to host more than one domain on your server, you need to create corresponding hosts on the webserver. That way, your server can deliver different content for different requests. In this tutorial, you will learn how to create Apache virtual hosts on CentOS 7.

1. Installing Apache

Before we begin, make sure that you have root access to your VPS or server using SSH connection. In Hostinger, the login credentials are located in the Servers tab of hPanel.

  1. Install Apache on your CentOS 7 machine by typing the following command:
    sudo yum -y install httpd
  2. Once the installation is completed, enable Apache as a CentOS service:
    sudo systemctl enable httpd.service
  3. Visit your server’s IP address to check whether Apache is already running or not. The page should look like this:
    Centos 7 apache 2.4.6 virtualhost 왜 없음

2. Creating Directory Tree

  1. A directory tree is used to hold website data. First, set the working directory to /var/www by running this command:
    cd /var/www/
  2. You should use a unique document root for each virtual host:
    mkdir -p yourdomain.com/public_html

    Remember to replace yourdomain.com with your actual domain name.

  3. Make the directory accessible to Apache. Run chown to change the ownership and chmod to set correct permissions for the whole web directory.
    chown -R apache:apache /var/www/yourdomain.com/public_html
    chmod -R 755 /var/www

Apache now has the required access to create additional directories and serve content for incoming queries.

3. Creating a Demo Page

It is recommended to make a demo page for your Apache virtual hosts. This way, you can check whether the host is working before you actually move your website files. Here’s how you do it:

  1. Use the nano editor to create index.html file in yourdomain.com/public_html directory:
    nano yourdomain.com/public_html/index.html
  2. Paste the following content to the file:
    <html>
      <head>
        <title>This is a test page</title>
      </head>
      <body>
        <h2>It works!</h2>
      </body>
    </html>
  3. Save the file by pressing CTRL + X and then Y.

4. Creating the Virtual Host

  1. Create a new virtual host .conf file in the Apache configuration directory:
    nano /etc/httpd/conf.d/yourdomain.com.conf
  2. Insert the following content into the .conf file:
    <VirtualHost *:80>
        ServerName www.yourdomain.com
        ServerAlias yourdomain.com
        DocumentRoot /var/www/yourdomain.com/public_html
        ErrorLog /var/www/yourdomain.com/error.log
        CustomLog /var/www/yourdomain.com/requests.log combined
    </VirtualHost>

    In the example above, we tell Apache that we will be using port 80 for the communication and that yourdomain.com is the name of the virtual host. Additionally, we also specify directories for the website files (document root) and error logs.

  3. Restart Apache for the changes to take effect:
    systemctl restart httpd.service

That’s it, you have just created an Apache virtual host for your domain! Now try to access the host and you should see the “It works!” text from the demo page we made earlier.

Conclusion

You have learned how to create an Apache virtual host in four easy steps. To summarize, let’s take a look at them once again:

  1. Install apache from CentOS 7.
  2. Create a directory tree that will be used to hold all your website files.
  3. Make a demo page to see if Apache virtual host is working properly.
  4. Create the virtual host by making configuration files in the Apache directory.

We hope this tutorial is useful. Feel free to comment below if you have any questions!

Edgaras is a veteran server administrator at Hostinger. He makes sure that every server runs at full throttle and has all the latest technological advancements. When he's not working, Edgaras enjoys skiing and exploring the world.

What is the latest version of Apache for CentOS 7?

Apache httpd 2.4.54 Released 2022-06-08 This latest release from the 2.4.x stable branch represents the best available version of Apache HTTP Server.

What is VirtualHost in Apache server?

The term Virtual Host refers to the practice of running more than one web site (such as company1.example.com and company2.example.com ) on a single machine.

How do I host multiple websites on CentOS 7?

How to Configure Apache Virtual Hosts on CentOS 7.
Step 1: Set Up Apache. Apache will be configured to host multiple sites out of the gate, so there is not much to do here. ... .
Step 2: Add Document Roots. We now need some domains to add to Apache. ... .
Step 3: Add Virtual Host Files. ... .
Step 4: Test And Restart Apache..

How do I set up a name based VirtualHost?

Table of Contents.
Introduction..
Requirements..
Create the directory structure..
Create test web pages for each virtual host..
Set up ownership and permissions..
Create virtual host configuration files..
Test the virtual hosts..