How to install the Apache web server on CentOS 8

In this article, you will learn how to install the Apache web server on CentOS/RHEL 8.

Apache is a web-server. And it is in the market since April 1995, also it is open-source software, which means anyone can view the software code, modify, and share it freely.

The primary role of any web server is to store, process, and serve the webpage to the end-users. Apache uses HTTP protocol to bring the web pages to the users. And, we are going to install and configure the Apache web server. Nowadays, many web servers’ companies are using Nginx. But Apache has its importance.

It has a 33% market share in the year 2020. It provides many powerful features, some of them mentioned below,

Apache Features

  • Dynamic Modules
  • Compatible with IPv6
  • Apache server supports HTTP/2
  • .htaccess per-directory configuration support
  • gzip compression and decompression
  • Handling of static files, index files, auto-indexing, and content negotiation
  • Perl, PHP, and Lua scripts are already build
  • FTP connections are possible with proper module

Alternative of Apache

  • Nginx
  • Apache Tomcat
  • Node.js
  • Lighttpd
  • Cherokee
  • Microsoft IIS
  • Appweb
  • Hiawatha

Preconditions

  • You have a CentOS 8 or RHEL 8 system.
  • You must log-in with a root user or a user with sudo privilege to make the changes.
  • Internet connection

Now let’s begin to install the Apache web server.

Installation of Apache

Step 1: Before going to install Apache, we’ll check any pending update of the software package with the below command.

dnf update

Step 2: * Apache is available in default software repositories, to install it, you can use the below command.

dnf install httpd

Manage the services of Apache

Step 3: Once Apache has been installed. You have to start the service and also need to enable on startup. The service name of the Apache is httpd. So, you can find the below command useful for this purpose.

systemctl enable --now httpd

And to verify the httpd service status, use the following command.

systemctl status httpd
install apache web server
Checking the status of httpd.

If you are getting the above picture like output, then Apache service is running. But, you have to allow the service or port in the firewall.

Configuring the firewall for Apache

Step 4: You need to configure the firewall for allowing the traffic. Use the below command to authorize in the firewall.

firewall-cmd --permanent --add-service http
firewall-cmd --permanent --add-service https

So, after making the changes in firewall, you have to reload the firewall service,

firewall-cmd --reload

Checking Apache server 

Step 5: You can check your Apache server using domain_name or using the IP address in the web browser. For instance, you can type as below.

http://domain_name_or_IP

For example, in my case the IP address for my server is  http://192.168.43.228.

As a result, you shall get the output like in the below picture.

httpd server default page
The Apache web-server test page

Managing the Apache service

Step 6: As of now, you’ll learn some basic process management of the Apache web server.

To stop the webserver

systemctl stop httpd

You can start the service if it is stopped, using the below command.

systemctl start httpd

Suppose, you are going to restart the service make sure that you know that firstly, it will stop the service, then it starts again. If someone is downloading or doing something on your website, he lost the connection.

systemctl restart httpd

If you simply executing configuration changes then you can only use reload without dropping the connections.

systemctl reload httpd

* Right now, our server is configured to start automatically on system startup. If you want to disable that.

systemctl disable httpd

Enabling the service on startup

systemctl enable httpd

To check, if the service is enabled on startup or not,

systemctl is-enabled httpd

Setting up Virtual Hosts

Step 7: If you want to host more than one domain server then you have to configure Apache virtual host. I am going to set up a domain with the name linuxgurus.in, But, you can replace this name with your domain name.

In Apache, by default location to store the website files are /var/www/html directory. It is good for one website, but if you are using it for multiple websites, then it is not good. The best practice to host the new website is creating a new directory with the name of the website, in my case, it is linuxgurus.in and I will create this directory inside /var/www. so let’s create it,

mkdir -p /var/www/linuxgurus.in/html

It is also a good convention to create a log directory, so that you can get it quickly, let’s create it.

mkdir -p /var/www/linuxgurus.in/log

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

chown -R $USER:$USER /var/www/linuxgurus.in/html

Make sure that your web root directory has the default permissions set,

chmod -R 755 /var/www

Creating a simple html file

Now we are going to create a simple html file or you can create as per your need. I’m using here vim editors to create this simple page.

vim /var/www/linuxgurus.in/html/index.html

Press i to go into insert mode and type the below lines.

<html>

    <head>

        <title>Welcome to the Linux Guru</title>

    </head>

    <body>

        <h1>

            <center>

        You have created successfully this page and The linuxgurus.in virtual host is working.

            </center>>

        </h1>

    </body>

</html>
 

To save the file, press Esc key and type :wq and press Enter key.

Creating a directory to hold the Virtual Host information

