0% found this document useful (0 votes)
3 views10 pages

SE181693 - Lab 5 - Part 2

This document provides a step-by-step guide to installing and configuring the Apache Web Server on CentOS 7, including setting up virtual hosts. Key steps include installing Apache, managing the service, creating directories for web content and logs, and adjusting SELinux permissions. The guide concludes with testing the virtual host setup to ensure proper functionality.

Uploaded by

quangndmse181693
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

SE181693 - Lab 5 - Part 2

This document provides a step-by-step guide to installing and configuring the Apache Web Server on CentOS 7, including setting up virtual hosts. Key steps include installing Apache, managing the service, creating directories for web content and logs, and adjusting SELinux permissions. The guide concludes with testing the virtual host setup to ensure proper functionality.

Uploaded by

quangndmse181693
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Install the Apache Web Server on CentOS 7

Introduction
The Apache HTTP server is the most widely-used web server in the world. It provides
many powerful features including dynamically loadable modules, robust media support,
and extensive integration with other popular software.
In this guide, you will install an Apache web server with virtual hosts on your CentOS 7
server.

Step 1 — Installing Apache

sudo yum update httpd

Once the packages are updated, install the Apache package: sudo

yum install httpd

After confirming the installation, yum will install Apache and all required dependencies.

If you haven’t already done so, you can do this by enabling firewalld’s http service with
the following command:

sudo firewall-cmd --permanent --add-service=http

Next, reload the firewall to put these new rules into effect: sudo

firewall-cmd –reload
Step 2 — Checking your Web Server
Apache does not automatically start on CentOS once the installation completes. You
will need to start the Apache process manually: sudo systemctl start httpd

Verify that the service is running with the following command:

sudo systemctl status httpd

You will see an active status when the service is running:

Type this at your server’s command prompt:

hostname -I
When you have your server’s IP address, enter it into your browser’s address bar:

http://your_server_ip

You’ll see the default CentOS 7 Apache web page:

Step 3 — Managing the Apache Process To

start the web server when it is stopped, type:

sudo systemctl start httpd

To stop and then start the service again, type:

sudo systemctl restart httpd

To re-enable the service to start up at boot, type:

sudo systemctl enable httpd

Apache will now start automatically when the server boots again.
Step 4 — Setting Up Virtual Hosts

Create the html directory for your_domain as follows, using the -p flag to create any
necessary parent directories:

sudo mkdir -p /var/www/your_domain/html

Create an additional directory to store log files for the site:

sudo mkdir -p /var/www/your_domain/log

Next, assign ownership of the html directory with the $USER environmental variable:

sudo chown -R $USER:$USER /var/www/your_domain/html

Make sure that your web root has the default permissions set: sudo

chmod -R 755 /var/www

Next, create a sample index.html page using vi or your favorite editor: sudo

vi /var/www/your_domain/html/index.html

Press i to switch to INSERT mode and add the following sample HTML to the file:
Save and close the file by pressing ESC, typing :wq, and pressing ENTER.

Create both directories with the following command:

sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled

Next, you will tell Apache to look for virtual hosts in the sites-enabled directory.

sudo vi /etc/httpd/conf/httpd.conf

Add this line to the end of the file:

IncludeOptional sites-enabled/*.conf
Save and close the file when you are done adding that line.

Start by creating a new file in the sites-available directory:

sudo vi /etc/httpd/sites-available/your_domain.conf
Add in the following configuration block, and change the your_domain domain to your
domain name:
Save and close the file when you are finished.

Create a symbolic link for each virtual host in the sites-enabled directory: sudo
ln -s /etc/httpd/sites-available/your_domain.conf
/etc/httpd/sitesenabled/your_domain.conf

Step 5 — Adjusting SELinux Permissions for Virtual Hosts

Run the following command to set a universal Apache policy:

sudo setsebool -P httpd_unified 1

Adjusting Apache Policies on a Directory


First, check the context type that SELinux gave the /var/www/your_domain/log directory:

sudo ls -dZ /var/www/your_domain/log/


This command lists and prints the SELinux context of the directory. You will see output
similar to the following:
sudo semanage fcontext -a -t httpd_log_t "/var/www/your_domain/log(/.*)?"

sudo restorecon -R -v /var/www/your_domain/log

You can list the contexts once more to see the changes:

sudo ls -lZ /var/www/your_domain/log

Step 6 — Testing the Virtual Host


Once the SELinux context has been updated with either method, Apache will be able to
write to the /var/www/your_domain/log directory. You can now successfully restart the
Apache service:

List the contents of the /var/www/your_domain/log directory to see if Apache created the
log files: ls -lZ /var/www/your_domain/log
You’ll see that Apache was able to create the error.log and requests.log files specified in
the virtual host configuration:

Now that you have your virtual host set up and SELinux permissions updated, Apache
will now serve your domain name. You can test this by navigating to http://your_domain,
where you should see something like this:
Note :

Config the hosts file on windows 10 or 11 by notepad ( run as administrator permissions )


Done Lab !

You might also like