24/7/365 Support

Installing Apache and serving web pages in CentOS

In this process, we will learn how to install and configure the Apache web server to enable the serving of static web pages. Apache is one of the world’s most popular open source web servers. It runs as the backend for over half of all the Internet’s web sites and can be used to serve both static and dynamic web pages. Commonly referred to as httpd, it supports an extensive range of features. It is the purpose of this process to show you how easily it can be installed using the YUM package manager so that you can maintain your server with the latest security updates. Apache 2.4 is available on CentOS 7.

To Start With: What Do You Need?

To complete this process, you will require a working installation of the CentOS 7 operating system with root privileges, a console-based text editor of your choice, and a connection to the Internet in order to download additional packages. It is expected that your server will be using a static IP address and a hostname.

The Process

Apache is not installed by default and for this reason, we will begin by installing the necessary packages using the YUM package manager.

  1. To begin, log in as root and type the following command:
    yum install httpd
  2. Create a home page by typing:
    vi /var/www/html/index.html
  3. Now add the required HTML. You can use the following code as a starting point but it is expected that you will want to modify it to suit your own needs:
    <!DOCTYPE html>
    <html lang="en">
    <head><title>Welcome to my new web server</title></head>
    <body><h1>Welcome to my new web server</h1>
    <p>Lorem ipsum dolor sit amet, adipiscing elit.</p></body>
    </html>
  4. You can now remove the Apache 2 test page with the following command:
    rm -f /etc/httpd/conf.d/welcome.conf
  5. Having completed these steps, we will now consider the need to configure the httpd service for basic usage. To do this, open the httpd configuration file in your favorite text editor by typing (after you have made a backup of the file):
    cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.BAK vi /etc/httpd/conf/httpd.conf
  6. Now scroll down to find the line ServerAdmin root@localhost. The traditional approach to setting this value is based on the use of the webmaster identity, so simply modify the e-mail address to reflect something more relevant to your own needs. For example, if your server’s domain name was www.centos7.home then your entry will look similar to this:
    ServerAdmin webmaster@centos7.home
  7. Now scroll down a few more lines to find the ServerName directive as follows: #ServerName www.example.com:80. Uncomment this line (which means remove the leading # sign at its beginning) and replace the value www.example.com with something more appropriate to your own needs. For example, if your server’s domain name was www.centos7.home then your entry will look as follows:
    ServerName www.centos7.home:80
  8.  Next, we will expand the DirectoryIndex directive a bit more. Find the line DirectoryIndex index.html, which is part of the <IfModule dir_module> block, then change it to:
    DirectoryIndex index.html index.htm
  9. Save and close the file, and then type the following command to test the config file:
    apachectl configtest
  10. Next, let’s configure our web server’s firewall by allowing incoming http connections (this defaults to port 80) to the server:
    firewall-cmd --permanent --add-service http && firewall-cmd --reload
  11. Now proceed to set the httpd service to start at boot and start the service:
    systemctl enable httpd && systemctl start httpd
  12. You can now test httpd from any computer in the same network as your web server (both systems should be able to see and ping each other), pointing your browser at the following URL by replacing XXX.XXX.XXX.XXX with the IP address of your server in order to see our own custom Apache test page we created:
    http://XXX.XXX.XXX.XXX.
  13. Alternatively, if you don’t have a web browser, you can check if Apache is up and running using curl by fetching our test page on any computer in your network:
    curl http://XXX.XXX.XXX

How Does It Work?

Apache is a software package that enables you to publish and serve web pages, and is more commonly known as httpd, Apache2 or simply Apache. It was the purpose of this process to show you how easily CentOS enables you to get started with your very first website.

So what did we learn from this experience?

We began the process by installing Apache via the YUM package manager and the package named httpd. Having done this, we learned that on CentOS 7 the default location to serve static HTML is /var/www/html so our first task was to create a suitable home page, which we put in /var/www/html/index.html. Here we used a basic HTML template to get you started and it is expected that you would like to customize the look and feel of this page yourself. Following this, we then removed the default Apache 2 welcome page found in /etc/httpd/conf.d/welcome.conf. Following this, the next stage was to open the httpd.conf configuration file in our favorite text editor after making a backup of it so we could revert our changes if any problems occurred. First we defined the server’s e-mail address and the server name, which often appear in the error messages on the server-generated web pages; for this reason it should reflect your domain name. Next, we adjusted the DirectoryIndex directive, which defines which files will be sent first to the browser if a directory is requested. Often people request not a specific web page but a directory instead. For example, if you browse to www.example.com, you request a directory, while www.example.com/welcome.html is a specific web page. By default Apache sends the index.html in the requested directory but we expanded this since a lot of websites use the .htm extension instead. Finally, we saved and closed the httpd configuration file in the usual way before proceeding to check if the Apache configuration file contained any errors by using the apachectl configtest command. This should print out a Syntax OK message so we could enable the httpd service to start at boot time. We had to open the standard HTTP port 80 in our firewalld to allow incoming HTTP requests to the server, and finally we then started the httpd service. Remember, you can also always reload Apache’s configuration file if it has been changed without fully restarting the service, by using: systemctl reload httpd. Having completed these steps, it was simply a matter of opening your browser from another computer in the same network and electing a method of viewing our new Apache start page. You can use your server’s IP address (for example, http://192.168.1.100), while those with hostname support can type the hostname (for example, http://www.centos7.home) instead. Apache’s access and error log files can be found in /var/log/httpd. To get a live view of who is currently accessing your web server, open /var/log/httpd/access_log; to see all the errors, type /var/log/httpd/error_log.

Apache is a big subject and we cannot cover every nuance, but over the coming processes, we will continue to expose additional functionalities that will enable you to build a web server of choice.

 

Help Category:

What Our Clients Say