As we have created an html file now we are going to create a directory with name sites-available which will hold the virtual host information. and we need also to create another directory sites-enabled this directory tells Apache that a virtual host is ready to serve the visitors. The sites-enabled directory will hold symbolic links to virtual hosts that we want to publish. Create both directories with the following command:

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

Now we have to configure Apache to get the virtual host information from the sites-enabled directory. So open the Apache configuration file. I’m using vim editors to open this file, but you can use whatever is right editors for you.

vim /etc/httpd/conf/httpd.conf

So, add this below line to the end of the file,

IncludeOptional sites-enabled/*.conf

Save the configuration file, pressing Esc key, and typing :wq and then press Enter key.

Creating Virtual Host file

Now, as we have created the virtual host directory, So, we can create our virtual host file in the directory sites-available.

In my case, the file name is linuxgurus.in and you have to add the file extension .conf in the end, so it will look like linuxgurus.in.conf.

For instance, I’m using again vim editor to create the file. So, you can see the command as below.

vim /etc/httpd/sites-available/linuxgurus.in.conf

Add in this file, the following configuration block, and change the linuxgurus.in domain name to your domain name.

<VirtualHost *:80>
ServerName www.linuxgurus.in 
ServerAlias linuxgurus.in 
DocumentRoot /var/www/linuxgurus.in/html 
ErrorLog /var/www/linuxgurus.in/log/error.log 
CustomLog /var/www/linuxgurus.in/log/requests.log combined
</VirtualHost>

It will tell Apache where to find the root directly that holds the publicly accessible web documents. It also tells Apache where to store error and request logs for this particular site. So, save and close the file when you are finished.

Now that you have created the virtual host files, you will enable them so that Apache knows to serve them to visitors. To do this, create a symbolic link for each virtual host in the sites-enabled directory.

ln -s /etc/httpd/sites-available/linuxgurus.in.conf /etc/httpd/sites-enabled/linuxgurus.in.conf

Your virtual host is configured and it is ready to serve content. If you have enabled SELinux, then you have to lookup for the few settings.

Altering SELinux Permissions for Virtual Hosts

Step 8: SELinux works with Apache by default but as we have created custom virtual host files so we need to update the SElinux policy to allow them. Before changing the policy in SELinux if you restarted the service of Apache, you will get an error so let’s set up that first, and then we’ll restart the Apache service.

Firstly, we have to install SELinux manage package so we can alter the settings of SELinux, use the below command to install it.

dnf install -y setroubleshoot-server selinux-policy-devel

Changing the Apache policy universally

Secondly, changing the SELinux boolean value.

setsebool -P httpd_unified 1

Here, The setsebool command alters SELinux boolean values. The -P flag will update the boot-time value, making this change persist across reboots. httpd_unified is the boolean that will tell SELinux to treat all Apache processes as the same type, so you have enabled it with a value of 1.

Changing the Apache policies for the directory

First, check the context type that SELinux gave the /var/www/linuxgurus.in/log directory:

ls -dZ /var/www/linuxgurus.in/log/
selinux apache server
Changing the Apache policies for the directory

The current context is httpd_sys_content_t, which tells SELinux that the Apache process can only read files created in this directory. In this tutorial, you will change the content type of the /var/www/linuxgurus.in/log directory to httpd_log_t. This type will allow Apache to generate and append to web application log files:

semanage fcontext -a -t httpd_log_t "/var/www/linuxgurus.in/log(/.*)?"

Now, use the restorecon command to apply these changes and have them persist across reboots:

restorecon -R -v /var/www/linuxgurus.in/log
restorecon -R -v /var/www/linuxgurus.in/html
Note:

Here, If you are changing any file or re-writing with any other name in html, you have run the command restorecon -R -v /var/www/linuxgurus.in/html

The -R flag runs this command recursively, meaning it will update any existing files to use the new context. The -v flag will print the context changes the command made. You will see the following output confirming the changes:

selinux apache changes

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

ls -dZ /var/www/linuxgurus.in/log/

The output reflects the updated context type:

selinux apache changes 3

Now that the /var/www/linuxgurus.in/log directory is using the httpd_log_t type, you are ready to test your virtual host configuration.

Now you can test the Virtual Host

Step 9: Now restart the Apache service

systemctl restart httpd

And you can check the logs Apache has created in directory /var/www/linuxgurus.in/log with name error.log and requests.log. And you get the list in SELinux that it has set in the policy

install apache web server

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

apache final site
The output of the Apache web server

If you want to create a new Virtual Host to set up a new website, you can follow the steps from step 7 to step 9.

Conclusion

In this tutorial, you learned how to install the apache web server on CentOS 8 and configure the virtual host. You can practice as many times as you want. I always recommend installing the Apache web server two or three times for practice. And try to know the terms. So, I hope you understand but, if you have any questions, you can ask in the comment section.

Also, you can further read,

 

Leave a Reply

Your email address will not be published.