RHEL7: Configure an Apache virtual host.

Share this link

Note: This is an RHCE 7 exam objective.

Prerequisites

First, follow the instructions to install an Apache web server.

Note: Don’t forget to install the httpd-manual package. This could help you a lot with any syntax issue.

Configuration Procedure

Let’s assume your website is called dummy-host.example.com.

Create the /var/www/html/dummy-host.example.com directory:

# cd /var/www/html
# mkdir dummy-host.example.com

Create an index.html file and assign the correct SELinux context:

# echo "This is a test." > dummy-host.example.com/index.html
# restorecon -R dummy-host.example.com

Create the /etc/httpd/conf.d/vhosts.conf file and paste the following lines:

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /var/www/html/dummy-host.example.com
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

Optionaly, rename the /etc/httpd/conf.d/ssl.conf file, otherwise you get an additional non-working https virtual host displayed in the configuration.

# cd /etc/httpd/conf.d; mv ssl.conf ssl.conf2

Check the validity of the configuration:

# apachectl configtest
Syntax OK

Note: You can also type: # httpd -t

Restart the httpd service:

# apachectl restart

Note1: You can also type: # systemctl restart httpd
Note2: For minor configuration changes, it is also possible to restart the Apache daemon without losing the current connections: # apachectl graceful

Check the virtual host(s) configuration:

# httpd -D DUMP_VHOSTS
VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server dummy-host.example.com (/etc/httpd/conf.d/vhosts.conf:1)
         port 80 namevhost dummy-host.example.com (/etc/httpd/conf.d/vhosts.conf:1)
         port 80 namevhost dummy-host.example.com (/etc/httpd/conf.d/vhosts.conf:1)

Testing Time

Check the configuration:

# yum install -y elinks
# elinks http://dummy-host.example.com
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 3.25 out of 5)
Loading...

Leave a Reply

30 Comments on "RHEL7: Configure an Apache virtual host."

Notify of
Sort by:   newest | oldest
nariman1064
Member
nariman1064
Thank you for your awesome RHCE notes, I really appreciate your time and effort to help others. Just in case few people might wonder, I like to add few notes here: – Time is gold during the exam, it seems to me, the fastest and shortest command that you can run to install everything you need for httpd\apache questions is : # yum groupinstall -y “Web server” – Just to make sure you don’t get any read permission issues, perform below command when you are done creating directories and files under /var/www – # chmod -R 755 /var/www – Don’t… Read more »
chamambom
Member
chamambom

your vitual host config for the second website should be enclosed inside

and one should obviously open port 8080 on the firewall

juliorc@upcmail.nl
Member
juliorc@upcmail.nl

Also use:https://www.certdepot.net/rhel7-use-selinux-port-labelling/

As you do not use one of the standard ports

ikambarov
Member
ikambarov

I was wondering, can we install httpd-manual and use it during exam?

twostep
Member
twostep

For people who do not want to remember the syntax (it is worth to know also to other applications):
1. Let’s find mem and doc files:
rpm -qd httpd
2. Let’s check the file from above list:
/usr/share/doc/httpd-2.4.6/httpd-vhosts.conf
3. Copy, paste and modify 🙂

raj
Member
raj

Hello All, is it normal to see 2 records for each vhost with the following command?
# httpd -D DUMP_VHOSTS

thegeekaid
Member
thegeekaid

Hi, everything is working fine, but I’m trying to list the files in the directory but without any luck, instead it shows me the default index of Apache 123..,
This is my configuration, can someone point me what I’m missing?

Servername practice.example.com
Documentroot /var/web/practice

Options indexes
AllowOverride None
Require all granted

phil_guy412
Member
phil_guy412
I’ve followed this tutorial as well as another one and can’t get this work. When I run the elinks test I get the “Unable to retrieve http://myvhost.example.com/: Host not found. So from start to finish here’s what I did: I installed httpd, started/enabled it, and added the service to the firewall. Assuming my website is myvhost.example.com I created the /var/www/html/myvhost.example.com directory. While i’m in that directory I ran an echo “This is VHOST” > /var/www/html/myvhost.example.com/index.html I then run a restorecon -R myvhost.example.com Then inside the /etc/httpd/conf.d/myvhost.conf file ServerAdmin admin@myvhost.example.com DocumentRoot /var/www/html/myvhost.example.com ServerName myvhost.example.com ErrorLog logs/myvhost.example.com-error_log CustomLog logs/myvhost.example.com-access_log common I then… Read more »
Sam
Member
Sam

At a quick glance it looks ok. Is your host up. Ping and a port scan (nmap) on myvhost.example.com. Ask your self if the ipaddress is correct. Also check the SElinux status. What Port are you running on? Check the log files in /var/log/ for http errors

