This tutorial explains how to install Apache on CentOS 8 and how to configure virtual hosts easily
From all the web servers available, the Apache Web Server is probably one of the most popular ones.
Developed by the Apache Foundation, Apache is so popular that it runs 70% of all the webservers online.
It is a reliable and secure web server that every system administrator should know.
Apache is part of the LAMP stack that stands for Linux, Apache, MariaDB and PHP and it is commonly used in many companies to host internal and external websites.
Today, we are going to see how you can install Apache on CentOS 8.
Prerequisites
In order to install the Apache Web server, you are going to need sudo privileges on CentOS 8.
To check if you have sudo privileges on your host, run the following command
$ sudo -l
User devconnected may run the following commands on localhost:
(ALL) ALL
Make sure that your firewall is running correctly on your CentOS 8 instance.
$ sudo systemctl status firewalld
Installing Apache on CentOS 8
In order to install the Apache Web Server, first update your local packages by running the following command
$ sudo yum update
When your update is done, you are ready to install Apache.
$ sudo yum install httpd
Start the Apache Web Server
In order to start your Apache Web server, run the following command
$ sudo systemctl start httpd
Make sure to enable your httpd service in order for it to start on system boot.
$ sudo systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service -> /usr/lib/systemd/system/httpd.service
Finally, make sure that your Apache Web server is running correctly by running a simple status command.
$ sudo systemctl status httpd
You can also check the version of your web server in order to make sure that it was installed correctly.
$ httpd -v
Test your Apache Web Server
In order to test that the Apache Web Server is working properly, you will first need to find your current IP address.
To get your IP address, run the following command
$ hostname -I | awk '{print $1}'
192.168.178.27
By default, Apache will run on the port 80 on your server.
In order to check that Apache is running correctly, you can either run a simple curl command or you can check with your web browser.
$ curl <ip_address>:80
This is the default page that you should see if you browse to the correct URL as discussed earlier.
This is just a standard presentation page with some basic instructions sitting on it. If you are new to web server administration, you can have a read at the paragraphs presented on this page.
Configuring your CentOS 8 firewall for Apache
In order for the web server to be available by external hosts, you are going to need to open specific ports on your firewall.
By default, CentOS uses firewalld which is a firewall that runs as a daemon on your host and provides basic security for it.
In order to accept HTTP and HTTPS connections, you are going to open the ports 80 and 443 on your server.
$ sudo firewall-cmd --permanent --zone=public --add-service=http
$ sudo firewall-cmd --permanent --zone=public --add-service=https
$ sudo firewall-cmd --reload
Make sure that the services are correctly authorized by running the following command
$ sudo firewall-cmd --list-all | grep services
services : cockpit dhcpv6-client http https ssh
Congratulations!
You successfully installed Apache on CentOS 8.
Your server is now accepting incoming HTTP requests to your web server.
Manage your Apache Web Server on CentOS 8
In order to manage your Apache Web Server, you have multiple options.
In order to restart Apache on CentOS 8, type the following command
$ sudo systemctl restart httpd
In order to stop the web server, run the following command
$ sudo systemctl stop httpd
If you want to start it again, you can run
$ sudo systemctl start httpd
If you made some Apache configuration modifications, you can reload your server instead of completely restarting it.
If you modified one website, it would restart the other unmodified websites which is obviously something that we want to avoid.
$ sudo systemctl reload httpd
If you want your web server to start at boot (which is recommended in case you update your server and forget to restart your web server), you have to run
$ sudo systemctl enable httpd
On the other hand, if you want to prevent your web server from starting at boot, run
$ sudo systemctl disable httpd
Creating Virtual Hosts for the Apache Web Server
Creating virtual hosts on Apache is very useful.
Instead of storing one single website on your web server, you can define multiple ones with a custom set of different rules. Each website can have its own set of policy rules, its own SSL keys and its own redirections.
It makes website management easier and it decouples websites one from another.
In order to create virtual hosts for Apache on CentOS 8, we are going to take the example of a website called “website.com”.
Create the domain folders
By default, your files are stored in “/var/www/html” on your host.
/var/www/
├── html
This path is also called “DocumentRoot” and it is used as the default entrypoint for your website on the server.
In order to store multiple websites, you are going to create multiple folders in your “/var/www/” and you are going to modify your httpd configuration file to point to those directories.
In this case, let’s create the following directory structure.
/var/www/
├── html
├── website.com
├── html
├── log
Create dedicated folders for your new website.
$ sudo mkdir -p /var/www/website.com/html
Make sure to create a file in order to store the log files of your website.
$ sudo mkdir -p /var/www/website.com/log
Create your first HTML page
Now that your folders are ready, you can create your first HTML page to be displayed to your users.
Create a new HTML file named “index.html”, and paste the following content inside.
$ cd /var/www/website.com/html
$ sudo vi index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Website.com</title>
<meta name="description" content="Website.com Homepage">
<meta name="author" content="devconnected">
</head>
<body>
This is the index page of website.com, welcome!
</body>
</html>
Save your file, and exit your editor.
Now that your website is ready, we can publish your website by creating a virtual host file.
Creating a virtual host file on CentOS 8
As discussed earlier, in order to publish your website, we are going to create a virtual host file.
Similarly to NGINX, we are going to create two directories :
- sites-available : that contains the entire list of websites available on our web server. Those websites are not necessarily enabled by default which is the purpose of the second folder.
- sites-enabled : that containers the list of websites that are accessible to users. A symbolic link will be created in this directory in order to activate and desactivate websites on demand.
First, create those two directories on your host.
$ sudo mkdir -p /etc/httpd/sites-enabled /etc/httpd/sites-available
Now that your folders are created, edit your default Apache configuration and find the following line.
$ sudo vi /etc/httpd/conf/httpd.conf
# Load config files in the "/etc/httpd/conf.d" directory if any
IncludeOptional conf.d/*.conf
Replace this line with the following line.
IncludeOptional sites-enabled/*.conf
Now that your Apache Web Server configuration is updated, create a virtual host file for your “website.com” website.
$ sudo vi /etc/httpd/sites-available/website.com.conf
Paste the following configuration in it.
<VirtualHost *:80>
ServerName website.com
ServerAlias www.website.com
DocumentRoot /var/www/website.com/html
ErrorLog /var/www/website.com/log/error.log
CustomLog /var/www/website.com/log/requests.log combined
</VirtualHost>
Save your file, and make sure that your configuration is okay by running the following command.
$ sudo apachectl configtest
Syntax OK
Now, your website won’t be directly available just by restarting your Apache Web server, it needs to be located in the sites-enabled folder.
To link it to the sites-enabled directory, create a symbolic link using this command.
$ sudo ln -s /etc/httpd/sites-available/website.com.conf /etc/httpd/sites-enabled/website.com.conf
Update your SELinux firewall rules
By default, SELinux is configured to work with default Apache configuration folders.
As you created custom ones, you need to enable them in SELinux.
In order for the Apache Web Server to start correctly, you need to modify your Apache policy to include custom log directories.
To enable custom directories, run the following command
$ sudo setsebool -P httpd_unified 1
Restart your Apache server
Now that everything is correctly set up, it is time for you to restart your server to see your changes.
$ sudo systemctl restart httpd
Head over to the URL that you specified in your virtual hosts file, and you should see your website up and running.
Conclusion
In this tutorial, you learnt how you can install Apache on CentOS 8.
You also learnt how you can set up virtual hosts in order to store many different websites on a single Apache web server.
If you are curious about system administration, we have a complete category dedicated to it on the website. Make sure to check it out!
5 comments
[…] on your host. You may have seen that specific accounts are used for mail administration, or to run a simple Apache server. Those accounts are often given restricted permissions and they are prevented from accessing an […]
IncludeOptional conf.d/*.conf, esto no lo he cambiado, si lo cambio según dice la publicación, no funciona
Thank you SCHKN !!!
Much welcome!
Hi SCHKN, thank you for this procedure. However, I noticed an error in it. While editing the /etc/httpd/conf/httpd.conf file during the configuration of virtual hosts, you say to replace the line IncludeOptional conf.d/*.conf with IncludeOptional sites-enabled/*.conf. This shouldn’t be a replace. The new line just needs to be added to the file. Replacing it prevents apache from reading its conf.d directory (and all configuration files in it) during startup, in effect disabling things like php.