phil_guy412
Member
phil_guy412
1. I can ping the IP address but not myvhost.example.com (I had to recreate my test server so I didn’t get around to installing a working DNS yet, so this might just be that issue) 2. I’m running this on the default port 80 3. I’ve tried turning SELinux off completely and still get the same error. 4. The /var/log/httpd/myvhost.example.com-error_log shows nothing. 5. This test server has an IP address of 172.31.122.164/20 and I added that to the /etc/hosts file as 172.31.122.164/20 myvhost.example.com And what do you mean that i need to check if my IP address is correct and… Read more »
brucemzn
Member
brucemzn
Hi phil_guy412 Let me try to help, one step at a time. No ping means no connectivity. You said your test server has an IP of 172.31.122.164 Whats the client’s IP? The issue could be “subnetting” /20 = 255.255.240.0 According to your configurations: Your Network = 172.31.112.0/20 Broadcast = 172.31.127.255/20 Usable IPS = 172.31.112.1 – 172.31.127.254 The easiest way to solve this problem without complicating network configurations, use the /24 subnet. /24 = 255.255.255.0 Its easy to work with /24. Change your IP addresses to anything between 172.31.20.1 – 172.31.20.254 (for example) server = 172.31.20.2/24 client1 = 172.31.20.3/24 client2 =… Read more »
phil_guy412
Member
phil_guy412

So now that I thought about it in regards to dns, I tried running elinks test with the IP address and it works haha. I’ll install a dns and see if I can get it to work then too.

phil_guy412
Member
phil_guy412

I figured it out. I tried running the elinks test with only the IP address and it worked. It did not work with myvhost.example.com so I either had a DNS issue or just had to update my /etc/hosts file.

Lisenet
Member

“Host not found” message should’ve been your first clue 🙂

Lisenet
Member

What does myvhost.example.com resolve to?

phil_guy412
Member
phil_guy412

What do you mean by that?

Lisenet
Member

That’s exactly what I mean, the host has to resolve to some IP address. The error that you get “Host not found” means that the host cannot be found, implying there is no DNS record available for the host you try to resolve. The error message tells you what the problem is.

phil_guy412
Member
phil_guy412

I have another question in general. If during the exam they ask me to configure a private directory for a specific user or group of users for a Virtual Host, do I just add the security options to the virtual host configuration?

For example…

ServerAdmin admin@myvhost.example.com
DocumentRoot /var/www/html/myvhost.example.com
ServerName myvhost.example.com
ErrorLog logs/myvhost.example.com-error_log
CustomLog logs/myvhost.example.com-access_log common
AuthType Basic
AuthName “Password Protected Area”
AuthGroupFile /etc/httpd/conf/htgroup
AuthUserFile /etc/httpd/conf/passwd
Require Group sales

I’m trying to test this now.

Lisenet
Member

Close but not quite there yet, you have to put those authentication setting for the directory you want to secure, not the whole DocumentRoot.

Sam
Member
Sam

phil_guy412 I recommend that you setup a local DNS server, and point all the test Workstations dns settings to the DNS Server.

It’s a good idea as part of RHCE, but a bit of a pain to setup.

Reference
https://www.certdepot.net/rhel7-configure-master-name-server/
And read the comments!

brucemzn
Member
brucemzn

Hi
This might help answer your question.
https://youtu.be/4qIbuj36QTY

slavonic
Member
slavonic
Hello everyone, I’d like to verify one thing regarding HTTP configuration for RHCE exam. Does it matter whether I do configuration for basic web server (I mean not virtual), secure web page, confidential web hosting and dynamic webpage in the path “/etc/httpd/conf.d/” or in the path “/etc/httpd/conf/”. I am asking because there is also an objective to configure virtual web hosting that should be configured in “/etc/httpd/conf.d/” for sure, but what about all others? There is no problem with the path, they are working in both paths, I just don’t want to loose points even though it will work fine… Read more »
Sam
Member
Sam

There is no way of knowing. I suggest you stick with the convention as detailed by Certdepot.

/etc/httpd/conf.d/
for vhosts.conf ssl.conf etc

/etc/httpd/conf/
for httpd.conf

This is a minor issue. Good luck with the exam.

slavonic
Member
slavonic

I’ve just passed the exam with 241 points reached today but now I am sad because I have no idea where I could make a mistake…is there anyone who has passed the exam with 300 points? I’d like to discuss some of the topics with that person. Thanks. 🙂

Sam
Member
Sam

Congratulations,

Please remember you signed a Non Disclosure Agreement. There is no way of knowing, whether it is a mistake or a issue in the testing script. I have been through this issue. That is the Advantage of the RHCE exam.

wpDiscuz

RHCSA7: Task of the day

Allowed time: 3 minutes.
Check that you've got no SELinux policy violations.

RHCE7: Task of the day

Allowed time: 5 minutes.
Set up time synchronization with default configuration.

Recent Comments

Follow me on Twitter

Poll for favorite RHEL 7 book

What is your favorite RHEL 7 book to prepare RHCSA & RHCE exams?

View Results

Loading ... Loading ...

Poll for most difficult RHCSA 7 topic

What do you think is the most difficult RHCSA 7 topic?

View Results

Loading ... Loading ...

Poll for most difficult RHCE 7 topic

What do you think is the most difficult RHCE 7 topic?

View Results

Loading ... Loading ...